1 // SPDX-License-Identifier: GPL-2.0+
5 * The interface to the IPMI driver for SMBus access to a SMBus
6 * compliant device. Called SSIF by the IPMI spec.
8 * Author: Intel Corporation
9 * Todd Davis <todd.c.davis@intel.com>
11 * Rewritten by Corey Minyard <minyard@acm.org> to support the
12 * non-blocking I2C interface, add support for multi-part
13 * transactions, add PEC support, and general clenaup.
15 * Copyright 2003 Intel Corporation
16 * Copyright 2005 MontaVista Software
20 * This file holds the "policy" for the interface to the SSIF state
21 * machine. It does the configuration, handles timers and interrupts,
22 * and drives the real SSIF state machine.
26 * TODO: Figure out how to use SMB alerts. This will require a new
27 * interface into the I2C driver, I believe.
30 #define pr_fmt(fmt) "ipmi_ssif: " fmt
32 #if defined(MODVERSIONS)
33 #include <linux/modversions.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/sched.h>
39 #include <linux/seq_file.h>
40 #include <linux/timer.h>
41 #include <linux/delay.h>
42 #include <linux/errno.h>
43 #include <linux/spinlock.h>
44 #include <linux/slab.h>
45 #include <linux/list.h>
46 #include <linux/i2c.h>
47 #include <linux/ipmi_smi.h>
48 #include <linux/init.h>
49 #include <linux/dmi.h>
50 #include <linux/kthread.h>
51 #include <linux/acpi.h>
52 #include <linux/ctype.h>
53 #include <linux/time64.h>
54 #include "ipmi_si_sm.h"
57 #define DEVICE_NAME "ipmi_ssif"
59 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
61 #define SSIF_IPMI_REQUEST 2
62 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
63 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
64 #define SSIF_IPMI_MULTI_PART_REQUEST_END 8
65 #define SSIF_IPMI_RESPONSE 3
66 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
68 /* ssif_debug is a bit-field
69 * SSIF_DEBUG_MSG - commands and their responses
70 * SSIF_DEBUG_STATES - message states
71 * SSIF_DEBUG_TIMING - Measure times between events in the driver
73 #define SSIF_DEBUG_TIMING 4
74 #define SSIF_DEBUG_STATE 2
75 #define SSIF_DEBUG_MSG 1
76 #define SSIF_NODEBUG 0
77 #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
82 #define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
83 #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
85 /* How many times to we retry sending/receiving the message. */
86 #define SSIF_SEND_RETRIES 5
87 #define SSIF_RECV_RETRIES 250
89 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
90 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
91 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
93 enum ssif_intf_state {
98 SSIF_GETTING_MESSAGES,
99 /* FIXME - add watchdog stuff. */
102 #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
103 && (ssif)->curr_msg == NULL)
106 * Indexes into stats[] in ssif_info below.
108 enum ssif_stat_indexes {
109 /* Number of total messages sent. */
110 SSIF_STAT_sent_messages = 0,
113 * Number of message parts sent. Messages may be broken into
114 * parts if they are long.
116 SSIF_STAT_sent_messages_parts,
119 * Number of time a message was retried.
121 SSIF_STAT_send_retries,
124 * Number of times the send of a message failed.
126 SSIF_STAT_send_errors,
129 * Number of message responses received.
131 SSIF_STAT_received_messages,
134 * Number of message fragments received.
136 SSIF_STAT_received_message_parts,
139 * Number of times the receive of a message was retried.
141 SSIF_STAT_receive_retries,
144 * Number of errors receiving messages.
146 SSIF_STAT_receive_errors,
149 * Number of times a flag fetch was requested.
151 SSIF_STAT_flag_fetches,
154 * Number of times the hardware didn't follow the state machine.
159 * Number of received events.
163 /* Number of asyncronous messages received. */
164 SSIF_STAT_incoming_messages,
166 /* Number of watchdog pretimeouts. */
167 SSIF_STAT_watchdog_pretimeouts,
169 /* Number of alers received. */
172 /* Always add statistics before this value, it must be last. */
176 struct ssif_addr_info {
177 struct i2c_board_info binfo;
181 enum ipmi_addr_src addr_src;
182 union ipmi_smi_info_union addr_info;
184 struct i2c_client *client;
186 struct i2c_client *added_client;
188 struct mutex clients_mutex;
189 struct list_head clients;
191 struct list_head link;
196 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
197 unsigned char *data, unsigned int len);
200 struct ipmi_smi *intf;
202 struct ipmi_smi_msg *waiting_msg;
203 struct ipmi_smi_msg *curr_msg;
204 enum ssif_intf_state ssif_state;
205 unsigned long ssif_debug;
207 struct ipmi_smi_handlers handlers;
209 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
210 union ipmi_smi_info_union addr_info;
213 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
214 * is set to hold the flags until we are done handling everything
217 #define RECEIVE_MSG_AVAIL 0x01
218 #define EVENT_MSG_BUFFER_FULL 0x02
219 #define WDT_PRE_TIMEOUT_INT 0x08
220 unsigned char msg_flags;
223 bool has_event_buffer;
227 * Used to tell what we should do with alerts. If we are
228 * waiting on a response, read the data immediately.
234 * If set to true, this will request events the next time the
235 * state machine is idle.
240 * If set to true, this will request flags the next time the
241 * state machine is idle.
246 * Used to perform timer operations when run-to-completion
247 * mode is on. This is a countdown timer.
251 /* Used for sending/receiving data. +1 for the length. */
252 unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
253 unsigned int data_len;
255 /* Temp receive buffer, gets copied into data. */
256 unsigned char recv[I2C_SMBUS_BLOCK_MAX];
258 struct i2c_client *client;
259 ssif_i2c_done done_handler;
261 /* Thread interface handling */
262 struct task_struct *thread;
263 struct completion wake_thread;
267 unsigned char *i2c_data;
268 unsigned int i2c_size;
270 struct timer_list retry_timer;
273 /* Info from SSIF cmd */
274 unsigned char max_xmit_msg_size;
275 unsigned char max_recv_msg_size;
276 bool cmd8_works; /* See test_multipart_messages() for details. */
277 unsigned int multi_support;
280 #define SSIF_NO_MULTI 0
281 #define SSIF_MULTI_2_PART 1
282 #define SSIF_MULTI_n_PART 2
283 unsigned char *multi_data;
284 unsigned int multi_len;
285 unsigned int multi_pos;
287 atomic_t stats[SSIF_NUM_STATS];
290 #define ssif_inc_stat(ssif, stat) \
291 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
292 #define ssif_get_stat(ssif, stat) \
293 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
295 static bool initialized;
297 static void return_hosed_msg(struct ssif_info *ssif_info,
298 struct ipmi_smi_msg *msg);
299 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
300 static int start_send(struct ssif_info *ssif_info,
304 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
305 unsigned long *flags)
307 spin_lock_irqsave(&ssif_info->lock, *flags);
311 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
312 unsigned long *flags)
314 spin_unlock_irqrestore(&ssif_info->lock, *flags);
317 static void deliver_recv_msg(struct ssif_info *ssif_info,
318 struct ipmi_smi_msg *msg)
320 if (msg->rsp_size < 0) {
321 return_hosed_msg(ssif_info, msg);
322 pr_err("%s: Malformed message: rsp_size = %d\n",
323 __func__, msg->rsp_size);
325 ipmi_smi_msg_received(ssif_info->intf, msg);
329 static void return_hosed_msg(struct ssif_info *ssif_info,
330 struct ipmi_smi_msg *msg)
332 ssif_inc_stat(ssif_info, hosed);
334 /* Make it a response */
335 msg->rsp[0] = msg->data[0] | 4;
336 msg->rsp[1] = msg->data[1];
337 msg->rsp[2] = 0xFF; /* Unknown error. */
340 deliver_recv_msg(ssif_info, msg);
344 * Must be called with the message lock held. This will release the
345 * message lock. Note that the caller will check SSIF_IDLE and start a
346 * new operation, so there is no need to check for new messages to
349 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
351 unsigned char msg[3];
353 ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
354 ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
355 ipmi_ssif_unlock_cond(ssif_info, flags);
357 /* Make sure the watchdog pre-timeout flag is not set at startup. */
358 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
359 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
360 msg[2] = WDT_PRE_TIMEOUT_INT;
362 if (start_send(ssif_info, msg, 3) != 0) {
363 /* Error, just go to normal state. */
364 ssif_info->ssif_state = SSIF_NORMAL;
368 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
372 ssif_info->req_flags = false;
373 ssif_info->ssif_state = SSIF_GETTING_FLAGS;
374 ipmi_ssif_unlock_cond(ssif_info, flags);
376 mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
377 mb[1] = IPMI_GET_MSG_FLAGS_CMD;
378 if (start_send(ssif_info, mb, 2) != 0)
379 ssif_info->ssif_state = SSIF_NORMAL;
382 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
383 struct ipmi_smi_msg *msg)
385 if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
386 unsigned long oflags;
388 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
389 ssif_info->curr_msg = NULL;
390 ssif_info->ssif_state = SSIF_NORMAL;
391 ipmi_ssif_unlock_cond(ssif_info, flags);
392 ipmi_free_smi_msg(msg);
396 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
398 struct ipmi_smi_msg *msg;
400 ssif_info->req_events = false;
402 msg = ipmi_alloc_smi_msg();
404 ssif_info->ssif_state = SSIF_NORMAL;
405 ipmi_ssif_unlock_cond(ssif_info, flags);
409 ssif_info->curr_msg = msg;
410 ssif_info->ssif_state = SSIF_GETTING_EVENTS;
411 ipmi_ssif_unlock_cond(ssif_info, flags);
413 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
414 msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
417 check_start_send(ssif_info, flags, msg);
420 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
421 unsigned long *flags)
423 struct ipmi_smi_msg *msg;
425 msg = ipmi_alloc_smi_msg();
427 ssif_info->ssif_state = SSIF_NORMAL;
428 ipmi_ssif_unlock_cond(ssif_info, flags);
432 ssif_info->curr_msg = msg;
433 ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
434 ipmi_ssif_unlock_cond(ssif_info, flags);
436 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
437 msg->data[1] = IPMI_GET_MSG_CMD;
440 check_start_send(ssif_info, flags, msg);
444 * Must be called with the message lock held. This will release the
445 * message lock. Note that the caller will check SSIF_IDLE and start a
446 * new operation, so there is no need to check for new messages to
449 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
451 if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
452 /* Watchdog pre-timeout */
453 ssif_inc_stat(ssif_info, watchdog_pretimeouts);
454 start_clear_flags(ssif_info, flags);
455 ipmi_smi_watchdog_pretimeout(ssif_info->intf);
456 } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
457 /* Messages available. */
458 start_recv_msg_fetch(ssif_info, flags);
459 else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
460 /* Events available. */
461 start_event_fetch(ssif_info, flags);
463 ssif_info->ssif_state = SSIF_NORMAL;
464 ipmi_ssif_unlock_cond(ssif_info, flags);
468 static int ipmi_ssif_thread(void *data)
470 struct ssif_info *ssif_info = data;
472 while (!kthread_should_stop()) {
475 /* Wait for something to do */
476 result = wait_for_completion_interruptible(
477 &ssif_info->wake_thread);
478 if (ssif_info->stopping)
480 if (result == -ERESTARTSYS)
482 init_completion(&ssif_info->wake_thread);
484 if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
485 result = i2c_smbus_write_block_data(
486 ssif_info->client, ssif_info->i2c_command,
487 ssif_info->i2c_data[0],
488 ssif_info->i2c_data + 1);
489 ssif_info->done_handler(ssif_info, result, NULL, 0);
491 result = i2c_smbus_read_block_data(
492 ssif_info->client, ssif_info->i2c_command,
493 ssif_info->i2c_data);
495 ssif_info->done_handler(ssif_info, result,
498 ssif_info->done_handler(ssif_info, 0,
507 static int ssif_i2c_send(struct ssif_info *ssif_info,
508 ssif_i2c_done handler,
509 int read_write, int command,
510 unsigned char *data, unsigned int size)
512 ssif_info->done_handler = handler;
514 ssif_info->i2c_read_write = read_write;
515 ssif_info->i2c_command = command;
516 ssif_info->i2c_data = data;
517 ssif_info->i2c_size = size;
518 complete(&ssif_info->wake_thread);
523 static void msg_done_handler(struct ssif_info *ssif_info, int result,
524 unsigned char *data, unsigned int len);
526 static void start_get(struct ssif_info *ssif_info)
530 ssif_info->rtc_us_timer = 0;
531 ssif_info->multi_pos = 0;
533 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
535 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
537 /* request failed, just return the error. */
538 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
539 pr_info("Error from i2c_non_blocking_op(5)\n");
541 msg_done_handler(ssif_info, -EIO, NULL, 0);
545 static void retry_timeout(struct timer_list *t)
547 struct ssif_info *ssif_info = from_timer(ssif_info, t, retry_timer);
548 unsigned long oflags, *flags;
551 if (ssif_info->stopping)
554 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
555 waiting = ssif_info->waiting_alert;
556 ssif_info->waiting_alert = false;
557 ipmi_ssif_unlock_cond(ssif_info, flags);
560 start_get(ssif_info);
564 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
567 struct ssif_info *ssif_info = i2c_get_clientdata(client);
568 unsigned long oflags, *flags;
571 if (type != I2C_PROTOCOL_SMBUS_ALERT)
574 ssif_inc_stat(ssif_info, alerts);
576 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
577 if (ssif_info->waiting_alert) {
578 ssif_info->waiting_alert = false;
579 del_timer(&ssif_info->retry_timer);
581 } else if (ssif_info->curr_msg) {
582 ssif_info->got_alert = true;
584 ipmi_ssif_unlock_cond(ssif_info, flags);
586 start_get(ssif_info);
589 static int start_resend(struct ssif_info *ssif_info);
591 static void msg_done_handler(struct ssif_info *ssif_info, int result,
592 unsigned char *data, unsigned int len)
594 struct ipmi_smi_msg *msg;
595 unsigned long oflags, *flags;
599 * We are single-threaded here, so no need for a lock until we
600 * start messing with driver states or the queues.
604 ssif_info->retries_left--;
605 if (ssif_info->retries_left > 0) {
606 ssif_inc_stat(ssif_info, receive_retries);
608 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
609 ssif_info->waiting_alert = true;
610 ssif_info->rtc_us_timer = SSIF_MSG_USEC;
611 if (!ssif_info->stopping)
612 mod_timer(&ssif_info->retry_timer,
613 jiffies + SSIF_MSG_JIFFIES);
614 ipmi_ssif_unlock_cond(ssif_info, flags);
618 ssif_inc_stat(ssif_info, receive_errors);
620 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
621 pr_info("Error in msg_done_handler: %d\n", result);
626 if ((len > 1) && (ssif_info->multi_pos == 0)
627 && (data[0] == 0x00) && (data[1] == 0x01)) {
628 /* Start of multi-part read. Start the next transaction. */
631 ssif_inc_stat(ssif_info, received_message_parts);
633 /* Remove the multi-part read marker. */
636 for (i = 0; i < len; i++)
637 ssif_info->data[i] = data[i];
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("Middle message with no data\n");
668 if (blocknum != 0xff && len != 31) {
669 /* All blocks but the last must have 31 data bytes. */
671 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
672 pr_info("Received middle message <31\n");
677 if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
678 /* Received message too big, abort the operation. */
680 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
681 pr_info("Received message too big\n");
686 for (i = 0; i < len; i++)
687 ssif_info->data[i + ssif_info->multi_len] = data[i];
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 != 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)
712 pr_info("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);
728 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
729 pr_info("DONE 1: state = %d, result=%d\n",
730 ssif_info->ssif_state, result);
732 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
733 msg = ssif_info->curr_msg;
736 if (msg->rsp_size > IPMI_MAX_MSG_LENGTH)
737 msg->rsp_size = IPMI_MAX_MSG_LENGTH;
738 memcpy(msg->rsp, data, msg->rsp_size);
739 ssif_info->curr_msg = NULL;
742 switch (ssif_info->ssif_state) {
744 ipmi_ssif_unlock_cond(ssif_info, flags);
749 return_hosed_msg(ssif_info, msg);
751 deliver_recv_msg(ssif_info, msg);
754 case SSIF_GETTING_FLAGS:
755 /* We got the flags from the SSIF, now handle them. */
756 if ((result < 0) || (len < 4) || (data[2] != 0)) {
758 * Error fetching flags, or invalid length,
759 * just give up for now.
761 ssif_info->ssif_state = SSIF_NORMAL;
762 ipmi_ssif_unlock_cond(ssif_info, flags);
763 pr_warn("Error getting flags: %d %d, %x\n",
764 result, len, (len >= 3) ? data[2] : 0);
765 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
766 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
768 * Don't abort here, maybe it was a queued
769 * response to a previous command.
771 ipmi_ssif_unlock_cond(ssif_info, flags);
772 pr_warn("Invalid response getting flags: %x %x\n",
775 ssif_inc_stat(ssif_info, flag_fetches);
776 ssif_info->msg_flags = data[3];
777 handle_flags(ssif_info, flags);
781 case SSIF_CLEARING_FLAGS:
782 /* We cleared the flags. */
783 if ((result < 0) || (len < 3) || (data[2] != 0)) {
784 /* Error clearing flags */
785 pr_warn("Error clearing flags: %d %d, %x\n",
786 result, len, (len >= 3) ? data[2] : 0);
787 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
788 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
789 pr_warn("Invalid response clearing flags: %x %x\n",
792 ssif_info->ssif_state = SSIF_NORMAL;
793 ipmi_ssif_unlock_cond(ssif_info, flags);
796 case SSIF_GETTING_EVENTS:
797 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
798 /* Error getting event, probably done. */
801 /* Take off the event flag. */
802 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
803 handle_flags(ssif_info, flags);
804 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
805 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
806 pr_warn("Invalid response getting events: %x %x\n",
807 msg->rsp[0], msg->rsp[1]);
809 /* Take off the event flag. */
810 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
811 handle_flags(ssif_info, flags);
813 handle_flags(ssif_info, flags);
814 ssif_inc_stat(ssif_info, events);
815 deliver_recv_msg(ssif_info, msg);
819 case SSIF_GETTING_MESSAGES:
820 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
821 /* Error getting event, probably done. */
824 /* Take off the msg flag. */
825 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
826 handle_flags(ssif_info, flags);
827 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
828 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
829 pr_warn("Invalid response clearing flags: %x %x\n",
830 msg->rsp[0], msg->rsp[1]);
833 /* Take off the msg flag. */
834 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
835 handle_flags(ssif_info, flags);
837 ssif_inc_stat(ssif_info, incoming_messages);
838 handle_flags(ssif_info, flags);
839 deliver_recv_msg(ssif_info, msg);
844 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
845 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
846 if (ssif_info->req_events)
847 start_event_fetch(ssif_info, flags);
848 else if (ssif_info->req_flags)
849 start_flag_fetch(ssif_info, flags);
851 start_next_msg(ssif_info, flags);
853 ipmi_ssif_unlock_cond(ssif_info, flags);
855 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
856 pr_info("DONE 2: state = %d.\n", ssif_info->ssif_state);
859 static void msg_written_handler(struct ssif_info *ssif_info, int result,
860 unsigned char *data, unsigned int len)
864 /* We are single-threaded here, so no need for a lock. */
866 ssif_info->retries_left--;
867 if (ssif_info->retries_left > 0) {
868 if (!start_resend(ssif_info)) {
869 ssif_inc_stat(ssif_info, send_retries);
872 /* request failed, just return the error. */
873 ssif_inc_stat(ssif_info, send_errors);
875 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
876 pr_info("%s: Out of retries\n", __func__);
877 msg_done_handler(ssif_info, -EIO, NULL, 0);
881 ssif_inc_stat(ssif_info, send_errors);
884 * Got an error on transmit, let the done routine
887 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
888 pr_info("Error in msg_written_handler: %d\n", result);
890 msg_done_handler(ssif_info, result, NULL, 0);
894 if (ssif_info->multi_data) {
896 * In the middle of a multi-data write. See the comment
897 * in the SSIF_MULTI_n_PART case in the probe function
898 * for details on the intricacies of this.
901 unsigned char *data_to_send;
904 ssif_inc_stat(ssif_info, sent_messages_parts);
906 left = ssif_info->multi_len - ssif_info->multi_pos;
911 ssif_info->multi_data[ssif_info->multi_pos] = to_write;
912 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
913 ssif_info->multi_pos += to_write;
914 cmd = SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE;
915 if (ssif_info->cmd8_works) {
916 if (left == to_write) {
917 cmd = SSIF_IPMI_MULTI_PART_REQUEST_END;
918 ssif_info->multi_data = NULL;
920 } else if (to_write < 32) {
921 ssif_info->multi_data = NULL;
924 rv = ssif_i2c_send(ssif_info, msg_written_handler,
925 I2C_SMBUS_WRITE, cmd,
926 data_to_send, I2C_SMBUS_BLOCK_DATA);
928 /* request failed, just return the error. */
929 ssif_inc_stat(ssif_info, send_errors);
931 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
932 pr_info("Error from i2c_non_blocking_op(3)\n");
933 msg_done_handler(ssif_info, -EIO, NULL, 0);
936 /* Ready to request the result. */
937 unsigned long oflags, *flags;
939 ssif_inc_stat(ssif_info, sent_messages);
940 ssif_inc_stat(ssif_info, sent_messages_parts);
942 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
943 if (ssif_info->got_alert) {
944 /* The result is already ready, just start it. */
945 ssif_info->got_alert = false;
946 ipmi_ssif_unlock_cond(ssif_info, flags);
947 start_get(ssif_info);
949 /* Wait a jiffie then request the next message */
950 ssif_info->waiting_alert = true;
951 ssif_info->retries_left = SSIF_RECV_RETRIES;
952 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
953 if (!ssif_info->stopping)
954 mod_timer(&ssif_info->retry_timer,
955 jiffies + SSIF_MSG_PART_JIFFIES);
956 ipmi_ssif_unlock_cond(ssif_info, flags);
961 static int start_resend(struct ssif_info *ssif_info)
966 ssif_info->got_alert = false;
968 if (ssif_info->data_len > 32) {
969 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
970 ssif_info->multi_data = ssif_info->data;
971 ssif_info->multi_len = ssif_info->data_len;
973 * Subtle thing, this is 32, not 33, because we will
974 * overwrite the thing at position 32 (which was just
975 * transmitted) with the new length.
977 ssif_info->multi_pos = 32;
978 ssif_info->data[0] = 32;
980 ssif_info->multi_data = NULL;
981 command = SSIF_IPMI_REQUEST;
982 ssif_info->data[0] = ssif_info->data_len;
985 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
986 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
987 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
988 pr_info("Error from i2c_non_blocking_op(4)\n");
992 static int start_send(struct ssif_info *ssif_info,
996 if (len > IPMI_MAX_MSG_LENGTH)
998 if (len > ssif_info->max_xmit_msg_size)
1001 ssif_info->retries_left = SSIF_SEND_RETRIES;
1002 memcpy(ssif_info->data + 1, data, len);
1003 ssif_info->data_len = len;
1004 return start_resend(ssif_info);
1007 /* Must be called with the message lock held. */
1008 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1010 struct ipmi_smi_msg *msg;
1011 unsigned long oflags;
1014 if (!SSIF_IDLE(ssif_info)) {
1015 ipmi_ssif_unlock_cond(ssif_info, flags);
1019 if (!ssif_info->waiting_msg) {
1020 ssif_info->curr_msg = NULL;
1021 ipmi_ssif_unlock_cond(ssif_info, flags);
1025 ssif_info->curr_msg = ssif_info->waiting_msg;
1026 ssif_info->waiting_msg = NULL;
1027 ipmi_ssif_unlock_cond(ssif_info, flags);
1028 rv = start_send(ssif_info,
1029 ssif_info->curr_msg->data,
1030 ssif_info->curr_msg->data_size);
1032 msg = ssif_info->curr_msg;
1033 ssif_info->curr_msg = NULL;
1034 return_hosed_msg(ssif_info, msg);
1035 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1041 static void sender(void *send_info,
1042 struct ipmi_smi_msg *msg)
1044 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1045 unsigned long oflags, *flags;
1047 BUG_ON(ssif_info->waiting_msg);
1048 ssif_info->waiting_msg = msg;
1050 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1051 start_next_msg(ssif_info, flags);
1053 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1054 struct timespec64 t;
1056 ktime_get_real_ts64(&t);
1057 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1058 msg->data[0], msg->data[1],
1059 (long long)t.tv_sec, (long)t.tv_nsec / NSEC_PER_USEC);
1063 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1065 struct ssif_info *ssif_info = send_info;
1067 data->addr_src = ssif_info->addr_source;
1068 data->dev = &ssif_info->client->dev;
1069 data->addr_info = ssif_info->addr_info;
1070 get_device(data->dev);
1076 * Instead of having our own timer to periodically check the message
1077 * flags, we let the message handler drive us.
1079 static void request_events(void *send_info)
1081 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1082 unsigned long oflags, *flags;
1084 if (!ssif_info->has_event_buffer)
1087 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1089 * Request flags first, not events, because the lower layer
1090 * doesn't have a way to send an attention. But make sure
1091 * event checking still happens.
1093 ssif_info->req_events = true;
1094 if (SSIF_IDLE(ssif_info))
1095 start_flag_fetch(ssif_info, flags);
1097 ssif_info->req_flags = true;
1098 ipmi_ssif_unlock_cond(ssif_info, flags);
1102 static int ssif_start_processing(void *send_info,
1103 struct ipmi_smi *intf)
1105 struct ssif_info *ssif_info = send_info;
1107 ssif_info->intf = intf;
1112 #define MAX_SSIF_BMCS 4
1114 static unsigned short addr[MAX_SSIF_BMCS];
1115 static int num_addrs;
1116 module_param_array(addr, ushort, &num_addrs, 0);
1117 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1119 static char *adapter_name[MAX_SSIF_BMCS];
1120 static int num_adapter_names;
1121 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1122 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1124 static int slave_addrs[MAX_SSIF_BMCS];
1125 static int num_slave_addrs;
1126 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1127 MODULE_PARM_DESC(slave_addrs,
1128 "The default IPMB slave address for the controller.");
1130 static bool alerts_broken;
1131 module_param(alerts_broken, bool, 0);
1132 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1135 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1136 * bit 2 enables timing debugging. This is an array indexed by
1139 static int dbg[MAX_SSIF_BMCS];
1141 module_param_array(dbg, int, &num_dbg, 0);
1142 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1144 static bool ssif_dbg_probe;
1145 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1146 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1148 static bool ssif_tryacpi = true;
1149 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1150 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1152 static bool ssif_trydmi = true;
1153 module_param_named(trydmi, ssif_trydmi, bool, 0);
1154 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1156 static DEFINE_MUTEX(ssif_infos_mutex);
1157 static LIST_HEAD(ssif_infos);
1159 #define IPMI_SSIF_ATTR(name) \
1160 static ssize_t ipmi_##name##_show(struct device *dev, \
1161 struct device_attribute *attr, \
1164 struct ssif_info *ssif_info = dev_get_drvdata(dev); \
1166 return snprintf(buf, 10, "%u\n", ssif_get_stat(ssif_info, name));\
1168 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1170 static ssize_t ipmi_type_show(struct device *dev,
1171 struct device_attribute *attr,
1174 return snprintf(buf, 10, "ssif\n");
1176 static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1178 IPMI_SSIF_ATTR(sent_messages);
1179 IPMI_SSIF_ATTR(sent_messages_parts);
1180 IPMI_SSIF_ATTR(send_retries);
1181 IPMI_SSIF_ATTR(send_errors);
1182 IPMI_SSIF_ATTR(received_messages);
1183 IPMI_SSIF_ATTR(received_message_parts);
1184 IPMI_SSIF_ATTR(receive_retries);
1185 IPMI_SSIF_ATTR(receive_errors);
1186 IPMI_SSIF_ATTR(flag_fetches);
1187 IPMI_SSIF_ATTR(hosed);
1188 IPMI_SSIF_ATTR(events);
1189 IPMI_SSIF_ATTR(watchdog_pretimeouts);
1190 IPMI_SSIF_ATTR(alerts);
1192 static struct attribute *ipmi_ssif_dev_attrs[] = {
1193 &dev_attr_type.attr,
1194 &dev_attr_sent_messages.attr,
1195 &dev_attr_sent_messages_parts.attr,
1196 &dev_attr_send_retries.attr,
1197 &dev_attr_send_errors.attr,
1198 &dev_attr_received_messages.attr,
1199 &dev_attr_received_message_parts.attr,
1200 &dev_attr_receive_retries.attr,
1201 &dev_attr_receive_errors.attr,
1202 &dev_attr_flag_fetches.attr,
1203 &dev_attr_hosed.attr,
1204 &dev_attr_events.attr,
1205 &dev_attr_watchdog_pretimeouts.attr,
1206 &dev_attr_alerts.attr,
1210 static const struct attribute_group ipmi_ssif_dev_attr_group = {
1211 .attrs = ipmi_ssif_dev_attrs,
1214 static void shutdown_ssif(void *send_info)
1216 struct ssif_info *ssif_info = send_info;
1218 device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1219 dev_set_drvdata(&ssif_info->client->dev, NULL);
1221 /* make sure the driver is not looking for flags any more. */
1222 while (ssif_info->ssif_state != SSIF_NORMAL)
1223 schedule_timeout(1);
1225 ssif_info->stopping = true;
1226 del_timer_sync(&ssif_info->retry_timer);
1227 if (ssif_info->thread) {
1228 complete(&ssif_info->wake_thread);
1229 kthread_stop(ssif_info->thread);
1233 static int ssif_remove(struct i2c_client *client)
1235 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1236 struct ssif_addr_info *addr_info;
1242 * After this point, we won't deliver anything asychronously
1243 * to the message handler. We can unregister ourself.
1245 ipmi_unregister_smi(ssif_info->intf);
1247 list_for_each_entry(addr_info, &ssif_infos, link) {
1248 if (addr_info->client == client) {
1249 addr_info->client = NULL;
1259 static int read_response(struct i2c_client *client, unsigned char *resp)
1261 int ret = -ENODEV, retry_cnt = SSIF_RECV_RETRIES;
1263 while (retry_cnt > 0) {
1264 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1268 msleep(SSIF_MSG_MSEC);
1277 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1278 int *resp_len, unsigned char *resp)
1283 retry_cnt = SSIF_SEND_RETRIES;
1285 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1293 ret = read_response(client, resp);
1295 /* Validate that the response is correct. */
1297 (resp[0] != (msg[0] | (1 << 2))) ||
1298 (resp[1] != msg[1]))
1300 else if (ret > IPMI_MAX_MSG_LENGTH) {
1311 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1313 unsigned char *resp;
1314 unsigned char msg[3];
1318 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1322 /* Do a Get Device ID command, since it is required. */
1323 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1324 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1325 rv = do_cmd(client, 2, msg, &len, resp);
1329 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1334 static int strcmp_nospace(char *s1, char *s2)
1336 while (*s1 && *s2) {
1337 while (isspace(*s1))
1339 while (isspace(*s2))
1351 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1353 bool match_null_name)
1355 struct ssif_addr_info *info, *found = NULL;
1358 list_for_each_entry(info, &ssif_infos, link) {
1359 if (info->binfo.addr == addr) {
1360 if (info->adapter_name || adapter_name) {
1361 if (!info->adapter_name != !adapter_name) {
1362 /* One is NULL and one is not */
1366 strcmp_nospace(info->adapter_name,
1368 /* Names do not match */
1376 if (!found && match_null_name) {
1377 /* Try to get an exact match first, then try with a NULL name */
1378 adapter_name = NULL;
1379 match_null_name = false;
1386 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1389 acpi_handle acpi_handle;
1391 acpi_handle = ACPI_HANDLE(dev);
1393 ssif_info->addr_source = SI_ACPI;
1394 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1401 static int find_slave_address(struct i2c_client *client, int slave_addr)
1403 #ifdef CONFIG_IPMI_DMI_DECODE
1405 slave_addr = ipmi_dmi_get_slave_addr(
1407 i2c_adapter_id(client->adapter),
1414 static int start_multipart_test(struct i2c_client *client,
1415 unsigned char *msg, bool do_middle)
1417 int retry_cnt = SSIF_SEND_RETRIES, ret;
1420 ret = i2c_smbus_write_block_data(client,
1421 SSIF_IPMI_MULTI_PART_REQUEST_START,
1427 dev_err(&client->dev, "Could not write multi-part start, though the BMC said it could handle it. Just limit sends to one part.\n");
1434 ret = i2c_smbus_write_block_data(client,
1435 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1438 dev_err(&client->dev, "Could not write multi-part middle, though the BMC said it could handle it. Just limit sends to one part.\n");
1445 static void test_multipart_messages(struct i2c_client *client,
1446 struct ssif_info *ssif_info,
1447 unsigned char *resp)
1449 unsigned char msg[65];
1453 if (ssif_info->max_xmit_msg_size <= 32)
1456 do_middle = ssif_info->max_xmit_msg_size > 63;
1458 memset(msg, 0, sizeof(msg));
1459 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1460 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1463 * The specification is all messed up dealing with sending
1464 * multi-part messages. Per what the specification says, it
1465 * is impossible to send a message that is a multiple of 32
1466 * bytes, except for 32 itself. It talks about a "start"
1467 * transaction (cmd=6) that must be 32 bytes, "middle"
1468 * transaction (cmd=7) that must be 32 bytes, and an "end"
1469 * transaction. The "end" transaction is shown as cmd=7 in
1470 * the text, but if that's the case there is no way to
1471 * differentiate between a middle and end part except the
1472 * length being less than 32. But there is a table at the far
1473 * end of the section (that I had never noticed until someone
1474 * pointed it out to me) that mentions it as cmd=8.
1476 * After some thought, I think the example is wrong and the
1477 * end transaction should be cmd=8. But some systems don't
1478 * implement cmd=8, they use a zero-length end transaction,
1479 * even though that violates the SMBus specification.
1481 * So, to work around this, this code tests if cmd=8 works.
1482 * If it does, then we use that. If not, it tests zero-
1483 * byte end transactions. If that works, good. If not,
1484 * we only allow 63-byte transactions max.
1487 ret = start_multipart_test(client, msg, do_middle);
1489 goto out_no_multi_part;
1491 ret = i2c_smbus_write_block_data(client,
1492 SSIF_IPMI_MULTI_PART_REQUEST_END,
1496 ret = read_response(client, resp);
1499 /* End transactions work, we are good. */
1500 ssif_info->cmd8_works = true;
1504 ret = start_multipart_test(client, msg, do_middle);
1506 dev_err(&client->dev, "Second multipart test failed.\n");
1507 goto out_no_multi_part;
1510 ret = i2c_smbus_write_block_data(client,
1511 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1514 ret = read_response(client, resp);
1516 /* Zero-size end parts work, use those. */
1519 /* Limit to 63 bytes and use a short middle command to mark the end. */
1520 if (ssif_info->max_xmit_msg_size > 63)
1521 ssif_info->max_xmit_msg_size = 63;
1525 ssif_info->max_xmit_msg_size = 32;
1530 * Global enables we care about.
1532 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1533 IPMI_BMC_EVT_MSG_INTR)
1535 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1537 unsigned char msg[3];
1538 unsigned char *resp;
1539 struct ssif_info *ssif_info;
1544 struct ssif_addr_info *addr_info = NULL;
1546 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1550 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1556 if (!check_acpi(ssif_info, &client->dev)) {
1557 addr_info = ssif_info_find(client->addr, client->adapter->name,
1560 /* Must have come in through sysfs. */
1561 ssif_info->addr_source = SI_HOTMOD;
1563 ssif_info->addr_source = addr_info->addr_src;
1564 ssif_info->ssif_debug = addr_info->debug;
1565 ssif_info->addr_info = addr_info->addr_info;
1566 addr_info->client = client;
1567 slave_addr = addr_info->slave_addr;
1571 slave_addr = find_slave_address(client, slave_addr);
1573 pr_info("Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1574 ipmi_addr_src_to_str(ssif_info->addr_source),
1575 client->addr, client->adapter->name, slave_addr);
1577 ssif_info->client = client;
1578 i2c_set_clientdata(client, ssif_info);
1580 /* Now check for system interface capabilities */
1581 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1582 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1583 msg[2] = 0; /* SSIF */
1584 rv = do_cmd(client, 3, msg, &len, resp);
1585 if (!rv && (len >= 3) && (resp[2] == 0)) {
1588 pr_info("SSIF info too short: %d\n", len);
1592 /* Got a good SSIF response, handle it. */
1593 ssif_info->max_xmit_msg_size = resp[5];
1594 ssif_info->max_recv_msg_size = resp[6];
1595 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1596 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1598 /* Sanitize the data */
1599 switch (ssif_info->multi_support) {
1601 if (ssif_info->max_xmit_msg_size > 32)
1602 ssif_info->max_xmit_msg_size = 32;
1603 if (ssif_info->max_recv_msg_size > 32)
1604 ssif_info->max_recv_msg_size = 32;
1607 case SSIF_MULTI_2_PART:
1608 if (ssif_info->max_xmit_msg_size > 63)
1609 ssif_info->max_xmit_msg_size = 63;
1610 if (ssif_info->max_recv_msg_size > 62)
1611 ssif_info->max_recv_msg_size = 62;
1614 case SSIF_MULTI_n_PART:
1615 /* We take whatever size given, but do some testing. */
1619 /* Data is not sane, just give up. */
1624 /* Assume no multi-part or PEC support */
1625 pr_info("Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1628 ssif_info->max_xmit_msg_size = 32;
1629 ssif_info->max_recv_msg_size = 32;
1630 ssif_info->multi_support = SSIF_NO_MULTI;
1631 ssif_info->supports_pec = 0;
1634 test_multipart_messages(client, ssif_info, resp);
1636 /* Make sure the NMI timeout is cleared. */
1637 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1638 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1639 msg[2] = WDT_PRE_TIMEOUT_INT;
1640 rv = do_cmd(client, 3, msg, &len, resp);
1641 if (rv || (len < 3) || (resp[2] != 0))
1642 pr_warn("Unable to clear message flags: %d %d %2.2x\n",
1645 /* Attempt to enable the event buffer. */
1646 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1647 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1648 rv = do_cmd(client, 2, msg, &len, resp);
1649 if (rv || (len < 4) || (resp[2] != 0)) {
1650 pr_warn("Error getting global enables: %d %d %2.2x\n",
1652 rv = 0; /* Not fatal */
1656 ssif_info->global_enables = resp[3];
1658 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1659 ssif_info->has_event_buffer = true;
1660 /* buffer is already enabled, nothing to do. */
1664 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1665 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1666 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1667 rv = do_cmd(client, 3, msg, &len, resp);
1668 if (rv || (len < 2)) {
1669 pr_warn("Error setting global enables: %d %d %2.2x\n",
1671 rv = 0; /* Not fatal */
1676 /* A successful return means the event buffer is supported. */
1677 ssif_info->has_event_buffer = true;
1678 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1681 /* Some systems don't behave well if you enable alerts. */
1685 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1686 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1687 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1688 rv = do_cmd(client, 3, msg, &len, resp);
1689 if (rv || (len < 2)) {
1690 pr_warn("Error setting global enables: %d %d %2.2x\n",
1692 rv = 0; /* Not fatal */
1697 /* A successful return means the alert is supported. */
1698 ssif_info->supports_alert = true;
1699 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1703 if (ssif_dbg_probe) {
1704 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1708 spin_lock_init(&ssif_info->lock);
1709 ssif_info->ssif_state = SSIF_NORMAL;
1710 timer_setup(&ssif_info->retry_timer, retry_timeout, 0);
1712 for (i = 0; i < SSIF_NUM_STATS; i++)
1713 atomic_set(&ssif_info->stats[i], 0);
1715 if (ssif_info->supports_pec)
1716 ssif_info->client->flags |= I2C_CLIENT_PEC;
1718 ssif_info->handlers.owner = THIS_MODULE;
1719 ssif_info->handlers.start_processing = ssif_start_processing;
1720 ssif_info->handlers.shutdown = shutdown_ssif;
1721 ssif_info->handlers.get_smi_info = get_smi_info;
1722 ssif_info->handlers.sender = sender;
1723 ssif_info->handlers.request_events = request_events;
1726 unsigned int thread_num;
1728 thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1730 ssif_info->client->addr);
1731 init_completion(&ssif_info->wake_thread);
1732 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1733 "kssif%4.4x", thread_num);
1734 if (IS_ERR(ssif_info->thread)) {
1735 rv = PTR_ERR(ssif_info->thread);
1736 dev_notice(&ssif_info->client->dev,
1737 "Could not start kernel thread: error %d\n",
1743 dev_set_drvdata(&ssif_info->client->dev, ssif_info);
1744 rv = device_add_group(&ssif_info->client->dev,
1745 &ipmi_ssif_dev_attr_group);
1747 dev_err(&ssif_info->client->dev,
1748 "Unable to add device attributes: error %d\n",
1753 rv = ipmi_register_smi(&ssif_info->handlers,
1755 &ssif_info->client->dev,
1758 pr_err("Unable to register device: error %d\n", rv);
1759 goto out_remove_attr;
1765 addr_info->client = NULL;
1767 dev_err(&client->dev, "Unable to start IPMI SSIF: %d\n", rv);
1774 device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1775 dev_set_drvdata(&ssif_info->client->dev, NULL);
1779 static int ssif_adapter_handler(struct device *adev, void *opaque)
1781 struct ssif_addr_info *addr_info = opaque;
1783 if (adev->type != &i2c_adapter_type)
1786 addr_info->added_client = i2c_new_device(to_i2c_adapter(adev),
1789 if (!addr_info->adapter_name)
1790 return 1; /* Only try the first I2C adapter by default. */
1794 static int new_ssif_client(int addr, char *adapter_name,
1795 int debug, int slave_addr,
1796 enum ipmi_addr_src addr_src,
1799 struct ssif_addr_info *addr_info;
1802 mutex_lock(&ssif_infos_mutex);
1803 if (ssif_info_find(addr, adapter_name, false)) {
1808 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1815 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1816 if (!addr_info->adapter_name) {
1823 strncpy(addr_info->binfo.type, DEVICE_NAME,
1824 sizeof(addr_info->binfo.type));
1825 addr_info->binfo.addr = addr;
1826 addr_info->binfo.platform_data = addr_info;
1827 addr_info->debug = debug;
1828 addr_info->slave_addr = slave_addr;
1829 addr_info->addr_src = addr_src;
1830 addr_info->dev = dev;
1833 dev_set_drvdata(dev, addr_info);
1835 list_add_tail(&addr_info->link, &ssif_infos);
1838 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1839 /* Otherwise address list will get it */
1842 mutex_unlock(&ssif_infos_mutex);
1846 static void free_ssif_clients(void)
1848 struct ssif_addr_info *info, *tmp;
1850 mutex_lock(&ssif_infos_mutex);
1851 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1852 list_del(&info->link);
1853 kfree(info->adapter_name);
1856 mutex_unlock(&ssif_infos_mutex);
1859 static unsigned short *ssif_address_list(void)
1861 struct ssif_addr_info *info;
1862 unsigned int count = 0, i = 0;
1863 unsigned short *address_list;
1865 list_for_each_entry(info, &ssif_infos, link)
1868 address_list = kcalloc(count + 1, sizeof(*address_list),
1873 list_for_each_entry(info, &ssif_infos, link) {
1874 unsigned short addr = info->binfo.addr;
1877 for (j = 0; j < i; j++) {
1878 if (address_list[j] == addr)
1882 if (j == i) /* Didn't find it in the list. */
1883 address_list[i++] = addr;
1885 address_list[i] = I2C_CLIENT_END;
1887 return address_list;
1891 static const struct acpi_device_id ssif_acpi_match[] = {
1895 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1899 static int dmi_ipmi_probe(struct platform_device *pdev)
1908 rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
1910 dev_warn(&pdev->dev, "No i2c-addr property\n");
1914 rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
1916 dev_warn(&pdev->dev, "device has no slave-addr property");
1918 return new_ssif_client(i2c_addr, NULL, 0,
1919 slave_addr, SI_SMBIOS, &pdev->dev);
1922 static int dmi_ipmi_probe(struct platform_device *pdev)
1928 static const struct i2c_device_id ssif_id[] = {
1932 MODULE_DEVICE_TABLE(i2c, ssif_id);
1934 static struct i2c_driver ssif_i2c_driver = {
1935 .class = I2C_CLASS_HWMON,
1939 .probe = ssif_probe,
1940 .remove = ssif_remove,
1941 .alert = ssif_alert,
1942 .id_table = ssif_id,
1943 .detect = ssif_detect
1946 static int ssif_platform_probe(struct platform_device *dev)
1948 return dmi_ipmi_probe(dev);
1951 static int ssif_platform_remove(struct platform_device *dev)
1953 struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
1958 mutex_lock(&ssif_infos_mutex);
1959 i2c_unregister_device(addr_info->added_client);
1961 list_del(&addr_info->link);
1963 mutex_unlock(&ssif_infos_mutex);
1967 static const struct platform_device_id ssif_plat_ids[] = {
1968 { "dmi-ipmi-ssif", 0 },
1972 static struct platform_driver ipmi_driver = {
1974 .name = DEVICE_NAME,
1976 .probe = ssif_platform_probe,
1977 .remove = ssif_platform_remove,
1978 .id_table = ssif_plat_ids
1981 static int init_ipmi_ssif(void)
1989 pr_info("IPMI SSIF Interface driver\n");
1991 /* build list for i2c from addr list */
1992 for (i = 0; i < num_addrs; i++) {
1993 rv = new_ssif_client(addr[i], adapter_name[i],
1994 dbg[i], slave_addrs[i],
1995 SI_HARDCODED, NULL);
1997 pr_err("Couldn't add hardcoded device at addr 0x%x\n",
2002 ssif_i2c_driver.driver.acpi_match_table =
2003 ACPI_PTR(ssif_acpi_match);
2006 rv = platform_driver_register(&ipmi_driver);
2008 pr_err("Unable to register driver: %d\n", rv);
2011 ssif_i2c_driver.address_list = ssif_address_list();
2013 rv = i2c_add_driver(&ssif_i2c_driver);
2019 module_init(init_ipmi_ssif);
2021 static void cleanup_ipmi_ssif(void)
2026 initialized = false;
2028 i2c_del_driver(&ssif_i2c_driver);
2030 kfree(ssif_i2c_driver.address_list);
2032 platform_driver_unregister(&ipmi_driver);
2034 free_ssif_clients();
2036 module_exit(cleanup_ipmi_ssif);
2038 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2039 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2040 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2041 MODULE_LICENSE("GPL");