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 {
177 struct i2c_board_info binfo;
181 enum ipmi_addr_src addr_src;
182 union ipmi_smi_info_union addr_info;
184 struct mutex clients_mutex;
185 struct list_head clients;
187 struct list_head link;
192 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
193 unsigned char *data, unsigned int len);
199 struct ipmi_smi_msg *waiting_msg;
200 struct ipmi_smi_msg *curr_msg;
201 enum ssif_intf_state ssif_state;
202 unsigned long ssif_debug;
204 struct ipmi_smi_handlers handlers;
206 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
207 union ipmi_smi_info_union addr_info;
210 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
211 * is set to hold the flags until we are done handling everything
214 #define RECEIVE_MSG_AVAIL 0x01
215 #define EVENT_MSG_BUFFER_FULL 0x02
216 #define WDT_PRE_TIMEOUT_INT 0x08
217 unsigned char msg_flags;
220 bool has_event_buffer;
224 * Used to tell what we should do with alerts. If we are
225 * waiting on a response, read the data immediately.
231 * If set to true, this will request events the next time the
232 * state machine is idle.
237 * If set to true, this will request flags the next time the
238 * state machine is idle.
243 * Used to perform timer operations when run-to-completion
244 * mode is on. This is a countdown timer.
248 /* Used for sending/receiving data. +1 for the length. */
249 unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
250 unsigned int data_len;
252 /* Temp receive buffer, gets copied into data. */
253 unsigned char recv[I2C_SMBUS_BLOCK_MAX];
255 struct i2c_client *client;
256 ssif_i2c_done done_handler;
258 /* Thread interface handling */
259 struct task_struct *thread;
260 struct completion wake_thread;
264 unsigned char *i2c_data;
265 unsigned int i2c_size;
267 /* From the device id response. */
268 struct ipmi_device_id device_id;
270 struct timer_list retry_timer;
273 /* Info from SSIF cmd */
274 unsigned char max_xmit_msg_size;
275 unsigned char max_recv_msg_size;
276 unsigned int multi_support;
279 #define SSIF_NO_MULTI 0
280 #define SSIF_MULTI_2_PART 1
281 #define SSIF_MULTI_n_PART 2
282 unsigned char *multi_data;
283 unsigned int multi_len;
284 unsigned int multi_pos;
286 atomic_t stats[SSIF_NUM_STATS];
289 #define ssif_inc_stat(ssif, stat) \
290 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
291 #define ssif_get_stat(ssif, stat) \
292 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
294 static bool initialized;
296 static atomic_t next_intf = ATOMIC_INIT(0);
298 static void return_hosed_msg(struct ssif_info *ssif_info,
299 struct ipmi_smi_msg *msg);
300 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
301 static int start_send(struct ssif_info *ssif_info,
305 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
306 unsigned long *flags)
308 spin_lock_irqsave(&ssif_info->lock, *flags);
312 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
313 unsigned long *flags)
315 spin_unlock_irqrestore(&ssif_info->lock, *flags);
318 static void deliver_recv_msg(struct ssif_info *ssif_info,
319 struct ipmi_smi_msg *msg)
321 ipmi_smi_t intf = ssif_info->intf;
324 ipmi_free_smi_msg(msg);
325 } else if (msg->rsp_size < 0) {
326 return_hosed_msg(ssif_info, msg);
328 "Malformed message in deliver_recv_msg: rsp_size = %d\n",
331 ipmi_smi_msg_received(intf, msg);
335 static void return_hosed_msg(struct ssif_info *ssif_info,
336 struct ipmi_smi_msg *msg)
338 ssif_inc_stat(ssif_info, hosed);
340 /* Make it a response */
341 msg->rsp[0] = msg->data[0] | 4;
342 msg->rsp[1] = msg->data[1];
343 msg->rsp[2] = 0xFF; /* Unknown error. */
346 deliver_recv_msg(ssif_info, msg);
350 * Must be called with the message lock held. This will release the
351 * message lock. Note that the caller will check SSIF_IDLE and start a
352 * new operation, so there is no need to check for new messages to
355 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
357 unsigned char msg[3];
359 ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
360 ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
361 ipmi_ssif_unlock_cond(ssif_info, flags);
363 /* Make sure the watchdog pre-timeout flag is not set at startup. */
364 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
365 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
366 msg[2] = WDT_PRE_TIMEOUT_INT;
368 if (start_send(ssif_info, msg, 3) != 0) {
369 /* Error, just go to normal state. */
370 ssif_info->ssif_state = SSIF_NORMAL;
374 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
378 ssif_info->req_flags = false;
379 ssif_info->ssif_state = SSIF_GETTING_FLAGS;
380 ipmi_ssif_unlock_cond(ssif_info, flags);
382 mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
383 mb[1] = IPMI_GET_MSG_FLAGS_CMD;
384 if (start_send(ssif_info, mb, 2) != 0)
385 ssif_info->ssif_state = SSIF_NORMAL;
388 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
389 struct ipmi_smi_msg *msg)
391 if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
392 unsigned long oflags;
394 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
395 ssif_info->curr_msg = NULL;
396 ssif_info->ssif_state = SSIF_NORMAL;
397 ipmi_ssif_unlock_cond(ssif_info, flags);
398 ipmi_free_smi_msg(msg);
402 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
404 struct ipmi_smi_msg *msg;
406 ssif_info->req_events = false;
408 msg = ipmi_alloc_smi_msg();
410 ssif_info->ssif_state = SSIF_NORMAL;
414 ssif_info->curr_msg = msg;
415 ssif_info->ssif_state = SSIF_GETTING_EVENTS;
416 ipmi_ssif_unlock_cond(ssif_info, flags);
418 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
419 msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
422 check_start_send(ssif_info, flags, msg);
425 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
426 unsigned long *flags)
428 struct ipmi_smi_msg *msg;
430 msg = ipmi_alloc_smi_msg();
432 ssif_info->ssif_state = SSIF_NORMAL;
436 ssif_info->curr_msg = msg;
437 ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
438 ipmi_ssif_unlock_cond(ssif_info, flags);
440 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
441 msg->data[1] = IPMI_GET_MSG_CMD;
444 check_start_send(ssif_info, flags, msg);
448 * Must be called with the message lock held. This will release the
449 * message lock. Note that the caller will check SSIF_IDLE and start a
450 * new operation, so there is no need to check for new messages to
453 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
455 if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
456 ipmi_smi_t intf = ssif_info->intf;
457 /* Watchdog pre-timeout */
458 ssif_inc_stat(ssif_info, watchdog_pretimeouts);
459 start_clear_flags(ssif_info, flags);
461 ipmi_smi_watchdog_pretimeout(intf);
462 } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
463 /* Messages available. */
464 start_recv_msg_fetch(ssif_info, flags);
465 else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
466 /* Events available. */
467 start_event_fetch(ssif_info, flags);
469 ssif_info->ssif_state = SSIF_NORMAL;
470 ipmi_ssif_unlock_cond(ssif_info, flags);
474 static int ipmi_ssif_thread(void *data)
476 struct ssif_info *ssif_info = data;
478 while (!kthread_should_stop()) {
481 /* Wait for something to do */
482 result = wait_for_completion_interruptible(
483 &ssif_info->wake_thread);
484 if (ssif_info->stopping)
486 if (result == -ERESTARTSYS)
488 init_completion(&ssif_info->wake_thread);
490 if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
491 result = i2c_smbus_write_block_data(
492 ssif_info->client, ssif_info->i2c_command,
493 ssif_info->i2c_data[0],
494 ssif_info->i2c_data + 1);
495 ssif_info->done_handler(ssif_info, result, NULL, 0);
497 result = i2c_smbus_read_block_data(
498 ssif_info->client, ssif_info->i2c_command,
499 ssif_info->i2c_data);
501 ssif_info->done_handler(ssif_info, result,
504 ssif_info->done_handler(ssif_info, 0,
513 static int ssif_i2c_send(struct ssif_info *ssif_info,
514 ssif_i2c_done handler,
515 int read_write, int command,
516 unsigned char *data, unsigned int size)
518 ssif_info->done_handler = handler;
520 ssif_info->i2c_read_write = read_write;
521 ssif_info->i2c_command = command;
522 ssif_info->i2c_data = data;
523 ssif_info->i2c_size = size;
524 complete(&ssif_info->wake_thread);
529 static void msg_done_handler(struct ssif_info *ssif_info, int result,
530 unsigned char *data, unsigned int len);
532 static void start_get(struct ssif_info *ssif_info)
536 ssif_info->rtc_us_timer = 0;
537 ssif_info->multi_pos = 0;
539 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
541 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
543 /* request failed, just return the error. */
544 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
545 pr_info("Error from i2c_non_blocking_op(5)\n");
547 msg_done_handler(ssif_info, -EIO, NULL, 0);
551 static void retry_timeout(unsigned long data)
553 struct ssif_info *ssif_info = (void *) data;
554 unsigned long oflags, *flags;
557 if (ssif_info->stopping)
560 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
561 waiting = ssif_info->waiting_alert;
562 ssif_info->waiting_alert = false;
563 ipmi_ssif_unlock_cond(ssif_info, flags);
566 start_get(ssif_info);
570 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
573 struct ssif_info *ssif_info = i2c_get_clientdata(client);
574 unsigned long oflags, *flags;
577 if (type != I2C_PROTOCOL_SMBUS_ALERT)
580 ssif_inc_stat(ssif_info, alerts);
582 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
583 if (ssif_info->waiting_alert) {
584 ssif_info->waiting_alert = false;
585 del_timer(&ssif_info->retry_timer);
587 } else if (ssif_info->curr_msg) {
588 ssif_info->got_alert = true;
590 ipmi_ssif_unlock_cond(ssif_info, flags);
592 start_get(ssif_info);
595 static int start_resend(struct ssif_info *ssif_info);
597 static void msg_done_handler(struct ssif_info *ssif_info, int result,
598 unsigned char *data, unsigned int len)
600 struct ipmi_smi_msg *msg;
601 unsigned long oflags, *flags;
605 * We are single-threaded here, so no need for a lock until we
606 * start messing with driver states or the queues.
610 ssif_info->retries_left--;
611 if (ssif_info->retries_left > 0) {
612 ssif_inc_stat(ssif_info, receive_retries);
614 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
615 ssif_info->waiting_alert = true;
616 ssif_info->rtc_us_timer = SSIF_MSG_USEC;
617 mod_timer(&ssif_info->retry_timer,
618 jiffies + SSIF_MSG_JIFFIES);
619 ipmi_ssif_unlock_cond(ssif_info, flags);
623 ssif_inc_stat(ssif_info, receive_errors);
625 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
626 pr_info("Error in msg_done_handler: %d\n", result);
631 if ((len > 1) && (ssif_info->multi_pos == 0)
632 && (data[0] == 0x00) && (data[1] == 0x01)) {
633 /* Start of multi-part read. Start the next transaction. */
636 ssif_inc_stat(ssif_info, received_message_parts);
638 /* Remove the multi-part read marker. */
640 for (i = 0; i < len; i++)
641 ssif_info->data[i] = data[i+2];
642 ssif_info->multi_len = len;
643 ssif_info->multi_pos = 1;
645 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
646 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
647 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
649 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
650 pr_info("Error from i2c_non_blocking_op(1)\n");
655 } else if (ssif_info->multi_pos) {
656 /* Middle of multi-part read. Start the next transaction. */
658 unsigned char blocknum;
662 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
663 pr_info(PFX "Middle message with no data\n");
670 if (ssif_info->multi_len + len - 1 > IPMI_MAX_MSG_LENGTH) {
671 /* Received message too big, abort the operation. */
673 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
674 pr_info("Received message too big\n");
679 /* Remove the blocknum from the data. */
681 for (i = 0; i < len; i++)
682 ssif_info->data[i + ssif_info->multi_len] = data[i + 1];
683 ssif_info->multi_len += len;
684 if (blocknum == 0xff) {
686 len = ssif_info->multi_len;
687 data = ssif_info->data;
688 } else if (blocknum + 1 != ssif_info->multi_pos) {
690 * Out of sequence block, just abort. Block
691 * numbers start at zero for the second block,
692 * but multi_pos starts at one, so the +1.
696 ssif_inc_stat(ssif_info, received_message_parts);
698 ssif_info->multi_pos++;
700 rv = ssif_i2c_send(ssif_info, msg_done_handler,
702 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
704 I2C_SMBUS_BLOCK_DATA);
706 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
708 "Error from ssif_i2c_send\n");
717 ssif_inc_stat(ssif_info, receive_errors);
719 ssif_inc_stat(ssif_info, received_messages);
720 ssif_inc_stat(ssif_info, received_message_parts);
725 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
726 pr_info(PFX "DONE 1: state = %d, result=%d.\n",
727 ssif_info->ssif_state, result);
729 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
730 msg = ssif_info->curr_msg;
733 if (msg->rsp_size > IPMI_MAX_MSG_LENGTH)
734 msg->rsp_size = IPMI_MAX_MSG_LENGTH;
735 memcpy(msg->rsp, data, msg->rsp_size);
736 ssif_info->curr_msg = NULL;
739 switch (ssif_info->ssif_state) {
741 ipmi_ssif_unlock_cond(ssif_info, flags);
746 return_hosed_msg(ssif_info, msg);
748 deliver_recv_msg(ssif_info, msg);
751 case SSIF_GETTING_FLAGS:
752 /* We got the flags from the SSIF, now handle them. */
753 if ((result < 0) || (len < 4) || (data[2] != 0)) {
755 * Error fetching flags, or invalid length,
756 * just give up for now.
758 ssif_info->ssif_state = SSIF_NORMAL;
759 ipmi_ssif_unlock_cond(ssif_info, flags);
760 pr_warn(PFX "Error getting flags: %d %d, %x\n",
761 result, len, data[2]);
762 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
763 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
764 pr_warn(PFX "Invalid response getting flags: %x %x\n",
767 ssif_inc_stat(ssif_info, flag_fetches);
768 ssif_info->msg_flags = data[3];
769 handle_flags(ssif_info, flags);
773 case SSIF_CLEARING_FLAGS:
774 /* We cleared the flags. */
775 if ((result < 0) || (len < 3) || (data[2] != 0)) {
776 /* Error clearing flags */
777 pr_warn(PFX "Error clearing flags: %d %d, %x\n",
778 result, len, data[2]);
779 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
780 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
781 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
784 ssif_info->ssif_state = SSIF_NORMAL;
785 ipmi_ssif_unlock_cond(ssif_info, flags);
788 case SSIF_GETTING_EVENTS:
789 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
790 /* Error getting event, probably done. */
793 /* Take off the event flag. */
794 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
795 handle_flags(ssif_info, flags);
796 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
797 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
798 pr_warn(PFX "Invalid response getting events: %x %x\n",
799 msg->rsp[0], msg->rsp[1]);
801 /* Take off the event flag. */
802 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
803 handle_flags(ssif_info, flags);
805 handle_flags(ssif_info, flags);
806 ssif_inc_stat(ssif_info, events);
807 deliver_recv_msg(ssif_info, msg);
811 case SSIF_GETTING_MESSAGES:
812 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
813 /* Error getting event, probably done. */
816 /* Take off the msg flag. */
817 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
818 handle_flags(ssif_info, flags);
819 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
820 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
821 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
822 msg->rsp[0], msg->rsp[1]);
825 /* Take off the msg flag. */
826 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
827 handle_flags(ssif_info, flags);
829 ssif_inc_stat(ssif_info, incoming_messages);
830 handle_flags(ssif_info, flags);
831 deliver_recv_msg(ssif_info, msg);
836 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
837 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
838 if (ssif_info->req_events)
839 start_event_fetch(ssif_info, flags);
840 else if (ssif_info->req_flags)
841 start_flag_fetch(ssif_info, flags);
843 start_next_msg(ssif_info, flags);
845 ipmi_ssif_unlock_cond(ssif_info, flags);
847 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
848 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
851 static void msg_written_handler(struct ssif_info *ssif_info, int result,
852 unsigned char *data, unsigned int len)
856 /* We are single-threaded here, so no need for a lock. */
858 ssif_info->retries_left--;
859 if (ssif_info->retries_left > 0) {
860 if (!start_resend(ssif_info)) {
861 ssif_inc_stat(ssif_info, send_retries);
864 /* request failed, just return the error. */
865 ssif_inc_stat(ssif_info, send_errors);
867 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
869 "Out of retries in msg_written_handler\n");
870 msg_done_handler(ssif_info, -EIO, NULL, 0);
874 ssif_inc_stat(ssif_info, send_errors);
877 * Got an error on transmit, let the done routine
880 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
881 pr_info("Error in msg_written_handler: %d\n", result);
883 msg_done_handler(ssif_info, result, NULL, 0);
887 if (ssif_info->multi_data) {
889 * In the middle of a multi-data write. See the comment
890 * in the SSIF_MULTI_n_PART case in the probe function
891 * for details on the intricacies of this.
894 unsigned char *data_to_send;
896 ssif_inc_stat(ssif_info, sent_messages_parts);
898 left = ssif_info->multi_len - ssif_info->multi_pos;
902 ssif_info->multi_data[ssif_info->multi_pos] = left;
903 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
904 ssif_info->multi_pos += left;
907 * Write is finished. Note that we must end
908 * with a write of less than 32 bytes to
909 * complete the transaction, even if it is
912 ssif_info->multi_data = NULL;
914 rv = ssif_i2c_send(ssif_info, msg_written_handler,
916 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
918 I2C_SMBUS_BLOCK_DATA);
920 /* request failed, just return the error. */
921 ssif_inc_stat(ssif_info, send_errors);
923 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
924 pr_info("Error from i2c_non_blocking_op(3)\n");
925 msg_done_handler(ssif_info, -EIO, NULL, 0);
928 /* Ready to request the result. */
929 unsigned long oflags, *flags;
931 ssif_inc_stat(ssif_info, sent_messages);
932 ssif_inc_stat(ssif_info, sent_messages_parts);
934 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
935 if (ssif_info->got_alert) {
936 /* The result is already ready, just start it. */
937 ssif_info->got_alert = false;
938 ipmi_ssif_unlock_cond(ssif_info, flags);
939 start_get(ssif_info);
941 /* Wait a jiffie then request the next message */
942 ssif_info->waiting_alert = true;
943 ssif_info->retries_left = SSIF_RECV_RETRIES;
944 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
945 mod_timer(&ssif_info->retry_timer,
946 jiffies + SSIF_MSG_PART_JIFFIES);
947 ipmi_ssif_unlock_cond(ssif_info, flags);
952 static int start_resend(struct ssif_info *ssif_info)
957 ssif_info->got_alert = false;
959 if (ssif_info->data_len > 32) {
960 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
961 ssif_info->multi_data = ssif_info->data;
962 ssif_info->multi_len = ssif_info->data_len;
964 * Subtle thing, this is 32, not 33, because we will
965 * overwrite the thing at position 32 (which was just
966 * transmitted) with the new length.
968 ssif_info->multi_pos = 32;
969 ssif_info->data[0] = 32;
971 ssif_info->multi_data = NULL;
972 command = SSIF_IPMI_REQUEST;
973 ssif_info->data[0] = ssif_info->data_len;
976 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
977 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
978 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
979 pr_info("Error from i2c_non_blocking_op(4)\n");
983 static int start_send(struct ssif_info *ssif_info,
987 if (len > IPMI_MAX_MSG_LENGTH)
989 if (len > ssif_info->max_xmit_msg_size)
992 ssif_info->retries_left = SSIF_SEND_RETRIES;
993 memcpy(ssif_info->data + 1, data, len);
994 ssif_info->data_len = len;
995 return start_resend(ssif_info);
998 /* Must be called with the message lock held. */
999 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1001 struct ipmi_smi_msg *msg;
1002 unsigned long oflags;
1005 if (!SSIF_IDLE(ssif_info)) {
1006 ipmi_ssif_unlock_cond(ssif_info, flags);
1010 if (!ssif_info->waiting_msg) {
1011 ssif_info->curr_msg = NULL;
1012 ipmi_ssif_unlock_cond(ssif_info, flags);
1016 ssif_info->curr_msg = ssif_info->waiting_msg;
1017 ssif_info->waiting_msg = NULL;
1018 ipmi_ssif_unlock_cond(ssif_info, flags);
1019 rv = start_send(ssif_info,
1020 ssif_info->curr_msg->data,
1021 ssif_info->curr_msg->data_size);
1023 msg = ssif_info->curr_msg;
1024 ssif_info->curr_msg = NULL;
1025 return_hosed_msg(ssif_info, msg);
1026 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1032 static void sender(void *send_info,
1033 struct ipmi_smi_msg *msg)
1035 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1036 unsigned long oflags, *flags;
1038 BUG_ON(ssif_info->waiting_msg);
1039 ssif_info->waiting_msg = msg;
1041 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1042 start_next_msg(ssif_info, flags);
1044 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1045 struct timespec64 t;
1047 ktime_get_real_ts64(&t);
1048 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1049 msg->data[0], msg->data[1],
1050 (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
1054 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1056 struct ssif_info *ssif_info = send_info;
1058 data->addr_src = ssif_info->addr_source;
1059 data->dev = &ssif_info->client->dev;
1060 data->addr_info = ssif_info->addr_info;
1061 get_device(data->dev);
1067 * Instead of having our own timer to periodically check the message
1068 * flags, we let the message handler drive us.
1070 static void request_events(void *send_info)
1072 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1073 unsigned long oflags, *flags;
1075 if (!ssif_info->has_event_buffer)
1078 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1080 * Request flags first, not events, because the lower layer
1081 * doesn't have a way to send an attention. But make sure
1082 * event checking still happens.
1084 ssif_info->req_events = true;
1085 if (SSIF_IDLE(ssif_info))
1086 start_flag_fetch(ssif_info, flags);
1088 ssif_info->req_flags = true;
1089 ipmi_ssif_unlock_cond(ssif_info, flags);
1093 static int inc_usecount(void *send_info)
1095 struct ssif_info *ssif_info = send_info;
1097 if (!i2c_get_adapter(ssif_info->client->adapter->nr))
1100 i2c_use_client(ssif_info->client);
1104 static void dec_usecount(void *send_info)
1106 struct ssif_info *ssif_info = send_info;
1108 i2c_release_client(ssif_info->client);
1109 i2c_put_adapter(ssif_info->client->adapter);
1112 static int ssif_start_processing(void *send_info,
1115 struct ssif_info *ssif_info = send_info;
1117 ssif_info->intf = intf;
1122 #define MAX_SSIF_BMCS 4
1124 static unsigned short addr[MAX_SSIF_BMCS];
1125 static int num_addrs;
1126 module_param_array(addr, ushort, &num_addrs, 0);
1127 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1129 static char *adapter_name[MAX_SSIF_BMCS];
1130 static int num_adapter_names;
1131 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1132 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1134 static int slave_addrs[MAX_SSIF_BMCS];
1135 static int num_slave_addrs;
1136 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1137 MODULE_PARM_DESC(slave_addrs,
1138 "The default IPMB slave address for the controller.");
1140 static bool alerts_broken;
1141 module_param(alerts_broken, bool, 0);
1142 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1145 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1146 * bit 2 enables timing debugging. This is an array indexed by
1149 static int dbg[MAX_SSIF_BMCS];
1151 module_param_array(dbg, int, &num_dbg, 0);
1152 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1154 static bool ssif_dbg_probe;
1155 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1156 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1158 static bool ssif_tryacpi = true;
1159 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1160 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1162 static bool ssif_trydmi = true;
1163 module_param_named(trydmi, ssif_trydmi, bool, 0);
1164 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1166 static DEFINE_MUTEX(ssif_infos_mutex);
1167 static LIST_HEAD(ssif_infos);
1169 static int ssif_remove(struct i2c_client *client)
1171 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1178 * After this point, we won't deliver anything asychronously
1179 * to the message handler. We can unregister ourself.
1181 rv = ipmi_unregister_smi(ssif_info->intf);
1183 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1186 ssif_info->intf = NULL;
1188 /* make sure the driver is not looking for flags any more. */
1189 while (ssif_info->ssif_state != SSIF_NORMAL)
1190 schedule_timeout(1);
1192 ssif_info->stopping = true;
1193 del_timer_sync(&ssif_info->retry_timer);
1194 if (ssif_info->thread) {
1195 complete(&ssif_info->wake_thread);
1196 kthread_stop(ssif_info->thread);
1200 * No message can be outstanding now, we have removed the
1201 * upper layer and it permitted us to do so.
1207 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1208 int *resp_len, unsigned char *resp)
1213 retry_cnt = SSIF_SEND_RETRIES;
1215 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1224 retry_cnt = SSIF_RECV_RETRIES;
1225 while (retry_cnt > 0) {
1226 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1230 msleep(SSIF_MSG_MSEC);
1237 /* Validate that the response is correct. */
1239 (resp[0] != (msg[0] | (1 << 2))) ||
1240 (resp[1] != msg[1]))
1251 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1253 unsigned char *resp;
1254 unsigned char msg[3];
1258 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1262 /* Do a Get Device ID command, since it is required. */
1263 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1264 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1265 rv = do_cmd(client, 2, msg, &len, resp);
1269 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1274 static int smi_type_proc_show(struct seq_file *m, void *v)
1276 seq_puts(m, "ssif\n");
1281 static int smi_type_proc_open(struct inode *inode, struct file *file)
1283 return single_open(file, smi_type_proc_show, inode->i_private);
1286 static const struct file_operations smi_type_proc_ops = {
1287 .open = smi_type_proc_open,
1289 .llseek = seq_lseek,
1290 .release = single_release,
1293 static int smi_stats_proc_show(struct seq_file *m, void *v)
1295 struct ssif_info *ssif_info = m->private;
1297 seq_printf(m, "sent_messages: %u\n",
1298 ssif_get_stat(ssif_info, sent_messages));
1299 seq_printf(m, "sent_messages_parts: %u\n",
1300 ssif_get_stat(ssif_info, sent_messages_parts));
1301 seq_printf(m, "send_retries: %u\n",
1302 ssif_get_stat(ssif_info, send_retries));
1303 seq_printf(m, "send_errors: %u\n",
1304 ssif_get_stat(ssif_info, send_errors));
1305 seq_printf(m, "received_messages: %u\n",
1306 ssif_get_stat(ssif_info, received_messages));
1307 seq_printf(m, "received_message_parts: %u\n",
1308 ssif_get_stat(ssif_info, received_message_parts));
1309 seq_printf(m, "receive_retries: %u\n",
1310 ssif_get_stat(ssif_info, receive_retries));
1311 seq_printf(m, "receive_errors: %u\n",
1312 ssif_get_stat(ssif_info, receive_errors));
1313 seq_printf(m, "flag_fetches: %u\n",
1314 ssif_get_stat(ssif_info, flag_fetches));
1315 seq_printf(m, "hosed: %u\n",
1316 ssif_get_stat(ssif_info, hosed));
1317 seq_printf(m, "events: %u\n",
1318 ssif_get_stat(ssif_info, events));
1319 seq_printf(m, "watchdog_pretimeouts: %u\n",
1320 ssif_get_stat(ssif_info, watchdog_pretimeouts));
1321 seq_printf(m, "alerts: %u\n",
1322 ssif_get_stat(ssif_info, alerts));
1326 static int smi_stats_proc_open(struct inode *inode, struct file *file)
1328 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1331 static const struct file_operations smi_stats_proc_ops = {
1332 .open = smi_stats_proc_open,
1334 .llseek = seq_lseek,
1335 .release = single_release,
1338 static int strcmp_nospace(char *s1, char *s2)
1340 while (*s1 && *s2) {
1341 while (isspace(*s1))
1343 while (isspace(*s2))
1355 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1357 bool match_null_name)
1359 struct ssif_addr_info *info, *found = NULL;
1362 list_for_each_entry(info, &ssif_infos, link) {
1363 if (info->binfo.addr == addr) {
1364 if (info->adapter_name || adapter_name) {
1365 if (!info->adapter_name != !adapter_name) {
1366 /* One is NULL and one is not */
1370 strcmp_nospace(info->adapter_name,
1372 /* Names do not match */
1380 if (!found && match_null_name) {
1381 /* Try to get an exact match first, then try with a NULL name */
1382 adapter_name = NULL;
1383 match_null_name = false;
1390 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1393 acpi_handle acpi_handle;
1395 acpi_handle = ACPI_HANDLE(dev);
1397 ssif_info->addr_source = SI_ACPI;
1398 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1405 static int find_slave_address(struct i2c_client *client, int slave_addr)
1407 struct ssif_addr_info *info;
1413 * Came in without a slave address, search around to see if
1414 * the other sources have a slave address. This lets us pick
1415 * up an SMBIOS slave address when using ACPI.
1417 list_for_each_entry(info, &ssif_infos, link) {
1418 if (info->binfo.addr != client->addr)
1420 if (info->adapter_name && client->adapter->name &&
1421 strcmp_nospace(info->adapter_name,
1422 client->adapter->name))
1424 if (info->slave_addr) {
1425 slave_addr = info->slave_addr;
1434 * Global enables we care about.
1436 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1437 IPMI_BMC_EVT_MSG_INTR)
1439 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1441 unsigned char msg[3];
1442 unsigned char *resp;
1443 struct ssif_info *ssif_info;
1448 struct ssif_addr_info *addr_info = NULL;
1451 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1455 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1461 if (!check_acpi(ssif_info, &client->dev)) {
1462 addr_info = ssif_info_find(client->addr, client->adapter->name,
1465 /* Must have come in through sysfs. */
1466 ssif_info->addr_source = SI_HOTMOD;
1468 ssif_info->addr_source = addr_info->addr_src;
1469 ssif_info->ssif_debug = addr_info->debug;
1470 ssif_info->addr_info = addr_info->addr_info;
1471 slave_addr = addr_info->slave_addr;
1475 slave_addr = find_slave_address(client, slave_addr);
1477 pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1478 ipmi_addr_src_to_str(ssif_info->addr_source),
1479 client->addr, client->adapter->name, slave_addr);
1482 * Do a Get Device ID command, since it comes back with some
1485 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1486 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1487 rv = do_cmd(client, 2, msg, &len, resp);
1491 rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1495 ssif_info->client = client;
1496 i2c_set_clientdata(client, ssif_info);
1498 /* Now check for system interface capabilities */
1499 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1500 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1501 msg[2] = 0; /* SSIF */
1502 rv = do_cmd(client, 3, msg, &len, resp);
1503 if (!rv && (len >= 3) && (resp[2] == 0)) {
1506 pr_info(PFX "SSIF info too short: %d\n", len);
1510 /* Got a good SSIF response, handle it. */
1511 ssif_info->max_xmit_msg_size = resp[5];
1512 ssif_info->max_recv_msg_size = resp[6];
1513 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1514 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1516 /* Sanitize the data */
1517 switch (ssif_info->multi_support) {
1519 if (ssif_info->max_xmit_msg_size > 32)
1520 ssif_info->max_xmit_msg_size = 32;
1521 if (ssif_info->max_recv_msg_size > 32)
1522 ssif_info->max_recv_msg_size = 32;
1525 case SSIF_MULTI_2_PART:
1526 if (ssif_info->max_xmit_msg_size > 63)
1527 ssif_info->max_xmit_msg_size = 63;
1528 if (ssif_info->max_recv_msg_size > 62)
1529 ssif_info->max_recv_msg_size = 62;
1532 case SSIF_MULTI_n_PART:
1534 * The specification is rather confusing at
1535 * this point, but I think I understand what
1536 * is meant. At least I have a workable
1537 * solution. With multi-part messages, you
1538 * cannot send a message that is a multiple of
1539 * 32-bytes in length, because the start and
1540 * middle messages are 32-bytes and the end
1541 * message must be at least one byte. You
1542 * can't fudge on an extra byte, that would
1543 * screw up things like fru data writes. So
1544 * we limit the length to 63 bytes. That way
1545 * a 32-byte message gets sent as a single
1546 * part. A larger message will be a 32-byte
1547 * start and the next message is always going
1548 * to be 1-31 bytes in length. Not ideal, but
1551 if (ssif_info->max_xmit_msg_size > 63)
1552 ssif_info->max_xmit_msg_size = 63;
1556 /* Data is not sane, just give up. */
1561 /* Assume no multi-part or PEC support */
1562 pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1565 ssif_info->max_xmit_msg_size = 32;
1566 ssif_info->max_recv_msg_size = 32;
1567 ssif_info->multi_support = SSIF_NO_MULTI;
1568 ssif_info->supports_pec = 0;
1571 /* Make sure the NMI timeout is cleared. */
1572 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1573 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1574 msg[2] = WDT_PRE_TIMEOUT_INT;
1575 rv = do_cmd(client, 3, msg, &len, resp);
1576 if (rv || (len < 3) || (resp[2] != 0))
1577 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1580 /* Attempt to enable the event buffer. */
1581 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1582 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1583 rv = do_cmd(client, 2, msg, &len, resp);
1584 if (rv || (len < 4) || (resp[2] != 0)) {
1585 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1587 rv = 0; /* Not fatal */
1591 ssif_info->global_enables = resp[3];
1593 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1594 ssif_info->has_event_buffer = true;
1595 /* buffer is already enabled, nothing to do. */
1599 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1600 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1601 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1602 rv = do_cmd(client, 3, msg, &len, resp);
1603 if (rv || (len < 2)) {
1604 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1606 rv = 0; /* Not fatal */
1611 /* A successful return means the event buffer is supported. */
1612 ssif_info->has_event_buffer = true;
1613 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1616 /* Some systems don't behave well if you enable alerts. */
1620 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1621 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1622 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1623 rv = do_cmd(client, 3, msg, &len, resp);
1624 if (rv || (len < 2)) {
1625 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1627 rv = 0; /* Not fatal */
1632 /* A successful return means the alert is supported. */
1633 ssif_info->supports_alert = true;
1634 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1638 ssif_info->intf_num = atomic_inc_return(&next_intf);
1640 if (ssif_dbg_probe) {
1641 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1645 spin_lock_init(&ssif_info->lock);
1646 ssif_info->ssif_state = SSIF_NORMAL;
1647 setup_timer(&ssif_info->retry_timer, retry_timeout,
1648 (unsigned long)ssif_info);
1650 for (i = 0; i < SSIF_NUM_STATS; i++)
1651 atomic_set(&ssif_info->stats[i], 0);
1653 if (ssif_info->supports_pec)
1654 ssif_info->client->flags |= I2C_CLIENT_PEC;
1656 ssif_info->handlers.owner = THIS_MODULE;
1657 ssif_info->handlers.start_processing = ssif_start_processing;
1658 ssif_info->handlers.get_smi_info = get_smi_info;
1659 ssif_info->handlers.sender = sender;
1660 ssif_info->handlers.request_events = request_events;
1661 ssif_info->handlers.inc_usecount = inc_usecount;
1662 ssif_info->handlers.dec_usecount = dec_usecount;
1665 unsigned int thread_num;
1667 thread_num = ((ssif_info->client->adapter->nr << 8) |
1668 ssif_info->client->addr);
1669 init_completion(&ssif_info->wake_thread);
1670 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1671 "kssif%4.4x", thread_num);
1672 if (IS_ERR(ssif_info->thread)) {
1673 rv = PTR_ERR(ssif_info->thread);
1674 dev_notice(&ssif_info->client->dev,
1675 "Could not start kernel thread: error %d\n",
1681 rv = ipmi_register_smi(&ssif_info->handlers,
1683 &ssif_info->device_id,
1684 &ssif_info->client->dev,
1687 pr_err(PFX "Unable to register device: error %d\n", rv);
1691 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1695 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1699 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1700 &smi_stats_proc_ops,
1703 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1714 ipmi_unregister_smi(ssif_info->intf);
1718 static int ssif_adapter_handler(struct device *adev, void *opaque)
1720 struct ssif_addr_info *addr_info = opaque;
1722 if (adev->type != &i2c_adapter_type)
1725 i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo);
1727 if (!addr_info->adapter_name)
1728 return 1; /* Only try the first I2C adapter by default. */
1732 static int new_ssif_client(int addr, char *adapter_name,
1733 int debug, int slave_addr,
1734 enum ipmi_addr_src addr_src)
1736 struct ssif_addr_info *addr_info;
1739 mutex_lock(&ssif_infos_mutex);
1740 if (ssif_info_find(addr, adapter_name, false)) {
1745 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1752 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1753 if (!addr_info->adapter_name) {
1760 strncpy(addr_info->binfo.type, DEVICE_NAME,
1761 sizeof(addr_info->binfo.type));
1762 addr_info->binfo.addr = addr;
1763 addr_info->binfo.platform_data = addr_info;
1764 addr_info->debug = debug;
1765 addr_info->slave_addr = slave_addr;
1766 addr_info->addr_src = addr_src;
1768 list_add_tail(&addr_info->link, &ssif_infos);
1771 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1772 /* Otherwise address list will get it */
1775 mutex_unlock(&ssif_infos_mutex);
1779 static void free_ssif_clients(void)
1781 struct ssif_addr_info *info, *tmp;
1783 mutex_lock(&ssif_infos_mutex);
1784 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1785 list_del(&info->link);
1786 kfree(info->adapter_name);
1789 mutex_unlock(&ssif_infos_mutex);
1792 static unsigned short *ssif_address_list(void)
1794 struct ssif_addr_info *info;
1795 unsigned int count = 0, i;
1796 unsigned short *address_list;
1798 list_for_each_entry(info, &ssif_infos, link)
1801 address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1806 list_for_each_entry(info, &ssif_infos, link) {
1807 unsigned short addr = info->binfo.addr;
1810 for (j = 0; j < i; j++) {
1811 if (address_list[j] == addr)
1814 address_list[i] = addr;
1818 address_list[i] = I2C_CLIENT_END;
1820 return address_list;
1824 static const struct acpi_device_id ssif_acpi_match[] = {
1828 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1831 * Once we get an ACPI failure, we don't try any more, because we go
1832 * through the tables sequentially. Once we don't find a table, there
1835 static int acpi_failure;
1838 * Defined in the IPMI 2.0 spec.
1849 s8 CreatorRevision[4];
1852 s16 SpecificationRevision;
1855 * Bit 0 - SCI interrupt supported
1856 * Bit 1 - I/O APIC/SAPIC
1861 * If bit 0 of InterruptType is set, then this is the SCI
1862 * interrupt in the GPEx_STS register.
1869 * If bit 1 of InterruptType is set, then this is the I/O
1870 * APIC/SAPIC interrupt.
1872 u32 GlobalSystemInterrupt;
1874 /* The actual register address. */
1875 struct acpi_generic_address addr;
1879 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1882 static int try_init_spmi(struct SPMITable *spmi)
1884 unsigned short myaddr;
1886 if (num_addrs >= MAX_SSIF_BMCS)
1889 if (spmi->IPMIlegacy != 1) {
1890 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1894 if (spmi->InterfaceType != 4)
1897 if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1898 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1899 spmi->addr.space_id);
1903 myaddr = spmi->addr.address & 0x7f;
1905 return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI);
1908 static void spmi_find_bmc(void)
1911 struct SPMITable *spmi;
1920 for (i = 0; ; i++) {
1921 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1922 (struct acpi_table_header **)&spmi);
1923 if (status != AE_OK)
1926 try_init_spmi(spmi);
1930 static void spmi_find_bmc(void) { }
1934 static int decode_dmi(const struct dmi_device *dmi_dev)
1936 struct dmi_header *dm = dmi_dev->device_data;
1937 u8 *data = (u8 *) dm;
1938 u8 len = dm->length;
1939 unsigned short myaddr;
1942 if (num_addrs >= MAX_SSIF_BMCS)
1948 if (data[0x04] != 4) /* Not SSIF */
1951 if ((data[8] >> 1) == 0) {
1953 * Some broken systems put the I2C address in
1954 * the slave address field. We try to
1955 * accommodate them here.
1957 myaddr = data[6] >> 1;
1960 myaddr = data[8] >> 1;
1961 slave_addr = data[6];
1964 return new_ssif_client(myaddr, NULL, 0, slave_addr, SI_SMBIOS);
1967 static void dmi_iterator(void)
1969 const struct dmi_device *dev = NULL;
1971 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
1975 static void dmi_iterator(void) { }
1978 static const struct i2c_device_id ssif_id[] = {
1982 MODULE_DEVICE_TABLE(i2c, ssif_id);
1984 static struct i2c_driver ssif_i2c_driver = {
1985 .class = I2C_CLASS_HWMON,
1989 .probe = ssif_probe,
1990 .remove = ssif_remove,
1991 .alert = ssif_alert,
1992 .id_table = ssif_id,
1993 .detect = ssif_detect
1996 static int init_ipmi_ssif(void)
2004 pr_info("IPMI SSIF Interface driver\n");
2006 /* build list for i2c from addr list */
2007 for (i = 0; i < num_addrs; i++) {
2008 rv = new_ssif_client(addr[i], adapter_name[i],
2009 dbg[i], slave_addrs[i],
2013 "Couldn't add hardcoded device at addr 0x%x\n",
2018 ssif_i2c_driver.driver.acpi_match_table =
2019 ACPI_PTR(ssif_acpi_match);
2025 ssif_i2c_driver.address_list = ssif_address_list();
2027 rv = i2c_add_driver(&ssif_i2c_driver);
2033 module_init(init_ipmi_ssif);
2035 static void cleanup_ipmi_ssif(void)
2040 initialized = false;
2042 i2c_del_driver(&ssif_i2c_driver);
2044 free_ssif_clients();
2046 module_exit(cleanup_ipmi_ssif);
2048 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2049 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2050 MODULE_LICENSE("GPL");