4 * The interface to the IPMI driver for SMBus access to a SMBus
5 * compliant device. Called SSIF by the IPMI spec.
7 * Author: Intel Corporation
8 * Todd Davis <todd.c.davis@intel.com>
10 * Rewritten by Corey Minyard <minyard@acm.org> to support the
11 * non-blocking I2C interface, add support for multi-part
12 * transactions, add PEC support, and general clenaup.
14 * Copyright 2003 Intel Corporation
15 * Copyright 2005 MontaVista Software
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 of the License, or (at your
20 * option) any later version.
24 * This file holds the "policy" for the interface to the SSIF state
25 * machine. It does the configuration, handles timers and interrupts,
26 * and drives the real SSIF state machine.
30 * TODO: Figure out how to use SMB alerts. This will require a new
31 * interface into the I2C driver, I believe.
34 #if defined(MODVERSIONS)
35 #include <linux/modversions.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/sched.h>
41 #include <linux/seq_file.h>
42 #include <linux/timer.h>
43 #include <linux/delay.h>
44 #include <linux/errno.h>
45 #include <linux/spinlock.h>
46 #include <linux/slab.h>
47 #include <linux/list.h>
48 #include <linux/i2c.h>
49 #include <linux/ipmi_smi.h>
50 #include <linux/init.h>
51 #include <linux/dmi.h>
52 #include <linux/kthread.h>
53 #include <linux/acpi.h>
54 #include <linux/ctype.h>
56 #define PFX "ipmi_ssif: "
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_RESPONSE 3
65 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
67 /* ssif_debug is a bit-field
68 * SSIF_DEBUG_MSG - commands and their responses
69 * SSIF_DEBUG_STATES - message states
70 * SSIF_DEBUG_TIMING - Measure times between events in the driver
72 #define SSIF_DEBUG_TIMING 4
73 #define SSIF_DEBUG_STATE 2
74 #define SSIF_DEBUG_MSG 1
75 #define SSIF_NODEBUG 0
76 #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
81 #define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
82 #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
84 /* How many times to we retry sending/receiving the message. */
85 #define SSIF_SEND_RETRIES 5
86 #define SSIF_RECV_RETRIES 250
88 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
89 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
90 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
92 enum ssif_intf_state
{
97 SSIF_GETTING_MESSAGES
,
98 /* FIXME - add watchdog stuff. */
101 #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
102 && (ssif)->curr_msg == NULL)
105 * Indexes into stats[] in ssif_info below.
107 enum ssif_stat_indexes
{
108 /* Number of total messages sent. */
109 SSIF_STAT_sent_messages
= 0,
112 * Number of message parts sent. Messages may be broken into
113 * parts if they are long.
115 SSIF_STAT_sent_messages_parts
,
118 * Number of time a message was retried.
120 SSIF_STAT_send_retries
,
123 * Number of times the send of a message failed.
125 SSIF_STAT_send_errors
,
128 * Number of message responses received.
130 SSIF_STAT_received_messages
,
133 * Number of message fragments received.
135 SSIF_STAT_received_message_parts
,
138 * Number of times the receive of a message was retried.
140 SSIF_STAT_receive_retries
,
143 * Number of errors receiving messages.
145 SSIF_STAT_receive_errors
,
148 * Number of times a flag fetch was requested.
150 SSIF_STAT_flag_fetches
,
153 * Number of times the hardware didn't follow the state machine.
158 * Number of received events.
162 /* Number of asyncronous messages received. */
163 SSIF_STAT_incoming_messages
,
165 /* Number of watchdog pretimeouts. */
166 SSIF_STAT_watchdog_pretimeouts
,
168 /* Number of alers received. */
171 /* Always add statistics before this value, it must be last. */
175 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 mutex clients_mutex
;
185 struct list_head clients
;
187 struct list_head link
;
192 typedef void (*ssif_i2c_done
)(struct ssif_info
*ssif_info
, int result
,
193 unsigned char *data
, unsigned int len
);
199 struct ipmi_smi_msg
*waiting_msg
;
200 struct ipmi_smi_msg
*curr_msg
;
201 enum ssif_intf_state ssif_state
;
202 unsigned long ssif_debug
;
204 struct ipmi_smi_handlers handlers
;
206 enum ipmi_addr_src addr_source
; /* ACPI, PCI, SMBIOS, hardcode, etc. */
207 union ipmi_smi_info_union addr_info
;
210 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
211 * is set to hold the flags until we are done handling everything
214 #define RECEIVE_MSG_AVAIL 0x01
215 #define EVENT_MSG_BUFFER_FULL 0x02
216 #define WDT_PRE_TIMEOUT_INT 0x08
217 unsigned char msg_flags
;
220 bool has_event_buffer
;
224 * Used to tell what we should do with alerts. If we are
225 * waiting on a response, read the data immediately.
231 * If set to true, this will request events the next time the
232 * state machine is idle.
237 * If set to true, this will request flags the next time the
238 * state machine is idle.
243 * Used to perform timer operations when run-to-completion
244 * mode is on. This is a countdown timer.
248 /* Used for sending/receiving data. +1 for the length. */
249 unsigned char data
[IPMI_MAX_MSG_LENGTH
+ 1];
250 unsigned int data_len
;
252 /* Temp receive buffer, gets copied into data. */
253 unsigned char recv
[I2C_SMBUS_BLOCK_MAX
];
255 struct i2c_client
*client
;
256 ssif_i2c_done done_handler
;
258 /* Thread interface handling */
259 struct task_struct
*thread
;
260 struct completion wake_thread
;
264 unsigned char *i2c_data
;
265 unsigned int i2c_size
;
267 /* From the device id response. */
268 struct ipmi_device_id device_id
;
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 unsigned int multi_support
;
279 #define SSIF_NO_MULTI 0
280 #define SSIF_MULTI_2_PART 1
281 #define SSIF_MULTI_n_PART 2
282 unsigned char *multi_data
;
283 unsigned int multi_len
;
284 unsigned int multi_pos
;
286 atomic_t stats
[SSIF_NUM_STATS
];
289 #define ssif_inc_stat(ssif, stat) \
290 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
291 #define ssif_get_stat(ssif, stat) \
292 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
294 static bool initialized
;
296 static atomic_t next_intf
= ATOMIC_INIT(0);
298 static void return_hosed_msg(struct ssif_info
*ssif_info
,
299 struct ipmi_smi_msg
*msg
);
300 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
);
301 static int start_send(struct ssif_info
*ssif_info
,
305 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info
*ssif_info
,
306 unsigned long *flags
)
308 spin_lock_irqsave(&ssif_info
->lock
, *flags
);
312 static void ipmi_ssif_unlock_cond(struct ssif_info
*ssif_info
,
313 unsigned long *flags
)
315 spin_unlock_irqrestore(&ssif_info
->lock
, *flags
);
318 static void deliver_recv_msg(struct ssif_info
*ssif_info
,
319 struct ipmi_smi_msg
*msg
)
321 ipmi_smi_t intf
= ssif_info
->intf
;
324 ipmi_free_smi_msg(msg
);
325 } else if (msg
->rsp_size
< 0) {
326 return_hosed_msg(ssif_info
, msg
);
328 "Malformed message in deliver_recv_msg: rsp_size = %d\n",
331 ipmi_smi_msg_received(intf
, msg
);
335 static void return_hosed_msg(struct ssif_info
*ssif_info
,
336 struct ipmi_smi_msg
*msg
)
338 ssif_inc_stat(ssif_info
, hosed
);
340 /* Make it a response */
341 msg
->rsp
[0] = msg
->data
[0] | 4;
342 msg
->rsp
[1] = msg
->data
[1];
343 msg
->rsp
[2] = 0xFF; /* Unknown error. */
346 deliver_recv_msg(ssif_info
, msg
);
350 * Must be called with the message lock held. This will release the
351 * message lock. Note that the caller will check SSIF_IDLE and start a
352 * new operation, so there is no need to check for new messages to
355 static void start_clear_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
357 unsigned char msg
[3];
359 ssif_info
->msg_flags
&= ~WDT_PRE_TIMEOUT_INT
;
360 ssif_info
->ssif_state
= SSIF_CLEARING_FLAGS
;
361 ipmi_ssif_unlock_cond(ssif_info
, flags
);
363 /* Make sure the watchdog pre-timeout flag is not set at startup. */
364 msg
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
365 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
366 msg
[2] = WDT_PRE_TIMEOUT_INT
;
368 if (start_send(ssif_info
, msg
, 3) != 0) {
369 /* Error, just go to normal state. */
370 ssif_info
->ssif_state
= SSIF_NORMAL
;
374 static void start_flag_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
378 ssif_info
->req_flags
= false;
379 ssif_info
->ssif_state
= SSIF_GETTING_FLAGS
;
380 ipmi_ssif_unlock_cond(ssif_info
, flags
);
382 mb
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
383 mb
[1] = IPMI_GET_MSG_FLAGS_CMD
;
384 if (start_send(ssif_info
, mb
, 2) != 0)
385 ssif_info
->ssif_state
= SSIF_NORMAL
;
388 static void check_start_send(struct ssif_info
*ssif_info
, unsigned long *flags
,
389 struct ipmi_smi_msg
*msg
)
391 if (start_send(ssif_info
, msg
->data
, msg
->data_size
) != 0) {
392 unsigned long oflags
;
394 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
395 ssif_info
->curr_msg
= NULL
;
396 ssif_info
->ssif_state
= SSIF_NORMAL
;
397 ipmi_ssif_unlock_cond(ssif_info
, flags
);
398 ipmi_free_smi_msg(msg
);
402 static void start_event_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
404 struct ipmi_smi_msg
*msg
;
406 ssif_info
->req_events
= false;
408 msg
= ipmi_alloc_smi_msg();
410 ssif_info
->ssif_state
= SSIF_NORMAL
;
414 ssif_info
->curr_msg
= msg
;
415 ssif_info
->ssif_state
= SSIF_GETTING_EVENTS
;
416 ipmi_ssif_unlock_cond(ssif_info
, flags
);
418 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
419 msg
->data
[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD
;
422 check_start_send(ssif_info
, flags
, msg
);
425 static void start_recv_msg_fetch(struct ssif_info
*ssif_info
,
426 unsigned long *flags
)
428 struct ipmi_smi_msg
*msg
;
430 msg
= ipmi_alloc_smi_msg();
432 ssif_info
->ssif_state
= SSIF_NORMAL
;
436 ssif_info
->curr_msg
= msg
;
437 ssif_info
->ssif_state
= SSIF_GETTING_MESSAGES
;
438 ipmi_ssif_unlock_cond(ssif_info
, flags
);
440 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
441 msg
->data
[1] = IPMI_GET_MSG_CMD
;
444 check_start_send(ssif_info
, flags
, msg
);
448 * Must be called with the message lock held. This will release the
449 * message lock. Note that the caller will check SSIF_IDLE and start a
450 * new operation, so there is no need to check for new messages to
453 static void handle_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
455 if (ssif_info
->msg_flags
& WDT_PRE_TIMEOUT_INT
) {
456 ipmi_smi_t intf
= ssif_info
->intf
;
457 /* Watchdog pre-timeout */
458 ssif_inc_stat(ssif_info
, watchdog_pretimeouts
);
459 start_clear_flags(ssif_info
, flags
);
461 ipmi_smi_watchdog_pretimeout(intf
);
462 } else if (ssif_info
->msg_flags
& RECEIVE_MSG_AVAIL
)
463 /* Messages available. */
464 start_recv_msg_fetch(ssif_info
, flags
);
465 else if (ssif_info
->msg_flags
& EVENT_MSG_BUFFER_FULL
)
466 /* Events available. */
467 start_event_fetch(ssif_info
, flags
);
469 ssif_info
->ssif_state
= SSIF_NORMAL
;
470 ipmi_ssif_unlock_cond(ssif_info
, flags
);
474 static int ipmi_ssif_thread(void *data
)
476 struct ssif_info
*ssif_info
= data
;
478 while (!kthread_should_stop()) {
481 /* Wait for something to do */
482 result
= wait_for_completion_interruptible(
483 &ssif_info
->wake_thread
);
484 if (ssif_info
->stopping
)
486 if (result
== -ERESTARTSYS
)
488 init_completion(&ssif_info
->wake_thread
);
490 if (ssif_info
->i2c_read_write
== I2C_SMBUS_WRITE
) {
491 result
= i2c_smbus_write_block_data(
492 ssif_info
->client
, ssif_info
->i2c_command
,
493 ssif_info
->i2c_data
[0],
494 ssif_info
->i2c_data
+ 1);
495 ssif_info
->done_handler(ssif_info
, result
, NULL
, 0);
497 result
= i2c_smbus_read_block_data(
498 ssif_info
->client
, ssif_info
->i2c_command
,
499 ssif_info
->i2c_data
);
501 ssif_info
->done_handler(ssif_info
, result
,
504 ssif_info
->done_handler(ssif_info
, 0,
513 static int ssif_i2c_send(struct ssif_info
*ssif_info
,
514 ssif_i2c_done handler
,
515 int read_write
, int command
,
516 unsigned char *data
, unsigned int size
)
518 ssif_info
->done_handler
= handler
;
520 ssif_info
->i2c_read_write
= read_write
;
521 ssif_info
->i2c_command
= command
;
522 ssif_info
->i2c_data
= data
;
523 ssif_info
->i2c_size
= size
;
524 complete(&ssif_info
->wake_thread
);
529 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
530 unsigned char *data
, unsigned int len
);
532 static void start_get(struct ssif_info
*ssif_info
)
536 ssif_info
->rtc_us_timer
= 0;
537 ssif_info
->multi_pos
= 0;
539 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
541 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
543 /* request failed, just return the error. */
544 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
545 pr_info("Error from i2c_non_blocking_op(5)\n");
547 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
551 static void retry_timeout(unsigned long data
)
553 struct ssif_info
*ssif_info
= (void *) data
;
554 unsigned long oflags
, *flags
;
557 if (ssif_info
->stopping
)
560 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
561 waiting
= ssif_info
->waiting_alert
;
562 ssif_info
->waiting_alert
= false;
563 ipmi_ssif_unlock_cond(ssif_info
, flags
);
566 start_get(ssif_info
);
570 static void ssif_alert(struct i2c_client
*client
, unsigned int data
)
572 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
573 unsigned long oflags
, *flags
;
576 ssif_inc_stat(ssif_info
, alerts
);
578 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
579 if (ssif_info
->waiting_alert
) {
580 ssif_info
->waiting_alert
= false;
581 del_timer(&ssif_info
->retry_timer
);
583 } else if (ssif_info
->curr_msg
) {
584 ssif_info
->got_alert
= true;
586 ipmi_ssif_unlock_cond(ssif_info
, flags
);
588 start_get(ssif_info
);
591 static int start_resend(struct ssif_info
*ssif_info
);
593 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
594 unsigned char *data
, unsigned int len
)
596 struct ipmi_smi_msg
*msg
;
597 unsigned long oflags
, *flags
;
601 * We are single-threaded here, so no need for a lock until we
602 * start messing with driver states or the queues.
606 ssif_info
->retries_left
--;
607 if (ssif_info
->retries_left
> 0) {
608 ssif_inc_stat(ssif_info
, receive_retries
);
610 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
611 ssif_info
->waiting_alert
= true;
612 ssif_info
->rtc_us_timer
= SSIF_MSG_USEC
;
613 mod_timer(&ssif_info
->retry_timer
,
614 jiffies
+ SSIF_MSG_JIFFIES
);
615 ipmi_ssif_unlock_cond(ssif_info
, flags
);
619 ssif_inc_stat(ssif_info
, receive_errors
);
621 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
622 pr_info("Error in msg_done_handler: %d\n", result
);
627 if ((len
> 1) && (ssif_info
->multi_pos
== 0)
628 && (data
[0] == 0x00) && (data
[1] == 0x01)) {
629 /* Start of multi-part read. Start the next transaction. */
632 ssif_inc_stat(ssif_info
, received_message_parts
);
634 /* Remove the multi-part read marker. */
636 for (i
= 0; i
< len
; i
++)
637 ssif_info
->data
[i
] = data
[i
+2];
638 ssif_info
->multi_len
= len
;
639 ssif_info
->multi_pos
= 1;
641 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
642 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
643 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
645 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
646 pr_info("Error from i2c_non_blocking_op(1)\n");
651 } else if (ssif_info
->multi_pos
) {
652 /* Middle of multi-part read. Start the next transaction. */
654 unsigned char blocknum
;
658 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
659 pr_info(PFX
"Middle message with no data\n");
666 if (ssif_info
->multi_len
+ len
- 1 > IPMI_MAX_MSG_LENGTH
) {
667 /* Received message too big, abort the operation. */
669 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
670 pr_info("Received message too big\n");
675 /* Remove the blocknum from the data. */
677 for (i
= 0; i
< len
; i
++)
678 ssif_info
->data
[i
+ ssif_info
->multi_len
] = data
[i
+ 1];
679 ssif_info
->multi_len
+= len
;
680 if (blocknum
== 0xff) {
682 len
= ssif_info
->multi_len
;
683 data
= ssif_info
->data
;
684 } else if (blocknum
+ 1 != ssif_info
->multi_pos
) {
686 * Out of sequence block, just abort. Block
687 * numbers start at zero for the second block,
688 * but multi_pos starts at one, so the +1.
692 ssif_inc_stat(ssif_info
, received_message_parts
);
694 ssif_info
->multi_pos
++;
696 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
,
698 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
700 I2C_SMBUS_BLOCK_DATA
);
702 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
704 "Error from ssif_i2c_send\n");
713 ssif_inc_stat(ssif_info
, receive_errors
);
715 ssif_inc_stat(ssif_info
, received_messages
);
716 ssif_inc_stat(ssif_info
, received_message_parts
);
721 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
722 pr_info(PFX
"DONE 1: state = %d, result=%d.\n",
723 ssif_info
->ssif_state
, result
);
725 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
726 msg
= ssif_info
->curr_msg
;
729 if (msg
->rsp_size
> IPMI_MAX_MSG_LENGTH
)
730 msg
->rsp_size
= IPMI_MAX_MSG_LENGTH
;
731 memcpy(msg
->rsp
, data
, msg
->rsp_size
);
732 ssif_info
->curr_msg
= NULL
;
735 switch (ssif_info
->ssif_state
) {
737 ipmi_ssif_unlock_cond(ssif_info
, flags
);
742 return_hosed_msg(ssif_info
, msg
);
744 deliver_recv_msg(ssif_info
, msg
);
747 case SSIF_GETTING_FLAGS
:
748 /* We got the flags from the SSIF, now handle them. */
749 if ((result
< 0) || (len
< 4) || (data
[2] != 0)) {
751 * Error fetching flags, or invalid length,
752 * just give up for now.
754 ssif_info
->ssif_state
= SSIF_NORMAL
;
755 ipmi_ssif_unlock_cond(ssif_info
, flags
);
756 pr_warn(PFX
"Error getting flags: %d %d, %x\n",
757 result
, len
, data
[2]);
758 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
759 || data
[1] != IPMI_GET_MSG_FLAGS_CMD
) {
760 pr_warn(PFX
"Invalid response getting flags: %x %x\n",
763 ssif_inc_stat(ssif_info
, flag_fetches
);
764 ssif_info
->msg_flags
= data
[3];
765 handle_flags(ssif_info
, flags
);
769 case SSIF_CLEARING_FLAGS
:
770 /* We cleared the flags. */
771 if ((result
< 0) || (len
< 3) || (data
[2] != 0)) {
772 /* Error clearing flags */
773 pr_warn(PFX
"Error clearing flags: %d %d, %x\n",
774 result
, len
, data
[2]);
775 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
776 || data
[1] != IPMI_CLEAR_MSG_FLAGS_CMD
) {
777 pr_warn(PFX
"Invalid response clearing flags: %x %x\n",
780 ssif_info
->ssif_state
= SSIF_NORMAL
;
781 ipmi_ssif_unlock_cond(ssif_info
, flags
);
784 case SSIF_GETTING_EVENTS
:
785 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
786 /* Error getting event, probably done. */
789 /* Take off the event flag. */
790 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
791 handle_flags(ssif_info
, flags
);
792 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
793 || msg
->rsp
[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD
) {
794 pr_warn(PFX
"Invalid response getting events: %x %x\n",
795 msg
->rsp
[0], msg
->rsp
[1]);
797 /* Take off the event flag. */
798 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
799 handle_flags(ssif_info
, flags
);
801 handle_flags(ssif_info
, flags
);
802 ssif_inc_stat(ssif_info
, events
);
803 deliver_recv_msg(ssif_info
, msg
);
807 case SSIF_GETTING_MESSAGES
:
808 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
809 /* Error getting event, probably done. */
812 /* Take off the msg flag. */
813 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
814 handle_flags(ssif_info
, flags
);
815 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
816 || msg
->rsp
[1] != IPMI_GET_MSG_CMD
) {
817 pr_warn(PFX
"Invalid response clearing flags: %x %x\n",
818 msg
->rsp
[0], msg
->rsp
[1]);
821 /* Take off the msg flag. */
822 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
823 handle_flags(ssif_info
, flags
);
825 ssif_inc_stat(ssif_info
, incoming_messages
);
826 handle_flags(ssif_info
, flags
);
827 deliver_recv_msg(ssif_info
, msg
);
832 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
833 if (SSIF_IDLE(ssif_info
) && !ssif_info
->stopping
) {
834 if (ssif_info
->req_events
)
835 start_event_fetch(ssif_info
, flags
);
836 else if (ssif_info
->req_flags
)
837 start_flag_fetch(ssif_info
, flags
);
839 start_next_msg(ssif_info
, flags
);
841 ipmi_ssif_unlock_cond(ssif_info
, flags
);
843 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
844 pr_info(PFX
"DONE 2: state = %d.\n", ssif_info
->ssif_state
);
847 static void msg_written_handler(struct ssif_info
*ssif_info
, int result
,
848 unsigned char *data
, unsigned int len
)
852 /* We are single-threaded here, so no need for a lock. */
854 ssif_info
->retries_left
--;
855 if (ssif_info
->retries_left
> 0) {
856 if (!start_resend(ssif_info
)) {
857 ssif_inc_stat(ssif_info
, send_retries
);
860 /* request failed, just return the error. */
861 ssif_inc_stat(ssif_info
, send_errors
);
863 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
865 "Out of retries in msg_written_handler\n");
866 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
870 ssif_inc_stat(ssif_info
, send_errors
);
873 * Got an error on transmit, let the done routine
876 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
877 pr_info("Error in msg_written_handler: %d\n", result
);
879 msg_done_handler(ssif_info
, result
, NULL
, 0);
883 if (ssif_info
->multi_data
) {
885 * In the middle of a multi-data write. See the comment
886 * in the SSIF_MULTI_n_PART case in the probe function
887 * for details on the intricacies of this.
891 ssif_inc_stat(ssif_info
, sent_messages_parts
);
893 left
= ssif_info
->multi_len
- ssif_info
->multi_pos
;
897 ssif_info
->multi_data
[ssif_info
->multi_pos
] = left
;
898 ssif_info
->multi_pos
+= left
;
901 * Write is finished. Note that we must end
902 * with a write of less than 32 bytes to
903 * complete the transaction, even if it is
906 ssif_info
->multi_data
= NULL
;
908 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
,
910 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
,
911 ssif_info
->multi_data
+ ssif_info
->multi_pos
,
912 I2C_SMBUS_BLOCK_DATA
);
914 /* request failed, just return the error. */
915 ssif_inc_stat(ssif_info
, send_errors
);
917 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
918 pr_info("Error from i2c_non_blocking_op(3)\n");
919 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
922 unsigned long oflags
, *flags
;
925 ssif_inc_stat(ssif_info
, sent_messages
);
926 ssif_inc_stat(ssif_info
, sent_messages_parts
);
928 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
929 got_alert
= ssif_info
->got_alert
;
931 ssif_info
->got_alert
= false;
932 ssif_info
->waiting_alert
= false;
936 ipmi_ssif_unlock_cond(ssif_info
, flags
);
937 /* The alert already happened, try now. */
938 retry_timeout((unsigned long) 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 mod_timer(&ssif_info
->retry_timer
,
945 jiffies
+ SSIF_MSG_PART_JIFFIES
);
946 ipmi_ssif_unlock_cond(ssif_info
, flags
);
951 static int start_resend(struct ssif_info
*ssif_info
)
956 ssif_info
->got_alert
= false;
958 if (ssif_info
->data_len
> 32) {
959 command
= SSIF_IPMI_MULTI_PART_REQUEST_START
;
960 ssif_info
->multi_data
= ssif_info
->data
;
961 ssif_info
->multi_len
= ssif_info
->data_len
;
963 * Subtle thing, this is 32, not 33, because we will
964 * overwrite the thing at position 32 (which was just
965 * transmitted) with the new length.
967 ssif_info
->multi_pos
= 32;
968 ssif_info
->data
[0] = 32;
970 ssif_info
->multi_data
= NULL
;
971 command
= SSIF_IPMI_REQUEST
;
972 ssif_info
->data
[0] = ssif_info
->data_len
;
975 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
, I2C_SMBUS_WRITE
,
976 command
, ssif_info
->data
, I2C_SMBUS_BLOCK_DATA
);
977 if (rv
&& (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
))
978 pr_info("Error from i2c_non_blocking_op(4)\n");
982 static int start_send(struct ssif_info
*ssif_info
,
986 if (len
> IPMI_MAX_MSG_LENGTH
)
988 if (len
> ssif_info
->max_xmit_msg_size
)
991 ssif_info
->retries_left
= SSIF_SEND_RETRIES
;
992 memcpy(ssif_info
->data
+ 1, data
, len
);
993 ssif_info
->data_len
= len
;
994 return start_resend(ssif_info
);
997 /* Must be called with the message lock held. */
998 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
)
1000 struct ipmi_smi_msg
*msg
;
1001 unsigned long oflags
;
1004 if (!SSIF_IDLE(ssif_info
)) {
1005 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1009 if (!ssif_info
->waiting_msg
) {
1010 ssif_info
->curr_msg
= NULL
;
1011 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1015 ssif_info
->curr_msg
= ssif_info
->waiting_msg
;
1016 ssif_info
->waiting_msg
= NULL
;
1017 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1018 rv
= start_send(ssif_info
,
1019 ssif_info
->curr_msg
->data
,
1020 ssif_info
->curr_msg
->data_size
);
1022 msg
= ssif_info
->curr_msg
;
1023 ssif_info
->curr_msg
= NULL
;
1024 return_hosed_msg(ssif_info
, msg
);
1025 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1031 static void sender(void *send_info
,
1032 struct ipmi_smi_msg
*msg
)
1034 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1035 unsigned long oflags
, *flags
;
1037 BUG_ON(ssif_info
->waiting_msg
);
1038 ssif_info
->waiting_msg
= msg
;
1040 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1041 start_next_msg(ssif_info
, flags
);
1043 if (ssif_info
->ssif_debug
& SSIF_DEBUG_TIMING
) {
1046 do_gettimeofday(&t
);
1047 pr_info("**Enqueue %02x %02x: %ld.%6.6ld\n",
1048 msg
->data
[0], msg
->data
[1],
1049 (long) t
.tv_sec
, (long) t
.tv_usec
);
1053 static int get_smi_info(void *send_info
, struct ipmi_smi_info
*data
)
1055 struct ssif_info
*ssif_info
= send_info
;
1057 data
->addr_src
= ssif_info
->addr_source
;
1058 data
->dev
= &ssif_info
->client
->dev
;
1059 data
->addr_info
= ssif_info
->addr_info
;
1060 get_device(data
->dev
);
1066 * Instead of having our own timer to periodically check the message
1067 * flags, we let the message handler drive us.
1069 static void request_events(void *send_info
)
1071 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1072 unsigned long oflags
, *flags
;
1074 if (!ssif_info
->has_event_buffer
)
1077 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1079 * Request flags first, not events, because the lower layer
1080 * doesn't have a way to send an attention. But make sure
1081 * event checking still happens.
1083 ssif_info
->req_events
= true;
1084 if (SSIF_IDLE(ssif_info
))
1085 start_flag_fetch(ssif_info
, flags
);
1087 ssif_info
->req_flags
= true;
1088 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1092 static int inc_usecount(void *send_info
)
1094 struct ssif_info
*ssif_info
= send_info
;
1096 if (!i2c_get_adapter(ssif_info
->client
->adapter
->nr
))
1099 i2c_use_client(ssif_info
->client
);
1103 static void dec_usecount(void *send_info
)
1105 struct ssif_info
*ssif_info
= send_info
;
1107 i2c_release_client(ssif_info
->client
);
1108 i2c_put_adapter(ssif_info
->client
->adapter
);
1111 static int ssif_start_processing(void *send_info
,
1114 struct ssif_info
*ssif_info
= send_info
;
1116 ssif_info
->intf
= intf
;
1121 #define MAX_SSIF_BMCS 4
1123 static unsigned short addr
[MAX_SSIF_BMCS
];
1124 static int num_addrs
;
1125 module_param_array(addr
, ushort
, &num_addrs
, 0);
1126 MODULE_PARM_DESC(addr
, "The addresses to scan for IPMI BMCs on the SSIFs.");
1128 static char *adapter_name
[MAX_SSIF_BMCS
];
1129 static int num_adapter_names
;
1130 module_param_array(adapter_name
, charp
, &num_adapter_names
, 0);
1131 MODULE_PARM_DESC(adapter_name
, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1133 static int slave_addrs
[MAX_SSIF_BMCS
];
1134 static int num_slave_addrs
;
1135 module_param_array(slave_addrs
, int, &num_slave_addrs
, 0);
1136 MODULE_PARM_DESC(slave_addrs
,
1137 "The default IPMB slave address for the controller.");
1140 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1141 * bit 2 enables timing debugging. This is an array indexed by
1144 static int dbg
[MAX_SSIF_BMCS
];
1146 module_param_array(dbg
, int, &num_dbg
, 0);
1147 MODULE_PARM_DESC(dbg
, "Turn on debugging.");
1149 static bool ssif_dbg_probe
;
1150 module_param_named(dbg_probe
, ssif_dbg_probe
, bool, 0);
1151 MODULE_PARM_DESC(dbg_probe
, "Enable debugging of probing of adapters.");
1153 static int use_thread
;
1154 module_param(use_thread
, int, 0);
1155 MODULE_PARM_DESC(use_thread
, "Use the thread interface.");
1157 static bool ssif_tryacpi
= true;
1158 module_param_named(tryacpi
, ssif_tryacpi
, bool, 0);
1159 MODULE_PARM_DESC(tryacpi
, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1161 static bool ssif_trydmi
= true;
1162 module_param_named(trydmi
, ssif_trydmi
, bool, 0);
1163 MODULE_PARM_DESC(trydmi
, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1165 static DEFINE_MUTEX(ssif_infos_mutex
);
1166 static LIST_HEAD(ssif_infos
);
1168 static int ssif_remove(struct i2c_client
*client
)
1170 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
1177 * After this point, we won't deliver anything asychronously
1178 * to the message handler. We can unregister ourself.
1180 rv
= ipmi_unregister_smi(ssif_info
->intf
);
1182 pr_err(PFX
"Unable to unregister device: errno=%d\n", rv
);
1185 ssif_info
->intf
= NULL
;
1187 /* make sure the driver is not looking for flags any more. */
1188 while (ssif_info
->ssif_state
!= SSIF_NORMAL
)
1189 schedule_timeout(1);
1191 ssif_info
->stopping
= true;
1192 del_timer_sync(&ssif_info
->retry_timer
);
1193 if (ssif_info
->thread
) {
1194 complete(&ssif_info
->wake_thread
);
1195 kthread_stop(ssif_info
->thread
);
1199 * No message can be outstanding now, we have removed the
1200 * upper layer and it permitted us to do so.
1206 static int do_cmd(struct i2c_client
*client
, int len
, unsigned char *msg
,
1207 int *resp_len
, unsigned char *resp
)
1212 retry_cnt
= SSIF_SEND_RETRIES
;
1214 ret
= i2c_smbus_write_block_data(client
, SSIF_IPMI_REQUEST
, len
, msg
);
1223 retry_cnt
= SSIF_RECV_RETRIES
;
1224 while (retry_cnt
> 0) {
1225 ret
= i2c_smbus_read_block_data(client
, SSIF_IPMI_RESPONSE
,
1229 msleep(SSIF_MSG_MSEC
);
1236 /* Validate that the response is correct. */
1238 (resp
[0] != (msg
[0] | (1 << 2))) ||
1239 (resp
[1] != msg
[1]))
1250 static int ssif_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
1252 unsigned char *resp
;
1253 unsigned char msg
[3];
1257 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1261 /* Do a Get Device ID command, since it is required. */
1262 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1263 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1264 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1268 strlcpy(info
->type
, DEVICE_NAME
, I2C_NAME_SIZE
);
1273 static int smi_type_proc_show(struct seq_file
*m
, void *v
)
1275 seq_puts(m
, "ssif\n");
1280 static int smi_type_proc_open(struct inode
*inode
, struct file
*file
)
1282 return single_open(file
, smi_type_proc_show
, inode
->i_private
);
1285 static const struct file_operations smi_type_proc_ops
= {
1286 .open
= smi_type_proc_open
,
1288 .llseek
= seq_lseek
,
1289 .release
= single_release
,
1292 static int smi_stats_proc_show(struct seq_file
*m
, void *v
)
1294 struct ssif_info
*ssif_info
= m
->private;
1296 seq_printf(m
, "sent_messages: %u\n",
1297 ssif_get_stat(ssif_info
, sent_messages
));
1298 seq_printf(m
, "sent_messages_parts: %u\n",
1299 ssif_get_stat(ssif_info
, sent_messages_parts
));
1300 seq_printf(m
, "send_retries: %u\n",
1301 ssif_get_stat(ssif_info
, send_retries
));
1302 seq_printf(m
, "send_errors: %u\n",
1303 ssif_get_stat(ssif_info
, send_errors
));
1304 seq_printf(m
, "received_messages: %u\n",
1305 ssif_get_stat(ssif_info
, received_messages
));
1306 seq_printf(m
, "received_message_parts: %u\n",
1307 ssif_get_stat(ssif_info
, received_message_parts
));
1308 seq_printf(m
, "receive_retries: %u\n",
1309 ssif_get_stat(ssif_info
, receive_retries
));
1310 seq_printf(m
, "receive_errors: %u\n",
1311 ssif_get_stat(ssif_info
, receive_errors
));
1312 seq_printf(m
, "flag_fetches: %u\n",
1313 ssif_get_stat(ssif_info
, flag_fetches
));
1314 seq_printf(m
, "hosed: %u\n",
1315 ssif_get_stat(ssif_info
, hosed
));
1316 seq_printf(m
, "events: %u\n",
1317 ssif_get_stat(ssif_info
, events
));
1318 seq_printf(m
, "watchdog_pretimeouts: %u\n",
1319 ssif_get_stat(ssif_info
, watchdog_pretimeouts
));
1320 seq_printf(m
, "alerts: %u\n",
1321 ssif_get_stat(ssif_info
, alerts
));
1325 static int smi_stats_proc_open(struct inode
*inode
, struct file
*file
)
1327 return single_open(file
, smi_stats_proc_show
, PDE_DATA(inode
));
1330 static const struct file_operations smi_stats_proc_ops
= {
1331 .open
= smi_stats_proc_open
,
1333 .llseek
= seq_lseek
,
1334 .release
= single_release
,
1337 static int strcmp_nospace(char *s1
, char *s2
)
1339 while (*s1
&& *s2
) {
1340 while (isspace(*s1
))
1342 while (isspace(*s2
))
1354 static struct ssif_addr_info
*ssif_info_find(unsigned short addr
,
1356 bool match_null_name
)
1358 struct ssif_addr_info
*info
, *found
= NULL
;
1361 list_for_each_entry(info
, &ssif_infos
, link
) {
1362 if (info
->binfo
.addr
== addr
) {
1363 if (info
->adapter_name
|| adapter_name
) {
1364 if (!info
->adapter_name
!= !adapter_name
) {
1365 /* One is NULL and one is not */
1369 strcmp_nospace(info
->adapter_name
,
1371 /* Names do not match */
1379 if (!found
&& match_null_name
) {
1380 /* Try to get an exact match first, then try with a NULL name */
1381 adapter_name
= NULL
;
1382 match_null_name
= false;
1389 static bool check_acpi(struct ssif_info
*ssif_info
, struct device
*dev
)
1392 acpi_handle acpi_handle
;
1394 acpi_handle
= ACPI_HANDLE(dev
);
1396 ssif_info
->addr_source
= SI_ACPI
;
1397 ssif_info
->addr_info
.acpi_info
.acpi_handle
= acpi_handle
;
1405 * Global enables we care about.
1407 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1408 IPMI_BMC_EVT_MSG_INTR)
1410 static int ssif_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1412 unsigned char msg
[3];
1413 unsigned char *resp
;
1414 struct ssif_info
*ssif_info
;
1419 struct ssif_addr_info
*addr_info
= NULL
;
1422 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1426 ssif_info
= kzalloc(sizeof(*ssif_info
), GFP_KERNEL
);
1432 if (!check_acpi(ssif_info
, &client
->dev
)) {
1433 addr_info
= ssif_info_find(client
->addr
, client
->adapter
->name
,
1436 /* Must have come in through sysfs. */
1437 ssif_info
->addr_source
= SI_HOTMOD
;
1439 ssif_info
->addr_source
= addr_info
->addr_src
;
1440 ssif_info
->ssif_debug
= addr_info
->debug
;
1441 ssif_info
->addr_info
= addr_info
->addr_info
;
1442 slave_addr
= addr_info
->slave_addr
;
1446 pr_info(PFX
"Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1447 ipmi_addr_src_to_str(ssif_info
->addr_source
),
1448 client
->addr
, client
->adapter
->name
, slave_addr
);
1451 * Do a Get Device ID command, since it comes back with some
1454 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1455 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1456 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1460 rv
= ipmi_demangle_device_id(resp
, len
, &ssif_info
->device_id
);
1464 ssif_info
->client
= client
;
1465 i2c_set_clientdata(client
, ssif_info
);
1467 /* Now check for system interface capabilities */
1468 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1469 msg
[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD
;
1470 msg
[2] = 0; /* SSIF */
1471 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1472 if (!rv
&& (len
>= 3) && (resp
[2] == 0)) {
1475 pr_info(PFX
"SSIF info too short: %d\n", len
);
1479 /* Got a good SSIF response, handle it. */
1480 ssif_info
->max_xmit_msg_size
= resp
[5];
1481 ssif_info
->max_recv_msg_size
= resp
[6];
1482 ssif_info
->multi_support
= (resp
[4] >> 6) & 0x3;
1483 ssif_info
->supports_pec
= (resp
[4] >> 3) & 0x1;
1485 /* Sanitize the data */
1486 switch (ssif_info
->multi_support
) {
1488 if (ssif_info
->max_xmit_msg_size
> 32)
1489 ssif_info
->max_xmit_msg_size
= 32;
1490 if (ssif_info
->max_recv_msg_size
> 32)
1491 ssif_info
->max_recv_msg_size
= 32;
1494 case SSIF_MULTI_2_PART
:
1495 if (ssif_info
->max_xmit_msg_size
> 63)
1496 ssif_info
->max_xmit_msg_size
= 63;
1497 if (ssif_info
->max_recv_msg_size
> 62)
1498 ssif_info
->max_recv_msg_size
= 62;
1501 case SSIF_MULTI_n_PART
:
1503 * The specification is rather confusing at
1504 * this point, but I think I understand what
1505 * is meant. At least I have a workable
1506 * solution. With multi-part messages, you
1507 * cannot send a message that is a multiple of
1508 * 32-bytes in length, because the start and
1509 * middle messages are 32-bytes and the end
1510 * message must be at least one byte. You
1511 * can't fudge on an extra byte, that would
1512 * screw up things like fru data writes. So
1513 * we limit the length to 63 bytes. That way
1514 * a 32-byte message gets sent as a single
1515 * part. A larger message will be a 32-byte
1516 * start and the next message is always going
1517 * to be 1-31 bytes in length. Not ideal, but
1520 if (ssif_info
->max_xmit_msg_size
> 63)
1521 ssif_info
->max_xmit_msg_size
= 63;
1525 /* Data is not sane, just give up. */
1530 /* Assume no multi-part or PEC support */
1531 pr_info(PFX
"Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1534 ssif_info
->max_xmit_msg_size
= 32;
1535 ssif_info
->max_recv_msg_size
= 32;
1536 ssif_info
->multi_support
= SSIF_NO_MULTI
;
1537 ssif_info
->supports_pec
= 0;
1540 /* Make sure the NMI timeout is cleared. */
1541 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1542 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
1543 msg
[2] = WDT_PRE_TIMEOUT_INT
;
1544 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1545 if (rv
|| (len
< 3) || (resp
[2] != 0))
1546 pr_warn(PFX
"Unable to clear message flags: %d %d %2.2x\n",
1549 /* Attempt to enable the event buffer. */
1550 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1551 msg
[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD
;
1552 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1553 if (rv
|| (len
< 4) || (resp
[2] != 0)) {
1554 pr_warn(PFX
"Error getting global enables: %d %d %2.2x\n",
1556 rv
= 0; /* Not fatal */
1560 ssif_info
->global_enables
= resp
[3];
1562 if (resp
[3] & IPMI_BMC_EVT_MSG_BUFF
) {
1563 ssif_info
->has_event_buffer
= true;
1564 /* buffer is already enabled, nothing to do. */
1568 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1569 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1570 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_EVT_MSG_BUFF
;
1571 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1572 if (rv
|| (len
< 2)) {
1573 pr_warn(PFX
"Error setting global enables: %d %d %2.2x\n",
1575 rv
= 0; /* Not fatal */
1580 /* A successful return means the event buffer is supported. */
1581 ssif_info
->has_event_buffer
= true;
1582 ssif_info
->global_enables
|= IPMI_BMC_EVT_MSG_BUFF
;
1585 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1586 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1587 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_RCV_MSG_INTR
;
1588 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1589 if (rv
|| (len
< 2)) {
1590 pr_warn(PFX
"Error setting global enables: %d %d %2.2x\n",
1592 rv
= 0; /* Not fatal */
1597 /* A successful return means the alert is supported. */
1598 ssif_info
->supports_alert
= true;
1599 ssif_info
->global_enables
|= IPMI_BMC_RCV_MSG_INTR
;
1603 ssif_info
->intf_num
= atomic_inc_return(&next_intf
);
1605 if (ssif_dbg_probe
) {
1606 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1610 spin_lock_init(&ssif_info
->lock
);
1611 ssif_info
->ssif_state
= SSIF_NORMAL
;
1612 init_timer(&ssif_info
->retry_timer
);
1613 ssif_info
->retry_timer
.data
= (unsigned long) ssif_info
;
1614 ssif_info
->retry_timer
.function
= retry_timeout
;
1616 for (i
= 0; i
< SSIF_NUM_STATS
; i
++)
1617 atomic_set(&ssif_info
->stats
[i
], 0);
1619 if (ssif_info
->supports_pec
)
1620 ssif_info
->client
->flags
|= I2C_CLIENT_PEC
;
1622 ssif_info
->handlers
.owner
= THIS_MODULE
;
1623 ssif_info
->handlers
.start_processing
= ssif_start_processing
;
1624 ssif_info
->handlers
.get_smi_info
= get_smi_info
;
1625 ssif_info
->handlers
.sender
= sender
;
1626 ssif_info
->handlers
.request_events
= request_events
;
1627 ssif_info
->handlers
.inc_usecount
= inc_usecount
;
1628 ssif_info
->handlers
.dec_usecount
= dec_usecount
;
1631 unsigned int thread_num
;
1633 thread_num
= ((ssif_info
->client
->adapter
->nr
<< 8) |
1634 ssif_info
->client
->addr
);
1635 init_completion(&ssif_info
->wake_thread
);
1636 ssif_info
->thread
= kthread_run(ipmi_ssif_thread
, ssif_info
,
1637 "kssif%4.4x", thread_num
);
1638 if (IS_ERR(ssif_info
->thread
)) {
1639 rv
= PTR_ERR(ssif_info
->thread
);
1640 dev_notice(&ssif_info
->client
->dev
,
1641 "Could not start kernel thread: error %d\n",
1647 rv
= ipmi_register_smi(&ssif_info
->handlers
,
1649 &ssif_info
->device_id
,
1650 &ssif_info
->client
->dev
,
1653 pr_err(PFX
"Unable to register device: error %d\n", rv
);
1657 rv
= ipmi_smi_add_proc_entry(ssif_info
->intf
, "type",
1661 pr_err(PFX
"Unable to create proc entry: %d\n", rv
);
1665 rv
= ipmi_smi_add_proc_entry(ssif_info
->intf
, "ssif_stats",
1666 &smi_stats_proc_ops
,
1669 pr_err(PFX
"Unable to create proc entry: %d\n", rv
);
1680 ipmi_unregister_smi(ssif_info
->intf
);
1684 static int ssif_adapter_handler(struct device
*adev
, void *opaque
)
1686 struct ssif_addr_info
*addr_info
= opaque
;
1688 if (adev
->type
!= &i2c_adapter_type
)
1691 i2c_new_device(to_i2c_adapter(adev
), &addr_info
->binfo
);
1693 if (!addr_info
->adapter_name
)
1694 return 1; /* Only try the first I2C adapter by default. */
1698 static int new_ssif_client(int addr
, char *adapter_name
,
1699 int debug
, int slave_addr
,
1700 enum ipmi_addr_src addr_src
)
1702 struct ssif_addr_info
*addr_info
;
1705 mutex_lock(&ssif_infos_mutex
);
1706 if (ssif_info_find(addr
, adapter_name
, false)) {
1711 addr_info
= kzalloc(sizeof(*addr_info
), GFP_KERNEL
);
1718 addr_info
->adapter_name
= kstrdup(adapter_name
, GFP_KERNEL
);
1719 if (!addr_info
->adapter_name
) {
1726 strncpy(addr_info
->binfo
.type
, DEVICE_NAME
,
1727 sizeof(addr_info
->binfo
.type
));
1728 addr_info
->binfo
.addr
= addr
;
1729 addr_info
->binfo
.platform_data
= addr_info
;
1730 addr_info
->debug
= debug
;
1731 addr_info
->slave_addr
= slave_addr
;
1732 addr_info
->addr_src
= addr_src
;
1734 list_add_tail(&addr_info
->link
, &ssif_infos
);
1737 i2c_for_each_dev(addr_info
, ssif_adapter_handler
);
1738 /* Otherwise address list will get it */
1741 mutex_unlock(&ssif_infos_mutex
);
1745 static void free_ssif_clients(void)
1747 struct ssif_addr_info
*info
, *tmp
;
1749 mutex_lock(&ssif_infos_mutex
);
1750 list_for_each_entry_safe(info
, tmp
, &ssif_infos
, link
) {
1751 list_del(&info
->link
);
1752 kfree(info
->adapter_name
);
1755 mutex_unlock(&ssif_infos_mutex
);
1758 static unsigned short *ssif_address_list(void)
1760 struct ssif_addr_info
*info
;
1761 unsigned int count
= 0, i
;
1762 unsigned short *address_list
;
1764 list_for_each_entry(info
, &ssif_infos
, link
)
1767 address_list
= kzalloc(sizeof(*address_list
) * (count
+ 1), GFP_KERNEL
);
1772 list_for_each_entry(info
, &ssif_infos
, link
) {
1773 unsigned short addr
= info
->binfo
.addr
;
1776 for (j
= 0; j
< i
; j
++) {
1777 if (address_list
[j
] == addr
)
1780 address_list
[i
] = addr
;
1784 address_list
[i
] = I2C_CLIENT_END
;
1786 return address_list
;
1790 static const struct acpi_device_id ssif_acpi_match
[] = {
1794 MODULE_DEVICE_TABLE(acpi
, ssif_acpi_match
);
1797 * Once we get an ACPI failure, we don't try any more, because we go
1798 * through the tables sequentially. Once we don't find a table, there
1801 static int acpi_failure
;
1804 * Defined in the IPMI 2.0 spec.
1815 s8 CreatorRevision
[4];
1818 s16 SpecificationRevision
;
1821 * Bit 0 - SCI interrupt supported
1822 * Bit 1 - I/O APIC/SAPIC
1827 * If bit 0 of InterruptType is set, then this is the SCI
1828 * interrupt in the GPEx_STS register.
1835 * If bit 1 of InterruptType is set, then this is the I/O
1836 * APIC/SAPIC interrupt.
1838 u32 GlobalSystemInterrupt
;
1840 /* The actual register address. */
1841 struct acpi_generic_address addr
;
1845 s8 spmi_id
[1]; /* A '\0' terminated array starts here. */
1848 static int try_init_spmi(struct SPMITable
*spmi
)
1850 unsigned short myaddr
;
1852 if (num_addrs
>= MAX_SSIF_BMCS
)
1855 if (spmi
->IPMIlegacy
!= 1) {
1856 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi
->IPMIlegacy
);
1860 if (spmi
->InterfaceType
!= 4)
1863 if (spmi
->addr
.space_id
!= ACPI_ADR_SPACE_SMBUS
) {
1864 pr_warn(PFX
"Invalid ACPI SSIF I/O Address type: %d\n",
1865 spmi
->addr
.space_id
);
1869 myaddr
= spmi
->addr
.address
>> 1;
1871 return new_ssif_client(myaddr
, NULL
, 0, 0, SI_SPMI
);
1874 static void spmi_find_bmc(void)
1877 struct SPMITable
*spmi
;
1886 for (i
= 0; ; i
++) {
1887 status
= acpi_get_table(ACPI_SIG_SPMI
, i
+1,
1888 (struct acpi_table_header
**)&spmi
);
1889 if (status
!= AE_OK
)
1892 try_init_spmi(spmi
);
1896 static void spmi_find_bmc(void) { }
1900 static int decode_dmi(const struct dmi_device
*dmi_dev
)
1902 struct dmi_header
*dm
= dmi_dev
->device_data
;
1903 u8
*data
= (u8
*) dm
;
1904 u8 len
= dm
->length
;
1905 unsigned short myaddr
;
1908 if (num_addrs
>= MAX_SSIF_BMCS
)
1914 if (data
[0x04] != 4) /* Not SSIF */
1917 if ((data
[8] >> 1) == 0) {
1919 * Some broken systems put the I2C address in
1920 * the slave address field. We try to
1921 * accommodate them here.
1923 myaddr
= data
[6] >> 1;
1926 myaddr
= data
[8] >> 1;
1927 slave_addr
= data
[6];
1930 return new_ssif_client(myaddr
, NULL
, 0, 0, SI_SMBIOS
);
1933 static void dmi_iterator(void)
1935 const struct dmi_device
*dev
= NULL
;
1937 while ((dev
= dmi_find_device(DMI_DEV_TYPE_IPMI
, NULL
, dev
)))
1941 static void dmi_iterator(void) { }
1944 static const struct i2c_device_id ssif_id
[] = {
1948 MODULE_DEVICE_TABLE(i2c
, ssif_id
);
1950 static struct i2c_driver ssif_i2c_driver
= {
1951 .class = I2C_CLASS_HWMON
,
1953 .owner
= THIS_MODULE
,
1956 .probe
= ssif_probe
,
1957 .remove
= ssif_remove
,
1958 .alert
= ssif_alert
,
1959 .id_table
= ssif_id
,
1960 .detect
= ssif_detect
1963 static int init_ipmi_ssif(void)
1971 pr_info("IPMI SSIF Interface driver\n");
1973 /* build list for i2c from addr list */
1974 for (i
= 0; i
< num_addrs
; i
++) {
1975 rv
= new_ssif_client(addr
[i
], adapter_name
[i
],
1976 dbg
[i
], slave_addrs
[i
],
1980 "Couldn't add hardcoded device at addr 0x%x\n",
1985 ssif_i2c_driver
.driver
.acpi_match_table
=
1986 ACPI_PTR(ssif_acpi_match
);
1992 ssif_i2c_driver
.address_list
= ssif_address_list();
1994 rv
= i2c_add_driver(&ssif_i2c_driver
);
2000 module_init(init_ipmi_ssif
);
2002 static void cleanup_ipmi_ssif(void)
2007 initialized
= false;
2009 i2c_del_driver(&ssif_i2c_driver
);
2011 free_ssif_clients();
2013 module_exit(cleanup_ipmi_ssif
);
2015 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2016 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2017 MODULE_LICENSE("GPL");