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>
55 #include <linux/time64.h>
58 #define PFX "ipmi_ssif: "
59 #define DEVICE_NAME "ipmi_ssif"
61 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
63 #define SSIF_IPMI_REQUEST 2
64 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
65 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
66 #define SSIF_IPMI_RESPONSE 3
67 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
69 /* ssif_debug is a bit-field
70 * SSIF_DEBUG_MSG - commands and their responses
71 * SSIF_DEBUG_STATES - message states
72 * SSIF_DEBUG_TIMING - Measure times between events in the driver
74 #define SSIF_DEBUG_TIMING 4
75 #define SSIF_DEBUG_STATE 2
76 #define SSIF_DEBUG_MSG 1
77 #define SSIF_NODEBUG 0
78 #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
83 #define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
84 #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
86 /* How many times to we retry sending/receiving the message. */
87 #define SSIF_SEND_RETRIES 5
88 #define SSIF_RECV_RETRIES 250
90 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
91 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
92 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
94 enum ssif_intf_state
{
99 SSIF_GETTING_MESSAGES
,
100 /* FIXME - add watchdog stuff. */
103 #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
104 && (ssif)->curr_msg == NULL)
107 * Indexes into stats[] in ssif_info below.
109 enum ssif_stat_indexes
{
110 /* Number of total messages sent. */
111 SSIF_STAT_sent_messages
= 0,
114 * Number of message parts sent. Messages may be broken into
115 * parts if they are long.
117 SSIF_STAT_sent_messages_parts
,
120 * Number of time a message was retried.
122 SSIF_STAT_send_retries
,
125 * Number of times the send of a message failed.
127 SSIF_STAT_send_errors
,
130 * Number of message responses received.
132 SSIF_STAT_received_messages
,
135 * Number of message fragments received.
137 SSIF_STAT_received_message_parts
,
140 * Number of times the receive of a message was retried.
142 SSIF_STAT_receive_retries
,
145 * Number of errors receiving messages.
147 SSIF_STAT_receive_errors
,
150 * Number of times a flag fetch was requested.
152 SSIF_STAT_flag_fetches
,
155 * Number of times the hardware didn't follow the state machine.
160 * Number of received events.
164 /* Number of asyncronous messages received. */
165 SSIF_STAT_incoming_messages
,
167 /* Number of watchdog pretimeouts. */
168 SSIF_STAT_watchdog_pretimeouts
,
170 /* Number of alers received. */
173 /* Always add statistics before this value, it must be last. */
177 struct ssif_addr_info
{
178 struct i2c_board_info binfo
;
182 enum ipmi_addr_src addr_src
;
183 union ipmi_smi_info_union addr_info
;
185 struct i2c_client
*client
;
187 struct mutex clients_mutex
;
188 struct list_head clients
;
190 struct list_head link
;
195 typedef void (*ssif_i2c_done
)(struct ssif_info
*ssif_info
, int result
,
196 unsigned char *data
, unsigned int len
);
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 /* From the device id response. */
271 struct ipmi_device_id device_id
;
273 struct timer_list retry_timer
;
276 /* Info from SSIF cmd */
277 unsigned char max_xmit_msg_size
;
278 unsigned char max_recv_msg_size
;
279 unsigned int multi_support
;
282 #define SSIF_NO_MULTI 0
283 #define SSIF_MULTI_2_PART 1
284 #define SSIF_MULTI_n_PART 2
285 unsigned char *multi_data
;
286 unsigned int multi_len
;
287 unsigned int multi_pos
;
289 atomic_t stats
[SSIF_NUM_STATS
];
292 #define ssif_inc_stat(ssif, stat) \
293 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
294 #define ssif_get_stat(ssif, stat) \
295 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
297 static bool initialized
;
299 static atomic_t next_intf
= ATOMIC_INIT(0);
301 static void return_hosed_msg(struct ssif_info
*ssif_info
,
302 struct ipmi_smi_msg
*msg
);
303 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
);
304 static int start_send(struct ssif_info
*ssif_info
,
308 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info
*ssif_info
,
309 unsigned long *flags
)
311 spin_lock_irqsave(&ssif_info
->lock
, *flags
);
315 static void ipmi_ssif_unlock_cond(struct ssif_info
*ssif_info
,
316 unsigned long *flags
)
318 spin_unlock_irqrestore(&ssif_info
->lock
, *flags
);
321 static void deliver_recv_msg(struct ssif_info
*ssif_info
,
322 struct ipmi_smi_msg
*msg
)
324 ipmi_smi_t intf
= ssif_info
->intf
;
327 ipmi_free_smi_msg(msg
);
328 } else if (msg
->rsp_size
< 0) {
329 return_hosed_msg(ssif_info
, msg
);
331 "Malformed message in deliver_recv_msg: rsp_size = %d\n",
334 ipmi_smi_msg_received(intf
, msg
);
338 static void return_hosed_msg(struct ssif_info
*ssif_info
,
339 struct ipmi_smi_msg
*msg
)
341 ssif_inc_stat(ssif_info
, hosed
);
343 /* Make it a response */
344 msg
->rsp
[0] = msg
->data
[0] | 4;
345 msg
->rsp
[1] = msg
->data
[1];
346 msg
->rsp
[2] = 0xFF; /* Unknown error. */
349 deliver_recv_msg(ssif_info
, msg
);
353 * Must be called with the message lock held. This will release the
354 * message lock. Note that the caller will check SSIF_IDLE and start a
355 * new operation, so there is no need to check for new messages to
358 static void start_clear_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
360 unsigned char msg
[3];
362 ssif_info
->msg_flags
&= ~WDT_PRE_TIMEOUT_INT
;
363 ssif_info
->ssif_state
= SSIF_CLEARING_FLAGS
;
364 ipmi_ssif_unlock_cond(ssif_info
, flags
);
366 /* Make sure the watchdog pre-timeout flag is not set at startup. */
367 msg
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
368 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
369 msg
[2] = WDT_PRE_TIMEOUT_INT
;
371 if (start_send(ssif_info
, msg
, 3) != 0) {
372 /* Error, just go to normal state. */
373 ssif_info
->ssif_state
= SSIF_NORMAL
;
377 static void start_flag_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
381 ssif_info
->req_flags
= false;
382 ssif_info
->ssif_state
= SSIF_GETTING_FLAGS
;
383 ipmi_ssif_unlock_cond(ssif_info
, flags
);
385 mb
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
386 mb
[1] = IPMI_GET_MSG_FLAGS_CMD
;
387 if (start_send(ssif_info
, mb
, 2) != 0)
388 ssif_info
->ssif_state
= SSIF_NORMAL
;
391 static void check_start_send(struct ssif_info
*ssif_info
, unsigned long *flags
,
392 struct ipmi_smi_msg
*msg
)
394 if (start_send(ssif_info
, msg
->data
, msg
->data_size
) != 0) {
395 unsigned long oflags
;
397 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
398 ssif_info
->curr_msg
= NULL
;
399 ssif_info
->ssif_state
= SSIF_NORMAL
;
400 ipmi_ssif_unlock_cond(ssif_info
, flags
);
401 ipmi_free_smi_msg(msg
);
405 static void start_event_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
407 struct ipmi_smi_msg
*msg
;
409 ssif_info
->req_events
= false;
411 msg
= ipmi_alloc_smi_msg();
413 ssif_info
->ssif_state
= SSIF_NORMAL
;
414 ipmi_ssif_unlock_cond(ssif_info
, flags
);
418 ssif_info
->curr_msg
= msg
;
419 ssif_info
->ssif_state
= SSIF_GETTING_EVENTS
;
420 ipmi_ssif_unlock_cond(ssif_info
, flags
);
422 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
423 msg
->data
[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD
;
426 check_start_send(ssif_info
, flags
, msg
);
429 static void start_recv_msg_fetch(struct ssif_info
*ssif_info
,
430 unsigned long *flags
)
432 struct ipmi_smi_msg
*msg
;
434 msg
= ipmi_alloc_smi_msg();
436 ssif_info
->ssif_state
= SSIF_NORMAL
;
437 ipmi_ssif_unlock_cond(ssif_info
, flags
);
441 ssif_info
->curr_msg
= msg
;
442 ssif_info
->ssif_state
= SSIF_GETTING_MESSAGES
;
443 ipmi_ssif_unlock_cond(ssif_info
, flags
);
445 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
446 msg
->data
[1] = IPMI_GET_MSG_CMD
;
449 check_start_send(ssif_info
, flags
, msg
);
453 * Must be called with the message lock held. This will release the
454 * message lock. Note that the caller will check SSIF_IDLE and start a
455 * new operation, so there is no need to check for new messages to
458 static void handle_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
460 if (ssif_info
->msg_flags
& WDT_PRE_TIMEOUT_INT
) {
461 ipmi_smi_t intf
= ssif_info
->intf
;
462 /* Watchdog pre-timeout */
463 ssif_inc_stat(ssif_info
, watchdog_pretimeouts
);
464 start_clear_flags(ssif_info
, flags
);
466 ipmi_smi_watchdog_pretimeout(intf
);
467 } else if (ssif_info
->msg_flags
& RECEIVE_MSG_AVAIL
)
468 /* Messages available. */
469 start_recv_msg_fetch(ssif_info
, flags
);
470 else if (ssif_info
->msg_flags
& EVENT_MSG_BUFFER_FULL
)
471 /* Events available. */
472 start_event_fetch(ssif_info
, flags
);
474 ssif_info
->ssif_state
= SSIF_NORMAL
;
475 ipmi_ssif_unlock_cond(ssif_info
, flags
);
479 static int ipmi_ssif_thread(void *data
)
481 struct ssif_info
*ssif_info
= data
;
483 while (!kthread_should_stop()) {
486 /* Wait for something to do */
487 result
= wait_for_completion_interruptible(
488 &ssif_info
->wake_thread
);
489 if (ssif_info
->stopping
)
491 if (result
== -ERESTARTSYS
)
493 init_completion(&ssif_info
->wake_thread
);
495 if (ssif_info
->i2c_read_write
== I2C_SMBUS_WRITE
) {
496 result
= i2c_smbus_write_block_data(
497 ssif_info
->client
, ssif_info
->i2c_command
,
498 ssif_info
->i2c_data
[0],
499 ssif_info
->i2c_data
+ 1);
500 ssif_info
->done_handler(ssif_info
, result
, NULL
, 0);
502 result
= i2c_smbus_read_block_data(
503 ssif_info
->client
, ssif_info
->i2c_command
,
504 ssif_info
->i2c_data
);
506 ssif_info
->done_handler(ssif_info
, result
,
509 ssif_info
->done_handler(ssif_info
, 0,
518 static int ssif_i2c_send(struct ssif_info
*ssif_info
,
519 ssif_i2c_done handler
,
520 int read_write
, int command
,
521 unsigned char *data
, unsigned int size
)
523 ssif_info
->done_handler
= handler
;
525 ssif_info
->i2c_read_write
= read_write
;
526 ssif_info
->i2c_command
= command
;
527 ssif_info
->i2c_data
= data
;
528 ssif_info
->i2c_size
= size
;
529 complete(&ssif_info
->wake_thread
);
534 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
535 unsigned char *data
, unsigned int len
);
537 static void start_get(struct ssif_info
*ssif_info
)
541 ssif_info
->rtc_us_timer
= 0;
542 ssif_info
->multi_pos
= 0;
544 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
546 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
548 /* request failed, just return the error. */
549 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
550 pr_info("Error from i2c_non_blocking_op(5)\n");
552 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
556 static void retry_timeout(unsigned long data
)
558 struct ssif_info
*ssif_info
= (void *) data
;
559 unsigned long oflags
, *flags
;
562 if (ssif_info
->stopping
)
565 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
566 waiting
= ssif_info
->waiting_alert
;
567 ssif_info
->waiting_alert
= false;
568 ipmi_ssif_unlock_cond(ssif_info
, flags
);
571 start_get(ssif_info
);
575 static void ssif_alert(struct i2c_client
*client
, enum i2c_alert_protocol type
,
578 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
579 unsigned long oflags
, *flags
;
582 if (type
!= I2C_PROTOCOL_SMBUS_ALERT
)
585 ssif_inc_stat(ssif_info
, alerts
);
587 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
588 if (ssif_info
->waiting_alert
) {
589 ssif_info
->waiting_alert
= false;
590 del_timer(&ssif_info
->retry_timer
);
592 } else if (ssif_info
->curr_msg
) {
593 ssif_info
->got_alert
= true;
595 ipmi_ssif_unlock_cond(ssif_info
, flags
);
597 start_get(ssif_info
);
600 static int start_resend(struct ssif_info
*ssif_info
);
602 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
603 unsigned char *data
, unsigned int len
)
605 struct ipmi_smi_msg
*msg
;
606 unsigned long oflags
, *flags
;
610 * We are single-threaded here, so no need for a lock until we
611 * start messing with driver states or the queues.
615 ssif_info
->retries_left
--;
616 if (ssif_info
->retries_left
> 0) {
617 ssif_inc_stat(ssif_info
, receive_retries
);
619 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
620 ssif_info
->waiting_alert
= true;
621 ssif_info
->rtc_us_timer
= SSIF_MSG_USEC
;
622 mod_timer(&ssif_info
->retry_timer
,
623 jiffies
+ SSIF_MSG_JIFFIES
);
624 ipmi_ssif_unlock_cond(ssif_info
, flags
);
628 ssif_inc_stat(ssif_info
, receive_errors
);
630 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
631 pr_info("Error in msg_done_handler: %d\n", result
);
636 if ((len
> 1) && (ssif_info
->multi_pos
== 0)
637 && (data
[0] == 0x00) && (data
[1] == 0x01)) {
638 /* Start of multi-part read. Start the next transaction. */
641 ssif_inc_stat(ssif_info
, received_message_parts
);
643 /* Remove the multi-part read marker. */
645 for (i
= 0; i
< len
; i
++)
646 ssif_info
->data
[i
] = data
[i
+2];
647 ssif_info
->multi_len
= len
;
648 ssif_info
->multi_pos
= 1;
650 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
651 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
652 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
654 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
655 pr_info("Error from i2c_non_blocking_op(1)\n");
660 } else if (ssif_info
->multi_pos
) {
661 /* Middle of multi-part read. Start the next transaction. */
663 unsigned char blocknum
;
667 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
668 pr_info(PFX
"Middle message with no data\n");
675 if (ssif_info
->multi_len
+ len
- 1 > IPMI_MAX_MSG_LENGTH
) {
676 /* Received message too big, abort the operation. */
678 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
679 pr_info("Received message too big\n");
684 /* Remove the blocknum from the data. */
686 for (i
= 0; i
< len
; i
++)
687 ssif_info
->data
[i
+ ssif_info
->multi_len
] = data
[i
+ 1];
688 ssif_info
->multi_len
+= len
;
689 if (blocknum
== 0xff) {
691 len
= ssif_info
->multi_len
;
692 data
= ssif_info
->data
;
693 } else if (blocknum
+ 1 != ssif_info
->multi_pos
) {
695 * Out of sequence block, just abort. Block
696 * numbers start at zero for the second block,
697 * but multi_pos starts at one, so the +1.
701 ssif_inc_stat(ssif_info
, received_message_parts
);
703 ssif_info
->multi_pos
++;
705 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
,
707 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
709 I2C_SMBUS_BLOCK_DATA
);
711 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
713 "Error from ssif_i2c_send\n");
722 ssif_inc_stat(ssif_info
, receive_errors
);
724 ssif_inc_stat(ssif_info
, received_messages
);
725 ssif_inc_stat(ssif_info
, received_message_parts
);
730 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
731 pr_info(PFX
"DONE 1: state = %d, result=%d.\n",
732 ssif_info
->ssif_state
, result
);
734 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
735 msg
= ssif_info
->curr_msg
;
738 if (msg
->rsp_size
> IPMI_MAX_MSG_LENGTH
)
739 msg
->rsp_size
= IPMI_MAX_MSG_LENGTH
;
740 memcpy(msg
->rsp
, data
, msg
->rsp_size
);
741 ssif_info
->curr_msg
= NULL
;
744 switch (ssif_info
->ssif_state
) {
746 ipmi_ssif_unlock_cond(ssif_info
, flags
);
751 return_hosed_msg(ssif_info
, msg
);
753 deliver_recv_msg(ssif_info
, msg
);
756 case SSIF_GETTING_FLAGS
:
757 /* We got the flags from the SSIF, now handle them. */
758 if ((result
< 0) || (len
< 4) || (data
[2] != 0)) {
760 * Error fetching flags, or invalid length,
761 * just give up for now.
763 ssif_info
->ssif_state
= SSIF_NORMAL
;
764 ipmi_ssif_unlock_cond(ssif_info
, flags
);
765 pr_warn(PFX
"Error getting flags: %d %d, %x\n",
766 result
, len
, data
[2]);
767 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
768 || data
[1] != IPMI_GET_MSG_FLAGS_CMD
) {
770 * Don't abort here, maybe it was a queued
771 * response to a previous command.
773 ipmi_ssif_unlock_cond(ssif_info
, flags
);
774 pr_warn(PFX
"Invalid response getting flags: %x %x\n",
777 ssif_inc_stat(ssif_info
, flag_fetches
);
778 ssif_info
->msg_flags
= data
[3];
779 handle_flags(ssif_info
, flags
);
783 case SSIF_CLEARING_FLAGS
:
784 /* We cleared the flags. */
785 if ((result
< 0) || (len
< 3) || (data
[2] != 0)) {
786 /* Error clearing flags */
787 pr_warn(PFX
"Error clearing flags: %d %d, %x\n",
788 result
, len
, data
[2]);
789 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
790 || data
[1] != IPMI_CLEAR_MSG_FLAGS_CMD
) {
791 pr_warn(PFX
"Invalid response clearing flags: %x %x\n",
794 ssif_info
->ssif_state
= SSIF_NORMAL
;
795 ipmi_ssif_unlock_cond(ssif_info
, flags
);
798 case SSIF_GETTING_EVENTS
:
799 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
800 /* Error getting event, probably done. */
803 /* Take off the event flag. */
804 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
805 handle_flags(ssif_info
, flags
);
806 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
807 || msg
->rsp
[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD
) {
808 pr_warn(PFX
"Invalid response getting events: %x %x\n",
809 msg
->rsp
[0], msg
->rsp
[1]);
811 /* Take off the event flag. */
812 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
813 handle_flags(ssif_info
, flags
);
815 handle_flags(ssif_info
, flags
);
816 ssif_inc_stat(ssif_info
, events
);
817 deliver_recv_msg(ssif_info
, msg
);
821 case SSIF_GETTING_MESSAGES
:
822 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
823 /* Error getting event, probably done. */
826 /* Take off the msg flag. */
827 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
828 handle_flags(ssif_info
, flags
);
829 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
830 || msg
->rsp
[1] != IPMI_GET_MSG_CMD
) {
831 pr_warn(PFX
"Invalid response clearing flags: %x %x\n",
832 msg
->rsp
[0], msg
->rsp
[1]);
835 /* Take off the msg flag. */
836 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
837 handle_flags(ssif_info
, flags
);
839 ssif_inc_stat(ssif_info
, incoming_messages
);
840 handle_flags(ssif_info
, flags
);
841 deliver_recv_msg(ssif_info
, msg
);
846 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
847 if (SSIF_IDLE(ssif_info
) && !ssif_info
->stopping
) {
848 if (ssif_info
->req_events
)
849 start_event_fetch(ssif_info
, flags
);
850 else if (ssif_info
->req_flags
)
851 start_flag_fetch(ssif_info
, flags
);
853 start_next_msg(ssif_info
, flags
);
855 ipmi_ssif_unlock_cond(ssif_info
, flags
);
857 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
858 pr_info(PFX
"DONE 2: state = %d.\n", ssif_info
->ssif_state
);
861 static void msg_written_handler(struct ssif_info
*ssif_info
, int result
,
862 unsigned char *data
, unsigned int len
)
866 /* We are single-threaded here, so no need for a lock. */
868 ssif_info
->retries_left
--;
869 if (ssif_info
->retries_left
> 0) {
870 if (!start_resend(ssif_info
)) {
871 ssif_inc_stat(ssif_info
, send_retries
);
874 /* request failed, just return the error. */
875 ssif_inc_stat(ssif_info
, send_errors
);
877 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
879 "Out of retries in msg_written_handler\n");
880 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
884 ssif_inc_stat(ssif_info
, send_errors
);
887 * Got an error on transmit, let the done routine
890 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
891 pr_info("Error in msg_written_handler: %d\n", result
);
893 msg_done_handler(ssif_info
, result
, NULL
, 0);
897 if (ssif_info
->multi_data
) {
899 * In the middle of a multi-data write. See the comment
900 * in the SSIF_MULTI_n_PART case in the probe function
901 * for details on the intricacies of this.
904 unsigned char *data_to_send
;
906 ssif_inc_stat(ssif_info
, sent_messages_parts
);
908 left
= ssif_info
->multi_len
- ssif_info
->multi_pos
;
912 ssif_info
->multi_data
[ssif_info
->multi_pos
] = left
;
913 data_to_send
= ssif_info
->multi_data
+ ssif_info
->multi_pos
;
914 ssif_info
->multi_pos
+= left
;
917 * Write is finished. Note that we must end
918 * with a write of less than 32 bytes to
919 * complete the transaction, even if it is
922 ssif_info
->multi_data
= NULL
;
924 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
,
926 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
,
928 I2C_SMBUS_BLOCK_DATA
);
930 /* request failed, just return the error. */
931 ssif_inc_stat(ssif_info
, send_errors
);
933 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
934 pr_info("Error from i2c_non_blocking_op(3)\n");
935 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
938 /* Ready to request the result. */
939 unsigned long oflags
, *flags
;
941 ssif_inc_stat(ssif_info
, sent_messages
);
942 ssif_inc_stat(ssif_info
, sent_messages_parts
);
944 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
945 if (ssif_info
->got_alert
) {
946 /* The result is already ready, just start it. */
947 ssif_info
->got_alert
= false;
948 ipmi_ssif_unlock_cond(ssif_info
, flags
);
949 start_get(ssif_info
);
951 /* Wait a jiffie then request the next message */
952 ssif_info
->waiting_alert
= true;
953 ssif_info
->retries_left
= SSIF_RECV_RETRIES
;
954 ssif_info
->rtc_us_timer
= SSIF_MSG_PART_USEC
;
955 mod_timer(&ssif_info
->retry_timer
,
956 jiffies
+ SSIF_MSG_PART_JIFFIES
);
957 ipmi_ssif_unlock_cond(ssif_info
, flags
);
962 static int start_resend(struct ssif_info
*ssif_info
)
967 ssif_info
->got_alert
= false;
969 if (ssif_info
->data_len
> 32) {
970 command
= SSIF_IPMI_MULTI_PART_REQUEST_START
;
971 ssif_info
->multi_data
= ssif_info
->data
;
972 ssif_info
->multi_len
= ssif_info
->data_len
;
974 * Subtle thing, this is 32, not 33, because we will
975 * overwrite the thing at position 32 (which was just
976 * transmitted) with the new length.
978 ssif_info
->multi_pos
= 32;
979 ssif_info
->data
[0] = 32;
981 ssif_info
->multi_data
= NULL
;
982 command
= SSIF_IPMI_REQUEST
;
983 ssif_info
->data
[0] = ssif_info
->data_len
;
986 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
, I2C_SMBUS_WRITE
,
987 command
, ssif_info
->data
, I2C_SMBUS_BLOCK_DATA
);
988 if (rv
&& (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
))
989 pr_info("Error from i2c_non_blocking_op(4)\n");
993 static int start_send(struct ssif_info
*ssif_info
,
997 if (len
> IPMI_MAX_MSG_LENGTH
)
999 if (len
> ssif_info
->max_xmit_msg_size
)
1002 ssif_info
->retries_left
= SSIF_SEND_RETRIES
;
1003 memcpy(ssif_info
->data
+ 1, data
, len
);
1004 ssif_info
->data_len
= len
;
1005 return start_resend(ssif_info
);
1008 /* Must be called with the message lock held. */
1009 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
)
1011 struct ipmi_smi_msg
*msg
;
1012 unsigned long oflags
;
1015 if (!SSIF_IDLE(ssif_info
)) {
1016 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1020 if (!ssif_info
->waiting_msg
) {
1021 ssif_info
->curr_msg
= NULL
;
1022 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1026 ssif_info
->curr_msg
= ssif_info
->waiting_msg
;
1027 ssif_info
->waiting_msg
= NULL
;
1028 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1029 rv
= start_send(ssif_info
,
1030 ssif_info
->curr_msg
->data
,
1031 ssif_info
->curr_msg
->data_size
);
1033 msg
= ssif_info
->curr_msg
;
1034 ssif_info
->curr_msg
= NULL
;
1035 return_hosed_msg(ssif_info
, msg
);
1036 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1042 static void sender(void *send_info
,
1043 struct ipmi_smi_msg
*msg
)
1045 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1046 unsigned long oflags
, *flags
;
1048 BUG_ON(ssif_info
->waiting_msg
);
1049 ssif_info
->waiting_msg
= msg
;
1051 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1052 start_next_msg(ssif_info
, flags
);
1054 if (ssif_info
->ssif_debug
& SSIF_DEBUG_TIMING
) {
1055 struct timespec64 t
;
1057 ktime_get_real_ts64(&t
);
1058 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1059 msg
->data
[0], msg
->data
[1],
1060 (long long) t
.tv_sec
, (long) t
.tv_nsec
/ NSEC_PER_USEC
);
1064 static int get_smi_info(void *send_info
, struct ipmi_smi_info
*data
)
1066 struct ssif_info
*ssif_info
= send_info
;
1068 data
->addr_src
= ssif_info
->addr_source
;
1069 data
->dev
= &ssif_info
->client
->dev
;
1070 data
->addr_info
= ssif_info
->addr_info
;
1071 get_device(data
->dev
);
1077 * Instead of having our own timer to periodically check the message
1078 * flags, we let the message handler drive us.
1080 static void request_events(void *send_info
)
1082 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1083 unsigned long oflags
, *flags
;
1085 if (!ssif_info
->has_event_buffer
)
1088 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1090 * Request flags first, not events, because the lower layer
1091 * doesn't have a way to send an attention. But make sure
1092 * event checking still happens.
1094 ssif_info
->req_events
= true;
1095 if (SSIF_IDLE(ssif_info
))
1096 start_flag_fetch(ssif_info
, flags
);
1098 ssif_info
->req_flags
= true;
1099 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1103 static int inc_usecount(void *send_info
)
1105 struct ssif_info
*ssif_info
= send_info
;
1107 if (!i2c_get_adapter(i2c_adapter_id(ssif_info
->client
->adapter
)))
1110 i2c_use_client(ssif_info
->client
);
1114 static void dec_usecount(void *send_info
)
1116 struct ssif_info
*ssif_info
= send_info
;
1118 i2c_release_client(ssif_info
->client
);
1119 i2c_put_adapter(ssif_info
->client
->adapter
);
1122 static int ssif_start_processing(void *send_info
,
1125 struct ssif_info
*ssif_info
= send_info
;
1127 ssif_info
->intf
= intf
;
1132 #define MAX_SSIF_BMCS 4
1134 static unsigned short addr
[MAX_SSIF_BMCS
];
1135 static int num_addrs
;
1136 module_param_array(addr
, ushort
, &num_addrs
, 0);
1137 MODULE_PARM_DESC(addr
, "The addresses to scan for IPMI BMCs on the SSIFs.");
1139 static char *adapter_name
[MAX_SSIF_BMCS
];
1140 static int num_adapter_names
;
1141 module_param_array(adapter_name
, charp
, &num_adapter_names
, 0);
1142 MODULE_PARM_DESC(adapter_name
, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1144 static int slave_addrs
[MAX_SSIF_BMCS
];
1145 static int num_slave_addrs
;
1146 module_param_array(slave_addrs
, int, &num_slave_addrs
, 0);
1147 MODULE_PARM_DESC(slave_addrs
,
1148 "The default IPMB slave address for the controller.");
1150 static bool alerts_broken
;
1151 module_param(alerts_broken
, bool, 0);
1152 MODULE_PARM_DESC(alerts_broken
, "Don't enable alerts for the controller.");
1155 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1156 * bit 2 enables timing debugging. This is an array indexed by
1159 static int dbg
[MAX_SSIF_BMCS
];
1161 module_param_array(dbg
, int, &num_dbg
, 0);
1162 MODULE_PARM_DESC(dbg
, "Turn on debugging.");
1164 static bool ssif_dbg_probe
;
1165 module_param_named(dbg_probe
, ssif_dbg_probe
, bool, 0);
1166 MODULE_PARM_DESC(dbg_probe
, "Enable debugging of probing of adapters.");
1168 static bool ssif_tryacpi
= true;
1169 module_param_named(tryacpi
, ssif_tryacpi
, bool, 0);
1170 MODULE_PARM_DESC(tryacpi
, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1172 static bool ssif_trydmi
= true;
1173 module_param_named(trydmi
, ssif_trydmi
, bool, 0);
1174 MODULE_PARM_DESC(trydmi
, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1176 static DEFINE_MUTEX(ssif_infos_mutex
);
1177 static LIST_HEAD(ssif_infos
);
1179 static int ssif_remove(struct i2c_client
*client
)
1181 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
1182 struct ssif_addr_info
*addr_info
;
1189 * After this point, we won't deliver anything asychronously
1190 * to the message handler. We can unregister ourself.
1192 rv
= ipmi_unregister_smi(ssif_info
->intf
);
1194 pr_err(PFX
"Unable to unregister device: errno=%d\n", rv
);
1197 ssif_info
->intf
= NULL
;
1199 /* make sure the driver is not looking for flags any more. */
1200 while (ssif_info
->ssif_state
!= SSIF_NORMAL
)
1201 schedule_timeout(1);
1203 ssif_info
->stopping
= true;
1204 del_timer_sync(&ssif_info
->retry_timer
);
1205 if (ssif_info
->thread
) {
1206 complete(&ssif_info
->wake_thread
);
1207 kthread_stop(ssif_info
->thread
);
1210 list_for_each_entry(addr_info
, &ssif_infos
, link
) {
1211 if (addr_info
->client
== client
) {
1212 addr_info
->client
= NULL
;
1218 * No message can be outstanding now, we have removed the
1219 * upper layer and it permitted us to do so.
1225 static int do_cmd(struct i2c_client
*client
, int len
, unsigned char *msg
,
1226 int *resp_len
, unsigned char *resp
)
1231 retry_cnt
= SSIF_SEND_RETRIES
;
1233 ret
= i2c_smbus_write_block_data(client
, SSIF_IPMI_REQUEST
, len
, msg
);
1242 retry_cnt
= SSIF_RECV_RETRIES
;
1243 while (retry_cnt
> 0) {
1244 ret
= i2c_smbus_read_block_data(client
, SSIF_IPMI_RESPONSE
,
1248 msleep(SSIF_MSG_MSEC
);
1255 /* Validate that the response is correct. */
1257 (resp
[0] != (msg
[0] | (1 << 2))) ||
1258 (resp
[1] != msg
[1]))
1269 static int ssif_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
1271 unsigned char *resp
;
1272 unsigned char msg
[3];
1276 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1280 /* Do a Get Device ID command, since it is required. */
1281 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1282 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1283 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1287 strlcpy(info
->type
, DEVICE_NAME
, I2C_NAME_SIZE
);
1292 static int smi_type_proc_show(struct seq_file
*m
, void *v
)
1294 seq_puts(m
, "ssif\n");
1299 static int smi_type_proc_open(struct inode
*inode
, struct file
*file
)
1301 return single_open(file
, smi_type_proc_show
, inode
->i_private
);
1304 static const struct file_operations smi_type_proc_ops
= {
1305 .open
= smi_type_proc_open
,
1307 .llseek
= seq_lseek
,
1308 .release
= single_release
,
1311 static int smi_stats_proc_show(struct seq_file
*m
, void *v
)
1313 struct ssif_info
*ssif_info
= m
->private;
1315 seq_printf(m
, "sent_messages: %u\n",
1316 ssif_get_stat(ssif_info
, sent_messages
));
1317 seq_printf(m
, "sent_messages_parts: %u\n",
1318 ssif_get_stat(ssif_info
, sent_messages_parts
));
1319 seq_printf(m
, "send_retries: %u\n",
1320 ssif_get_stat(ssif_info
, send_retries
));
1321 seq_printf(m
, "send_errors: %u\n",
1322 ssif_get_stat(ssif_info
, send_errors
));
1323 seq_printf(m
, "received_messages: %u\n",
1324 ssif_get_stat(ssif_info
, received_messages
));
1325 seq_printf(m
, "received_message_parts: %u\n",
1326 ssif_get_stat(ssif_info
, received_message_parts
));
1327 seq_printf(m
, "receive_retries: %u\n",
1328 ssif_get_stat(ssif_info
, receive_retries
));
1329 seq_printf(m
, "receive_errors: %u\n",
1330 ssif_get_stat(ssif_info
, receive_errors
));
1331 seq_printf(m
, "flag_fetches: %u\n",
1332 ssif_get_stat(ssif_info
, flag_fetches
));
1333 seq_printf(m
, "hosed: %u\n",
1334 ssif_get_stat(ssif_info
, hosed
));
1335 seq_printf(m
, "events: %u\n",
1336 ssif_get_stat(ssif_info
, events
));
1337 seq_printf(m
, "watchdog_pretimeouts: %u\n",
1338 ssif_get_stat(ssif_info
, watchdog_pretimeouts
));
1339 seq_printf(m
, "alerts: %u\n",
1340 ssif_get_stat(ssif_info
, alerts
));
1344 static int smi_stats_proc_open(struct inode
*inode
, struct file
*file
)
1346 return single_open(file
, smi_stats_proc_show
, PDE_DATA(inode
));
1349 static const struct file_operations smi_stats_proc_ops
= {
1350 .open
= smi_stats_proc_open
,
1352 .llseek
= seq_lseek
,
1353 .release
= single_release
,
1356 static int strcmp_nospace(char *s1
, char *s2
)
1358 while (*s1
&& *s2
) {
1359 while (isspace(*s1
))
1361 while (isspace(*s2
))
1373 static struct ssif_addr_info
*ssif_info_find(unsigned short addr
,
1375 bool match_null_name
)
1377 struct ssif_addr_info
*info
, *found
= NULL
;
1380 list_for_each_entry(info
, &ssif_infos
, link
) {
1381 if (info
->binfo
.addr
== addr
) {
1382 if (info
->adapter_name
|| adapter_name
) {
1383 if (!info
->adapter_name
!= !adapter_name
) {
1384 /* One is NULL and one is not */
1388 strcmp_nospace(info
->adapter_name
,
1390 /* Names do not match */
1398 if (!found
&& match_null_name
) {
1399 /* Try to get an exact match first, then try with a NULL name */
1400 adapter_name
= NULL
;
1401 match_null_name
= false;
1408 static bool check_acpi(struct ssif_info
*ssif_info
, struct device
*dev
)
1411 acpi_handle acpi_handle
;
1413 acpi_handle
= ACPI_HANDLE(dev
);
1415 ssif_info
->addr_source
= SI_ACPI
;
1416 ssif_info
->addr_info
.acpi_info
.acpi_handle
= acpi_handle
;
1423 static int find_slave_address(struct i2c_client
*client
, int slave_addr
)
1425 #ifdef CONFIG_IPMI_DMI_DECODE
1427 slave_addr
= ipmi_dmi_get_slave_addr(
1429 i2c_adapter_id(client
->adapter
),
1437 * Global enables we care about.
1439 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1440 IPMI_BMC_EVT_MSG_INTR)
1442 static int ssif_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1444 unsigned char msg
[3];
1445 unsigned char *resp
;
1446 struct ssif_info
*ssif_info
;
1451 struct ssif_addr_info
*addr_info
= NULL
;
1453 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1457 ssif_info
= kzalloc(sizeof(*ssif_info
), GFP_KERNEL
);
1463 if (!check_acpi(ssif_info
, &client
->dev
)) {
1464 addr_info
= ssif_info_find(client
->addr
, client
->adapter
->name
,
1467 /* Must have come in through sysfs. */
1468 ssif_info
->addr_source
= SI_HOTMOD
;
1470 ssif_info
->addr_source
= addr_info
->addr_src
;
1471 ssif_info
->ssif_debug
= addr_info
->debug
;
1472 ssif_info
->addr_info
= addr_info
->addr_info
;
1473 addr_info
->client
= client
;
1474 slave_addr
= addr_info
->slave_addr
;
1478 slave_addr
= find_slave_address(client
, slave_addr
);
1480 pr_info(PFX
"Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1481 ipmi_addr_src_to_str(ssif_info
->addr_source
),
1482 client
->addr
, client
->adapter
->name
, slave_addr
);
1485 * Do a Get Device ID command, since it comes back with some
1488 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1489 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1490 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1494 rv
= ipmi_demangle_device_id(resp
, len
, &ssif_info
->device_id
);
1498 ssif_info
->client
= client
;
1499 i2c_set_clientdata(client
, ssif_info
);
1501 /* Now check for system interface capabilities */
1502 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1503 msg
[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD
;
1504 msg
[2] = 0; /* SSIF */
1505 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1506 if (!rv
&& (len
>= 3) && (resp
[2] == 0)) {
1509 pr_info(PFX
"SSIF info too short: %d\n", len
);
1513 /* Got a good SSIF response, handle it. */
1514 ssif_info
->max_xmit_msg_size
= resp
[5];
1515 ssif_info
->max_recv_msg_size
= resp
[6];
1516 ssif_info
->multi_support
= (resp
[4] >> 6) & 0x3;
1517 ssif_info
->supports_pec
= (resp
[4] >> 3) & 0x1;
1519 /* Sanitize the data */
1520 switch (ssif_info
->multi_support
) {
1522 if (ssif_info
->max_xmit_msg_size
> 32)
1523 ssif_info
->max_xmit_msg_size
= 32;
1524 if (ssif_info
->max_recv_msg_size
> 32)
1525 ssif_info
->max_recv_msg_size
= 32;
1528 case SSIF_MULTI_2_PART
:
1529 if (ssif_info
->max_xmit_msg_size
> 63)
1530 ssif_info
->max_xmit_msg_size
= 63;
1531 if (ssif_info
->max_recv_msg_size
> 62)
1532 ssif_info
->max_recv_msg_size
= 62;
1535 case SSIF_MULTI_n_PART
:
1537 * The specification is rather confusing at
1538 * this point, but I think I understand what
1539 * is meant. At least I have a workable
1540 * solution. With multi-part messages, you
1541 * cannot send a message that is a multiple of
1542 * 32-bytes in length, because the start and
1543 * middle messages are 32-bytes and the end
1544 * message must be at least one byte. You
1545 * can't fudge on an extra byte, that would
1546 * screw up things like fru data writes. So
1547 * we limit the length to 63 bytes. That way
1548 * a 32-byte message gets sent as a single
1549 * part. A larger message will be a 32-byte
1550 * start and the next message is always going
1551 * to be 1-31 bytes in length. Not ideal, but
1554 if (ssif_info
->max_xmit_msg_size
> 63)
1555 ssif_info
->max_xmit_msg_size
= 63;
1559 /* Data is not sane, just give up. */
1564 /* Assume no multi-part or PEC support */
1565 pr_info(PFX
"Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1568 ssif_info
->max_xmit_msg_size
= 32;
1569 ssif_info
->max_recv_msg_size
= 32;
1570 ssif_info
->multi_support
= SSIF_NO_MULTI
;
1571 ssif_info
->supports_pec
= 0;
1574 /* Make sure the NMI timeout is cleared. */
1575 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1576 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
1577 msg
[2] = WDT_PRE_TIMEOUT_INT
;
1578 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1579 if (rv
|| (len
< 3) || (resp
[2] != 0))
1580 pr_warn(PFX
"Unable to clear message flags: %d %d %2.2x\n",
1583 /* Attempt to enable the event buffer. */
1584 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1585 msg
[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD
;
1586 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1587 if (rv
|| (len
< 4) || (resp
[2] != 0)) {
1588 pr_warn(PFX
"Error getting global enables: %d %d %2.2x\n",
1590 rv
= 0; /* Not fatal */
1594 ssif_info
->global_enables
= resp
[3];
1596 if (resp
[3] & IPMI_BMC_EVT_MSG_BUFF
) {
1597 ssif_info
->has_event_buffer
= true;
1598 /* buffer is already enabled, nothing to do. */
1602 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1603 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1604 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_EVT_MSG_BUFF
;
1605 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1606 if (rv
|| (len
< 2)) {
1607 pr_warn(PFX
"Error setting global enables: %d %d %2.2x\n",
1609 rv
= 0; /* Not fatal */
1614 /* A successful return means the event buffer is supported. */
1615 ssif_info
->has_event_buffer
= true;
1616 ssif_info
->global_enables
|= IPMI_BMC_EVT_MSG_BUFF
;
1619 /* Some systems don't behave well if you enable alerts. */
1623 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1624 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1625 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_RCV_MSG_INTR
;
1626 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1627 if (rv
|| (len
< 2)) {
1628 pr_warn(PFX
"Error setting global enables: %d %d %2.2x\n",
1630 rv
= 0; /* Not fatal */
1635 /* A successful return means the alert is supported. */
1636 ssif_info
->supports_alert
= true;
1637 ssif_info
->global_enables
|= IPMI_BMC_RCV_MSG_INTR
;
1641 ssif_info
->intf_num
= atomic_inc_return(&next_intf
);
1643 if (ssif_dbg_probe
) {
1644 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1648 spin_lock_init(&ssif_info
->lock
);
1649 ssif_info
->ssif_state
= SSIF_NORMAL
;
1650 setup_timer(&ssif_info
->retry_timer
, retry_timeout
,
1651 (unsigned long)ssif_info
);
1653 for (i
= 0; i
< SSIF_NUM_STATS
; i
++)
1654 atomic_set(&ssif_info
->stats
[i
], 0);
1656 if (ssif_info
->supports_pec
)
1657 ssif_info
->client
->flags
|= I2C_CLIENT_PEC
;
1659 ssif_info
->handlers
.owner
= THIS_MODULE
;
1660 ssif_info
->handlers
.start_processing
= ssif_start_processing
;
1661 ssif_info
->handlers
.get_smi_info
= get_smi_info
;
1662 ssif_info
->handlers
.sender
= sender
;
1663 ssif_info
->handlers
.request_events
= request_events
;
1664 ssif_info
->handlers
.inc_usecount
= inc_usecount
;
1665 ssif_info
->handlers
.dec_usecount
= dec_usecount
;
1668 unsigned int thread_num
;
1670 thread_num
= ((i2c_adapter_id(ssif_info
->client
->adapter
)
1672 ssif_info
->client
->addr
);
1673 init_completion(&ssif_info
->wake_thread
);
1674 ssif_info
->thread
= kthread_run(ipmi_ssif_thread
, ssif_info
,
1675 "kssif%4.4x", thread_num
);
1676 if (IS_ERR(ssif_info
->thread
)) {
1677 rv
= PTR_ERR(ssif_info
->thread
);
1678 dev_notice(&ssif_info
->client
->dev
,
1679 "Could not start kernel thread: error %d\n",
1685 rv
= ipmi_register_smi(&ssif_info
->handlers
,
1687 &ssif_info
->device_id
,
1688 &ssif_info
->client
->dev
,
1691 pr_err(PFX
"Unable to register device: error %d\n", rv
);
1695 rv
= ipmi_smi_add_proc_entry(ssif_info
->intf
, "type",
1699 pr_err(PFX
"Unable to create proc entry: %d\n", rv
);
1703 rv
= ipmi_smi_add_proc_entry(ssif_info
->intf
, "ssif_stats",
1704 &smi_stats_proc_ops
,
1707 pr_err(PFX
"Unable to create proc entry: %d\n", rv
);
1714 * Note that if addr_info->client is assigned, we
1715 * leave it. The i2c client hangs around even if we
1716 * return a failure here, and the failure here is not
1717 * propagated back to the i2c code. This seems to be
1718 * design intent, strange as it may be. But if we
1719 * don't leave it, ssif_platform_remove will not remove
1720 * the client like it should.
1722 dev_err(&client
->dev
, "Unable to start IPMI SSIF: %d\n", rv
);
1729 ipmi_unregister_smi(ssif_info
->intf
);
1733 static int ssif_adapter_handler(struct device
*adev
, void *opaque
)
1735 struct ssif_addr_info
*addr_info
= opaque
;
1737 if (adev
->type
!= &i2c_adapter_type
)
1740 i2c_new_device(to_i2c_adapter(adev
), &addr_info
->binfo
);
1742 if (!addr_info
->adapter_name
)
1743 return 1; /* Only try the first I2C adapter by default. */
1747 static int new_ssif_client(int addr
, char *adapter_name
,
1748 int debug
, int slave_addr
,
1749 enum ipmi_addr_src addr_src
,
1752 struct ssif_addr_info
*addr_info
;
1755 mutex_lock(&ssif_infos_mutex
);
1756 if (ssif_info_find(addr
, adapter_name
, false)) {
1761 addr_info
= kzalloc(sizeof(*addr_info
), GFP_KERNEL
);
1768 addr_info
->adapter_name
= kstrdup(adapter_name
, GFP_KERNEL
);
1769 if (!addr_info
->adapter_name
) {
1776 strncpy(addr_info
->binfo
.type
, DEVICE_NAME
,
1777 sizeof(addr_info
->binfo
.type
));
1778 addr_info
->binfo
.addr
= addr
;
1779 addr_info
->binfo
.platform_data
= addr_info
;
1780 addr_info
->debug
= debug
;
1781 addr_info
->slave_addr
= slave_addr
;
1782 addr_info
->addr_src
= addr_src
;
1783 addr_info
->dev
= dev
;
1786 dev_set_drvdata(dev
, addr_info
);
1788 list_add_tail(&addr_info
->link
, &ssif_infos
);
1791 i2c_for_each_dev(addr_info
, ssif_adapter_handler
);
1792 /* Otherwise address list will get it */
1795 mutex_unlock(&ssif_infos_mutex
);
1799 static void free_ssif_clients(void)
1801 struct ssif_addr_info
*info
, *tmp
;
1803 mutex_lock(&ssif_infos_mutex
);
1804 list_for_each_entry_safe(info
, tmp
, &ssif_infos
, link
) {
1805 list_del(&info
->link
);
1806 kfree(info
->adapter_name
);
1809 mutex_unlock(&ssif_infos_mutex
);
1812 static unsigned short *ssif_address_list(void)
1814 struct ssif_addr_info
*info
;
1815 unsigned int count
= 0, i
;
1816 unsigned short *address_list
;
1818 list_for_each_entry(info
, &ssif_infos
, link
)
1821 address_list
= kzalloc(sizeof(*address_list
) * (count
+ 1), GFP_KERNEL
);
1826 list_for_each_entry(info
, &ssif_infos
, link
) {
1827 unsigned short addr
= info
->binfo
.addr
;
1830 for (j
= 0; j
< i
; j
++) {
1831 if (address_list
[j
] == addr
)
1834 address_list
[i
] = addr
;
1838 address_list
[i
] = I2C_CLIENT_END
;
1840 return address_list
;
1844 static const struct acpi_device_id ssif_acpi_match
[] = {
1848 MODULE_DEVICE_TABLE(acpi
, ssif_acpi_match
);
1851 * Once we get an ACPI failure, we don't try any more, because we go
1852 * through the tables sequentially. Once we don't find a table, there
1855 static int acpi_failure
;
1858 * Defined in the IPMI 2.0 spec.
1869 s8 CreatorRevision
[4];
1872 s16 SpecificationRevision
;
1875 * Bit 0 - SCI interrupt supported
1876 * Bit 1 - I/O APIC/SAPIC
1881 * If bit 0 of InterruptType is set, then this is the SCI
1882 * interrupt in the GPEx_STS register.
1889 * If bit 1 of InterruptType is set, then this is the I/O
1890 * APIC/SAPIC interrupt.
1892 u32 GlobalSystemInterrupt
;
1894 /* The actual register address. */
1895 struct acpi_generic_address addr
;
1899 s8 spmi_id
[1]; /* A '\0' terminated array starts here. */
1902 static int try_init_spmi(struct SPMITable
*spmi
)
1904 unsigned short myaddr
;
1906 if (num_addrs
>= MAX_SSIF_BMCS
)
1909 if (spmi
->IPMIlegacy
!= 1) {
1910 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi
->IPMIlegacy
);
1914 if (spmi
->InterfaceType
!= 4)
1917 if (spmi
->addr
.space_id
!= ACPI_ADR_SPACE_SMBUS
) {
1918 pr_warn(PFX
"Invalid ACPI SSIF I/O Address type: %d\n",
1919 spmi
->addr
.space_id
);
1923 myaddr
= spmi
->addr
.address
& 0x7f;
1925 return new_ssif_client(myaddr
, NULL
, 0, 0, SI_SPMI
, NULL
);
1928 static void spmi_find_bmc(void)
1931 struct SPMITable
*spmi
;
1940 for (i
= 0; ; i
++) {
1941 status
= acpi_get_table(ACPI_SIG_SPMI
, i
+1,
1942 (struct acpi_table_header
**)&spmi
);
1943 if (status
!= AE_OK
)
1946 try_init_spmi(spmi
);
1950 static void spmi_find_bmc(void) { }
1954 static int dmi_ipmi_probe(struct platform_device
*pdev
)
1956 u8 type
, slave_addr
= 0;
1963 rv
= device_property_read_u8(&pdev
->dev
, "ipmi-type", &type
);
1967 if (type
!= IPMI_DMI_TYPE_SSIF
)
1970 rv
= device_property_read_u16(&pdev
->dev
, "i2c-addr", &i2c_addr
);
1972 dev_warn(&pdev
->dev
, PFX
"No i2c-addr property\n");
1976 rv
= device_property_read_u8(&pdev
->dev
, "slave-addr", &slave_addr
);
1978 dev_warn(&pdev
->dev
, "device has no slave-addr property");
1980 return new_ssif_client(i2c_addr
, NULL
, 0,
1981 slave_addr
, SI_SMBIOS
, &pdev
->dev
);
1984 static int dmi_ipmi_probe(struct platform_device
*pdev
)
1990 static const struct i2c_device_id ssif_id
[] = {
1994 MODULE_DEVICE_TABLE(i2c
, ssif_id
);
1996 static struct i2c_driver ssif_i2c_driver
= {
1997 .class = I2C_CLASS_HWMON
,
2001 .probe
= ssif_probe
,
2002 .remove
= ssif_remove
,
2003 .alert
= ssif_alert
,
2004 .id_table
= ssif_id
,
2005 .detect
= ssif_detect
2008 static int ssif_platform_probe(struct platform_device
*dev
)
2010 return dmi_ipmi_probe(dev
);
2013 static int ssif_platform_remove(struct platform_device
*dev
)
2015 struct ssif_addr_info
*addr_info
= dev_get_drvdata(&dev
->dev
);
2020 mutex_lock(&ssif_infos_mutex
);
2021 if (addr_info
->client
)
2022 i2c_unregister_device(addr_info
->client
);
2024 list_del(&addr_info
->link
);
2026 mutex_unlock(&ssif_infos_mutex
);
2030 static struct platform_driver ipmi_driver
= {
2032 .name
= DEVICE_NAME
,
2034 .probe
= ssif_platform_probe
,
2035 .remove
= ssif_platform_remove
,
2038 static int init_ipmi_ssif(void)
2046 pr_info("IPMI SSIF Interface driver\n");
2048 /* build list for i2c from addr list */
2049 for (i
= 0; i
< num_addrs
; i
++) {
2050 rv
= new_ssif_client(addr
[i
], adapter_name
[i
],
2051 dbg
[i
], slave_addrs
[i
],
2052 SI_HARDCODED
, NULL
);
2055 "Couldn't add hardcoded device at addr 0x%x\n",
2060 ssif_i2c_driver
.driver
.acpi_match_table
=
2061 ACPI_PTR(ssif_acpi_match
);
2067 rv
= platform_driver_register(&ipmi_driver
);
2069 pr_err(PFX
"Unable to register driver: %d\n", rv
);
2072 ssif_i2c_driver
.address_list
= ssif_address_list();
2074 rv
= i2c_add_driver(&ssif_i2c_driver
);
2080 module_init(init_ipmi_ssif
);
2082 static void cleanup_ipmi_ssif(void)
2087 initialized
= false;
2089 i2c_del_driver(&ssif_i2c_driver
);
2091 platform_driver_unregister(&ipmi_driver
);
2093 free_ssif_clients();
2095 module_exit(cleanup_ipmi_ssif
);
2097 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2098 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2099 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2100 MODULE_LICENSE("GPL");