4 * The interface to the IPMI driver for SMBus access to a SMBus
5 * compliant device. Called SSIF by the IPMI spec.
7 * Author: Intel Corporation
8 * Todd Davis <todd.c.davis@intel.com>
10 * Rewritten by Corey Minyard <minyard@acm.org> to support the
11 * non-blocking I2C interface, add support for multi-part
12 * transactions, add PEC support, and general clenaup.
14 * Copyright 2003 Intel Corporation
15 * Copyright 2005 MontaVista Software
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 of the License, or (at your
20 * option) any later version.
24 * This file holds the "policy" for the interface to the SSIF state
25 * machine. It does the configuration, handles timers and interrupts,
26 * and drives the real SSIF state machine.
30 * TODO: Figure out how to use SMB alerts. This will require a new
31 * interface into the I2C driver, I believe.
34 #if defined(MODVERSIONS)
35 #include <linux/modversions.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/sched.h>
41 #include <linux/seq_file.h>
42 #include <linux/timer.h>
43 #include <linux/delay.h>
44 #include <linux/errno.h>
45 #include <linux/spinlock.h>
46 #include <linux/slab.h>
47 #include <linux/list.h>
48 #include <linux/i2c.h>
49 #include <linux/ipmi_smi.h>
50 #include <linux/init.h>
51 #include <linux/dmi.h>
52 #include <linux/kthread.h>
53 #include <linux/acpi.h>
54 #include <linux/ctype.h>
55 #include <linux/time64.h>
57 #define PFX "ipmi_ssif: "
58 #define DEVICE_NAME "ipmi_ssif"
60 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
62 #define SSIF_IPMI_REQUEST 2
63 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
64 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
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 {
178 struct i2c_board_info binfo;
182 enum ipmi_addr_src addr_src;
183 union ipmi_smi_info_union addr_info;
185 struct mutex clients_mutex;
186 struct list_head clients;
188 struct list_head link;
193 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
194 unsigned char *data, unsigned int len);
200 struct ipmi_smi_msg *waiting_msg;
201 struct ipmi_smi_msg *curr_msg;
202 enum ssif_intf_state ssif_state;
203 unsigned long ssif_debug;
205 struct ipmi_smi_handlers handlers;
207 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
208 union ipmi_smi_info_union addr_info;
211 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
212 * is set to hold the flags until we are done handling everything
215 #define RECEIVE_MSG_AVAIL 0x01
216 #define EVENT_MSG_BUFFER_FULL 0x02
217 #define WDT_PRE_TIMEOUT_INT 0x08
218 unsigned char msg_flags;
221 bool has_event_buffer;
225 * Used to tell what we should do with alerts. If we are
226 * waiting on a response, read the data immediately.
232 * If set to true, this will request events the next time the
233 * state machine is idle.
238 * If set to true, this will request flags the next time the
239 * state machine is idle.
244 * Used to perform timer operations when run-to-completion
245 * mode is on. This is a countdown timer.
249 /* Used for sending/receiving data. +1 for the length. */
250 unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
251 unsigned int data_len;
253 /* Temp receive buffer, gets copied into data. */
254 unsigned char recv[I2C_SMBUS_BLOCK_MAX];
256 struct i2c_client *client;
257 ssif_i2c_done done_handler;
259 /* Thread interface handling */
260 struct task_struct *thread;
261 struct completion wake_thread;
265 unsigned char *i2c_data;
266 unsigned int i2c_size;
268 /* From the device id response. */
269 struct ipmi_device_id device_id;
271 struct timer_list retry_timer;
274 /* Info from SSIF cmd */
275 unsigned char max_xmit_msg_size;
276 unsigned char max_recv_msg_size;
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 atomic_t next_intf = ATOMIC_INIT(0);
299 static void return_hosed_msg(struct ssif_info *ssif_info,
300 struct ipmi_smi_msg *msg);
301 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
302 static int start_send(struct ssif_info *ssif_info,
306 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
307 unsigned long *flags)
309 spin_lock_irqsave(&ssif_info->lock, *flags);
313 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
314 unsigned long *flags)
316 spin_unlock_irqrestore(&ssif_info->lock, *flags);
319 static void deliver_recv_msg(struct ssif_info *ssif_info,
320 struct ipmi_smi_msg *msg)
322 ipmi_smi_t intf = ssif_info->intf;
325 ipmi_free_smi_msg(msg);
326 } else if (msg->rsp_size < 0) {
327 return_hosed_msg(ssif_info, msg);
329 "Malformed message in deliver_recv_msg: rsp_size = %d\n",
332 ipmi_smi_msg_received(intf, msg);
336 static void return_hosed_msg(struct ssif_info *ssif_info,
337 struct ipmi_smi_msg *msg)
339 ssif_inc_stat(ssif_info, hosed);
341 /* Make it a response */
342 msg->rsp[0] = msg->data[0] | 4;
343 msg->rsp[1] = msg->data[1];
344 msg->rsp[2] = 0xFF; /* Unknown error. */
347 deliver_recv_msg(ssif_info, msg);
351 * Must be called with the message lock held. This will release the
352 * message lock. Note that the caller will check SSIF_IDLE and start a
353 * new operation, so there is no need to check for new messages to
356 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
358 unsigned char msg[3];
360 ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
361 ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
362 ipmi_ssif_unlock_cond(ssif_info, flags);
364 /* Make sure the watchdog pre-timeout flag is not set at startup. */
365 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
366 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
367 msg[2] = WDT_PRE_TIMEOUT_INT;
369 if (start_send(ssif_info, msg, 3) != 0) {
370 /* Error, just go to normal state. */
371 ssif_info->ssif_state = SSIF_NORMAL;
375 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
379 ssif_info->req_flags = false;
380 ssif_info->ssif_state = SSIF_GETTING_FLAGS;
381 ipmi_ssif_unlock_cond(ssif_info, flags);
383 mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
384 mb[1] = IPMI_GET_MSG_FLAGS_CMD;
385 if (start_send(ssif_info, mb, 2) != 0)
386 ssif_info->ssif_state = SSIF_NORMAL;
389 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
390 struct ipmi_smi_msg *msg)
392 if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
393 unsigned long oflags;
395 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
396 ssif_info->curr_msg = NULL;
397 ssif_info->ssif_state = SSIF_NORMAL;
398 ipmi_ssif_unlock_cond(ssif_info, flags);
399 ipmi_free_smi_msg(msg);
403 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
405 struct ipmi_smi_msg *msg;
407 ssif_info->req_events = false;
409 msg = ipmi_alloc_smi_msg();
411 ssif_info->ssif_state = SSIF_NORMAL;
412 ipmi_ssif_unlock_cond(ssif_info, flags);
416 ssif_info->curr_msg = msg;
417 ssif_info->ssif_state = SSIF_GETTING_EVENTS;
418 ipmi_ssif_unlock_cond(ssif_info, flags);
420 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
421 msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
424 check_start_send(ssif_info, flags, msg);
427 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
428 unsigned long *flags)
430 struct ipmi_smi_msg *msg;
432 msg = ipmi_alloc_smi_msg();
434 ssif_info->ssif_state = SSIF_NORMAL;
435 ipmi_ssif_unlock_cond(ssif_info, flags);
439 ssif_info->curr_msg = msg;
440 ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
441 ipmi_ssif_unlock_cond(ssif_info, flags);
443 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
444 msg->data[1] = IPMI_GET_MSG_CMD;
447 check_start_send(ssif_info, flags, msg);
451 * Must be called with the message lock held. This will release the
452 * message lock. Note that the caller will check SSIF_IDLE and start a
453 * new operation, so there is no need to check for new messages to
456 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
458 if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
459 ipmi_smi_t intf = ssif_info->intf;
460 /* Watchdog pre-timeout */
461 ssif_inc_stat(ssif_info, watchdog_pretimeouts);
462 start_clear_flags(ssif_info, flags);
464 ipmi_smi_watchdog_pretimeout(intf);
465 } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
466 /* Messages available. */
467 start_recv_msg_fetch(ssif_info, flags);
468 else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
469 /* Events available. */
470 start_event_fetch(ssif_info, flags);
472 ssif_info->ssif_state = SSIF_NORMAL;
473 ipmi_ssif_unlock_cond(ssif_info, flags);
477 static int ipmi_ssif_thread(void *data)
479 struct ssif_info *ssif_info = data;
481 while (!kthread_should_stop()) {
484 /* Wait for something to do */
485 result = wait_for_completion_interruptible(
486 &ssif_info->wake_thread);
487 if (ssif_info->stopping)
489 if (result == -ERESTARTSYS)
491 init_completion(&ssif_info->wake_thread);
493 if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
494 result = i2c_smbus_write_block_data(
495 ssif_info->client, ssif_info->i2c_command,
496 ssif_info->i2c_data[0],
497 ssif_info->i2c_data + 1);
498 ssif_info->done_handler(ssif_info, result, NULL, 0);
500 result = i2c_smbus_read_block_data(
501 ssif_info->client, ssif_info->i2c_command,
502 ssif_info->i2c_data);
504 ssif_info->done_handler(ssif_info, result,
507 ssif_info->done_handler(ssif_info, 0,
516 static int ssif_i2c_send(struct ssif_info *ssif_info,
517 ssif_i2c_done handler,
518 int read_write, int command,
519 unsigned char *data, unsigned int size)
521 ssif_info->done_handler = handler;
523 ssif_info->i2c_read_write = read_write;
524 ssif_info->i2c_command = command;
525 ssif_info->i2c_data = data;
526 ssif_info->i2c_size = size;
527 complete(&ssif_info->wake_thread);
532 static void msg_done_handler(struct ssif_info *ssif_info, int result,
533 unsigned char *data, unsigned int len);
535 static void start_get(struct ssif_info *ssif_info)
539 ssif_info->rtc_us_timer = 0;
540 ssif_info->multi_pos = 0;
542 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
544 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
546 /* request failed, just return the error. */
547 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
548 pr_info("Error from i2c_non_blocking_op(5)\n");
550 msg_done_handler(ssif_info, -EIO, NULL, 0);
554 static void retry_timeout(unsigned long data)
556 struct ssif_info *ssif_info = (void *) data;
557 unsigned long oflags, *flags;
560 if (ssif_info->stopping)
563 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
564 waiting = ssif_info->waiting_alert;
565 ssif_info->waiting_alert = false;
566 ipmi_ssif_unlock_cond(ssif_info, flags);
569 start_get(ssif_info);
573 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
576 struct ssif_info *ssif_info = i2c_get_clientdata(client);
577 unsigned long oflags, *flags;
580 if (type != I2C_PROTOCOL_SMBUS_ALERT)
583 ssif_inc_stat(ssif_info, alerts);
585 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
586 if (ssif_info->waiting_alert) {
587 ssif_info->waiting_alert = false;
588 del_timer(&ssif_info->retry_timer);
590 } else if (ssif_info->curr_msg) {
591 ssif_info->got_alert = true;
593 ipmi_ssif_unlock_cond(ssif_info, flags);
595 start_get(ssif_info);
598 static int start_resend(struct ssif_info *ssif_info);
600 static void msg_done_handler(struct ssif_info *ssif_info, int result,
601 unsigned char *data, unsigned int len)
603 struct ipmi_smi_msg *msg;
604 unsigned long oflags, *flags;
608 * We are single-threaded here, so no need for a lock until we
609 * start messing with driver states or the queues.
613 ssif_info->retries_left--;
614 if (ssif_info->retries_left > 0) {
615 ssif_inc_stat(ssif_info, receive_retries);
617 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
618 ssif_info->waiting_alert = true;
619 ssif_info->rtc_us_timer = SSIF_MSG_USEC;
620 if (!ssif_info->stopping)
621 mod_timer(&ssif_info->retry_timer,
622 jiffies + SSIF_MSG_JIFFIES);
623 ipmi_ssif_unlock_cond(ssif_info, flags);
627 ssif_inc_stat(ssif_info, receive_errors);
629 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
630 pr_info("Error in msg_done_handler: %d\n", result);
635 if ((len > 1) && (ssif_info->multi_pos == 0)
636 && (data[0] == 0x00) && (data[1] == 0x01)) {
637 /* Start of multi-part read. Start the next transaction. */
640 ssif_inc_stat(ssif_info, received_message_parts);
642 /* Remove the multi-part read marker. */
645 for (i = 0; i < len; i++)
646 ssif_info->data[i] = data[i];
647 ssif_info->multi_len = len;
648 ssif_info->multi_pos = 1;
650 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
651 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
652 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
654 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
655 pr_info("Error from i2c_non_blocking_op(1)\n");
660 } else if (ssif_info->multi_pos) {
661 /* Middle of multi-part read. Start the next transaction. */
663 unsigned char blocknum;
667 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
668 pr_info(PFX "Middle message with no data\n");
677 if (blocknum != 0xff && len != 31) {
678 /* All blocks but the last must have 31 data bytes. */
680 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
681 pr_info("Received middle message <31\n");
686 if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
687 /* Received message too big, abort the operation. */
689 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
690 pr_info("Received message too big\n");
695 for (i = 0; i < len; i++)
696 ssif_info->data[i + ssif_info->multi_len] = data[i];
697 ssif_info->multi_len += len;
698 if (blocknum == 0xff) {
700 len = ssif_info->multi_len;
701 data = ssif_info->data;
702 } else if (blocknum + 1 != ssif_info->multi_pos) {
704 * Out of sequence block, just abort. Block
705 * numbers start at zero for the second block,
706 * but multi_pos starts at one, so the +1.
708 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
709 dev_dbg(&ssif_info->client->dev,
710 "Received message out of sequence, expected %u, got %u\n",
711 ssif_info->multi_pos - 1, blocknum);
714 ssif_inc_stat(ssif_info, received_message_parts);
716 ssif_info->multi_pos++;
718 rv = ssif_i2c_send(ssif_info, msg_done_handler,
720 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
722 I2C_SMBUS_BLOCK_DATA);
724 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
726 "Error from ssif_i2c_send\n");
736 ssif_inc_stat(ssif_info, receive_errors);
738 ssif_inc_stat(ssif_info, received_messages);
739 ssif_inc_stat(ssif_info, received_message_parts);
742 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
743 pr_info(PFX "DONE 1: state = %d, result=%d.\n",
744 ssif_info->ssif_state, result);
746 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
747 msg = ssif_info->curr_msg;
750 if (len > IPMI_MAX_MSG_LENGTH)
751 len = IPMI_MAX_MSG_LENGTH;
752 memcpy(msg->rsp, data, len);
757 ssif_info->curr_msg = NULL;
760 switch (ssif_info->ssif_state) {
762 ipmi_ssif_unlock_cond(ssif_info, flags);
767 return_hosed_msg(ssif_info, msg);
769 deliver_recv_msg(ssif_info, msg);
772 case SSIF_GETTING_FLAGS:
773 /* We got the flags from the SSIF, now handle them. */
774 if ((result < 0) || (len < 4) || (data[2] != 0)) {
776 * Error fetching flags, or invalid length,
777 * just give up for now.
779 ssif_info->ssif_state = SSIF_NORMAL;
780 ipmi_ssif_unlock_cond(ssif_info, flags);
781 pr_warn(PFX "Error getting flags: %d %d, %x\n",
782 result, len, (len >= 3) ? data[2] : 0);
783 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
784 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
786 * Don't abort here, maybe it was a queued
787 * response to a previous command.
789 ipmi_ssif_unlock_cond(ssif_info, flags);
790 pr_warn(PFX "Invalid response getting flags: %x %x\n",
793 ssif_inc_stat(ssif_info, flag_fetches);
794 ssif_info->msg_flags = data[3];
795 handle_flags(ssif_info, flags);
799 case SSIF_CLEARING_FLAGS:
800 /* We cleared the flags. */
801 if ((result < 0) || (len < 3) || (data[2] != 0)) {
802 /* Error clearing flags */
803 pr_warn(PFX "Error clearing flags: %d %d, %x\n",
804 result, len, (len >= 3) ? data[2] : 0);
805 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
806 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
807 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
810 ssif_info->ssif_state = SSIF_NORMAL;
811 ipmi_ssif_unlock_cond(ssif_info, flags);
814 case SSIF_GETTING_EVENTS:
815 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
816 /* Error getting event, probably done. */
819 /* Take off the event flag. */
820 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
821 handle_flags(ssif_info, flags);
822 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
823 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
824 pr_warn(PFX "Invalid response getting events: %x %x\n",
825 msg->rsp[0], msg->rsp[1]);
827 /* Take off the event flag. */
828 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
829 handle_flags(ssif_info, flags);
831 handle_flags(ssif_info, flags);
832 ssif_inc_stat(ssif_info, events);
833 deliver_recv_msg(ssif_info, msg);
837 case SSIF_GETTING_MESSAGES:
838 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
839 /* Error getting event, probably done. */
842 /* Take off the msg flag. */
843 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
844 handle_flags(ssif_info, flags);
845 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
846 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
847 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
848 msg->rsp[0], msg->rsp[1]);
851 /* Take off the msg flag. */
852 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
853 handle_flags(ssif_info, flags);
855 ssif_inc_stat(ssif_info, incoming_messages);
856 handle_flags(ssif_info, flags);
857 deliver_recv_msg(ssif_info, msg);
862 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
863 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
864 if (ssif_info->req_events)
865 start_event_fetch(ssif_info, flags);
866 else if (ssif_info->req_flags)
867 start_flag_fetch(ssif_info, flags);
869 start_next_msg(ssif_info, flags);
871 ipmi_ssif_unlock_cond(ssif_info, flags);
873 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
874 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
877 static void msg_written_handler(struct ssif_info *ssif_info, int result,
878 unsigned char *data, unsigned int len)
882 /* We are single-threaded here, so no need for a lock. */
884 ssif_info->retries_left--;
885 if (ssif_info->retries_left > 0) {
886 if (!start_resend(ssif_info)) {
887 ssif_inc_stat(ssif_info, send_retries);
890 /* request failed, just return the error. */
891 ssif_inc_stat(ssif_info, send_errors);
893 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
895 "Out of retries in msg_written_handler\n");
896 msg_done_handler(ssif_info, -EIO, NULL, 0);
900 ssif_inc_stat(ssif_info, send_errors);
903 * Got an error on transmit, let the done routine
906 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
907 pr_info("Error in msg_written_handler: %d\n", result);
909 msg_done_handler(ssif_info, result, NULL, 0);
913 if (ssif_info->multi_data) {
915 * In the middle of a multi-data write. See the comment
916 * in the SSIF_MULTI_n_PART case in the probe function
917 * for details on the intricacies of this.
920 unsigned char *data_to_send;
922 ssif_inc_stat(ssif_info, sent_messages_parts);
924 left = ssif_info->multi_len - ssif_info->multi_pos;
928 ssif_info->multi_data[ssif_info->multi_pos] = left;
929 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
930 ssif_info->multi_pos += left;
933 * Write is finished. Note that we must end
934 * with a write of less than 32 bytes to
935 * complete the transaction, even if it is
938 ssif_info->multi_data = NULL;
940 rv = ssif_i2c_send(ssif_info, msg_written_handler,
942 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
944 I2C_SMBUS_BLOCK_DATA);
946 /* request failed, just return the error. */
947 ssif_inc_stat(ssif_info, send_errors);
949 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
950 pr_info("Error from i2c_non_blocking_op(3)\n");
951 msg_done_handler(ssif_info, -EIO, NULL, 0);
954 /* Ready to request the result. */
955 unsigned long oflags, *flags;
957 ssif_inc_stat(ssif_info, sent_messages);
958 ssif_inc_stat(ssif_info, sent_messages_parts);
960 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
961 if (ssif_info->got_alert) {
962 /* The result is already ready, just start it. */
963 ssif_info->got_alert = false;
964 ipmi_ssif_unlock_cond(ssif_info, flags);
965 start_get(ssif_info);
967 /* Wait a jiffie then request the next message */
968 ssif_info->waiting_alert = true;
969 ssif_info->retries_left = SSIF_RECV_RETRIES;
970 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
971 if (!ssif_info->stopping)
972 mod_timer(&ssif_info->retry_timer,
973 jiffies + SSIF_MSG_PART_JIFFIES);
974 ipmi_ssif_unlock_cond(ssif_info, flags);
979 static int start_resend(struct ssif_info *ssif_info)
984 ssif_info->got_alert = false;
986 if (ssif_info->data_len > 32) {
987 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
988 ssif_info->multi_data = ssif_info->data;
989 ssif_info->multi_len = ssif_info->data_len;
991 * Subtle thing, this is 32, not 33, because we will
992 * overwrite the thing at position 32 (which was just
993 * transmitted) with the new length.
995 ssif_info->multi_pos = 32;
996 ssif_info->data[0] = 32;
998 ssif_info->multi_data = NULL;
999 command = SSIF_IPMI_REQUEST;
1000 ssif_info->data[0] = ssif_info->data_len;
1003 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1004 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1005 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
1006 pr_info("Error from i2c_non_blocking_op(4)\n");
1010 static int start_send(struct ssif_info *ssif_info,
1011 unsigned char *data,
1014 if (len > IPMI_MAX_MSG_LENGTH)
1016 if (len > ssif_info->max_xmit_msg_size)
1019 ssif_info->retries_left = SSIF_SEND_RETRIES;
1020 memcpy(ssif_info->data + 1, data, len);
1021 ssif_info->data_len = len;
1022 return start_resend(ssif_info);
1025 /* Must be called with the message lock held. */
1026 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1028 struct ipmi_smi_msg *msg;
1029 unsigned long oflags;
1032 if (!SSIF_IDLE(ssif_info)) {
1033 ipmi_ssif_unlock_cond(ssif_info, flags);
1037 if (!ssif_info->waiting_msg) {
1038 ssif_info->curr_msg = NULL;
1039 ipmi_ssif_unlock_cond(ssif_info, flags);
1043 ssif_info->curr_msg = ssif_info->waiting_msg;
1044 ssif_info->waiting_msg = NULL;
1045 ipmi_ssif_unlock_cond(ssif_info, flags);
1046 rv = start_send(ssif_info,
1047 ssif_info->curr_msg->data,
1048 ssif_info->curr_msg->data_size);
1050 msg = ssif_info->curr_msg;
1051 ssif_info->curr_msg = NULL;
1052 return_hosed_msg(ssif_info, msg);
1053 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1059 static void sender(void *send_info,
1060 struct ipmi_smi_msg *msg)
1062 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1063 unsigned long oflags, *flags;
1065 BUG_ON(ssif_info->waiting_msg);
1066 ssif_info->waiting_msg = msg;
1068 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1069 start_next_msg(ssif_info, flags);
1071 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1072 struct timespec64 t;
1074 ktime_get_real_ts64(&t);
1075 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1076 msg->data[0], msg->data[1],
1077 (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
1081 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1083 struct ssif_info *ssif_info = send_info;
1085 data->addr_src = ssif_info->addr_source;
1086 data->dev = &ssif_info->client->dev;
1087 data->addr_info = ssif_info->addr_info;
1088 get_device(data->dev);
1094 * Instead of having our own timer to periodically check the message
1095 * flags, we let the message handler drive us.
1097 static void request_events(void *send_info)
1099 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1100 unsigned long oflags, *flags;
1102 if (!ssif_info->has_event_buffer)
1105 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1107 * Request flags first, not events, because the lower layer
1108 * doesn't have a way to send an attention. But make sure
1109 * event checking still happens.
1111 ssif_info->req_events = true;
1112 if (SSIF_IDLE(ssif_info))
1113 start_flag_fetch(ssif_info, flags);
1115 ssif_info->req_flags = true;
1116 ipmi_ssif_unlock_cond(ssif_info, flags);
1120 static int inc_usecount(void *send_info)
1122 struct ssif_info *ssif_info = send_info;
1124 if (!i2c_get_adapter(ssif_info->client->adapter->nr))
1127 i2c_use_client(ssif_info->client);
1131 static void dec_usecount(void *send_info)
1133 struct ssif_info *ssif_info = send_info;
1135 i2c_release_client(ssif_info->client);
1136 i2c_put_adapter(ssif_info->client->adapter);
1139 static int ssif_start_processing(void *send_info,
1142 struct ssif_info *ssif_info = send_info;
1144 ssif_info->intf = intf;
1149 #define MAX_SSIF_BMCS 4
1151 static unsigned short addr[MAX_SSIF_BMCS];
1152 static int num_addrs;
1153 module_param_array(addr, ushort, &num_addrs, 0);
1154 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1156 static char *adapter_name[MAX_SSIF_BMCS];
1157 static int num_adapter_names;
1158 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1159 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1161 static int slave_addrs[MAX_SSIF_BMCS];
1162 static int num_slave_addrs;
1163 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1164 MODULE_PARM_DESC(slave_addrs,
1165 "The default IPMB slave address for the controller.");
1167 static bool alerts_broken;
1168 module_param(alerts_broken, bool, 0);
1169 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1172 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1173 * bit 2 enables timing debugging. This is an array indexed by
1176 static int dbg[MAX_SSIF_BMCS];
1178 module_param_array(dbg, int, &num_dbg, 0);
1179 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1181 static bool ssif_dbg_probe;
1182 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1183 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1185 static int use_thread;
1186 module_param(use_thread, int, 0);
1187 MODULE_PARM_DESC(use_thread, "Use the thread interface.");
1189 static bool ssif_tryacpi = true;
1190 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1191 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1193 static bool ssif_trydmi = true;
1194 module_param_named(trydmi, ssif_trydmi, bool, 0);
1195 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1197 static DEFINE_MUTEX(ssif_infos_mutex);
1198 static LIST_HEAD(ssif_infos);
1200 static int ssif_remove(struct i2c_client *client)
1202 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1209 * After this point, we won't deliver anything asychronously
1210 * to the message handler. We can unregister ourself.
1212 rv = ipmi_unregister_smi(ssif_info->intf);
1214 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1217 ssif_info->intf = NULL;
1219 /* make sure the driver is not looking for flags any more. */
1220 while (ssif_info->ssif_state != SSIF_NORMAL)
1221 schedule_timeout(1);
1223 ssif_info->stopping = true;
1224 del_timer_sync(&ssif_info->retry_timer);
1225 if (ssif_info->thread) {
1226 complete(&ssif_info->wake_thread);
1227 kthread_stop(ssif_info->thread);
1231 * No message can be outstanding now, we have removed the
1232 * upper layer and it permitted us to do so.
1238 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1239 int *resp_len, unsigned char *resp)
1244 retry_cnt = SSIF_SEND_RETRIES;
1246 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1255 retry_cnt = SSIF_RECV_RETRIES;
1256 while (retry_cnt > 0) {
1257 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1261 msleep(SSIF_MSG_MSEC);
1268 /* Validate that the response is correct. */
1270 (resp[0] != (msg[0] | (1 << 2))) ||
1271 (resp[1] != msg[1]))
1282 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1284 unsigned char *resp;
1285 unsigned char msg[3];
1289 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1293 /* Do a Get Device ID command, since it is required. */
1294 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1295 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1296 rv = do_cmd(client, 2, msg, &len, resp);
1300 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1305 static int smi_type_proc_show(struct seq_file *m, void *v)
1307 seq_puts(m, "ssif\n");
1312 static int smi_type_proc_open(struct inode *inode, struct file *file)
1314 return single_open(file, smi_type_proc_show, inode->i_private);
1317 static const struct file_operations smi_type_proc_ops = {
1318 .open = smi_type_proc_open,
1320 .llseek = seq_lseek,
1321 .release = single_release,
1324 static int smi_stats_proc_show(struct seq_file *m, void *v)
1326 struct ssif_info *ssif_info = m->private;
1328 seq_printf(m, "sent_messages: %u\n",
1329 ssif_get_stat(ssif_info, sent_messages));
1330 seq_printf(m, "sent_messages_parts: %u\n",
1331 ssif_get_stat(ssif_info, sent_messages_parts));
1332 seq_printf(m, "send_retries: %u\n",
1333 ssif_get_stat(ssif_info, send_retries));
1334 seq_printf(m, "send_errors: %u\n",
1335 ssif_get_stat(ssif_info, send_errors));
1336 seq_printf(m, "received_messages: %u\n",
1337 ssif_get_stat(ssif_info, received_messages));
1338 seq_printf(m, "received_message_parts: %u\n",
1339 ssif_get_stat(ssif_info, received_message_parts));
1340 seq_printf(m, "receive_retries: %u\n",
1341 ssif_get_stat(ssif_info, receive_retries));
1342 seq_printf(m, "receive_errors: %u\n",
1343 ssif_get_stat(ssif_info, receive_errors));
1344 seq_printf(m, "flag_fetches: %u\n",
1345 ssif_get_stat(ssif_info, flag_fetches));
1346 seq_printf(m, "hosed: %u\n",
1347 ssif_get_stat(ssif_info, hosed));
1348 seq_printf(m, "events: %u\n",
1349 ssif_get_stat(ssif_info, events));
1350 seq_printf(m, "watchdog_pretimeouts: %u\n",
1351 ssif_get_stat(ssif_info, watchdog_pretimeouts));
1352 seq_printf(m, "alerts: %u\n",
1353 ssif_get_stat(ssif_info, alerts));
1357 static int smi_stats_proc_open(struct inode *inode, struct file *file)
1359 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1362 static const struct file_operations smi_stats_proc_ops = {
1363 .open = smi_stats_proc_open,
1365 .llseek = seq_lseek,
1366 .release = single_release,
1369 static int strcmp_nospace(char *s1, char *s2)
1371 while (*s1 && *s2) {
1372 while (isspace(*s1))
1374 while (isspace(*s2))
1386 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1388 bool match_null_name)
1390 struct ssif_addr_info *info, *found = NULL;
1393 list_for_each_entry(info, &ssif_infos, link) {
1394 if (info->binfo.addr == addr) {
1395 if (info->adapter_name || adapter_name) {
1396 if (!info->adapter_name != !adapter_name) {
1397 /* One is NULL and one is not */
1401 strcmp_nospace(info->adapter_name,
1403 /* Names do not match */
1411 if (!found && match_null_name) {
1412 /* Try to get an exact match first, then try with a NULL name */
1413 adapter_name = NULL;
1414 match_null_name = false;
1421 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1424 acpi_handle acpi_handle;
1426 acpi_handle = ACPI_HANDLE(dev);
1428 ssif_info->addr_source = SI_ACPI;
1429 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1437 * Global enables we care about.
1439 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1440 IPMI_BMC_EVT_MSG_INTR)
1442 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1444 unsigned char msg[3];
1445 unsigned char *resp;
1446 struct ssif_info *ssif_info;
1451 struct ssif_addr_info *addr_info = NULL;
1454 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1458 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1464 if (!check_acpi(ssif_info, &client->dev)) {
1465 addr_info = ssif_info_find(client->addr, client->adapter->name,
1468 /* Must have come in through sysfs. */
1469 ssif_info->addr_source = SI_HOTMOD;
1471 ssif_info->addr_source = addr_info->addr_src;
1472 ssif_info->ssif_debug = addr_info->debug;
1473 ssif_info->addr_info = addr_info->addr_info;
1474 slave_addr = addr_info->slave_addr;
1478 pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1479 ipmi_addr_src_to_str(ssif_info->addr_source),
1480 client->addr, client->adapter->name, slave_addr);
1483 * Do a Get Device ID command, since it comes back with some
1486 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1487 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1488 rv = do_cmd(client, 2, msg, &len, resp);
1492 rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1496 ssif_info->client = client;
1497 i2c_set_clientdata(client, ssif_info);
1499 /* Now check for system interface capabilities */
1500 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1501 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1502 msg[2] = 0; /* SSIF */
1503 rv = do_cmd(client, 3, msg, &len, resp);
1504 if (!rv && (len >= 3) && (resp[2] == 0)) {
1507 pr_info(PFX "SSIF info too short: %d\n", len);
1511 /* Got a good SSIF response, handle it. */
1512 ssif_info->max_xmit_msg_size = resp[5];
1513 ssif_info->max_recv_msg_size = resp[6];
1514 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1515 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1517 /* Sanitize the data */
1518 switch (ssif_info->multi_support) {
1520 if (ssif_info->max_xmit_msg_size > 32)
1521 ssif_info->max_xmit_msg_size = 32;
1522 if (ssif_info->max_recv_msg_size > 32)
1523 ssif_info->max_recv_msg_size = 32;
1526 case SSIF_MULTI_2_PART:
1527 if (ssif_info->max_xmit_msg_size > 63)
1528 ssif_info->max_xmit_msg_size = 63;
1529 if (ssif_info->max_recv_msg_size > 62)
1530 ssif_info->max_recv_msg_size = 62;
1533 case SSIF_MULTI_n_PART:
1535 * The specification is rather confusing at
1536 * this point, but I think I understand what
1537 * is meant. At least I have a workable
1538 * solution. With multi-part messages, you
1539 * cannot send a message that is a multiple of
1540 * 32-bytes in length, because the start and
1541 * middle messages are 32-bytes and the end
1542 * message must be at least one byte. You
1543 * can't fudge on an extra byte, that would
1544 * screw up things like fru data writes. So
1545 * we limit the length to 63 bytes. That way
1546 * a 32-byte message gets sent as a single
1547 * part. A larger message will be a 32-byte
1548 * start and the next message is always going
1549 * to be 1-31 bytes in length. Not ideal, but
1552 if (ssif_info->max_xmit_msg_size > 63)
1553 ssif_info->max_xmit_msg_size = 63;
1557 /* Data is not sane, just give up. */
1562 /* Assume no multi-part or PEC support */
1563 pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1566 ssif_info->max_xmit_msg_size = 32;
1567 ssif_info->max_recv_msg_size = 32;
1568 ssif_info->multi_support = SSIF_NO_MULTI;
1569 ssif_info->supports_pec = 0;
1572 /* Make sure the NMI timeout is cleared. */
1573 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1574 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1575 msg[2] = WDT_PRE_TIMEOUT_INT;
1576 rv = do_cmd(client, 3, msg, &len, resp);
1577 if (rv || (len < 3) || (resp[2] != 0))
1578 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1581 /* Attempt to enable the event buffer. */
1582 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1583 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1584 rv = do_cmd(client, 2, msg, &len, resp);
1585 if (rv || (len < 4) || (resp[2] != 0)) {
1586 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1588 rv = 0; /* Not fatal */
1592 ssif_info->global_enables = resp[3];
1594 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1595 ssif_info->has_event_buffer = true;
1596 /* buffer is already enabled, nothing to do. */
1600 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1601 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1602 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1603 rv = do_cmd(client, 3, msg, &len, resp);
1604 if (rv || (len < 2)) {
1605 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1607 rv = 0; /* Not fatal */
1612 /* A successful return means the event buffer is supported. */
1613 ssif_info->has_event_buffer = true;
1614 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1617 /* Some systems don't behave well if you enable alerts. */
1621 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1622 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1623 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1624 rv = do_cmd(client, 3, msg, &len, resp);
1625 if (rv || (len < 2)) {
1626 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1628 rv = 0; /* Not fatal */
1633 /* A successful return means the alert is supported. */
1634 ssif_info->supports_alert = true;
1635 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1639 ssif_info->intf_num = atomic_inc_return(&next_intf);
1641 if (ssif_dbg_probe) {
1642 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1646 spin_lock_init(&ssif_info->lock);
1647 ssif_info->ssif_state = SSIF_NORMAL;
1648 init_timer(&ssif_info->retry_timer);
1649 ssif_info->retry_timer.data = (unsigned long) ssif_info;
1650 ssif_info->retry_timer.function = retry_timeout;
1652 for (i = 0; i < SSIF_NUM_STATS; i++)
1653 atomic_set(&ssif_info->stats[i], 0);
1655 if (ssif_info->supports_pec)
1656 ssif_info->client->flags |= I2C_CLIENT_PEC;
1658 ssif_info->handlers.owner = THIS_MODULE;
1659 ssif_info->handlers.start_processing = ssif_start_processing;
1660 ssif_info->handlers.get_smi_info = get_smi_info;
1661 ssif_info->handlers.sender = sender;
1662 ssif_info->handlers.request_events = request_events;
1663 ssif_info->handlers.inc_usecount = inc_usecount;
1664 ssif_info->handlers.dec_usecount = dec_usecount;
1667 unsigned int thread_num;
1669 thread_num = ((ssif_info->client->adapter->nr << 8) |
1670 ssif_info->client->addr);
1671 init_completion(&ssif_info->wake_thread);
1672 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1673 "kssif%4.4x", thread_num);
1674 if (IS_ERR(ssif_info->thread)) {
1675 rv = PTR_ERR(ssif_info->thread);
1676 dev_notice(&ssif_info->client->dev,
1677 "Could not start kernel thread: error %d\n",
1683 rv = ipmi_register_smi(&ssif_info->handlers,
1685 &ssif_info->device_id,
1686 &ssif_info->client->dev,
1689 pr_err(PFX "Unable to register device: error %d\n", rv);
1693 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1697 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1701 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1702 &smi_stats_proc_ops,
1705 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1716 ipmi_unregister_smi(ssif_info->intf);
1720 static int ssif_adapter_handler(struct device *adev, void *opaque)
1722 struct ssif_addr_info *addr_info = opaque;
1724 if (adev->type != &i2c_adapter_type)
1727 i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo);
1729 if (!addr_info->adapter_name)
1730 return 1; /* Only try the first I2C adapter by default. */
1734 static int new_ssif_client(int addr, char *adapter_name,
1735 int debug, int slave_addr,
1736 enum ipmi_addr_src addr_src)
1738 struct ssif_addr_info *addr_info;
1741 mutex_lock(&ssif_infos_mutex);
1742 if (ssif_info_find(addr, adapter_name, false)) {
1747 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1754 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1755 if (!addr_info->adapter_name) {
1762 strncpy(addr_info->binfo.type, DEVICE_NAME,
1763 sizeof(addr_info->binfo.type));
1764 addr_info->binfo.addr = addr;
1765 addr_info->binfo.platform_data = addr_info;
1766 addr_info->debug = debug;
1767 addr_info->slave_addr = slave_addr;
1768 addr_info->addr_src = addr_src;
1770 list_add_tail(&addr_info->link, &ssif_infos);
1773 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1774 /* Otherwise address list will get it */
1777 mutex_unlock(&ssif_infos_mutex);
1781 static void free_ssif_clients(void)
1783 struct ssif_addr_info *info, *tmp;
1785 mutex_lock(&ssif_infos_mutex);
1786 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1787 list_del(&info->link);
1788 kfree(info->adapter_name);
1791 mutex_unlock(&ssif_infos_mutex);
1794 static unsigned short *ssif_address_list(void)
1796 struct ssif_addr_info *info;
1797 unsigned int count = 0, i;
1798 unsigned short *address_list;
1800 list_for_each_entry(info, &ssif_infos, link)
1803 address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1808 list_for_each_entry(info, &ssif_infos, link) {
1809 unsigned short addr = info->binfo.addr;
1812 for (j = 0; j < i; j++) {
1813 if (address_list[j] == addr)
1816 address_list[i] = addr;
1820 address_list[i] = I2C_CLIENT_END;
1822 return address_list;
1826 static const struct acpi_device_id ssif_acpi_match[] = {
1830 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1833 * Once we get an ACPI failure, we don't try any more, because we go
1834 * through the tables sequentially. Once we don't find a table, there
1837 static int acpi_failure;
1840 * Defined in the IPMI 2.0 spec.
1851 s8 CreatorRevision[4];
1854 s16 SpecificationRevision;
1857 * Bit 0 - SCI interrupt supported
1858 * Bit 1 - I/O APIC/SAPIC
1863 * If bit 0 of InterruptType is set, then this is the SCI
1864 * interrupt in the GPEx_STS register.
1871 * If bit 1 of InterruptType is set, then this is the I/O
1872 * APIC/SAPIC interrupt.
1874 u32 GlobalSystemInterrupt;
1876 /* The actual register address. */
1877 struct acpi_generic_address addr;
1881 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1884 static int try_init_spmi(struct SPMITable *spmi)
1886 unsigned short myaddr;
1888 if (num_addrs >= MAX_SSIF_BMCS)
1891 if (spmi->IPMIlegacy != 1) {
1892 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1896 if (spmi->InterfaceType != 4)
1899 if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1900 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1901 spmi->addr.space_id);
1905 myaddr = spmi->addr.address & 0x7f;
1907 return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI);
1910 static void spmi_find_bmc(void)
1913 struct SPMITable *spmi;
1922 for (i = 0; ; i++) {
1923 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1924 (struct acpi_table_header **)&spmi);
1925 if (status != AE_OK)
1928 try_init_spmi(spmi);
1932 static void spmi_find_bmc(void) { }
1936 static int decode_dmi(const struct dmi_device *dmi_dev)
1938 struct dmi_header *dm = dmi_dev->device_data;
1939 u8 *data = (u8 *) dm;
1940 u8 len = dm->length;
1941 unsigned short myaddr;
1944 if (num_addrs >= MAX_SSIF_BMCS)
1950 if (data[0x04] != 4) /* Not SSIF */
1953 if ((data[8] >> 1) == 0) {
1955 * Some broken systems put the I2C address in
1956 * the slave address field. We try to
1957 * accommodate them here.
1959 myaddr = data[6] >> 1;
1962 myaddr = data[8] >> 1;
1963 slave_addr = data[6];
1966 return new_ssif_client(myaddr, NULL, 0, 0, SI_SMBIOS);
1969 static void dmi_iterator(void)
1971 const struct dmi_device *dev = NULL;
1973 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
1977 static void dmi_iterator(void) { }
1980 static const struct i2c_device_id ssif_id[] = {
1984 MODULE_DEVICE_TABLE(i2c, ssif_id);
1986 static struct i2c_driver ssif_i2c_driver = {
1987 .class = I2C_CLASS_HWMON,
1991 .probe = ssif_probe,
1992 .remove = ssif_remove,
1993 .alert = ssif_alert,
1994 .id_table = ssif_id,
1995 .detect = ssif_detect
1998 static int init_ipmi_ssif(void)
2006 pr_info("IPMI SSIF Interface driver\n");
2008 /* build list for i2c from addr list */
2009 for (i = 0; i < num_addrs; i++) {
2010 rv = new_ssif_client(addr[i], adapter_name[i],
2011 dbg[i], slave_addrs[i],
2015 "Couldn't add hardcoded device at addr 0x%x\n",
2020 ssif_i2c_driver.driver.acpi_match_table =
2021 ACPI_PTR(ssif_acpi_match);
2027 ssif_i2c_driver.address_list = ssif_address_list();
2029 rv = i2c_add_driver(&ssif_i2c_driver);
2035 module_init(init_ipmi_ssif);
2037 static void cleanup_ipmi_ssif(void)
2042 initialized = false;
2044 i2c_del_driver(&ssif_i2c_driver);
2046 free_ssif_clients();
2048 module_exit(cleanup_ipmi_ssif);
2050 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2051 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2052 MODULE_LICENSE("GPL");