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.
25 #define pr_fmt(fmt) "ipmi_ssif: " fmt
26 #define dev_fmt(fmt) "ipmi_ssif: " fmt
28 #if defined(MODVERSIONS)
29 #include <linux/modversions.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/sched.h>
35 #include <linux/seq_file.h>
36 #include <linux/timer.h>
37 #include <linux/delay.h>
38 #include <linux/errno.h>
39 #include <linux/spinlock.h>
40 #include <linux/slab.h>
41 #include <linux/list.h>
42 #include <linux/i2c.h>
43 #include <linux/ipmi_smi.h>
44 #include <linux/init.h>
45 #include <linux/dmi.h>
46 #include <linux/kthread.h>
47 #include <linux/acpi.h>
48 #include <linux/ctype.h>
49 #include <linux/time64.h>
52 #define DEVICE_NAME "ipmi_ssif"
54 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
56 #define SSIF_IPMI_REQUEST 2
57 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
58 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
59 #define SSIF_IPMI_MULTI_PART_REQUEST_END 8
60 #define SSIF_IPMI_RESPONSE 3
61 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
63 /* ssif_debug is a bit-field
64 * SSIF_DEBUG_MSG - commands and their responses
65 * SSIF_DEBUG_STATES - message states
66 * SSIF_DEBUG_TIMING - Measure times between events in the driver
68 #define SSIF_DEBUG_TIMING 4
69 #define SSIF_DEBUG_STATE 2
70 #define SSIF_DEBUG_MSG 1
71 #define SSIF_NODEBUG 0
72 #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
77 #define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
78 #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
80 /* How many times to we retry sending/receiving the message. */
81 #define SSIF_SEND_RETRIES 5
82 #define SSIF_RECV_RETRIES 250
84 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
85 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
86 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
89 * Timeout for the watch, only used for get flag timer.
91 #define SSIF_WATCH_MSG_TIMEOUT msecs_to_jiffies(10)
92 #define SSIF_WATCH_WATCHDOG_TIMEOUT msecs_to_jiffies(250)
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);
199 struct ipmi_smi *intf;
201 struct ipmi_smi_msg *waiting_msg;
202 struct ipmi_smi_msg *curr_msg;
203 enum ssif_intf_state ssif_state;
204 unsigned long ssif_debug;
206 struct ipmi_smi_handlers handlers;
208 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
209 union ipmi_smi_info_union addr_info;
212 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
213 * is set to hold the flags until we are done handling everything
216 #define RECEIVE_MSG_AVAIL 0x01
217 #define EVENT_MSG_BUFFER_FULL 0x02
218 #define WDT_PRE_TIMEOUT_INT 0x08
219 unsigned char msg_flags;
222 bool has_event_buffer;
226 * Used to tell what we should do with alerts. If we are
227 * waiting on a response, read the data immediately.
233 * If set to true, this will request events the next time the
234 * state machine is idle.
239 * If set to true, this will request flags the next time the
240 * state machine is idle.
245 * Used to perform timer operations when run-to-completion
246 * mode is on. This is a countdown timer.
250 /* Used for sending/receiving data. +1 for the length. */
251 unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
252 unsigned int data_len;
254 /* Temp receive buffer, gets copied into data. */
255 unsigned char recv[I2C_SMBUS_BLOCK_MAX];
257 struct i2c_client *client;
258 ssif_i2c_done done_handler;
260 /* Thread interface handling */
261 struct task_struct *thread;
262 struct completion wake_thread;
266 unsigned char *i2c_data;
267 unsigned int i2c_size;
269 struct timer_list retry_timer;
272 long watch_timeout; /* Timeout for flags check, 0 if off. */
273 struct timer_list watch_timer; /* Flag fetch timer. */
275 /* Info from SSIF cmd */
276 unsigned char max_xmit_msg_size;
277 unsigned char max_recv_msg_size;
278 bool cmd8_works; /* See test_multipart_messages() for details. */
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;
298 static bool platform_registered;
300 static void return_hosed_msg(struct ssif_info *ssif_info,
301 struct ipmi_smi_msg *msg);
302 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
303 static int start_send(struct ssif_info *ssif_info,
307 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
308 unsigned long *flags)
309 __acquires(&ssif_info->lock)
311 spin_lock_irqsave(&ssif_info->lock, *flags);
315 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
316 unsigned long *flags)
317 __releases(&ssif_info->lock)
319 spin_unlock_irqrestore(&ssif_info->lock, *flags);
322 static void deliver_recv_msg(struct ssif_info *ssif_info,
323 struct ipmi_smi_msg *msg)
325 if (msg->rsp_size < 0) {
326 return_hosed_msg(ssif_info, msg);
327 dev_err(&ssif_info->client->dev,
328 "%s: Malformed message: rsp_size = %d\n",
329 __func__, msg->rsp_size);
331 ipmi_smi_msg_received(ssif_info->intf, msg);
335 static void return_hosed_msg(struct ssif_info *ssif_info,
336 struct ipmi_smi_msg *msg)
338 ssif_inc_stat(ssif_info, hosed);
340 /* Make it a response */
341 msg->rsp[0] = msg->data[0] | 4;
342 msg->rsp[1] = msg->data[1];
343 msg->rsp[2] = 0xFF; /* Unknown error. */
346 deliver_recv_msg(ssif_info, msg);
350 * Must be called with the message lock held. This will release the
351 * message lock. Note that the caller will check SSIF_IDLE and start a
352 * new operation, so there is no need to check for new messages to
355 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
357 unsigned char msg[3];
359 ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
360 ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
361 ipmi_ssif_unlock_cond(ssif_info, flags);
363 /* Make sure the watchdog pre-timeout flag is not set at startup. */
364 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
365 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
366 msg[2] = WDT_PRE_TIMEOUT_INT;
368 if (start_send(ssif_info, msg, 3) != 0) {
369 /* Error, just go to normal state. */
370 ssif_info->ssif_state = SSIF_NORMAL;
374 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
378 ssif_info->req_flags = false;
379 ssif_info->ssif_state = SSIF_GETTING_FLAGS;
380 ipmi_ssif_unlock_cond(ssif_info, flags);
382 mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
383 mb[1] = IPMI_GET_MSG_FLAGS_CMD;
384 if (start_send(ssif_info, mb, 2) != 0)
385 ssif_info->ssif_state = SSIF_NORMAL;
388 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
389 struct ipmi_smi_msg *msg)
391 if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
392 unsigned long oflags;
394 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
395 ssif_info->curr_msg = NULL;
396 ssif_info->ssif_state = SSIF_NORMAL;
397 ipmi_ssif_unlock_cond(ssif_info, flags);
398 ipmi_free_smi_msg(msg);
402 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
404 struct ipmi_smi_msg *msg;
406 ssif_info->req_events = false;
408 msg = ipmi_alloc_smi_msg();
410 ssif_info->ssif_state = SSIF_NORMAL;
411 ipmi_ssif_unlock_cond(ssif_info, flags);
415 ssif_info->curr_msg = msg;
416 ssif_info->ssif_state = SSIF_GETTING_EVENTS;
417 ipmi_ssif_unlock_cond(ssif_info, flags);
419 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
420 msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
423 check_start_send(ssif_info, flags, msg);
426 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
427 unsigned long *flags)
429 struct ipmi_smi_msg *msg;
431 msg = ipmi_alloc_smi_msg();
433 ssif_info->ssif_state = SSIF_NORMAL;
434 ipmi_ssif_unlock_cond(ssif_info, flags);
438 ssif_info->curr_msg = msg;
439 ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
440 ipmi_ssif_unlock_cond(ssif_info, flags);
442 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
443 msg->data[1] = IPMI_GET_MSG_CMD;
446 check_start_send(ssif_info, flags, msg);
450 * Must be called with the message lock held. This will release the
451 * message lock. Note that the caller will check SSIF_IDLE and start a
452 * new operation, so there is no need to check for new messages to
455 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
457 if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
458 /* Watchdog pre-timeout */
459 ssif_inc_stat(ssif_info, watchdog_pretimeouts);
460 start_clear_flags(ssif_info, flags);
461 ipmi_smi_watchdog_pretimeout(ssif_info->intf);
462 } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
463 /* Messages available. */
464 start_recv_msg_fetch(ssif_info, flags);
465 else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
466 /* Events available. */
467 start_event_fetch(ssif_info, flags);
469 ssif_info->ssif_state = SSIF_NORMAL;
470 ipmi_ssif_unlock_cond(ssif_info, flags);
474 static int ipmi_ssif_thread(void *data)
476 struct ssif_info *ssif_info = data;
478 while (!kthread_should_stop()) {
481 /* Wait for something to do */
482 result = wait_for_completion_interruptible(
483 &ssif_info->wake_thread);
484 if (ssif_info->stopping)
486 if (result == -ERESTARTSYS)
488 init_completion(&ssif_info->wake_thread);
490 if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
491 result = i2c_smbus_write_block_data(
492 ssif_info->client, ssif_info->i2c_command,
493 ssif_info->i2c_data[0],
494 ssif_info->i2c_data + 1);
495 ssif_info->done_handler(ssif_info, result, NULL, 0);
497 result = i2c_smbus_read_block_data(
498 ssif_info->client, ssif_info->i2c_command,
499 ssif_info->i2c_data);
501 ssif_info->done_handler(ssif_info, result,
504 ssif_info->done_handler(ssif_info, 0,
513 static int ssif_i2c_send(struct ssif_info *ssif_info,
514 ssif_i2c_done handler,
515 int read_write, int command,
516 unsigned char *data, unsigned int size)
518 ssif_info->done_handler = handler;
520 ssif_info->i2c_read_write = read_write;
521 ssif_info->i2c_command = command;
522 ssif_info->i2c_data = data;
523 ssif_info->i2c_size = size;
524 complete(&ssif_info->wake_thread);
529 static void msg_done_handler(struct ssif_info *ssif_info, int result,
530 unsigned char *data, unsigned int len);
532 static void start_get(struct ssif_info *ssif_info)
536 ssif_info->rtc_us_timer = 0;
537 ssif_info->multi_pos = 0;
539 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
541 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
543 /* request failed, just return the error. */
544 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
545 dev_dbg(&ssif_info->client->dev,
546 "Error from i2c_non_blocking_op(5)\n");
548 msg_done_handler(ssif_info, -EIO, NULL, 0);
552 static void retry_timeout(struct timer_list *t)
554 struct ssif_info *ssif_info = from_timer(ssif_info, t, retry_timer);
555 unsigned long oflags, *flags;
558 if (ssif_info->stopping)
561 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
562 waiting = ssif_info->waiting_alert;
563 ssif_info->waiting_alert = false;
564 ipmi_ssif_unlock_cond(ssif_info, flags);
567 start_get(ssif_info);
570 static void watch_timeout(struct timer_list *t)
572 struct ssif_info *ssif_info = from_timer(ssif_info, t, watch_timer);
573 unsigned long oflags, *flags;
575 if (ssif_info->stopping)
578 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
579 if (ssif_info->watch_timeout) {
580 mod_timer(&ssif_info->watch_timer,
581 jiffies + ssif_info->watch_timeout);
582 if (SSIF_IDLE(ssif_info)) {
583 start_flag_fetch(ssif_info, flags); /* Releases lock */
586 ssif_info->req_flags = true;
588 ipmi_ssif_unlock_cond(ssif_info, flags);
591 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
594 struct ssif_info *ssif_info = i2c_get_clientdata(client);
595 unsigned long oflags, *flags;
598 if (type != I2C_PROTOCOL_SMBUS_ALERT)
601 ssif_inc_stat(ssif_info, alerts);
603 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
604 if (ssif_info->waiting_alert) {
605 ssif_info->waiting_alert = false;
606 del_timer(&ssif_info->retry_timer);
608 } else if (ssif_info->curr_msg) {
609 ssif_info->got_alert = true;
611 ipmi_ssif_unlock_cond(ssif_info, flags);
613 start_get(ssif_info);
616 static int start_resend(struct ssif_info *ssif_info);
618 static void msg_done_handler(struct ssif_info *ssif_info, int result,
619 unsigned char *data, unsigned int len)
621 struct ipmi_smi_msg *msg;
622 unsigned long oflags, *flags;
626 * We are single-threaded here, so no need for a lock until we
627 * start messing with driver states or the queues.
631 ssif_info->retries_left--;
632 if (ssif_info->retries_left > 0) {
633 ssif_inc_stat(ssif_info, receive_retries);
635 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
636 ssif_info->waiting_alert = true;
637 ssif_info->rtc_us_timer = SSIF_MSG_USEC;
638 if (!ssif_info->stopping)
639 mod_timer(&ssif_info->retry_timer,
640 jiffies + SSIF_MSG_JIFFIES);
641 ipmi_ssif_unlock_cond(ssif_info, flags);
645 ssif_inc_stat(ssif_info, receive_errors);
647 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
648 dev_dbg(&ssif_info->client->dev,
649 "%s: Error %d\n", __func__, result);
654 if ((len > 1) && (ssif_info->multi_pos == 0)
655 && (data[0] == 0x00) && (data[1] == 0x01)) {
656 /* Start of multi-part read. Start the next transaction. */
659 ssif_inc_stat(ssif_info, received_message_parts);
661 /* Remove the multi-part read marker. */
664 for (i = 0; i < len; i++)
665 ssif_info->data[i] = data[i];
666 ssif_info->multi_len = len;
667 ssif_info->multi_pos = 1;
669 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
670 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
671 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
673 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
674 dev_dbg(&ssif_info->client->dev,
675 "Error from i2c_non_blocking_op(1)\n");
680 } else if (ssif_info->multi_pos) {
681 /* Middle of multi-part read. Start the next transaction. */
683 unsigned char blocknum;
687 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
688 dev_dbg(&ssif_info->client->dev,
689 "Middle message with no data\n");
698 if (blocknum != 0xff && len != 31) {
699 /* All blocks but the last must have 31 data bytes. */
701 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
702 dev_dbg(&ssif_info->client->dev,
703 "Received middle message <31\n");
708 if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
709 /* Received message too big, abort the operation. */
711 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
712 dev_dbg(&ssif_info->client->dev,
713 "Received message too big\n");
718 for (i = 0; i < len; i++)
719 ssif_info->data[i + ssif_info->multi_len] = data[i];
720 ssif_info->multi_len += len;
721 if (blocknum == 0xff) {
723 len = ssif_info->multi_len;
724 data = ssif_info->data;
725 } else if (blocknum + 1 != ssif_info->multi_pos) {
727 * Out of sequence block, just abort. Block
728 * numbers start at zero for the second block,
729 * but multi_pos starts at one, so the +1.
731 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
732 dev_dbg(&ssif_info->client->dev,
733 "Received message out of sequence, expected %u, got %u\n",
734 ssif_info->multi_pos - 1, blocknum);
737 ssif_inc_stat(ssif_info, received_message_parts);
739 ssif_info->multi_pos++;
741 rv = ssif_i2c_send(ssif_info, msg_done_handler,
743 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
745 I2C_SMBUS_BLOCK_DATA);
747 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
748 dev_dbg(&ssif_info->client->dev,
749 "Error from ssif_i2c_send\n");
759 ssif_inc_stat(ssif_info, receive_errors);
761 ssif_inc_stat(ssif_info, received_messages);
762 ssif_inc_stat(ssif_info, received_message_parts);
765 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
766 dev_dbg(&ssif_info->client->dev,
767 "DONE 1: state = %d, result=%d\n",
768 ssif_info->ssif_state, result);
770 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
771 msg = ssif_info->curr_msg;
774 if (len > IPMI_MAX_MSG_LENGTH)
775 len = IPMI_MAX_MSG_LENGTH;
776 memcpy(msg->rsp, data, len);
781 ssif_info->curr_msg = NULL;
784 switch (ssif_info->ssif_state) {
786 ipmi_ssif_unlock_cond(ssif_info, flags);
791 return_hosed_msg(ssif_info, msg);
793 deliver_recv_msg(ssif_info, msg);
796 case SSIF_GETTING_FLAGS:
797 /* We got the flags from the SSIF, now handle them. */
798 if ((result < 0) || (len < 4) || (data[2] != 0)) {
800 * Error fetching flags, or invalid length,
801 * just give up for now.
803 ssif_info->ssif_state = SSIF_NORMAL;
804 ipmi_ssif_unlock_cond(ssif_info, flags);
805 dev_warn(&ssif_info->client->dev,
806 "Error getting flags: %d %d, %x\n",
807 result, len, (len >= 3) ? data[2] : 0);
808 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
809 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
811 * Don't abort here, maybe it was a queued
812 * response to a previous command.
814 ipmi_ssif_unlock_cond(ssif_info, flags);
815 dev_warn(&ssif_info->client->dev,
816 "Invalid response getting flags: %x %x\n",
819 ssif_inc_stat(ssif_info, flag_fetches);
820 ssif_info->msg_flags = data[3];
821 handle_flags(ssif_info, flags);
825 case SSIF_CLEARING_FLAGS:
826 /* We cleared the flags. */
827 if ((result < 0) || (len < 3) || (data[2] != 0)) {
828 /* Error clearing flags */
829 dev_warn(&ssif_info->client->dev,
830 "Error clearing flags: %d %d, %x\n",
831 result, len, (len >= 3) ? data[2] : 0);
832 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
833 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
834 dev_warn(&ssif_info->client->dev,
835 "Invalid response clearing flags: %x %x\n",
838 ssif_info->ssif_state = SSIF_NORMAL;
839 ipmi_ssif_unlock_cond(ssif_info, flags);
842 case SSIF_GETTING_EVENTS:
844 /* Should never happen, but just in case. */
845 dev_warn(&ssif_info->client->dev,
846 "No message set while getting events\n");
847 ipmi_ssif_unlock_cond(ssif_info, flags);
851 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
852 /* Error getting event, probably done. */
855 /* Take off the event flag. */
856 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
857 handle_flags(ssif_info, flags);
858 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
859 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
860 dev_warn(&ssif_info->client->dev,
861 "Invalid response getting events: %x %x\n",
862 msg->rsp[0], msg->rsp[1]);
864 /* Take off the event flag. */
865 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
866 handle_flags(ssif_info, flags);
868 handle_flags(ssif_info, flags);
869 ssif_inc_stat(ssif_info, events);
870 deliver_recv_msg(ssif_info, msg);
874 case SSIF_GETTING_MESSAGES:
876 /* Should never happen, but just in case. */
877 dev_warn(&ssif_info->client->dev,
878 "No message set while getting messages\n");
879 ipmi_ssif_unlock_cond(ssif_info, flags);
883 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
884 /* Error getting event, probably done. */
887 /* Take off the msg flag. */
888 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
889 handle_flags(ssif_info, flags);
890 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
891 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
892 dev_warn(&ssif_info->client->dev,
893 "Invalid response clearing flags: %x %x\n",
894 msg->rsp[0], msg->rsp[1]);
897 /* Take off the msg flag. */
898 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
899 handle_flags(ssif_info, flags);
901 ssif_inc_stat(ssif_info, incoming_messages);
902 handle_flags(ssif_info, flags);
903 deliver_recv_msg(ssif_info, msg);
908 /* Should never happen, but just in case. */
909 dev_warn(&ssif_info->client->dev,
910 "Invalid state in message done handling: %d\n",
911 ssif_info->ssif_state);
912 ipmi_ssif_unlock_cond(ssif_info, flags);
915 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
916 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
917 if (ssif_info->req_events)
918 start_event_fetch(ssif_info, flags);
919 else if (ssif_info->req_flags)
920 start_flag_fetch(ssif_info, flags);
922 start_next_msg(ssif_info, flags);
924 ipmi_ssif_unlock_cond(ssif_info, flags);
926 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
927 dev_dbg(&ssif_info->client->dev,
928 "DONE 2: state = %d.\n", ssif_info->ssif_state);
931 static void msg_written_handler(struct ssif_info *ssif_info, int result,
932 unsigned char *data, unsigned int len)
936 /* We are single-threaded here, so no need for a lock. */
938 ssif_info->retries_left--;
939 if (ssif_info->retries_left > 0) {
940 if (!start_resend(ssif_info)) {
941 ssif_inc_stat(ssif_info, send_retries);
944 /* request failed, just return the error. */
945 ssif_inc_stat(ssif_info, send_errors);
947 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
948 dev_dbg(&ssif_info->client->dev,
949 "%s: Out of retries\n", __func__);
950 msg_done_handler(ssif_info, -EIO, NULL, 0);
954 ssif_inc_stat(ssif_info, send_errors);
957 * Got an error on transmit, let the done routine
960 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
961 dev_dbg(&ssif_info->client->dev,
962 "%s: Error %d\n", __func__, result);
964 msg_done_handler(ssif_info, result, NULL, 0);
968 if (ssif_info->multi_data) {
970 * In the middle of a multi-data write. See the comment
971 * in the SSIF_MULTI_n_PART case in the probe function
972 * for details on the intricacies of this.
975 unsigned char *data_to_send;
978 ssif_inc_stat(ssif_info, sent_messages_parts);
980 left = ssif_info->multi_len - ssif_info->multi_pos;
985 ssif_info->multi_data[ssif_info->multi_pos] = to_write;
986 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
987 ssif_info->multi_pos += to_write;
988 cmd = SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE;
989 if (ssif_info->cmd8_works) {
990 if (left == to_write) {
991 cmd = SSIF_IPMI_MULTI_PART_REQUEST_END;
992 ssif_info->multi_data = NULL;
994 } else if (to_write < 32) {
995 ssif_info->multi_data = NULL;
998 rv = ssif_i2c_send(ssif_info, msg_written_handler,
999 I2C_SMBUS_WRITE, cmd,
1000 data_to_send, I2C_SMBUS_BLOCK_DATA);
1002 /* request failed, just return the error. */
1003 ssif_inc_stat(ssif_info, send_errors);
1005 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
1006 dev_dbg(&ssif_info->client->dev,
1007 "Error from i2c_non_blocking_op(3)\n");
1008 msg_done_handler(ssif_info, -EIO, NULL, 0);
1011 /* Ready to request the result. */
1012 unsigned long oflags, *flags;
1014 ssif_inc_stat(ssif_info, sent_messages);
1015 ssif_inc_stat(ssif_info, sent_messages_parts);
1017 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1018 if (ssif_info->got_alert) {
1019 /* The result is already ready, just start it. */
1020 ssif_info->got_alert = false;
1021 ipmi_ssif_unlock_cond(ssif_info, flags);
1022 start_get(ssif_info);
1024 /* Wait a jiffie then request the next message */
1025 ssif_info->waiting_alert = true;
1026 ssif_info->retries_left = SSIF_RECV_RETRIES;
1027 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
1028 if (!ssif_info->stopping)
1029 mod_timer(&ssif_info->retry_timer,
1030 jiffies + SSIF_MSG_PART_JIFFIES);
1031 ipmi_ssif_unlock_cond(ssif_info, flags);
1036 static int start_resend(struct ssif_info *ssif_info)
1041 ssif_info->got_alert = false;
1043 if (ssif_info->data_len > 32) {
1044 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
1045 ssif_info->multi_data = ssif_info->data;
1046 ssif_info->multi_len = ssif_info->data_len;
1048 * Subtle thing, this is 32, not 33, because we will
1049 * overwrite the thing at position 32 (which was just
1050 * transmitted) with the new length.
1052 ssif_info->multi_pos = 32;
1053 ssif_info->data[0] = 32;
1055 ssif_info->multi_data = NULL;
1056 command = SSIF_IPMI_REQUEST;
1057 ssif_info->data[0] = ssif_info->data_len;
1060 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1061 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1062 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
1063 dev_dbg(&ssif_info->client->dev,
1064 "Error from i2c_non_blocking_op(4)\n");
1068 static int start_send(struct ssif_info *ssif_info,
1069 unsigned char *data,
1072 if (len > IPMI_MAX_MSG_LENGTH)
1074 if (len > ssif_info->max_xmit_msg_size)
1077 ssif_info->retries_left = SSIF_SEND_RETRIES;
1078 memcpy(ssif_info->data + 1, data, len);
1079 ssif_info->data_len = len;
1080 return start_resend(ssif_info);
1083 /* Must be called with the message lock held. */
1084 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1086 struct ipmi_smi_msg *msg;
1087 unsigned long oflags;
1090 if (!SSIF_IDLE(ssif_info)) {
1091 ipmi_ssif_unlock_cond(ssif_info, flags);
1095 if (!ssif_info->waiting_msg) {
1096 ssif_info->curr_msg = NULL;
1097 ipmi_ssif_unlock_cond(ssif_info, flags);
1101 ssif_info->curr_msg = ssif_info->waiting_msg;
1102 ssif_info->waiting_msg = NULL;
1103 ipmi_ssif_unlock_cond(ssif_info, flags);
1104 rv = start_send(ssif_info,
1105 ssif_info->curr_msg->data,
1106 ssif_info->curr_msg->data_size);
1108 msg = ssif_info->curr_msg;
1109 ssif_info->curr_msg = NULL;
1110 return_hosed_msg(ssif_info, msg);
1111 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1117 static void sender(void *send_info,
1118 struct ipmi_smi_msg *msg)
1120 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1121 unsigned long oflags, *flags;
1123 BUG_ON(ssif_info->waiting_msg);
1124 ssif_info->waiting_msg = msg;
1126 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1127 start_next_msg(ssif_info, flags);
1129 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1130 struct timespec64 t;
1132 ktime_get_real_ts64(&t);
1133 dev_dbg(&ssif_info->client->dev,
1134 "**Enqueue %02x %02x: %lld.%6.6ld\n",
1135 msg->data[0], msg->data[1],
1136 (long long)t.tv_sec, (long)t.tv_nsec / NSEC_PER_USEC);
1140 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1142 struct ssif_info *ssif_info = send_info;
1144 data->addr_src = ssif_info->addr_source;
1145 data->dev = &ssif_info->client->dev;
1146 data->addr_info = ssif_info->addr_info;
1147 get_device(data->dev);
1153 * Upper layer wants us to request events.
1155 static void request_events(void *send_info)
1157 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1158 unsigned long oflags, *flags;
1160 if (!ssif_info->has_event_buffer)
1163 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1164 ssif_info->req_events = true;
1165 ipmi_ssif_unlock_cond(ssif_info, flags);
1169 * Upper layer is changing the flag saying whether we need to request
1170 * flags periodically or not.
1172 static void ssif_set_need_watch(void *send_info, unsigned int watch_mask)
1174 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1175 unsigned long oflags, *flags;
1178 if (watch_mask & IPMI_WATCH_MASK_CHECK_MESSAGES)
1179 timeout = SSIF_WATCH_MSG_TIMEOUT;
1180 else if (watch_mask)
1181 timeout = SSIF_WATCH_WATCHDOG_TIMEOUT;
1183 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1184 if (timeout != ssif_info->watch_timeout) {
1185 ssif_info->watch_timeout = timeout;
1186 if (ssif_info->watch_timeout)
1187 mod_timer(&ssif_info->watch_timer,
1188 jiffies + ssif_info->watch_timeout);
1190 ipmi_ssif_unlock_cond(ssif_info, flags);
1193 static int ssif_start_processing(void *send_info,
1194 struct ipmi_smi *intf)
1196 struct ssif_info *ssif_info = send_info;
1198 ssif_info->intf = intf;
1203 #define MAX_SSIF_BMCS 4
1205 static unsigned short addr[MAX_SSIF_BMCS];
1206 static int num_addrs;
1207 module_param_array(addr, ushort, &num_addrs, 0);
1208 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1210 static char *adapter_name[MAX_SSIF_BMCS];
1211 static int num_adapter_names;
1212 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1213 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1215 static int slave_addrs[MAX_SSIF_BMCS];
1216 static int num_slave_addrs;
1217 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1218 MODULE_PARM_DESC(slave_addrs,
1219 "The default IPMB slave address for the controller.");
1221 static bool alerts_broken;
1222 module_param(alerts_broken, bool, 0);
1223 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1226 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1227 * bit 2 enables timing debugging. This is an array indexed by
1230 static int dbg[MAX_SSIF_BMCS];
1232 module_param_array(dbg, int, &num_dbg, 0);
1233 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1235 static bool ssif_dbg_probe;
1236 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1237 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1239 static bool ssif_tryacpi = true;
1240 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1241 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1243 static bool ssif_trydmi = true;
1244 module_param_named(trydmi, ssif_trydmi, bool, 0);
1245 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1247 static DEFINE_MUTEX(ssif_infos_mutex);
1248 static LIST_HEAD(ssif_infos);
1250 #define IPMI_SSIF_ATTR(name) \
1251 static ssize_t ipmi_##name##_show(struct device *dev, \
1252 struct device_attribute *attr, \
1255 struct ssif_info *ssif_info = dev_get_drvdata(dev); \
1257 return snprintf(buf, 10, "%u\n", ssif_get_stat(ssif_info, name));\
1259 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1261 static ssize_t ipmi_type_show(struct device *dev,
1262 struct device_attribute *attr,
1265 return snprintf(buf, 10, "ssif\n");
1267 static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1269 IPMI_SSIF_ATTR(sent_messages);
1270 IPMI_SSIF_ATTR(sent_messages_parts);
1271 IPMI_SSIF_ATTR(send_retries);
1272 IPMI_SSIF_ATTR(send_errors);
1273 IPMI_SSIF_ATTR(received_messages);
1274 IPMI_SSIF_ATTR(received_message_parts);
1275 IPMI_SSIF_ATTR(receive_retries);
1276 IPMI_SSIF_ATTR(receive_errors);
1277 IPMI_SSIF_ATTR(flag_fetches);
1278 IPMI_SSIF_ATTR(hosed);
1279 IPMI_SSIF_ATTR(events);
1280 IPMI_SSIF_ATTR(watchdog_pretimeouts);
1281 IPMI_SSIF_ATTR(alerts);
1283 static struct attribute *ipmi_ssif_dev_attrs[] = {
1284 &dev_attr_type.attr,
1285 &dev_attr_sent_messages.attr,
1286 &dev_attr_sent_messages_parts.attr,
1287 &dev_attr_send_retries.attr,
1288 &dev_attr_send_errors.attr,
1289 &dev_attr_received_messages.attr,
1290 &dev_attr_received_message_parts.attr,
1291 &dev_attr_receive_retries.attr,
1292 &dev_attr_receive_errors.attr,
1293 &dev_attr_flag_fetches.attr,
1294 &dev_attr_hosed.attr,
1295 &dev_attr_events.attr,
1296 &dev_attr_watchdog_pretimeouts.attr,
1297 &dev_attr_alerts.attr,
1301 static const struct attribute_group ipmi_ssif_dev_attr_group = {
1302 .attrs = ipmi_ssif_dev_attrs,
1305 static void shutdown_ssif(void *send_info)
1307 struct ssif_info *ssif_info = send_info;
1309 device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1310 dev_set_drvdata(&ssif_info->client->dev, NULL);
1312 /* make sure the driver is not looking for flags any more. */
1313 while (ssif_info->ssif_state != SSIF_NORMAL)
1314 schedule_timeout(1);
1316 ssif_info->stopping = true;
1317 del_timer_sync(&ssif_info->watch_timer);
1318 del_timer_sync(&ssif_info->retry_timer);
1319 if (ssif_info->thread) {
1320 complete(&ssif_info->wake_thread);
1321 kthread_stop(ssif_info->thread);
1325 static int ssif_remove(struct i2c_client *client)
1327 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1328 struct ssif_addr_info *addr_info;
1334 * After this point, we won't deliver anything asychronously
1335 * to the message handler. We can unregister ourself.
1337 ipmi_unregister_smi(ssif_info->intf);
1339 list_for_each_entry(addr_info, &ssif_infos, link) {
1340 if (addr_info->client == client) {
1341 addr_info->client = NULL;
1351 static int read_response(struct i2c_client *client, unsigned char *resp)
1353 int ret = -ENODEV, retry_cnt = SSIF_RECV_RETRIES;
1355 while (retry_cnt > 0) {
1356 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1360 msleep(SSIF_MSG_MSEC);
1369 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1370 int *resp_len, unsigned char *resp)
1375 retry_cnt = SSIF_SEND_RETRIES;
1377 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1385 ret = read_response(client, resp);
1387 /* Validate that the response is correct. */
1389 (resp[0] != (msg[0] | (1 << 2))) ||
1390 (resp[1] != msg[1]))
1392 else if (ret > IPMI_MAX_MSG_LENGTH) {
1403 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1405 unsigned char *resp;
1406 unsigned char msg[3];
1410 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1414 /* Do a Get Device ID command, since it is required. */
1415 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1416 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1417 rv = do_cmd(client, 2, msg, &len, resp);
1421 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1426 static int strcmp_nospace(char *s1, char *s2)
1428 while (*s1 && *s2) {
1429 while (isspace(*s1))
1431 while (isspace(*s2))
1443 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1445 bool match_null_name)
1447 struct ssif_addr_info *info, *found = NULL;
1450 list_for_each_entry(info, &ssif_infos, link) {
1451 if (info->binfo.addr == addr) {
1452 if (info->addr_src == SI_SMBIOS)
1453 info->adapter_name = kstrdup(adapter_name,
1456 if (info->adapter_name || adapter_name) {
1457 if (!info->adapter_name != !adapter_name) {
1458 /* One is NULL and one is not */
1462 strcmp_nospace(info->adapter_name,
1464 /* Names do not match */
1472 if (!found && match_null_name) {
1473 /* Try to get an exact match first, then try with a NULL name */
1474 adapter_name = NULL;
1475 match_null_name = false;
1482 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1485 acpi_handle acpi_handle;
1487 acpi_handle = ACPI_HANDLE(dev);
1489 ssif_info->addr_source = SI_ACPI;
1490 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1491 request_module("acpi_ipmi");
1498 static int find_slave_address(struct i2c_client *client, int slave_addr)
1500 #ifdef CONFIG_IPMI_DMI_DECODE
1502 slave_addr = ipmi_dmi_get_slave_addr(
1504 i2c_adapter_id(client->adapter),
1511 static int start_multipart_test(struct i2c_client *client,
1512 unsigned char *msg, bool do_middle)
1514 int retry_cnt = SSIF_SEND_RETRIES, ret;
1517 ret = i2c_smbus_write_block_data(client,
1518 SSIF_IPMI_MULTI_PART_REQUEST_START,
1524 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");
1531 ret = i2c_smbus_write_block_data(client,
1532 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1535 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");
1542 static void test_multipart_messages(struct i2c_client *client,
1543 struct ssif_info *ssif_info,
1544 unsigned char *resp)
1546 unsigned char msg[65];
1550 if (ssif_info->max_xmit_msg_size <= 32)
1553 do_middle = ssif_info->max_xmit_msg_size > 63;
1555 memset(msg, 0, sizeof(msg));
1556 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1557 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1560 * The specification is all messed up dealing with sending
1561 * multi-part messages. Per what the specification says, it
1562 * is impossible to send a message that is a multiple of 32
1563 * bytes, except for 32 itself. It talks about a "start"
1564 * transaction (cmd=6) that must be 32 bytes, "middle"
1565 * transaction (cmd=7) that must be 32 bytes, and an "end"
1566 * transaction. The "end" transaction is shown as cmd=7 in
1567 * the text, but if that's the case there is no way to
1568 * differentiate between a middle and end part except the
1569 * length being less than 32. But there is a table at the far
1570 * end of the section (that I had never noticed until someone
1571 * pointed it out to me) that mentions it as cmd=8.
1573 * After some thought, I think the example is wrong and the
1574 * end transaction should be cmd=8. But some systems don't
1575 * implement cmd=8, they use a zero-length end transaction,
1576 * even though that violates the SMBus specification.
1578 * So, to work around this, this code tests if cmd=8 works.
1579 * If it does, then we use that. If not, it tests zero-
1580 * byte end transactions. If that works, good. If not,
1581 * we only allow 63-byte transactions max.
1584 ret = start_multipart_test(client, msg, do_middle);
1586 goto out_no_multi_part;
1588 ret = i2c_smbus_write_block_data(client,
1589 SSIF_IPMI_MULTI_PART_REQUEST_END,
1593 ret = read_response(client, resp);
1596 /* End transactions work, we are good. */
1597 ssif_info->cmd8_works = true;
1601 ret = start_multipart_test(client, msg, do_middle);
1603 dev_err(&client->dev, "Second multipart test failed.\n");
1604 goto out_no_multi_part;
1607 ret = i2c_smbus_write_block_data(client,
1608 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1611 ret = read_response(client, resp);
1613 /* Zero-size end parts work, use those. */
1616 /* Limit to 63 bytes and use a short middle command to mark the end. */
1617 if (ssif_info->max_xmit_msg_size > 63)
1618 ssif_info->max_xmit_msg_size = 63;
1622 ssif_info->max_xmit_msg_size = 32;
1627 * Global enables we care about.
1629 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1630 IPMI_BMC_EVT_MSG_INTR)
1632 static void ssif_remove_dup(struct i2c_client *client)
1634 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1636 ipmi_unregister_smi(ssif_info->intf);
1640 static int ssif_add_infos(struct i2c_client *client)
1642 struct ssif_addr_info *info;
1644 info = kzalloc(sizeof(*info), GFP_KERNEL);
1647 info->addr_src = SI_ACPI;
1648 info->client = client;
1649 info->adapter_name = kstrdup(client->adapter->name, GFP_KERNEL);
1650 info->binfo.addr = client->addr;
1651 list_add_tail(&info->link, &ssif_infos);
1656 * Prefer ACPI over SMBIOS, if both are available.
1657 * So if we get an ACPI interface and have already registered a SMBIOS
1658 * interface at the same address, remove the SMBIOS and add the ACPI one.
1660 static int ssif_check_and_remove(struct i2c_client *client,
1661 struct ssif_info *ssif_info)
1663 struct ssif_addr_info *info;
1665 list_for_each_entry(info, &ssif_infos, link) {
1668 if (!strcmp(info->adapter_name, client->adapter->name) &&
1669 info->binfo.addr == client->addr) {
1670 if (info->addr_src == SI_ACPI)
1673 if (ssif_info->addr_source == SI_ACPI &&
1674 info->addr_src == SI_SMBIOS) {
1675 dev_info(&client->dev,
1676 "Removing %s-specified SSIF interface in favor of ACPI\n",
1677 ipmi_addr_src_to_str(info->addr_src));
1678 ssif_remove_dup(info->client);
1686 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1688 unsigned char msg[3];
1689 unsigned char *resp;
1690 struct ssif_info *ssif_info;
1695 struct ssif_addr_info *addr_info = NULL;
1697 mutex_lock(&ssif_infos_mutex);
1698 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1700 mutex_unlock(&ssif_infos_mutex);
1704 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1707 mutex_unlock(&ssif_infos_mutex);
1711 if (!check_acpi(ssif_info, &client->dev)) {
1712 addr_info = ssif_info_find(client->addr, client->adapter->name,
1715 /* Must have come in through sysfs. */
1716 ssif_info->addr_source = SI_HOTMOD;
1718 ssif_info->addr_source = addr_info->addr_src;
1719 ssif_info->ssif_debug = addr_info->debug;
1720 ssif_info->addr_info = addr_info->addr_info;
1721 addr_info->client = client;
1722 slave_addr = addr_info->slave_addr;
1726 ssif_info->client = client;
1727 i2c_set_clientdata(client, ssif_info);
1729 rv = ssif_check_and_remove(client, ssif_info);
1730 /* If rv is 0 and addr source is not SI_ACPI, continue probing */
1731 if (!rv && ssif_info->addr_source == SI_ACPI) {
1732 rv = ssif_add_infos(client);
1734 dev_err(&client->dev, "Out of memory!, exiting ..\n");
1738 dev_err(&client->dev, "Not probing, Interface already present\n");
1742 slave_addr = find_slave_address(client, slave_addr);
1744 dev_info(&client->dev,
1745 "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1746 ipmi_addr_src_to_str(ssif_info->addr_source),
1747 client->addr, client->adapter->name, slave_addr);
1749 /* Now check for system interface capabilities */
1750 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1751 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1752 msg[2] = 0; /* SSIF */
1753 rv = do_cmd(client, 3, msg, &len, resp);
1754 if (!rv && (len >= 3) && (resp[2] == 0)) {
1757 dev_dbg(&ssif_info->client->dev,
1758 "SSIF info too short: %d\n", len);
1762 /* Got a good SSIF response, handle it. */
1763 ssif_info->max_xmit_msg_size = resp[5];
1764 ssif_info->max_recv_msg_size = resp[6];
1765 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1766 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1768 /* Sanitize the data */
1769 switch (ssif_info->multi_support) {
1771 if (ssif_info->max_xmit_msg_size > 32)
1772 ssif_info->max_xmit_msg_size = 32;
1773 if (ssif_info->max_recv_msg_size > 32)
1774 ssif_info->max_recv_msg_size = 32;
1777 case SSIF_MULTI_2_PART:
1778 if (ssif_info->max_xmit_msg_size > 63)
1779 ssif_info->max_xmit_msg_size = 63;
1780 if (ssif_info->max_recv_msg_size > 62)
1781 ssif_info->max_recv_msg_size = 62;
1784 case SSIF_MULTI_n_PART:
1785 /* We take whatever size given, but do some testing. */
1789 /* Data is not sane, just give up. */
1794 /* Assume no multi-part or PEC support */
1795 dev_info(&ssif_info->client->dev,
1796 "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1799 ssif_info->max_xmit_msg_size = 32;
1800 ssif_info->max_recv_msg_size = 32;
1801 ssif_info->multi_support = SSIF_NO_MULTI;
1802 ssif_info->supports_pec = 0;
1805 test_multipart_messages(client, ssif_info, resp);
1807 /* Make sure the NMI timeout is cleared. */
1808 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1809 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1810 msg[2] = WDT_PRE_TIMEOUT_INT;
1811 rv = do_cmd(client, 3, msg, &len, resp);
1812 if (rv || (len < 3) || (resp[2] != 0))
1813 dev_warn(&ssif_info->client->dev,
1814 "Unable to clear message flags: %d %d %2.2x\n",
1817 /* Attempt to enable the event buffer. */
1818 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1819 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1820 rv = do_cmd(client, 2, msg, &len, resp);
1821 if (rv || (len < 4) || (resp[2] != 0)) {
1822 dev_warn(&ssif_info->client->dev,
1823 "Error getting global enables: %d %d %2.2x\n",
1825 rv = 0; /* Not fatal */
1829 ssif_info->global_enables = resp[3];
1831 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1832 ssif_info->has_event_buffer = true;
1833 /* buffer is already enabled, nothing to do. */
1837 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1838 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1839 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1840 rv = do_cmd(client, 3, msg, &len, resp);
1841 if (rv || (len < 2)) {
1842 dev_warn(&ssif_info->client->dev,
1843 "Error setting global enables: %d %d %2.2x\n",
1845 rv = 0; /* Not fatal */
1850 /* A successful return means the event buffer is supported. */
1851 ssif_info->has_event_buffer = true;
1852 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1855 /* Some systems don't behave well if you enable alerts. */
1859 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1860 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1861 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1862 rv = do_cmd(client, 3, msg, &len, resp);
1863 if (rv || (len < 2)) {
1864 dev_warn(&ssif_info->client->dev,
1865 "Error setting global enables: %d %d %2.2x\n",
1867 rv = 0; /* Not fatal */
1872 /* A successful return means the alert is supported. */
1873 ssif_info->supports_alert = true;
1874 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1878 if (ssif_dbg_probe) {
1879 dev_dbg(&ssif_info->client->dev,
1880 "%s: i2c_probe found device at i2c address %x\n",
1881 __func__, client->addr);
1884 spin_lock_init(&ssif_info->lock);
1885 ssif_info->ssif_state = SSIF_NORMAL;
1886 timer_setup(&ssif_info->retry_timer, retry_timeout, 0);
1887 timer_setup(&ssif_info->watch_timer, watch_timeout, 0);
1889 for (i = 0; i < SSIF_NUM_STATS; i++)
1890 atomic_set(&ssif_info->stats[i], 0);
1892 if (ssif_info->supports_pec)
1893 ssif_info->client->flags |= I2C_CLIENT_PEC;
1895 ssif_info->handlers.owner = THIS_MODULE;
1896 ssif_info->handlers.start_processing = ssif_start_processing;
1897 ssif_info->handlers.shutdown = shutdown_ssif;
1898 ssif_info->handlers.get_smi_info = get_smi_info;
1899 ssif_info->handlers.sender = sender;
1900 ssif_info->handlers.request_events = request_events;
1901 ssif_info->handlers.set_need_watch = ssif_set_need_watch;
1904 unsigned int thread_num;
1906 thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1908 ssif_info->client->addr);
1909 init_completion(&ssif_info->wake_thread);
1910 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1911 "kssif%4.4x", thread_num);
1912 if (IS_ERR(ssif_info->thread)) {
1913 rv = PTR_ERR(ssif_info->thread);
1914 dev_notice(&ssif_info->client->dev,
1915 "Could not start kernel thread: error %d\n",
1921 dev_set_drvdata(&ssif_info->client->dev, ssif_info);
1922 rv = device_add_group(&ssif_info->client->dev,
1923 &ipmi_ssif_dev_attr_group);
1925 dev_err(&ssif_info->client->dev,
1926 "Unable to add device attributes: error %d\n",
1931 rv = ipmi_register_smi(&ssif_info->handlers,
1933 &ssif_info->client->dev,
1936 dev_err(&ssif_info->client->dev,
1937 "Unable to register device: error %d\n", rv);
1938 goto out_remove_attr;
1944 addr_info->client = NULL;
1946 dev_err(&ssif_info->client->dev,
1947 "Unable to start IPMI SSIF: %d\n", rv);
1948 i2c_set_clientdata(client, NULL);
1952 mutex_unlock(&ssif_infos_mutex);
1956 device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1957 dev_set_drvdata(&ssif_info->client->dev, NULL);
1961 static int new_ssif_client(int addr, char *adapter_name,
1962 int debug, int slave_addr,
1963 enum ipmi_addr_src addr_src,
1966 struct ssif_addr_info *addr_info;
1969 mutex_lock(&ssif_infos_mutex);
1970 if (ssif_info_find(addr, adapter_name, false)) {
1975 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1982 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1983 if (!addr_info->adapter_name) {
1990 strncpy(addr_info->binfo.type, DEVICE_NAME,
1991 sizeof(addr_info->binfo.type));
1992 addr_info->binfo.addr = addr;
1993 addr_info->binfo.platform_data = addr_info;
1994 addr_info->debug = debug;
1995 addr_info->slave_addr = slave_addr;
1996 addr_info->addr_src = addr_src;
1997 addr_info->dev = dev;
2000 dev_set_drvdata(dev, addr_info);
2002 list_add_tail(&addr_info->link, &ssif_infos);
2004 /* Address list will get it */
2007 mutex_unlock(&ssif_infos_mutex);
2011 static void free_ssif_clients(void)
2013 struct ssif_addr_info *info, *tmp;
2015 mutex_lock(&ssif_infos_mutex);
2016 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
2017 list_del(&info->link);
2018 kfree(info->adapter_name);
2021 mutex_unlock(&ssif_infos_mutex);
2024 static unsigned short *ssif_address_list(void)
2026 struct ssif_addr_info *info;
2027 unsigned int count = 0, i = 0;
2028 unsigned short *address_list;
2030 list_for_each_entry(info, &ssif_infos, link)
2033 address_list = kcalloc(count + 1, sizeof(*address_list),
2038 list_for_each_entry(info, &ssif_infos, link) {
2039 unsigned short addr = info->binfo.addr;
2042 for (j = 0; j < i; j++) {
2043 if (address_list[j] == addr)
2047 if (j == i) /* Didn't find it in the list. */
2048 address_list[i++] = addr;
2050 address_list[i] = I2C_CLIENT_END;
2052 return address_list;
2056 static const struct acpi_device_id ssif_acpi_match[] = {
2060 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
2064 static int dmi_ipmi_probe(struct platform_device *pdev)
2073 rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
2075 dev_warn(&pdev->dev, "No i2c-addr property\n");
2079 rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
2083 return new_ssif_client(i2c_addr, NULL, 0,
2084 slave_addr, SI_SMBIOS, &pdev->dev);
2087 static int dmi_ipmi_probe(struct platform_device *pdev)
2093 static const struct i2c_device_id ssif_id[] = {
2097 MODULE_DEVICE_TABLE(i2c, ssif_id);
2099 static struct i2c_driver ssif_i2c_driver = {
2100 .class = I2C_CLASS_HWMON,
2104 .probe = ssif_probe,
2105 .remove = ssif_remove,
2106 .alert = ssif_alert,
2107 .id_table = ssif_id,
2108 .detect = ssif_detect
2111 static int ssif_platform_probe(struct platform_device *dev)
2113 return dmi_ipmi_probe(dev);
2116 static int ssif_platform_remove(struct platform_device *dev)
2118 struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2123 mutex_lock(&ssif_infos_mutex);
2124 list_del(&addr_info->link);
2126 mutex_unlock(&ssif_infos_mutex);
2130 static const struct platform_device_id ssif_plat_ids[] = {
2131 { "dmi-ipmi-ssif", 0 },
2135 static struct platform_driver ipmi_driver = {
2137 .name = DEVICE_NAME,
2139 .probe = ssif_platform_probe,
2140 .remove = ssif_platform_remove,
2141 .id_table = ssif_plat_ids
2144 static int init_ipmi_ssif(void)
2152 pr_info("IPMI SSIF Interface driver\n");
2154 /* build list for i2c from addr list */
2155 for (i = 0; i < num_addrs; i++) {
2156 rv = new_ssif_client(addr[i], adapter_name[i],
2157 dbg[i], slave_addrs[i],
2158 SI_HARDCODED, NULL);
2160 pr_err("Couldn't add hardcoded device at addr 0x%x\n",
2165 ssif_i2c_driver.driver.acpi_match_table =
2166 ACPI_PTR(ssif_acpi_match);
2169 rv = platform_driver_register(&ipmi_driver);
2171 pr_err("Unable to register driver: %d\n", rv);
2173 platform_registered = true;
2176 ssif_i2c_driver.address_list = ssif_address_list();
2178 rv = i2c_add_driver(&ssif_i2c_driver);
2184 module_init(init_ipmi_ssif);
2186 static void cleanup_ipmi_ssif(void)
2191 initialized = false;
2193 i2c_del_driver(&ssif_i2c_driver);
2195 kfree(ssif_i2c_driver.address_list);
2197 if (ssif_trydmi && platform_registered)
2198 platform_driver_unregister(&ipmi_driver);
2200 free_ssif_clients();
2202 module_exit(cleanup_ipmi_ssif);
2204 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2205 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2206 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2207 MODULE_LICENSE("GPL");