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
32 #if defined(MODVERSIONS)
33 #include <linux/modversions.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/sched.h>
39 #include <linux/seq_file.h>
40 #include <linux/timer.h>
41 #include <linux/delay.h>
42 #include <linux/errno.h>
43 #include <linux/spinlock.h>
44 #include <linux/slab.h>
45 #include <linux/list.h>
46 #include <linux/i2c.h>
47 #include <linux/ipmi_smi.h>
48 #include <linux/init.h>
49 #include <linux/dmi.h>
50 #include <linux/kthread.h>
51 #include <linux/acpi.h>
52 #include <linux/ctype.h>
53 #include <linux/time64.h>
54 #include "ipmi_si_sm.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)
93 enum ssif_intf_state
{
98 SSIF_GETTING_MESSAGES
,
99 /* FIXME - add watchdog stuff. */
102 #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
103 && (ssif)->curr_msg == NULL)
106 * Indexes into stats[] in ssif_info below.
108 enum ssif_stat_indexes
{
109 /* Number of total messages sent. */
110 SSIF_STAT_sent_messages
= 0,
113 * Number of message parts sent. Messages may be broken into
114 * parts if they are long.
116 SSIF_STAT_sent_messages_parts
,
119 * Number of time a message was retried.
121 SSIF_STAT_send_retries
,
124 * Number of times the send of a message failed.
126 SSIF_STAT_send_errors
,
129 * Number of message responses received.
131 SSIF_STAT_received_messages
,
134 * Number of message fragments received.
136 SSIF_STAT_received_message_parts
,
139 * Number of times the receive of a message was retried.
141 SSIF_STAT_receive_retries
,
144 * Number of errors receiving messages.
146 SSIF_STAT_receive_errors
,
149 * Number of times a flag fetch was requested.
151 SSIF_STAT_flag_fetches
,
154 * Number of times the hardware didn't follow the state machine.
159 * Number of received events.
163 /* Number of asyncronous messages received. */
164 SSIF_STAT_incoming_messages
,
166 /* Number of watchdog pretimeouts. */
167 SSIF_STAT_watchdog_pretimeouts
,
169 /* Number of alers received. */
172 /* Always add statistics before this value, it must be last. */
176 struct ssif_addr_info
{
177 struct i2c_board_info binfo
;
181 enum ipmi_addr_src addr_src
;
182 union ipmi_smi_info_union addr_info
;
184 struct i2c_client
*client
;
186 struct i2c_client
*added_client
;
188 struct mutex clients_mutex
;
189 struct list_head clients
;
191 struct list_head link
;
196 typedef void (*ssif_i2c_done
)(struct ssif_info
*ssif_info
, int result
,
197 unsigned char *data
, unsigned int len
);
200 struct ipmi_smi
*intf
;
202 struct ipmi_smi_msg
*waiting_msg
;
203 struct ipmi_smi_msg
*curr_msg
;
204 enum ssif_intf_state ssif_state
;
205 unsigned long ssif_debug
;
207 struct ipmi_smi_handlers handlers
;
209 enum ipmi_addr_src addr_source
; /* ACPI, PCI, SMBIOS, hardcode, etc. */
210 union ipmi_smi_info_union addr_info
;
213 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
214 * is set to hold the flags until we are done handling everything
217 #define RECEIVE_MSG_AVAIL 0x01
218 #define EVENT_MSG_BUFFER_FULL 0x02
219 #define WDT_PRE_TIMEOUT_INT 0x08
220 unsigned char msg_flags
;
223 bool has_event_buffer
;
227 * Used to tell what we should do with alerts. If we are
228 * waiting on a response, read the data immediately.
234 * If set to true, this will request events the next time the
235 * state machine is idle.
240 * If set to true, this will request flags the next time the
241 * state machine is idle.
246 * Used to perform timer operations when run-to-completion
247 * mode is on. This is a countdown timer.
251 /* Used for sending/receiving data. +1 for the length. */
252 unsigned char data
[IPMI_MAX_MSG_LENGTH
+ 1];
253 unsigned int data_len
;
255 /* Temp receive buffer, gets copied into data. */
256 unsigned char recv
[I2C_SMBUS_BLOCK_MAX
];
258 struct i2c_client
*client
;
259 ssif_i2c_done done_handler
;
261 /* Thread interface handling */
262 struct task_struct
*thread
;
263 struct completion wake_thread
;
267 unsigned char *i2c_data
;
268 unsigned int i2c_size
;
270 struct timer_list retry_timer
;
273 /* Info from SSIF cmd */
274 unsigned char max_xmit_msg_size
;
275 unsigned char max_recv_msg_size
;
276 bool cmd8_works
; /* See test_multipart_messages() for details. */
277 unsigned int multi_support
;
280 #define SSIF_NO_MULTI 0
281 #define SSIF_MULTI_2_PART 1
282 #define SSIF_MULTI_n_PART 2
283 unsigned char *multi_data
;
284 unsigned int multi_len
;
285 unsigned int multi_pos
;
287 atomic_t stats
[SSIF_NUM_STATS
];
290 #define ssif_inc_stat(ssif, stat) \
291 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
292 #define ssif_get_stat(ssif, stat) \
293 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
295 static bool initialized
;
297 static void return_hosed_msg(struct ssif_info
*ssif_info
,
298 struct ipmi_smi_msg
*msg
);
299 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
);
300 static int start_send(struct ssif_info
*ssif_info
,
304 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info
*ssif_info
,
305 unsigned long *flags
)
307 spin_lock_irqsave(&ssif_info
->lock
, *flags
);
311 static void ipmi_ssif_unlock_cond(struct ssif_info
*ssif_info
,
312 unsigned long *flags
)
314 spin_unlock_irqrestore(&ssif_info
->lock
, *flags
);
317 static void deliver_recv_msg(struct ssif_info
*ssif_info
,
318 struct ipmi_smi_msg
*msg
)
320 if (msg
->rsp_size
< 0) {
321 return_hosed_msg(ssif_info
, msg
);
322 pr_err("%s: Malformed message: rsp_size = %d\n",
323 __func__
, msg
->rsp_size
);
325 ipmi_smi_msg_received(ssif_info
->intf
, msg
);
329 static void return_hosed_msg(struct ssif_info
*ssif_info
,
330 struct ipmi_smi_msg
*msg
)
332 ssif_inc_stat(ssif_info
, hosed
);
334 /* Make it a response */
335 msg
->rsp
[0] = msg
->data
[0] | 4;
336 msg
->rsp
[1] = msg
->data
[1];
337 msg
->rsp
[2] = 0xFF; /* Unknown error. */
340 deliver_recv_msg(ssif_info
, msg
);
344 * Must be called with the message lock held. This will release the
345 * message lock. Note that the caller will check SSIF_IDLE and start a
346 * new operation, so there is no need to check for new messages to
349 static void start_clear_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
351 unsigned char msg
[3];
353 ssif_info
->msg_flags
&= ~WDT_PRE_TIMEOUT_INT
;
354 ssif_info
->ssif_state
= SSIF_CLEARING_FLAGS
;
355 ipmi_ssif_unlock_cond(ssif_info
, flags
);
357 /* Make sure the watchdog pre-timeout flag is not set at startup. */
358 msg
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
359 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
360 msg
[2] = WDT_PRE_TIMEOUT_INT
;
362 if (start_send(ssif_info
, msg
, 3) != 0) {
363 /* Error, just go to normal state. */
364 ssif_info
->ssif_state
= SSIF_NORMAL
;
368 static void start_flag_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
372 ssif_info
->req_flags
= false;
373 ssif_info
->ssif_state
= SSIF_GETTING_FLAGS
;
374 ipmi_ssif_unlock_cond(ssif_info
, flags
);
376 mb
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
377 mb
[1] = IPMI_GET_MSG_FLAGS_CMD
;
378 if (start_send(ssif_info
, mb
, 2) != 0)
379 ssif_info
->ssif_state
= SSIF_NORMAL
;
382 static void check_start_send(struct ssif_info
*ssif_info
, unsigned long *flags
,
383 struct ipmi_smi_msg
*msg
)
385 if (start_send(ssif_info
, msg
->data
, msg
->data_size
) != 0) {
386 unsigned long oflags
;
388 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
389 ssif_info
->curr_msg
= NULL
;
390 ssif_info
->ssif_state
= SSIF_NORMAL
;
391 ipmi_ssif_unlock_cond(ssif_info
, flags
);
392 ipmi_free_smi_msg(msg
);
396 static void start_event_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
398 struct ipmi_smi_msg
*msg
;
400 ssif_info
->req_events
= false;
402 msg
= ipmi_alloc_smi_msg();
404 ssif_info
->ssif_state
= SSIF_NORMAL
;
405 ipmi_ssif_unlock_cond(ssif_info
, flags
);
409 ssif_info
->curr_msg
= msg
;
410 ssif_info
->ssif_state
= SSIF_GETTING_EVENTS
;
411 ipmi_ssif_unlock_cond(ssif_info
, flags
);
413 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
414 msg
->data
[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD
;
417 check_start_send(ssif_info
, flags
, msg
);
420 static void start_recv_msg_fetch(struct ssif_info
*ssif_info
,
421 unsigned long *flags
)
423 struct ipmi_smi_msg
*msg
;
425 msg
= ipmi_alloc_smi_msg();
427 ssif_info
->ssif_state
= SSIF_NORMAL
;
428 ipmi_ssif_unlock_cond(ssif_info
, flags
);
432 ssif_info
->curr_msg
= msg
;
433 ssif_info
->ssif_state
= SSIF_GETTING_MESSAGES
;
434 ipmi_ssif_unlock_cond(ssif_info
, flags
);
436 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
437 msg
->data
[1] = IPMI_GET_MSG_CMD
;
440 check_start_send(ssif_info
, flags
, msg
);
444 * Must be called with the message lock held. This will release the
445 * message lock. Note that the caller will check SSIF_IDLE and start a
446 * new operation, so there is no need to check for new messages to
449 static void handle_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
451 if (ssif_info
->msg_flags
& WDT_PRE_TIMEOUT_INT
) {
452 /* Watchdog pre-timeout */
453 ssif_inc_stat(ssif_info
, watchdog_pretimeouts
);
454 start_clear_flags(ssif_info
, flags
);
455 ipmi_smi_watchdog_pretimeout(ssif_info
->intf
);
456 } else if (ssif_info
->msg_flags
& RECEIVE_MSG_AVAIL
)
457 /* Messages available. */
458 start_recv_msg_fetch(ssif_info
, flags
);
459 else if (ssif_info
->msg_flags
& EVENT_MSG_BUFFER_FULL
)
460 /* Events available. */
461 start_event_fetch(ssif_info
, flags
);
463 ssif_info
->ssif_state
= SSIF_NORMAL
;
464 ipmi_ssif_unlock_cond(ssif_info
, flags
);
468 static int ipmi_ssif_thread(void *data
)
470 struct ssif_info
*ssif_info
= data
;
472 while (!kthread_should_stop()) {
475 /* Wait for something to do */
476 result
= wait_for_completion_interruptible(
477 &ssif_info
->wake_thread
);
478 if (ssif_info
->stopping
)
480 if (result
== -ERESTARTSYS
)
482 init_completion(&ssif_info
->wake_thread
);
484 if (ssif_info
->i2c_read_write
== I2C_SMBUS_WRITE
) {
485 result
= i2c_smbus_write_block_data(
486 ssif_info
->client
, ssif_info
->i2c_command
,
487 ssif_info
->i2c_data
[0],
488 ssif_info
->i2c_data
+ 1);
489 ssif_info
->done_handler(ssif_info
, result
, NULL
, 0);
491 result
= i2c_smbus_read_block_data(
492 ssif_info
->client
, ssif_info
->i2c_command
,
493 ssif_info
->i2c_data
);
495 ssif_info
->done_handler(ssif_info
, result
,
498 ssif_info
->done_handler(ssif_info
, 0,
507 static int ssif_i2c_send(struct ssif_info
*ssif_info
,
508 ssif_i2c_done handler
,
509 int read_write
, int command
,
510 unsigned char *data
, unsigned int size
)
512 ssif_info
->done_handler
= handler
;
514 ssif_info
->i2c_read_write
= read_write
;
515 ssif_info
->i2c_command
= command
;
516 ssif_info
->i2c_data
= data
;
517 ssif_info
->i2c_size
= size
;
518 complete(&ssif_info
->wake_thread
);
523 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
524 unsigned char *data
, unsigned int len
);
526 static void start_get(struct ssif_info
*ssif_info
)
530 ssif_info
->rtc_us_timer
= 0;
531 ssif_info
->multi_pos
= 0;
533 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
535 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
537 /* request failed, just return the error. */
538 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
539 pr_info("Error from i2c_non_blocking_op(5)\n");
541 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
545 static void retry_timeout(struct timer_list
*t
)
547 struct ssif_info
*ssif_info
= from_timer(ssif_info
, t
, retry_timer
);
548 unsigned long oflags
, *flags
;
551 if (ssif_info
->stopping
)
554 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
555 waiting
= ssif_info
->waiting_alert
;
556 ssif_info
->waiting_alert
= false;
557 ipmi_ssif_unlock_cond(ssif_info
, flags
);
560 start_get(ssif_info
);
564 static void ssif_alert(struct i2c_client
*client
, enum i2c_alert_protocol type
,
567 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
568 unsigned long oflags
, *flags
;
571 if (type
!= I2C_PROTOCOL_SMBUS_ALERT
)
574 ssif_inc_stat(ssif_info
, alerts
);
576 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
577 if (ssif_info
->waiting_alert
) {
578 ssif_info
->waiting_alert
= false;
579 del_timer(&ssif_info
->retry_timer
);
581 } else if (ssif_info
->curr_msg
) {
582 ssif_info
->got_alert
= true;
584 ipmi_ssif_unlock_cond(ssif_info
, flags
);
586 start_get(ssif_info
);
589 static int start_resend(struct ssif_info
*ssif_info
);
591 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
592 unsigned char *data
, unsigned int len
)
594 struct ipmi_smi_msg
*msg
;
595 unsigned long oflags
, *flags
;
599 * We are single-threaded here, so no need for a lock until we
600 * start messing with driver states or the queues.
604 ssif_info
->retries_left
--;
605 if (ssif_info
->retries_left
> 0) {
606 ssif_inc_stat(ssif_info
, receive_retries
);
608 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
609 ssif_info
->waiting_alert
= true;
610 ssif_info
->rtc_us_timer
= SSIF_MSG_USEC
;
611 if (!ssif_info
->stopping
)
612 mod_timer(&ssif_info
->retry_timer
,
613 jiffies
+ SSIF_MSG_JIFFIES
);
614 ipmi_ssif_unlock_cond(ssif_info
, flags
);
618 ssif_inc_stat(ssif_info
, receive_errors
);
620 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
621 pr_info("Error in msg_done_handler: %d\n", result
);
626 if ((len
> 1) && (ssif_info
->multi_pos
== 0)
627 && (data
[0] == 0x00) && (data
[1] == 0x01)) {
628 /* Start of multi-part read. Start the next transaction. */
631 ssif_inc_stat(ssif_info
, received_message_parts
);
633 /* Remove the multi-part read marker. */
635 for (i
= 0; i
< len
; i
++)
636 ssif_info
->data
[i
] = data
[i
+2];
637 ssif_info
->multi_len
= len
;
638 ssif_info
->multi_pos
= 1;
640 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
641 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
642 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
644 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
645 pr_info("Error from i2c_non_blocking_op(1)\n");
650 } else if (ssif_info
->multi_pos
) {
651 /* Middle of multi-part read. Start the next transaction. */
653 unsigned char blocknum
;
657 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
658 pr_info("Middle message with no data\n");
665 if (ssif_info
->multi_len
+ len
- 1 > IPMI_MAX_MSG_LENGTH
) {
666 /* Received message too big, abort the operation. */
668 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
669 pr_info("Received message too big\n");
674 /* Remove the blocknum from the data. */
676 for (i
= 0; i
< len
; i
++)
677 ssif_info
->data
[i
+ ssif_info
->multi_len
] = data
[i
+ 1];
678 ssif_info
->multi_len
+= len
;
679 if (blocknum
== 0xff) {
681 len
= ssif_info
->multi_len
;
682 data
= ssif_info
->data
;
683 } else if (blocknum
+ 1 != ssif_info
->multi_pos
) {
685 * Out of sequence block, just abort. Block
686 * numbers start at zero for the second block,
687 * but multi_pos starts at one, so the +1.
691 ssif_inc_stat(ssif_info
, received_message_parts
);
693 ssif_info
->multi_pos
++;
695 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
,
697 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
699 I2C_SMBUS_BLOCK_DATA
);
701 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
702 pr_info("Error from ssif_i2c_send\n");
711 ssif_inc_stat(ssif_info
, receive_errors
);
713 ssif_inc_stat(ssif_info
, received_messages
);
714 ssif_inc_stat(ssif_info
, received_message_parts
);
719 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
720 pr_info("DONE 1: state = %d, result=%d\n",
721 ssif_info
->ssif_state
, result
);
723 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
724 msg
= ssif_info
->curr_msg
;
727 if (msg
->rsp_size
> IPMI_MAX_MSG_LENGTH
)
728 msg
->rsp_size
= IPMI_MAX_MSG_LENGTH
;
729 memcpy(msg
->rsp
, data
, msg
->rsp_size
);
730 ssif_info
->curr_msg
= NULL
;
733 switch (ssif_info
->ssif_state
) {
735 ipmi_ssif_unlock_cond(ssif_info
, flags
);
740 return_hosed_msg(ssif_info
, msg
);
742 deliver_recv_msg(ssif_info
, msg
);
745 case SSIF_GETTING_FLAGS
:
746 /* We got the flags from the SSIF, now handle them. */
747 if ((result
< 0) || (len
< 4) || (data
[2] != 0)) {
749 * Error fetching flags, or invalid length,
750 * just give up for now.
752 ssif_info
->ssif_state
= SSIF_NORMAL
;
753 ipmi_ssif_unlock_cond(ssif_info
, flags
);
754 pr_warn("Error getting flags: %d %d, %x\n",
755 result
, len
, (len
>= 3) ? data
[2] : 0);
756 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
757 || data
[1] != IPMI_GET_MSG_FLAGS_CMD
) {
759 * Don't abort here, maybe it was a queued
760 * response to a previous command.
762 ipmi_ssif_unlock_cond(ssif_info
, flags
);
763 pr_warn("Invalid response getting flags: %x %x\n",
766 ssif_inc_stat(ssif_info
, flag_fetches
);
767 ssif_info
->msg_flags
= data
[3];
768 handle_flags(ssif_info
, flags
);
772 case SSIF_CLEARING_FLAGS
:
773 /* We cleared the flags. */
774 if ((result
< 0) || (len
< 3) || (data
[2] != 0)) {
775 /* Error clearing flags */
776 pr_warn("Error clearing flags: %d %d, %x\n",
777 result
, len
, (len
>= 3) ? data
[2] : 0);
778 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
779 || data
[1] != IPMI_CLEAR_MSG_FLAGS_CMD
) {
780 pr_warn("Invalid response clearing flags: %x %x\n",
783 ssif_info
->ssif_state
= SSIF_NORMAL
;
784 ipmi_ssif_unlock_cond(ssif_info
, flags
);
787 case SSIF_GETTING_EVENTS
:
788 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
789 /* Error getting event, probably done. */
792 /* Take off the event flag. */
793 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
794 handle_flags(ssif_info
, flags
);
795 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
796 || msg
->rsp
[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD
) {
797 pr_warn("Invalid response getting events: %x %x\n",
798 msg
->rsp
[0], msg
->rsp
[1]);
800 /* Take off the event flag. */
801 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
802 handle_flags(ssif_info
, flags
);
804 handle_flags(ssif_info
, flags
);
805 ssif_inc_stat(ssif_info
, events
);
806 deliver_recv_msg(ssif_info
, msg
);
810 case SSIF_GETTING_MESSAGES
:
811 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
812 /* Error getting event, probably done. */
815 /* Take off the msg flag. */
816 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
817 handle_flags(ssif_info
, flags
);
818 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
819 || msg
->rsp
[1] != IPMI_GET_MSG_CMD
) {
820 pr_warn("Invalid response clearing flags: %x %x\n",
821 msg
->rsp
[0], msg
->rsp
[1]);
824 /* Take off the msg flag. */
825 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
826 handle_flags(ssif_info
, flags
);
828 ssif_inc_stat(ssif_info
, incoming_messages
);
829 handle_flags(ssif_info
, flags
);
830 deliver_recv_msg(ssif_info
, msg
);
835 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
836 if (SSIF_IDLE(ssif_info
) && !ssif_info
->stopping
) {
837 if (ssif_info
->req_events
)
838 start_event_fetch(ssif_info
, flags
);
839 else if (ssif_info
->req_flags
)
840 start_flag_fetch(ssif_info
, flags
);
842 start_next_msg(ssif_info
, flags
);
844 ipmi_ssif_unlock_cond(ssif_info
, flags
);
846 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
847 pr_info("DONE 2: state = %d.\n", ssif_info
->ssif_state
);
850 static void msg_written_handler(struct ssif_info
*ssif_info
, int result
,
851 unsigned char *data
, unsigned int len
)
855 /* We are single-threaded here, so no need for a lock. */
857 ssif_info
->retries_left
--;
858 if (ssif_info
->retries_left
> 0) {
859 if (!start_resend(ssif_info
)) {
860 ssif_inc_stat(ssif_info
, send_retries
);
863 /* request failed, just return the error. */
864 ssif_inc_stat(ssif_info
, send_errors
);
866 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
867 pr_info("%s: Out of retries\n", __func__
);
868 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
872 ssif_inc_stat(ssif_info
, send_errors
);
875 * Got an error on transmit, let the done routine
878 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
879 pr_info("Error in msg_written_handler: %d\n", result
);
881 msg_done_handler(ssif_info
, result
, NULL
, 0);
885 if (ssif_info
->multi_data
) {
887 * In the middle of a multi-data write. See the comment
888 * in the SSIF_MULTI_n_PART case in the probe function
889 * for details on the intricacies of this.
892 unsigned char *data_to_send
;
895 ssif_inc_stat(ssif_info
, sent_messages_parts
);
897 left
= ssif_info
->multi_len
- ssif_info
->multi_pos
;
902 ssif_info
->multi_data
[ssif_info
->multi_pos
] = to_write
;
903 data_to_send
= ssif_info
->multi_data
+ ssif_info
->multi_pos
;
904 ssif_info
->multi_pos
+= to_write
;
905 cmd
= SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
;
906 if (ssif_info
->cmd8_works
) {
907 if (left
== to_write
) {
908 cmd
= SSIF_IPMI_MULTI_PART_REQUEST_END
;
909 ssif_info
->multi_data
= NULL
;
911 } else if (to_write
< 32) {
912 ssif_info
->multi_data
= NULL
;
915 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
,
916 I2C_SMBUS_WRITE
, cmd
,
917 data_to_send
, I2C_SMBUS_BLOCK_DATA
);
919 /* request failed, just return the error. */
920 ssif_inc_stat(ssif_info
, send_errors
);
922 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
923 pr_info("Error from i2c_non_blocking_op(3)\n");
924 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
927 /* Ready to request the result. */
928 unsigned long oflags
, *flags
;
930 ssif_inc_stat(ssif_info
, sent_messages
);
931 ssif_inc_stat(ssif_info
, sent_messages_parts
);
933 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
934 if (ssif_info
->got_alert
) {
935 /* The result is already ready, just start it. */
936 ssif_info
->got_alert
= false;
937 ipmi_ssif_unlock_cond(ssif_info
, flags
);
938 start_get(ssif_info
);
940 /* Wait a jiffie then request the next message */
941 ssif_info
->waiting_alert
= true;
942 ssif_info
->retries_left
= SSIF_RECV_RETRIES
;
943 ssif_info
->rtc_us_timer
= SSIF_MSG_PART_USEC
;
944 if (!ssif_info
->stopping
)
945 mod_timer(&ssif_info
->retry_timer
,
946 jiffies
+ SSIF_MSG_PART_JIFFIES
);
947 ipmi_ssif_unlock_cond(ssif_info
, flags
);
952 static int start_resend(struct ssif_info
*ssif_info
)
957 ssif_info
->got_alert
= false;
959 if (ssif_info
->data_len
> 32) {
960 command
= SSIF_IPMI_MULTI_PART_REQUEST_START
;
961 ssif_info
->multi_data
= ssif_info
->data
;
962 ssif_info
->multi_len
= ssif_info
->data_len
;
964 * Subtle thing, this is 32, not 33, because we will
965 * overwrite the thing at position 32 (which was just
966 * transmitted) with the new length.
968 ssif_info
->multi_pos
= 32;
969 ssif_info
->data
[0] = 32;
971 ssif_info
->multi_data
= NULL
;
972 command
= SSIF_IPMI_REQUEST
;
973 ssif_info
->data
[0] = ssif_info
->data_len
;
976 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
, I2C_SMBUS_WRITE
,
977 command
, ssif_info
->data
, I2C_SMBUS_BLOCK_DATA
);
978 if (rv
&& (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
))
979 pr_info("Error from i2c_non_blocking_op(4)\n");
983 static int start_send(struct ssif_info
*ssif_info
,
987 if (len
> IPMI_MAX_MSG_LENGTH
)
989 if (len
> ssif_info
->max_xmit_msg_size
)
992 ssif_info
->retries_left
= SSIF_SEND_RETRIES
;
993 memcpy(ssif_info
->data
+ 1, data
, len
);
994 ssif_info
->data_len
= len
;
995 return start_resend(ssif_info
);
998 /* Must be called with the message lock held. */
999 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
)
1001 struct ipmi_smi_msg
*msg
;
1002 unsigned long oflags
;
1005 if (!SSIF_IDLE(ssif_info
)) {
1006 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1010 if (!ssif_info
->waiting_msg
) {
1011 ssif_info
->curr_msg
= NULL
;
1012 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1016 ssif_info
->curr_msg
= ssif_info
->waiting_msg
;
1017 ssif_info
->waiting_msg
= NULL
;
1018 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1019 rv
= start_send(ssif_info
,
1020 ssif_info
->curr_msg
->data
,
1021 ssif_info
->curr_msg
->data_size
);
1023 msg
= ssif_info
->curr_msg
;
1024 ssif_info
->curr_msg
= NULL
;
1025 return_hosed_msg(ssif_info
, msg
);
1026 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1032 static void sender(void *send_info
,
1033 struct ipmi_smi_msg
*msg
)
1035 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1036 unsigned long oflags
, *flags
;
1038 BUG_ON(ssif_info
->waiting_msg
);
1039 ssif_info
->waiting_msg
= msg
;
1041 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1042 start_next_msg(ssif_info
, flags
);
1044 if (ssif_info
->ssif_debug
& SSIF_DEBUG_TIMING
) {
1045 struct timespec64 t
;
1047 ktime_get_real_ts64(&t
);
1048 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1049 msg
->data
[0], msg
->data
[1],
1050 (long long)t
.tv_sec
, (long)t
.tv_nsec
/ NSEC_PER_USEC
);
1054 static int get_smi_info(void *send_info
, struct ipmi_smi_info
*data
)
1056 struct ssif_info
*ssif_info
= send_info
;
1058 data
->addr_src
= ssif_info
->addr_source
;
1059 data
->dev
= &ssif_info
->client
->dev
;
1060 data
->addr_info
= ssif_info
->addr_info
;
1061 get_device(data
->dev
);
1067 * Instead of having our own timer to periodically check the message
1068 * flags, we let the message handler drive us.
1070 static void request_events(void *send_info
)
1072 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1073 unsigned long oflags
, *flags
;
1075 if (!ssif_info
->has_event_buffer
)
1078 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1080 * Request flags first, not events, because the lower layer
1081 * doesn't have a way to send an attention. But make sure
1082 * event checking still happens.
1084 ssif_info
->req_events
= true;
1085 if (SSIF_IDLE(ssif_info
))
1086 start_flag_fetch(ssif_info
, flags
);
1088 ssif_info
->req_flags
= true;
1089 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1093 static int ssif_start_processing(void *send_info
,
1094 struct ipmi_smi
*intf
)
1096 struct ssif_info
*ssif_info
= send_info
;
1098 ssif_info
->intf
= intf
;
1103 #define MAX_SSIF_BMCS 4
1105 static unsigned short addr
[MAX_SSIF_BMCS
];
1106 static int num_addrs
;
1107 module_param_array(addr
, ushort
, &num_addrs
, 0);
1108 MODULE_PARM_DESC(addr
, "The addresses to scan for IPMI BMCs on the SSIFs.");
1110 static char *adapter_name
[MAX_SSIF_BMCS
];
1111 static int num_adapter_names
;
1112 module_param_array(adapter_name
, charp
, &num_adapter_names
, 0);
1113 MODULE_PARM_DESC(adapter_name
, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1115 static int slave_addrs
[MAX_SSIF_BMCS
];
1116 static int num_slave_addrs
;
1117 module_param_array(slave_addrs
, int, &num_slave_addrs
, 0);
1118 MODULE_PARM_DESC(slave_addrs
,
1119 "The default IPMB slave address for the controller.");
1121 static bool alerts_broken
;
1122 module_param(alerts_broken
, bool, 0);
1123 MODULE_PARM_DESC(alerts_broken
, "Don't enable alerts for the controller.");
1126 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1127 * bit 2 enables timing debugging. This is an array indexed by
1130 static int dbg
[MAX_SSIF_BMCS
];
1132 module_param_array(dbg
, int, &num_dbg
, 0);
1133 MODULE_PARM_DESC(dbg
, "Turn on debugging.");
1135 static bool ssif_dbg_probe
;
1136 module_param_named(dbg_probe
, ssif_dbg_probe
, bool, 0);
1137 MODULE_PARM_DESC(dbg_probe
, "Enable debugging of probing of adapters.");
1139 static bool ssif_tryacpi
= true;
1140 module_param_named(tryacpi
, ssif_tryacpi
, bool, 0);
1141 MODULE_PARM_DESC(tryacpi
, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1143 static bool ssif_trydmi
= true;
1144 module_param_named(trydmi
, ssif_trydmi
, bool, 0);
1145 MODULE_PARM_DESC(trydmi
, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1147 static DEFINE_MUTEX(ssif_infos_mutex
);
1148 static LIST_HEAD(ssif_infos
);
1150 #define IPMI_SSIF_ATTR(name) \
1151 static ssize_t ipmi_##name##_show(struct device *dev, \
1152 struct device_attribute *attr, \
1155 struct ssif_info *ssif_info = dev_get_drvdata(dev); \
1157 return snprintf(buf, 10, "%u\n", ssif_get_stat(ssif_info, name));\
1159 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1161 static ssize_t
ipmi_type_show(struct device
*dev
,
1162 struct device_attribute
*attr
,
1165 return snprintf(buf
, 10, "ssif\n");
1167 static DEVICE_ATTR(type
, S_IRUGO
, ipmi_type_show
, NULL
);
1169 IPMI_SSIF_ATTR(sent_messages
);
1170 IPMI_SSIF_ATTR(sent_messages_parts
);
1171 IPMI_SSIF_ATTR(send_retries
);
1172 IPMI_SSIF_ATTR(send_errors
);
1173 IPMI_SSIF_ATTR(received_messages
);
1174 IPMI_SSIF_ATTR(received_message_parts
);
1175 IPMI_SSIF_ATTR(receive_retries
);
1176 IPMI_SSIF_ATTR(receive_errors
);
1177 IPMI_SSIF_ATTR(flag_fetches
);
1178 IPMI_SSIF_ATTR(hosed
);
1179 IPMI_SSIF_ATTR(events
);
1180 IPMI_SSIF_ATTR(watchdog_pretimeouts
);
1181 IPMI_SSIF_ATTR(alerts
);
1183 static struct attribute
*ipmi_ssif_dev_attrs
[] = {
1184 &dev_attr_type
.attr
,
1185 &dev_attr_sent_messages
.attr
,
1186 &dev_attr_sent_messages_parts
.attr
,
1187 &dev_attr_send_retries
.attr
,
1188 &dev_attr_send_errors
.attr
,
1189 &dev_attr_received_messages
.attr
,
1190 &dev_attr_received_message_parts
.attr
,
1191 &dev_attr_receive_retries
.attr
,
1192 &dev_attr_receive_errors
.attr
,
1193 &dev_attr_flag_fetches
.attr
,
1194 &dev_attr_hosed
.attr
,
1195 &dev_attr_events
.attr
,
1196 &dev_attr_watchdog_pretimeouts
.attr
,
1197 &dev_attr_alerts
.attr
,
1201 static const struct attribute_group ipmi_ssif_dev_attr_group
= {
1202 .attrs
= ipmi_ssif_dev_attrs
,
1205 static void shutdown_ssif(void *send_info
)
1207 struct ssif_info
*ssif_info
= send_info
;
1209 device_remove_group(&ssif_info
->client
->dev
, &ipmi_ssif_dev_attr_group
);
1210 dev_set_drvdata(&ssif_info
->client
->dev
, NULL
);
1212 /* make sure the driver is not looking for flags any more. */
1213 while (ssif_info
->ssif_state
!= SSIF_NORMAL
)
1214 schedule_timeout(1);
1216 ssif_info
->stopping
= true;
1217 del_timer_sync(&ssif_info
->retry_timer
);
1218 if (ssif_info
->thread
) {
1219 complete(&ssif_info
->wake_thread
);
1220 kthread_stop(ssif_info
->thread
);
1224 static int ssif_remove(struct i2c_client
*client
)
1226 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
1227 struct ssif_addr_info
*addr_info
;
1233 * After this point, we won't deliver anything asychronously
1234 * to the message handler. We can unregister ourself.
1236 ipmi_unregister_smi(ssif_info
->intf
);
1238 list_for_each_entry(addr_info
, &ssif_infos
, link
) {
1239 if (addr_info
->client
== client
) {
1240 addr_info
->client
= NULL
;
1250 static int read_response(struct i2c_client
*client
, unsigned char *resp
)
1252 int ret
= -ENODEV
, retry_cnt
= SSIF_RECV_RETRIES
;
1254 while (retry_cnt
> 0) {
1255 ret
= i2c_smbus_read_block_data(client
, SSIF_IPMI_RESPONSE
,
1259 msleep(SSIF_MSG_MSEC
);
1268 static int do_cmd(struct i2c_client
*client
, int len
, unsigned char *msg
,
1269 int *resp_len
, unsigned char *resp
)
1274 retry_cnt
= SSIF_SEND_RETRIES
;
1276 ret
= i2c_smbus_write_block_data(client
, SSIF_IPMI_REQUEST
, len
, msg
);
1284 ret
= read_response(client
, resp
);
1286 /* Validate that the response is correct. */
1288 (resp
[0] != (msg
[0] | (1 << 2))) ||
1289 (resp
[1] != msg
[1]))
1291 else if (ret
> IPMI_MAX_MSG_LENGTH
) {
1302 static int ssif_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
1304 unsigned char *resp
;
1305 unsigned char msg
[3];
1309 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1313 /* Do a Get Device ID command, since it is required. */
1314 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1315 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1316 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1320 strlcpy(info
->type
, DEVICE_NAME
, I2C_NAME_SIZE
);
1325 static int strcmp_nospace(char *s1
, char *s2
)
1327 while (*s1
&& *s2
) {
1328 while (isspace(*s1
))
1330 while (isspace(*s2
))
1342 static struct ssif_addr_info
*ssif_info_find(unsigned short addr
,
1344 bool match_null_name
)
1346 struct ssif_addr_info
*info
, *found
= NULL
;
1349 list_for_each_entry(info
, &ssif_infos
, link
) {
1350 if (info
->binfo
.addr
== addr
) {
1351 if (info
->adapter_name
|| adapter_name
) {
1352 if (!info
->adapter_name
!= !adapter_name
) {
1353 /* One is NULL and one is not */
1357 strcmp_nospace(info
->adapter_name
,
1359 /* Names do not match */
1367 if (!found
&& match_null_name
) {
1368 /* Try to get an exact match first, then try with a NULL name */
1369 adapter_name
= NULL
;
1370 match_null_name
= false;
1377 static bool check_acpi(struct ssif_info
*ssif_info
, struct device
*dev
)
1380 acpi_handle acpi_handle
;
1382 acpi_handle
= ACPI_HANDLE(dev
);
1384 ssif_info
->addr_source
= SI_ACPI
;
1385 ssif_info
->addr_info
.acpi_info
.acpi_handle
= acpi_handle
;
1392 static int find_slave_address(struct i2c_client
*client
, int slave_addr
)
1394 #ifdef CONFIG_IPMI_DMI_DECODE
1396 slave_addr
= ipmi_dmi_get_slave_addr(
1398 i2c_adapter_id(client
->adapter
),
1405 static int start_multipart_test(struct i2c_client
*client
,
1406 unsigned char *msg
, bool do_middle
)
1408 int retry_cnt
= SSIF_SEND_RETRIES
, ret
;
1411 ret
= i2c_smbus_write_block_data(client
,
1412 SSIF_IPMI_MULTI_PART_REQUEST_START
,
1418 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");
1425 ret
= i2c_smbus_write_block_data(client
,
1426 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
,
1429 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");
1436 static void test_multipart_messages(struct i2c_client
*client
,
1437 struct ssif_info
*ssif_info
,
1438 unsigned char *resp
)
1440 unsigned char msg
[65];
1444 if (ssif_info
->max_xmit_msg_size
<= 32)
1447 do_middle
= ssif_info
->max_xmit_msg_size
> 63;
1449 memset(msg
, 0, sizeof(msg
));
1450 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1451 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1454 * The specification is all messed up dealing with sending
1455 * multi-part messages. Per what the specification says, it
1456 * is impossible to send a message that is a multiple of 32
1457 * bytes, except for 32 itself. It talks about a "start"
1458 * transaction (cmd=6) that must be 32 bytes, "middle"
1459 * transaction (cmd=7) that must be 32 bytes, and an "end"
1460 * transaction. The "end" transaction is shown as cmd=7 in
1461 * the text, but if that's the case there is no way to
1462 * differentiate between a middle and end part except the
1463 * length being less than 32. But there is a table at the far
1464 * end of the section (that I had never noticed until someone
1465 * pointed it out to me) that mentions it as cmd=8.
1467 * After some thought, I think the example is wrong and the
1468 * end transaction should be cmd=8. But some systems don't
1469 * implement cmd=8, they use a zero-length end transaction,
1470 * even though that violates the SMBus specification.
1472 * So, to work around this, this code tests if cmd=8 works.
1473 * If it does, then we use that. If not, it tests zero-
1474 * byte end transactions. If that works, good. If not,
1475 * we only allow 63-byte transactions max.
1478 ret
= start_multipart_test(client
, msg
, do_middle
);
1480 goto out_no_multi_part
;
1482 ret
= i2c_smbus_write_block_data(client
,
1483 SSIF_IPMI_MULTI_PART_REQUEST_END
,
1487 ret
= read_response(client
, resp
);
1490 /* End transactions work, we are good. */
1491 ssif_info
->cmd8_works
= true;
1495 ret
= start_multipart_test(client
, msg
, do_middle
);
1497 dev_err(&client
->dev
, "Second multipart test failed.\n");
1498 goto out_no_multi_part
;
1501 ret
= i2c_smbus_write_block_data(client
,
1502 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
,
1505 ret
= read_response(client
, resp
);
1507 /* Zero-size end parts work, use those. */
1510 /* Limit to 63 bytes and use a short middle command to mark the end. */
1511 if (ssif_info
->max_xmit_msg_size
> 63)
1512 ssif_info
->max_xmit_msg_size
= 63;
1516 ssif_info
->max_xmit_msg_size
= 32;
1521 * Global enables we care about.
1523 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1524 IPMI_BMC_EVT_MSG_INTR)
1526 static int ssif_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1528 unsigned char msg
[3];
1529 unsigned char *resp
;
1530 struct ssif_info
*ssif_info
;
1535 struct ssif_addr_info
*addr_info
= NULL
;
1537 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1541 ssif_info
= kzalloc(sizeof(*ssif_info
), GFP_KERNEL
);
1547 if (!check_acpi(ssif_info
, &client
->dev
)) {
1548 addr_info
= ssif_info_find(client
->addr
, client
->adapter
->name
,
1551 /* Must have come in through sysfs. */
1552 ssif_info
->addr_source
= SI_HOTMOD
;
1554 ssif_info
->addr_source
= addr_info
->addr_src
;
1555 ssif_info
->ssif_debug
= addr_info
->debug
;
1556 ssif_info
->addr_info
= addr_info
->addr_info
;
1557 addr_info
->client
= client
;
1558 slave_addr
= addr_info
->slave_addr
;
1562 slave_addr
= find_slave_address(client
, slave_addr
);
1564 pr_info("Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1565 ipmi_addr_src_to_str(ssif_info
->addr_source
),
1566 client
->addr
, client
->adapter
->name
, slave_addr
);
1568 ssif_info
->client
= client
;
1569 i2c_set_clientdata(client
, ssif_info
);
1571 /* Now check for system interface capabilities */
1572 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1573 msg
[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD
;
1574 msg
[2] = 0; /* SSIF */
1575 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1576 if (!rv
&& (len
>= 3) && (resp
[2] == 0)) {
1579 pr_info("SSIF info too short: %d\n", len
);
1583 /* Got a good SSIF response, handle it. */
1584 ssif_info
->max_xmit_msg_size
= resp
[5];
1585 ssif_info
->max_recv_msg_size
= resp
[6];
1586 ssif_info
->multi_support
= (resp
[4] >> 6) & 0x3;
1587 ssif_info
->supports_pec
= (resp
[4] >> 3) & 0x1;
1589 /* Sanitize the data */
1590 switch (ssif_info
->multi_support
) {
1592 if (ssif_info
->max_xmit_msg_size
> 32)
1593 ssif_info
->max_xmit_msg_size
= 32;
1594 if (ssif_info
->max_recv_msg_size
> 32)
1595 ssif_info
->max_recv_msg_size
= 32;
1598 case SSIF_MULTI_2_PART
:
1599 if (ssif_info
->max_xmit_msg_size
> 63)
1600 ssif_info
->max_xmit_msg_size
= 63;
1601 if (ssif_info
->max_recv_msg_size
> 62)
1602 ssif_info
->max_recv_msg_size
= 62;
1605 case SSIF_MULTI_n_PART
:
1606 /* We take whatever size given, but do some testing. */
1610 /* Data is not sane, just give up. */
1615 /* Assume no multi-part or PEC support */
1616 pr_info("Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1619 ssif_info
->max_xmit_msg_size
= 32;
1620 ssif_info
->max_recv_msg_size
= 32;
1621 ssif_info
->multi_support
= SSIF_NO_MULTI
;
1622 ssif_info
->supports_pec
= 0;
1625 test_multipart_messages(client
, ssif_info
, resp
);
1627 /* Make sure the NMI timeout is cleared. */
1628 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1629 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
1630 msg
[2] = WDT_PRE_TIMEOUT_INT
;
1631 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1632 if (rv
|| (len
< 3) || (resp
[2] != 0))
1633 pr_warn("Unable to clear message flags: %d %d %2.2x\n",
1636 /* Attempt to enable the event buffer. */
1637 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1638 msg
[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD
;
1639 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1640 if (rv
|| (len
< 4) || (resp
[2] != 0)) {
1641 pr_warn("Error getting global enables: %d %d %2.2x\n",
1643 rv
= 0; /* Not fatal */
1647 ssif_info
->global_enables
= resp
[3];
1649 if (resp
[3] & IPMI_BMC_EVT_MSG_BUFF
) {
1650 ssif_info
->has_event_buffer
= true;
1651 /* buffer is already enabled, nothing to do. */
1655 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1656 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1657 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_EVT_MSG_BUFF
;
1658 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1659 if (rv
|| (len
< 2)) {
1660 pr_warn("Error setting global enables: %d %d %2.2x\n",
1662 rv
= 0; /* Not fatal */
1667 /* A successful return means the event buffer is supported. */
1668 ssif_info
->has_event_buffer
= true;
1669 ssif_info
->global_enables
|= IPMI_BMC_EVT_MSG_BUFF
;
1672 /* Some systems don't behave well if you enable alerts. */
1676 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1677 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1678 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_RCV_MSG_INTR
;
1679 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1680 if (rv
|| (len
< 2)) {
1681 pr_warn("Error setting global enables: %d %d %2.2x\n",
1683 rv
= 0; /* Not fatal */
1688 /* A successful return means the alert is supported. */
1689 ssif_info
->supports_alert
= true;
1690 ssif_info
->global_enables
|= IPMI_BMC_RCV_MSG_INTR
;
1694 if (ssif_dbg_probe
) {
1695 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1699 spin_lock_init(&ssif_info
->lock
);
1700 ssif_info
->ssif_state
= SSIF_NORMAL
;
1701 timer_setup(&ssif_info
->retry_timer
, retry_timeout
, 0);
1703 for (i
= 0; i
< SSIF_NUM_STATS
; i
++)
1704 atomic_set(&ssif_info
->stats
[i
], 0);
1706 if (ssif_info
->supports_pec
)
1707 ssif_info
->client
->flags
|= I2C_CLIENT_PEC
;
1709 ssif_info
->handlers
.owner
= THIS_MODULE
;
1710 ssif_info
->handlers
.start_processing
= ssif_start_processing
;
1711 ssif_info
->handlers
.shutdown
= shutdown_ssif
;
1712 ssif_info
->handlers
.get_smi_info
= get_smi_info
;
1713 ssif_info
->handlers
.sender
= sender
;
1714 ssif_info
->handlers
.request_events
= request_events
;
1717 unsigned int thread_num
;
1719 thread_num
= ((i2c_adapter_id(ssif_info
->client
->adapter
)
1721 ssif_info
->client
->addr
);
1722 init_completion(&ssif_info
->wake_thread
);
1723 ssif_info
->thread
= kthread_run(ipmi_ssif_thread
, ssif_info
,
1724 "kssif%4.4x", thread_num
);
1725 if (IS_ERR(ssif_info
->thread
)) {
1726 rv
= PTR_ERR(ssif_info
->thread
);
1727 dev_notice(&ssif_info
->client
->dev
,
1728 "Could not start kernel thread: error %d\n",
1734 dev_set_drvdata(&ssif_info
->client
->dev
, ssif_info
);
1735 rv
= device_add_group(&ssif_info
->client
->dev
,
1736 &ipmi_ssif_dev_attr_group
);
1738 dev_err(&ssif_info
->client
->dev
,
1739 "Unable to add device attributes: error %d\n",
1744 rv
= ipmi_register_smi(&ssif_info
->handlers
,
1746 &ssif_info
->client
->dev
,
1749 pr_err("Unable to register device: error %d\n", rv
);
1750 goto out_remove_attr
;
1756 addr_info
->client
= NULL
;
1758 dev_err(&client
->dev
, "Unable to start IPMI SSIF: %d\n", rv
);
1765 device_remove_group(&ssif_info
->client
->dev
, &ipmi_ssif_dev_attr_group
);
1766 dev_set_drvdata(&ssif_info
->client
->dev
, NULL
);
1770 static int ssif_adapter_handler(struct device
*adev
, void *opaque
)
1772 struct ssif_addr_info
*addr_info
= opaque
;
1774 if (adev
->type
!= &i2c_adapter_type
)
1777 addr_info
->added_client
= i2c_new_device(to_i2c_adapter(adev
),
1780 if (!addr_info
->adapter_name
)
1781 return 1; /* Only try the first I2C adapter by default. */
1785 static int new_ssif_client(int addr
, char *adapter_name
,
1786 int debug
, int slave_addr
,
1787 enum ipmi_addr_src addr_src
,
1790 struct ssif_addr_info
*addr_info
;
1793 mutex_lock(&ssif_infos_mutex
);
1794 if (ssif_info_find(addr
, adapter_name
, false)) {
1799 addr_info
= kzalloc(sizeof(*addr_info
), GFP_KERNEL
);
1806 addr_info
->adapter_name
= kstrdup(adapter_name
, GFP_KERNEL
);
1807 if (!addr_info
->adapter_name
) {
1814 strncpy(addr_info
->binfo
.type
, DEVICE_NAME
,
1815 sizeof(addr_info
->binfo
.type
));
1816 addr_info
->binfo
.addr
= addr
;
1817 addr_info
->binfo
.platform_data
= addr_info
;
1818 addr_info
->debug
= debug
;
1819 addr_info
->slave_addr
= slave_addr
;
1820 addr_info
->addr_src
= addr_src
;
1821 addr_info
->dev
= dev
;
1824 dev_set_drvdata(dev
, addr_info
);
1826 list_add_tail(&addr_info
->link
, &ssif_infos
);
1829 i2c_for_each_dev(addr_info
, ssif_adapter_handler
);
1830 /* Otherwise address list will get it */
1833 mutex_unlock(&ssif_infos_mutex
);
1837 static void free_ssif_clients(void)
1839 struct ssif_addr_info
*info
, *tmp
;
1841 mutex_lock(&ssif_infos_mutex
);
1842 list_for_each_entry_safe(info
, tmp
, &ssif_infos
, link
) {
1843 list_del(&info
->link
);
1844 kfree(info
->adapter_name
);
1847 mutex_unlock(&ssif_infos_mutex
);
1850 static unsigned short *ssif_address_list(void)
1852 struct ssif_addr_info
*info
;
1853 unsigned int count
= 0, i
= 0;
1854 unsigned short *address_list
;
1856 list_for_each_entry(info
, &ssif_infos
, link
)
1859 address_list
= kcalloc(count
+ 1, sizeof(*address_list
),
1864 list_for_each_entry(info
, &ssif_infos
, link
) {
1865 unsigned short addr
= info
->binfo
.addr
;
1868 for (j
= 0; j
< i
; j
++) {
1869 if (address_list
[j
] == addr
)
1873 if (j
== i
) /* Didn't find it in the list. */
1874 address_list
[i
++] = addr
;
1876 address_list
[i
] = I2C_CLIENT_END
;
1878 return address_list
;
1882 static const struct acpi_device_id ssif_acpi_match
[] = {
1886 MODULE_DEVICE_TABLE(acpi
, ssif_acpi_match
);
1890 static int dmi_ipmi_probe(struct platform_device
*pdev
)
1899 rv
= device_property_read_u16(&pdev
->dev
, "i2c-addr", &i2c_addr
);
1901 dev_warn(&pdev
->dev
, "No i2c-addr property\n");
1905 rv
= device_property_read_u8(&pdev
->dev
, "slave-addr", &slave_addr
);
1907 dev_warn(&pdev
->dev
, "device has no slave-addr property");
1909 return new_ssif_client(i2c_addr
, NULL
, 0,
1910 slave_addr
, SI_SMBIOS
, &pdev
->dev
);
1913 static int dmi_ipmi_probe(struct platform_device
*pdev
)
1919 static const struct i2c_device_id ssif_id
[] = {
1923 MODULE_DEVICE_TABLE(i2c
, ssif_id
);
1925 static struct i2c_driver ssif_i2c_driver
= {
1926 .class = I2C_CLASS_HWMON
,
1930 .probe
= ssif_probe
,
1931 .remove
= ssif_remove
,
1932 .alert
= ssif_alert
,
1933 .id_table
= ssif_id
,
1934 .detect
= ssif_detect
1937 static int ssif_platform_probe(struct platform_device
*dev
)
1939 return dmi_ipmi_probe(dev
);
1942 static int ssif_platform_remove(struct platform_device
*dev
)
1944 struct ssif_addr_info
*addr_info
= dev_get_drvdata(&dev
->dev
);
1949 mutex_lock(&ssif_infos_mutex
);
1950 i2c_unregister_device(addr_info
->added_client
);
1952 list_del(&addr_info
->link
);
1954 mutex_unlock(&ssif_infos_mutex
);
1958 static const struct platform_device_id ssif_plat_ids
[] = {
1959 { "dmi-ipmi-ssif", 0 },
1963 static struct platform_driver ipmi_driver
= {
1965 .name
= DEVICE_NAME
,
1967 .probe
= ssif_platform_probe
,
1968 .remove
= ssif_platform_remove
,
1969 .id_table
= ssif_plat_ids
1972 static int init_ipmi_ssif(void)
1980 pr_info("IPMI SSIF Interface driver\n");
1982 /* build list for i2c from addr list */
1983 for (i
= 0; i
< num_addrs
; i
++) {
1984 rv
= new_ssif_client(addr
[i
], adapter_name
[i
],
1985 dbg
[i
], slave_addrs
[i
],
1986 SI_HARDCODED
, NULL
);
1988 pr_err("Couldn't add hardcoded device at addr 0x%x\n",
1993 ssif_i2c_driver
.driver
.acpi_match_table
=
1994 ACPI_PTR(ssif_acpi_match
);
1997 rv
= platform_driver_register(&ipmi_driver
);
1999 pr_err("Unable to register driver: %d\n", rv
);
2002 ssif_i2c_driver
.address_list
= ssif_address_list();
2004 rv
= i2c_add_driver(&ssif_i2c_driver
);
2010 module_init(init_ipmi_ssif
);
2012 static void cleanup_ipmi_ssif(void)
2017 initialized
= false;
2019 i2c_del_driver(&ssif_i2c_driver
);
2021 kfree(ssif_i2c_driver
.address_list
);
2023 platform_driver_unregister(&ipmi_driver
);
2025 free_ssif_clients();
2027 module_exit(cleanup_ipmi_ssif
);
2029 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2030 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2031 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2032 MODULE_LICENSE("GPL");