4 * The interface to the IPMI driver for SMBus access to a SMBus
5 * compliant device. Called SSIF by the IPMI spec.
7 * Author: Intel Corporation
8 * Todd Davis <todd.c.davis@intel.com>
10 * Rewritten by Corey Minyard <minyard@acm.org> to support the
11 * non-blocking I2C interface, add support for multi-part
12 * transactions, add PEC support, and general clenaup.
14 * Copyright 2003 Intel Corporation
15 * Copyright 2005 MontaVista Software
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 of the License, or (at your
20 * option) any later version.
24 * This file holds the "policy" for the interface to the SSIF state
25 * machine. It does the configuration, handles timers and interrupts,
26 * and drives the real SSIF state machine.
30 * TODO: Figure out how to use SMB alerts. This will require a new
31 * interface into the I2C driver, I believe.
34 #if defined(MODVERSIONS)
35 #include <linux/modversions.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/sched.h>
41 #include <linux/seq_file.h>
42 #include <linux/timer.h>
43 #include <linux/delay.h>
44 #include <linux/errno.h>
45 #include <linux/spinlock.h>
46 #include <linux/slab.h>
47 #include <linux/list.h>
48 #include <linux/i2c.h>
49 #include <linux/ipmi_smi.h>
50 #include <linux/init.h>
51 #include <linux/dmi.h>
52 #include <linux/kthread.h>
53 #include <linux/acpi.h>
54 #include <linux/ctype.h>
55 #include <linux/time64.h>
58 #define PFX "ipmi_ssif: "
59 #define DEVICE_NAME "ipmi_ssif"
61 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
63 #define SSIF_IPMI_REQUEST 2
64 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
65 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
66 #define SSIF_IPMI_RESPONSE 3
67 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
69 /* ssif_debug is a bit-field
70 * SSIF_DEBUG_MSG - commands and their responses
71 * SSIF_DEBUG_STATES - message states
72 * SSIF_DEBUG_TIMING - Measure times between events in the driver
74 #define SSIF_DEBUG_TIMING 4
75 #define SSIF_DEBUG_STATE 2
76 #define SSIF_DEBUG_MSG 1
77 #define SSIF_NODEBUG 0
78 #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
83 #define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
84 #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
86 /* How many times to we retry sending/receiving the message. */
87 #define SSIF_SEND_RETRIES 5
88 #define SSIF_RECV_RETRIES 250
90 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
91 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
92 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
94 enum ssif_intf_state {
99 SSIF_GETTING_MESSAGES,
100 /* FIXME - add watchdog stuff. */
103 #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
104 && (ssif)->curr_msg == NULL)
107 * Indexes into stats[] in ssif_info below.
109 enum ssif_stat_indexes {
110 /* Number of total messages sent. */
111 SSIF_STAT_sent_messages = 0,
114 * Number of message parts sent. Messages may be broken into
115 * parts if they are long.
117 SSIF_STAT_sent_messages_parts,
120 * Number of time a message was retried.
122 SSIF_STAT_send_retries,
125 * Number of times the send of a message failed.
127 SSIF_STAT_send_errors,
130 * Number of message responses received.
132 SSIF_STAT_received_messages,
135 * Number of message fragments received.
137 SSIF_STAT_received_message_parts,
140 * Number of times the receive of a message was retried.
142 SSIF_STAT_receive_retries,
145 * Number of errors receiving messages.
147 SSIF_STAT_receive_errors,
150 * Number of times a flag fetch was requested.
152 SSIF_STAT_flag_fetches,
155 * Number of times the hardware didn't follow the state machine.
160 * Number of received events.
164 /* Number of asyncronous messages received. */
165 SSIF_STAT_incoming_messages,
167 /* Number of watchdog pretimeouts. */
168 SSIF_STAT_watchdog_pretimeouts,
170 /* Number of alers received. */
173 /* Always add statistics before this value, it must be last. */
177 struct ssif_addr_info {
178 struct i2c_board_info binfo;
182 enum ipmi_addr_src addr_src;
183 union ipmi_smi_info_union addr_info;
185 struct i2c_client *client;
187 struct i2c_client *added_client;
189 struct mutex clients_mutex;
190 struct list_head clients;
192 struct list_head link;
197 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
198 unsigned char *data, unsigned int len);
204 struct ipmi_smi_msg *waiting_msg;
205 struct ipmi_smi_msg *curr_msg;
206 enum ssif_intf_state ssif_state;
207 unsigned long ssif_debug;
209 struct ipmi_smi_handlers handlers;
211 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
212 union ipmi_smi_info_union addr_info;
215 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
216 * is set to hold the flags until we are done handling everything
219 #define RECEIVE_MSG_AVAIL 0x01
220 #define EVENT_MSG_BUFFER_FULL 0x02
221 #define WDT_PRE_TIMEOUT_INT 0x08
222 unsigned char msg_flags;
225 bool has_event_buffer;
229 * Used to tell what we should do with alerts. If we are
230 * waiting on a response, read the data immediately.
236 * If set to true, this will request events the next time the
237 * state machine is idle.
242 * If set to true, this will request flags the next time the
243 * state machine is idle.
248 * Used to perform timer operations when run-to-completion
249 * mode is on. This is a countdown timer.
253 /* Used for sending/receiving data. +1 for the length. */
254 unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
255 unsigned int data_len;
257 /* Temp receive buffer, gets copied into data. */
258 unsigned char recv[I2C_SMBUS_BLOCK_MAX];
260 struct i2c_client *client;
261 ssif_i2c_done done_handler;
263 /* Thread interface handling */
264 struct task_struct *thread;
265 struct completion wake_thread;
269 unsigned char *i2c_data;
270 unsigned int i2c_size;
272 /* From the device id response. */
273 struct ipmi_device_id device_id;
275 struct timer_list retry_timer;
278 /* Info from SSIF cmd */
279 unsigned char max_xmit_msg_size;
280 unsigned char max_recv_msg_size;
281 unsigned int multi_support;
284 #define SSIF_NO_MULTI 0
285 #define SSIF_MULTI_2_PART 1
286 #define SSIF_MULTI_n_PART 2
287 unsigned char *multi_data;
288 unsigned int multi_len;
289 unsigned int multi_pos;
291 atomic_t stats[SSIF_NUM_STATS];
294 #define ssif_inc_stat(ssif, stat) \
295 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
296 #define ssif_get_stat(ssif, stat) \
297 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
299 static bool initialized;
301 static atomic_t next_intf = ATOMIC_INIT(0);
303 static void return_hosed_msg(struct ssif_info *ssif_info,
304 struct ipmi_smi_msg *msg);
305 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
306 static int start_send(struct ssif_info *ssif_info,
310 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
311 unsigned long *flags)
313 spin_lock_irqsave(&ssif_info->lock, *flags);
317 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
318 unsigned long *flags)
320 spin_unlock_irqrestore(&ssif_info->lock, *flags);
323 static void deliver_recv_msg(struct ssif_info *ssif_info,
324 struct ipmi_smi_msg *msg)
326 ipmi_smi_t intf = ssif_info->intf;
329 ipmi_free_smi_msg(msg);
330 } else if (msg->rsp_size < 0) {
331 return_hosed_msg(ssif_info, msg);
333 "Malformed message in deliver_recv_msg: rsp_size = %d\n",
336 ipmi_smi_msg_received(intf, msg);
340 static void return_hosed_msg(struct ssif_info *ssif_info,
341 struct ipmi_smi_msg *msg)
343 ssif_inc_stat(ssif_info, hosed);
345 /* Make it a response */
346 msg->rsp[0] = msg->data[0] | 4;
347 msg->rsp[1] = msg->data[1];
348 msg->rsp[2] = 0xFF; /* Unknown error. */
351 deliver_recv_msg(ssif_info, msg);
355 * Must be called with the message lock held. This will release the
356 * message lock. Note that the caller will check SSIF_IDLE and start a
357 * new operation, so there is no need to check for new messages to
360 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
362 unsigned char msg[3];
364 ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
365 ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
366 ipmi_ssif_unlock_cond(ssif_info, flags);
368 /* Make sure the watchdog pre-timeout flag is not set at startup. */
369 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
370 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
371 msg[2] = WDT_PRE_TIMEOUT_INT;
373 if (start_send(ssif_info, msg, 3) != 0) {
374 /* Error, just go to normal state. */
375 ssif_info->ssif_state = SSIF_NORMAL;
379 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
383 ssif_info->req_flags = false;
384 ssif_info->ssif_state = SSIF_GETTING_FLAGS;
385 ipmi_ssif_unlock_cond(ssif_info, flags);
387 mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
388 mb[1] = IPMI_GET_MSG_FLAGS_CMD;
389 if (start_send(ssif_info, mb, 2) != 0)
390 ssif_info->ssif_state = SSIF_NORMAL;
393 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
394 struct ipmi_smi_msg *msg)
396 if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
397 unsigned long oflags;
399 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
400 ssif_info->curr_msg = NULL;
401 ssif_info->ssif_state = SSIF_NORMAL;
402 ipmi_ssif_unlock_cond(ssif_info, flags);
403 ipmi_free_smi_msg(msg);
407 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
409 struct ipmi_smi_msg *msg;
411 ssif_info->req_events = false;
413 msg = ipmi_alloc_smi_msg();
415 ssif_info->ssif_state = SSIF_NORMAL;
416 ipmi_ssif_unlock_cond(ssif_info, flags);
420 ssif_info->curr_msg = msg;
421 ssif_info->ssif_state = SSIF_GETTING_EVENTS;
422 ipmi_ssif_unlock_cond(ssif_info, flags);
424 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
425 msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
428 check_start_send(ssif_info, flags, msg);
431 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
432 unsigned long *flags)
434 struct ipmi_smi_msg *msg;
436 msg = ipmi_alloc_smi_msg();
438 ssif_info->ssif_state = SSIF_NORMAL;
439 ipmi_ssif_unlock_cond(ssif_info, flags);
443 ssif_info->curr_msg = msg;
444 ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
445 ipmi_ssif_unlock_cond(ssif_info, flags);
447 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
448 msg->data[1] = IPMI_GET_MSG_CMD;
451 check_start_send(ssif_info, flags, msg);
455 * Must be called with the message lock held. This will release the
456 * message lock. Note that the caller will check SSIF_IDLE and start a
457 * new operation, so there is no need to check for new messages to
460 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
462 if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
463 ipmi_smi_t intf = ssif_info->intf;
464 /* Watchdog pre-timeout */
465 ssif_inc_stat(ssif_info, watchdog_pretimeouts);
466 start_clear_flags(ssif_info, flags);
468 ipmi_smi_watchdog_pretimeout(intf);
469 } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
470 /* Messages available. */
471 start_recv_msg_fetch(ssif_info, flags);
472 else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
473 /* Events available. */
474 start_event_fetch(ssif_info, flags);
476 ssif_info->ssif_state = SSIF_NORMAL;
477 ipmi_ssif_unlock_cond(ssif_info, flags);
481 static int ipmi_ssif_thread(void *data)
483 struct ssif_info *ssif_info = data;
485 while (!kthread_should_stop()) {
488 /* Wait for something to do */
489 result = wait_for_completion_interruptible(
490 &ssif_info->wake_thread);
491 if (ssif_info->stopping)
493 if (result == -ERESTARTSYS)
495 init_completion(&ssif_info->wake_thread);
497 if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
498 result = i2c_smbus_write_block_data(
499 ssif_info->client, ssif_info->i2c_command,
500 ssif_info->i2c_data[0],
501 ssif_info->i2c_data + 1);
502 ssif_info->done_handler(ssif_info, result, NULL, 0);
504 result = i2c_smbus_read_block_data(
505 ssif_info->client, ssif_info->i2c_command,
506 ssif_info->i2c_data);
508 ssif_info->done_handler(ssif_info, result,
511 ssif_info->done_handler(ssif_info, 0,
520 static int ssif_i2c_send(struct ssif_info *ssif_info,
521 ssif_i2c_done handler,
522 int read_write, int command,
523 unsigned char *data, unsigned int size)
525 ssif_info->done_handler = handler;
527 ssif_info->i2c_read_write = read_write;
528 ssif_info->i2c_command = command;
529 ssif_info->i2c_data = data;
530 ssif_info->i2c_size = size;
531 complete(&ssif_info->wake_thread);
536 static void msg_done_handler(struct ssif_info *ssif_info, int result,
537 unsigned char *data, unsigned int len);
539 static void start_get(struct ssif_info *ssif_info)
543 ssif_info->rtc_us_timer = 0;
544 ssif_info->multi_pos = 0;
546 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
548 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
550 /* request failed, just return the error. */
551 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
552 pr_info("Error from i2c_non_blocking_op(5)\n");
554 msg_done_handler(ssif_info, -EIO, NULL, 0);
558 static void retry_timeout(unsigned long data)
560 struct ssif_info *ssif_info = (void *) data;
561 unsigned long oflags, *flags;
564 if (ssif_info->stopping)
567 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
568 waiting = ssif_info->waiting_alert;
569 ssif_info->waiting_alert = false;
570 ipmi_ssif_unlock_cond(ssif_info, flags);
573 start_get(ssif_info);
577 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
580 struct ssif_info *ssif_info = i2c_get_clientdata(client);
581 unsigned long oflags, *flags;
584 if (type != I2C_PROTOCOL_SMBUS_ALERT)
587 ssif_inc_stat(ssif_info, alerts);
589 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
590 if (ssif_info->waiting_alert) {
591 ssif_info->waiting_alert = false;
592 del_timer(&ssif_info->retry_timer);
594 } else if (ssif_info->curr_msg) {
595 ssif_info->got_alert = true;
597 ipmi_ssif_unlock_cond(ssif_info, flags);
599 start_get(ssif_info);
602 static int start_resend(struct ssif_info *ssif_info);
604 static void msg_done_handler(struct ssif_info *ssif_info, int result,
605 unsigned char *data, unsigned int len)
607 struct ipmi_smi_msg *msg;
608 unsigned long oflags, *flags;
612 * We are single-threaded here, so no need for a lock until we
613 * start messing with driver states or the queues.
617 ssif_info->retries_left--;
618 if (ssif_info->retries_left > 0) {
619 ssif_inc_stat(ssif_info, receive_retries);
621 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
622 ssif_info->waiting_alert = true;
623 ssif_info->rtc_us_timer = SSIF_MSG_USEC;
624 if (!ssif_info->stopping)
625 mod_timer(&ssif_info->retry_timer,
626 jiffies + SSIF_MSG_JIFFIES);
627 ipmi_ssif_unlock_cond(ssif_info, flags);
631 ssif_inc_stat(ssif_info, receive_errors);
633 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
634 pr_info("Error in msg_done_handler: %d\n", result);
639 if ((len > 1) && (ssif_info->multi_pos == 0)
640 && (data[0] == 0x00) && (data[1] == 0x01)) {
641 /* Start of multi-part read. Start the next transaction. */
644 ssif_inc_stat(ssif_info, received_message_parts);
646 /* Remove the multi-part read marker. */
649 for (i = 0; i < len; i++)
650 ssif_info->data[i] = data[i];
651 ssif_info->multi_len = len;
652 ssif_info->multi_pos = 1;
654 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
655 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
656 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
658 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
659 pr_info("Error from i2c_non_blocking_op(1)\n");
664 } else if (ssif_info->multi_pos) {
665 /* Middle of multi-part read. Start the next transaction. */
667 unsigned char blocknum;
671 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
672 pr_info(PFX "Middle message with no data\n");
681 if (blocknum != 0xff && len != 31) {
682 /* All blocks but the last must have 31 data bytes. */
684 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
685 pr_info("Received middle message <31\n");
690 if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
691 /* Received message too big, abort the operation. */
693 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
694 pr_info("Received message too big\n");
699 for (i = 0; i < len; i++)
700 ssif_info->data[i + ssif_info->multi_len] = data[i];
701 ssif_info->multi_len += len;
702 if (blocknum == 0xff) {
704 len = ssif_info->multi_len;
705 data = ssif_info->data;
706 } else if (blocknum + 1 != ssif_info->multi_pos) {
708 * Out of sequence block, just abort. Block
709 * numbers start at zero for the second block,
710 * but multi_pos starts at one, so the +1.
712 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
713 dev_dbg(&ssif_info->client->dev,
714 "Received message out of sequence, expected %u, got %u\n",
715 ssif_info->multi_pos - 1, blocknum);
718 ssif_inc_stat(ssif_info, received_message_parts);
720 ssif_info->multi_pos++;
722 rv = ssif_i2c_send(ssif_info, msg_done_handler,
724 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
726 I2C_SMBUS_BLOCK_DATA);
728 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
730 "Error from ssif_i2c_send\n");
740 ssif_inc_stat(ssif_info, receive_errors);
742 ssif_inc_stat(ssif_info, received_messages);
743 ssif_inc_stat(ssif_info, received_message_parts);
746 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
747 pr_info(PFX "DONE 1: state = %d, result=%d.\n",
748 ssif_info->ssif_state, result);
750 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
751 msg = ssif_info->curr_msg;
754 if (len > IPMI_MAX_MSG_LENGTH)
755 len = IPMI_MAX_MSG_LENGTH;
756 memcpy(msg->rsp, data, len);
761 ssif_info->curr_msg = NULL;
764 switch (ssif_info->ssif_state) {
766 ipmi_ssif_unlock_cond(ssif_info, flags);
771 return_hosed_msg(ssif_info, msg);
773 deliver_recv_msg(ssif_info, msg);
776 case SSIF_GETTING_FLAGS:
777 /* We got the flags from the SSIF, now handle them. */
778 if ((result < 0) || (len < 4) || (data[2] != 0)) {
780 * Error fetching flags, or invalid length,
781 * just give up for now.
783 ssif_info->ssif_state = SSIF_NORMAL;
784 ipmi_ssif_unlock_cond(ssif_info, flags);
785 pr_warn(PFX "Error getting flags: %d %d, %x\n",
786 result, len, (len >= 3) ? data[2] : 0);
787 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
788 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
790 * Don't abort here, maybe it was a queued
791 * response to a previous command.
793 ipmi_ssif_unlock_cond(ssif_info, flags);
794 pr_warn(PFX "Invalid response getting flags: %x %x\n",
797 ssif_inc_stat(ssif_info, flag_fetches);
798 ssif_info->msg_flags = data[3];
799 handle_flags(ssif_info, flags);
803 case SSIF_CLEARING_FLAGS:
804 /* We cleared the flags. */
805 if ((result < 0) || (len < 3) || (data[2] != 0)) {
806 /* Error clearing flags */
807 pr_warn(PFX "Error clearing flags: %d %d, %x\n",
808 result, len, (len >= 3) ? data[2] : 0);
809 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
810 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
811 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
814 ssif_info->ssif_state = SSIF_NORMAL;
815 ipmi_ssif_unlock_cond(ssif_info, flags);
818 case SSIF_GETTING_EVENTS:
820 /* Should never happen, but just in case. */
821 dev_warn(&ssif_info->client->dev,
822 "No message set while getting events\n");
823 ipmi_ssif_unlock_cond(ssif_info, flags);
827 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
828 /* Error getting event, probably done. */
831 /* Take off the event flag. */
832 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
833 handle_flags(ssif_info, flags);
834 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
835 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
836 pr_warn(PFX "Invalid response getting events: %x %x\n",
837 msg->rsp[0], msg->rsp[1]);
839 /* Take off the event flag. */
840 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
841 handle_flags(ssif_info, flags);
843 handle_flags(ssif_info, flags);
844 ssif_inc_stat(ssif_info, events);
845 deliver_recv_msg(ssif_info, msg);
849 case SSIF_GETTING_MESSAGES:
851 /* Should never happen, but just in case. */
852 dev_warn(&ssif_info->client->dev,
853 "No message set while getting messages\n");
854 ipmi_ssif_unlock_cond(ssif_info, flags);
858 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
859 /* Error getting event, probably done. */
862 /* Take off the msg flag. */
863 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
864 handle_flags(ssif_info, flags);
865 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
866 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
867 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
868 msg->rsp[0], msg->rsp[1]);
871 /* Take off the msg flag. */
872 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
873 handle_flags(ssif_info, flags);
875 ssif_inc_stat(ssif_info, incoming_messages);
876 handle_flags(ssif_info, flags);
877 deliver_recv_msg(ssif_info, msg);
882 /* Should never happen, but just in case. */
883 dev_warn(&ssif_info->client->dev,
884 "Invalid state in message done handling: %d\n",
885 ssif_info->ssif_state);
886 ipmi_ssif_unlock_cond(ssif_info, flags);
889 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
890 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
891 if (ssif_info->req_events)
892 start_event_fetch(ssif_info, flags);
893 else if (ssif_info->req_flags)
894 start_flag_fetch(ssif_info, flags);
896 start_next_msg(ssif_info, flags);
898 ipmi_ssif_unlock_cond(ssif_info, flags);
900 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
901 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
904 static void msg_written_handler(struct ssif_info *ssif_info, int result,
905 unsigned char *data, unsigned int len)
909 /* We are single-threaded here, so no need for a lock. */
911 ssif_info->retries_left--;
912 if (ssif_info->retries_left > 0) {
913 if (!start_resend(ssif_info)) {
914 ssif_inc_stat(ssif_info, send_retries);
917 /* request failed, just return the error. */
918 ssif_inc_stat(ssif_info, send_errors);
920 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
922 "Out of retries in msg_written_handler\n");
923 msg_done_handler(ssif_info, -EIO, NULL, 0);
927 ssif_inc_stat(ssif_info, send_errors);
930 * Got an error on transmit, let the done routine
933 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
934 pr_info("Error in msg_written_handler: %d\n", result);
936 msg_done_handler(ssif_info, result, NULL, 0);
940 if (ssif_info->multi_data) {
942 * In the middle of a multi-data write. See the comment
943 * in the SSIF_MULTI_n_PART case in the probe function
944 * for details on the intricacies of this.
947 unsigned char *data_to_send;
949 ssif_inc_stat(ssif_info, sent_messages_parts);
951 left = ssif_info->multi_len - ssif_info->multi_pos;
955 ssif_info->multi_data[ssif_info->multi_pos] = left;
956 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
957 ssif_info->multi_pos += left;
960 * Write is finished. Note that we must end
961 * with a write of less than 32 bytes to
962 * complete the transaction, even if it is
965 ssif_info->multi_data = NULL;
967 rv = ssif_i2c_send(ssif_info, msg_written_handler,
969 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
971 I2C_SMBUS_BLOCK_DATA);
973 /* request failed, just return the error. */
974 ssif_inc_stat(ssif_info, send_errors);
976 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
977 pr_info("Error from i2c_non_blocking_op(3)\n");
978 msg_done_handler(ssif_info, -EIO, NULL, 0);
981 /* Ready to request the result. */
982 unsigned long oflags, *flags;
984 ssif_inc_stat(ssif_info, sent_messages);
985 ssif_inc_stat(ssif_info, sent_messages_parts);
987 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
988 if (ssif_info->got_alert) {
989 /* The result is already ready, just start it. */
990 ssif_info->got_alert = false;
991 ipmi_ssif_unlock_cond(ssif_info, flags);
992 start_get(ssif_info);
994 /* Wait a jiffie then request the next message */
995 ssif_info->waiting_alert = true;
996 ssif_info->retries_left = SSIF_RECV_RETRIES;
997 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
998 if (!ssif_info->stopping)
999 mod_timer(&ssif_info->retry_timer,
1000 jiffies + SSIF_MSG_PART_JIFFIES);
1001 ipmi_ssif_unlock_cond(ssif_info, flags);
1006 static int start_resend(struct ssif_info *ssif_info)
1011 ssif_info->got_alert = false;
1013 if (ssif_info->data_len > 32) {
1014 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
1015 ssif_info->multi_data = ssif_info->data;
1016 ssif_info->multi_len = ssif_info->data_len;
1018 * Subtle thing, this is 32, not 33, because we will
1019 * overwrite the thing at position 32 (which was just
1020 * transmitted) with the new length.
1022 ssif_info->multi_pos = 32;
1023 ssif_info->data[0] = 32;
1025 ssif_info->multi_data = NULL;
1026 command = SSIF_IPMI_REQUEST;
1027 ssif_info->data[0] = ssif_info->data_len;
1030 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1031 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1032 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
1033 pr_info("Error from i2c_non_blocking_op(4)\n");
1037 static int start_send(struct ssif_info *ssif_info,
1038 unsigned char *data,
1041 if (len > IPMI_MAX_MSG_LENGTH)
1043 if (len > ssif_info->max_xmit_msg_size)
1046 ssif_info->retries_left = SSIF_SEND_RETRIES;
1047 memcpy(ssif_info->data + 1, data, len);
1048 ssif_info->data_len = len;
1049 return start_resend(ssif_info);
1052 /* Must be called with the message lock held. */
1053 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1055 struct ipmi_smi_msg *msg;
1056 unsigned long oflags;
1059 if (!SSIF_IDLE(ssif_info)) {
1060 ipmi_ssif_unlock_cond(ssif_info, flags);
1064 if (!ssif_info->waiting_msg) {
1065 ssif_info->curr_msg = NULL;
1066 ipmi_ssif_unlock_cond(ssif_info, flags);
1070 ssif_info->curr_msg = ssif_info->waiting_msg;
1071 ssif_info->waiting_msg = NULL;
1072 ipmi_ssif_unlock_cond(ssif_info, flags);
1073 rv = start_send(ssif_info,
1074 ssif_info->curr_msg->data,
1075 ssif_info->curr_msg->data_size);
1077 msg = ssif_info->curr_msg;
1078 ssif_info->curr_msg = NULL;
1079 return_hosed_msg(ssif_info, msg);
1080 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1086 static void sender(void *send_info,
1087 struct ipmi_smi_msg *msg)
1089 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1090 unsigned long oflags, *flags;
1092 BUG_ON(ssif_info->waiting_msg);
1093 ssif_info->waiting_msg = msg;
1095 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1096 start_next_msg(ssif_info, flags);
1098 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1099 struct timespec64 t;
1101 ktime_get_real_ts64(&t);
1102 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1103 msg->data[0], msg->data[1],
1104 (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
1108 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1110 struct ssif_info *ssif_info = send_info;
1112 data->addr_src = ssif_info->addr_source;
1113 data->dev = &ssif_info->client->dev;
1114 data->addr_info = ssif_info->addr_info;
1115 get_device(data->dev);
1121 * Instead of having our own timer to periodically check the message
1122 * flags, we let the message handler drive us.
1124 static void request_events(void *send_info)
1126 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1127 unsigned long oflags, *flags;
1129 if (!ssif_info->has_event_buffer)
1132 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1134 * Request flags first, not events, because the lower layer
1135 * doesn't have a way to send an attention. But make sure
1136 * event checking still happens.
1138 ssif_info->req_events = true;
1139 if (SSIF_IDLE(ssif_info))
1140 start_flag_fetch(ssif_info, flags);
1142 ssif_info->req_flags = true;
1143 ipmi_ssif_unlock_cond(ssif_info, flags);
1147 static int inc_usecount(void *send_info)
1149 struct ssif_info *ssif_info = send_info;
1151 if (!i2c_get_adapter(i2c_adapter_id(ssif_info->client->adapter)))
1154 i2c_use_client(ssif_info->client);
1158 static void dec_usecount(void *send_info)
1160 struct ssif_info *ssif_info = send_info;
1162 i2c_release_client(ssif_info->client);
1163 i2c_put_adapter(ssif_info->client->adapter);
1166 static int ssif_start_processing(void *send_info,
1169 struct ssif_info *ssif_info = send_info;
1171 ssif_info->intf = intf;
1176 #define MAX_SSIF_BMCS 4
1178 static unsigned short addr[MAX_SSIF_BMCS];
1179 static int num_addrs;
1180 module_param_array(addr, ushort, &num_addrs, 0);
1181 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1183 static char *adapter_name[MAX_SSIF_BMCS];
1184 static int num_adapter_names;
1185 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1186 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1188 static int slave_addrs[MAX_SSIF_BMCS];
1189 static int num_slave_addrs;
1190 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1191 MODULE_PARM_DESC(slave_addrs,
1192 "The default IPMB slave address for the controller.");
1194 static bool alerts_broken;
1195 module_param(alerts_broken, bool, 0);
1196 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1199 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1200 * bit 2 enables timing debugging. This is an array indexed by
1203 static int dbg[MAX_SSIF_BMCS];
1205 module_param_array(dbg, int, &num_dbg, 0);
1206 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1208 static bool ssif_dbg_probe;
1209 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1210 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1212 static bool ssif_tryacpi = true;
1213 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1214 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1216 static bool ssif_trydmi = true;
1217 module_param_named(trydmi, ssif_trydmi, bool, 0);
1218 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1220 static DEFINE_MUTEX(ssif_infos_mutex);
1221 static LIST_HEAD(ssif_infos);
1223 static int ssif_remove(struct i2c_client *client)
1225 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1226 struct ssif_addr_info *addr_info;
1233 * After this point, we won't deliver anything asychronously
1234 * to the message handler. We can unregister ourself.
1236 rv = ipmi_unregister_smi(ssif_info->intf);
1238 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1241 ssif_info->intf = NULL;
1243 /* make sure the driver is not looking for flags any more. */
1244 while (ssif_info->ssif_state != SSIF_NORMAL)
1245 schedule_timeout(1);
1247 ssif_info->stopping = true;
1248 del_timer_sync(&ssif_info->retry_timer);
1249 if (ssif_info->thread) {
1250 complete(&ssif_info->wake_thread);
1251 kthread_stop(ssif_info->thread);
1254 list_for_each_entry(addr_info, &ssif_infos, link) {
1255 if (addr_info->client == client) {
1256 addr_info->client = NULL;
1262 * No message can be outstanding now, we have removed the
1263 * upper layer and it permitted us to do so.
1269 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1270 int *resp_len, unsigned char *resp)
1275 retry_cnt = SSIF_SEND_RETRIES;
1277 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1286 retry_cnt = SSIF_RECV_RETRIES;
1287 while (retry_cnt > 0) {
1288 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1292 msleep(SSIF_MSG_MSEC);
1299 /* Validate that the response is correct. */
1301 (resp[0] != (msg[0] | (1 << 2))) ||
1302 (resp[1] != msg[1]))
1313 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1315 unsigned char *resp;
1316 unsigned char msg[3];
1320 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1324 /* Do a Get Device ID command, since it is required. */
1325 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1326 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1327 rv = do_cmd(client, 2, msg, &len, resp);
1331 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1336 static int smi_type_proc_show(struct seq_file *m, void *v)
1338 seq_puts(m, "ssif\n");
1343 static int smi_type_proc_open(struct inode *inode, struct file *file)
1345 return single_open(file, smi_type_proc_show, inode->i_private);
1348 static const struct file_operations smi_type_proc_ops = {
1349 .open = smi_type_proc_open,
1351 .llseek = seq_lseek,
1352 .release = single_release,
1355 static int smi_stats_proc_show(struct seq_file *m, void *v)
1357 struct ssif_info *ssif_info = m->private;
1359 seq_printf(m, "sent_messages: %u\n",
1360 ssif_get_stat(ssif_info, sent_messages));
1361 seq_printf(m, "sent_messages_parts: %u\n",
1362 ssif_get_stat(ssif_info, sent_messages_parts));
1363 seq_printf(m, "send_retries: %u\n",
1364 ssif_get_stat(ssif_info, send_retries));
1365 seq_printf(m, "send_errors: %u\n",
1366 ssif_get_stat(ssif_info, send_errors));
1367 seq_printf(m, "received_messages: %u\n",
1368 ssif_get_stat(ssif_info, received_messages));
1369 seq_printf(m, "received_message_parts: %u\n",
1370 ssif_get_stat(ssif_info, received_message_parts));
1371 seq_printf(m, "receive_retries: %u\n",
1372 ssif_get_stat(ssif_info, receive_retries));
1373 seq_printf(m, "receive_errors: %u\n",
1374 ssif_get_stat(ssif_info, receive_errors));
1375 seq_printf(m, "flag_fetches: %u\n",
1376 ssif_get_stat(ssif_info, flag_fetches));
1377 seq_printf(m, "hosed: %u\n",
1378 ssif_get_stat(ssif_info, hosed));
1379 seq_printf(m, "events: %u\n",
1380 ssif_get_stat(ssif_info, events));
1381 seq_printf(m, "watchdog_pretimeouts: %u\n",
1382 ssif_get_stat(ssif_info, watchdog_pretimeouts));
1383 seq_printf(m, "alerts: %u\n",
1384 ssif_get_stat(ssif_info, alerts));
1388 static int smi_stats_proc_open(struct inode *inode, struct file *file)
1390 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1393 static const struct file_operations smi_stats_proc_ops = {
1394 .open = smi_stats_proc_open,
1396 .llseek = seq_lseek,
1397 .release = single_release,
1400 static int strcmp_nospace(char *s1, char *s2)
1402 while (*s1 && *s2) {
1403 while (isspace(*s1))
1405 while (isspace(*s2))
1417 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1419 bool match_null_name)
1421 struct ssif_addr_info *info, *found = NULL;
1424 list_for_each_entry(info, &ssif_infos, link) {
1425 if (info->binfo.addr == addr) {
1426 if (info->adapter_name || adapter_name) {
1427 if (!info->adapter_name != !adapter_name) {
1428 /* One is NULL and one is not */
1432 strcmp_nospace(info->adapter_name,
1434 /* Names do not match */
1442 if (!found && match_null_name) {
1443 /* Try to get an exact match first, then try with a NULL name */
1444 adapter_name = NULL;
1445 match_null_name = false;
1452 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1455 acpi_handle acpi_handle;
1457 acpi_handle = ACPI_HANDLE(dev);
1459 ssif_info->addr_source = SI_ACPI;
1460 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1467 static int find_slave_address(struct i2c_client *client, int slave_addr)
1469 #ifdef CONFIG_IPMI_DMI_DECODE
1471 slave_addr = ipmi_dmi_get_slave_addr(
1473 i2c_adapter_id(client->adapter),
1481 * Global enables we care about.
1483 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1484 IPMI_BMC_EVT_MSG_INTR)
1486 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1488 unsigned char msg[3];
1489 unsigned char *resp;
1490 struct ssif_info *ssif_info;
1495 struct ssif_addr_info *addr_info = NULL;
1497 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1501 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1507 if (!check_acpi(ssif_info, &client->dev)) {
1508 addr_info = ssif_info_find(client->addr, client->adapter->name,
1511 /* Must have come in through sysfs. */
1512 ssif_info->addr_source = SI_HOTMOD;
1514 ssif_info->addr_source = addr_info->addr_src;
1515 ssif_info->ssif_debug = addr_info->debug;
1516 ssif_info->addr_info = addr_info->addr_info;
1517 addr_info->client = client;
1518 slave_addr = addr_info->slave_addr;
1522 slave_addr = find_slave_address(client, slave_addr);
1524 pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1525 ipmi_addr_src_to_str(ssif_info->addr_source),
1526 client->addr, client->adapter->name, slave_addr);
1529 * Do a Get Device ID command, since it comes back with some
1532 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1533 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1534 rv = do_cmd(client, 2, msg, &len, resp);
1538 rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1542 ssif_info->client = client;
1543 i2c_set_clientdata(client, ssif_info);
1545 /* Now check for system interface capabilities */
1546 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1547 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1548 msg[2] = 0; /* SSIF */
1549 rv = do_cmd(client, 3, msg, &len, resp);
1550 if (!rv && (len >= 3) && (resp[2] == 0)) {
1553 pr_info(PFX "SSIF info too short: %d\n", len);
1557 /* Got a good SSIF response, handle it. */
1558 ssif_info->max_xmit_msg_size = resp[5];
1559 ssif_info->max_recv_msg_size = resp[6];
1560 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1561 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1563 /* Sanitize the data */
1564 switch (ssif_info->multi_support) {
1566 if (ssif_info->max_xmit_msg_size > 32)
1567 ssif_info->max_xmit_msg_size = 32;
1568 if (ssif_info->max_recv_msg_size > 32)
1569 ssif_info->max_recv_msg_size = 32;
1572 case SSIF_MULTI_2_PART:
1573 if (ssif_info->max_xmit_msg_size > 63)
1574 ssif_info->max_xmit_msg_size = 63;
1575 if (ssif_info->max_recv_msg_size > 62)
1576 ssif_info->max_recv_msg_size = 62;
1579 case SSIF_MULTI_n_PART:
1581 * The specification is rather confusing at
1582 * this point, but I think I understand what
1583 * is meant. At least I have a workable
1584 * solution. With multi-part messages, you
1585 * cannot send a message that is a multiple of
1586 * 32-bytes in length, because the start and
1587 * middle messages are 32-bytes and the end
1588 * message must be at least one byte. You
1589 * can't fudge on an extra byte, that would
1590 * screw up things like fru data writes. So
1591 * we limit the length to 63 bytes. That way
1592 * a 32-byte message gets sent as a single
1593 * part. A larger message will be a 32-byte
1594 * start and the next message is always going
1595 * to be 1-31 bytes in length. Not ideal, but
1598 if (ssif_info->max_xmit_msg_size > 63)
1599 ssif_info->max_xmit_msg_size = 63;
1603 /* Data is not sane, just give up. */
1608 /* Assume no multi-part or PEC support */
1609 pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1612 ssif_info->max_xmit_msg_size = 32;
1613 ssif_info->max_recv_msg_size = 32;
1614 ssif_info->multi_support = SSIF_NO_MULTI;
1615 ssif_info->supports_pec = 0;
1618 /* Make sure the NMI timeout is cleared. */
1619 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1620 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1621 msg[2] = WDT_PRE_TIMEOUT_INT;
1622 rv = do_cmd(client, 3, msg, &len, resp);
1623 if (rv || (len < 3) || (resp[2] != 0))
1624 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1627 /* Attempt to enable the event buffer. */
1628 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1629 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1630 rv = do_cmd(client, 2, msg, &len, resp);
1631 if (rv || (len < 4) || (resp[2] != 0)) {
1632 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1634 rv = 0; /* Not fatal */
1638 ssif_info->global_enables = resp[3];
1640 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1641 ssif_info->has_event_buffer = true;
1642 /* buffer is already enabled, nothing to do. */
1646 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1647 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1648 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1649 rv = do_cmd(client, 3, msg, &len, resp);
1650 if (rv || (len < 2)) {
1651 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1653 rv = 0; /* Not fatal */
1658 /* A successful return means the event buffer is supported. */
1659 ssif_info->has_event_buffer = true;
1660 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1663 /* Some systems don't behave well if you enable alerts. */
1667 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1668 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1669 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1670 rv = do_cmd(client, 3, msg, &len, resp);
1671 if (rv || (len < 2)) {
1672 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1674 rv = 0; /* Not fatal */
1679 /* A successful return means the alert is supported. */
1680 ssif_info->supports_alert = true;
1681 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1685 ssif_info->intf_num = atomic_inc_return(&next_intf);
1687 if (ssif_dbg_probe) {
1688 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1692 spin_lock_init(&ssif_info->lock);
1693 ssif_info->ssif_state = SSIF_NORMAL;
1694 setup_timer(&ssif_info->retry_timer, retry_timeout,
1695 (unsigned long)ssif_info);
1697 for (i = 0; i < SSIF_NUM_STATS; i++)
1698 atomic_set(&ssif_info->stats[i], 0);
1700 if (ssif_info->supports_pec)
1701 ssif_info->client->flags |= I2C_CLIENT_PEC;
1703 ssif_info->handlers.owner = THIS_MODULE;
1704 ssif_info->handlers.start_processing = ssif_start_processing;
1705 ssif_info->handlers.get_smi_info = get_smi_info;
1706 ssif_info->handlers.sender = sender;
1707 ssif_info->handlers.request_events = request_events;
1708 ssif_info->handlers.inc_usecount = inc_usecount;
1709 ssif_info->handlers.dec_usecount = dec_usecount;
1712 unsigned int thread_num;
1714 thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1716 ssif_info->client->addr);
1717 init_completion(&ssif_info->wake_thread);
1718 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1719 "kssif%4.4x", thread_num);
1720 if (IS_ERR(ssif_info->thread)) {
1721 rv = PTR_ERR(ssif_info->thread);
1722 dev_notice(&ssif_info->client->dev,
1723 "Could not start kernel thread: error %d\n",
1729 rv = ipmi_register_smi(&ssif_info->handlers,
1731 &ssif_info->device_id,
1732 &ssif_info->client->dev,
1735 pr_err(PFX "Unable to register device: error %d\n", rv);
1739 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1743 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1747 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1748 &smi_stats_proc_ops,
1751 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1758 addr_info->client = NULL;
1760 dev_err(&client->dev, "Unable to start IPMI SSIF: %d\n", rv);
1767 ipmi_unregister_smi(ssif_info->intf);
1771 static int ssif_adapter_handler(struct device *adev, void *opaque)
1773 struct ssif_addr_info *addr_info = opaque;
1775 if (adev->type != &i2c_adapter_type)
1778 addr_info->added_client = i2c_new_device(to_i2c_adapter(adev),
1781 if (!addr_info->adapter_name)
1782 return 1; /* Only try the first I2C adapter by default. */
1786 static int new_ssif_client(int addr, char *adapter_name,
1787 int debug, int slave_addr,
1788 enum ipmi_addr_src addr_src,
1791 struct ssif_addr_info *addr_info;
1794 mutex_lock(&ssif_infos_mutex);
1795 if (ssif_info_find(addr, adapter_name, false)) {
1800 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1807 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1808 if (!addr_info->adapter_name) {
1815 strncpy(addr_info->binfo.type, DEVICE_NAME,
1816 sizeof(addr_info->binfo.type));
1817 addr_info->binfo.addr = addr;
1818 addr_info->binfo.platform_data = addr_info;
1819 addr_info->debug = debug;
1820 addr_info->slave_addr = slave_addr;
1821 addr_info->addr_src = addr_src;
1822 addr_info->dev = dev;
1825 dev_set_drvdata(dev, addr_info);
1827 list_add_tail(&addr_info->link, &ssif_infos);
1830 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1831 /* Otherwise address list will get it */
1834 mutex_unlock(&ssif_infos_mutex);
1838 static void free_ssif_clients(void)
1840 struct ssif_addr_info *info, *tmp;
1842 mutex_lock(&ssif_infos_mutex);
1843 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1844 list_del(&info->link);
1845 kfree(info->adapter_name);
1848 mutex_unlock(&ssif_infos_mutex);
1851 static unsigned short *ssif_address_list(void)
1853 struct ssif_addr_info *info;
1854 unsigned int count = 0, i;
1855 unsigned short *address_list;
1857 list_for_each_entry(info, &ssif_infos, link)
1860 address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1865 list_for_each_entry(info, &ssif_infos, link) {
1866 unsigned short addr = info->binfo.addr;
1869 for (j = 0; j < i; j++) {
1870 if (address_list[j] == addr)
1873 address_list[i] = addr;
1877 address_list[i] = I2C_CLIENT_END;
1879 return address_list;
1883 static const struct acpi_device_id ssif_acpi_match[] = {
1887 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1890 * Once we get an ACPI failure, we don't try any more, because we go
1891 * through the tables sequentially. Once we don't find a table, there
1894 static int acpi_failure;
1897 * Defined in the IPMI 2.0 spec.
1908 s8 CreatorRevision[4];
1911 s16 SpecificationRevision;
1914 * Bit 0 - SCI interrupt supported
1915 * Bit 1 - I/O APIC/SAPIC
1920 * If bit 0 of InterruptType is set, then this is the SCI
1921 * interrupt in the GPEx_STS register.
1928 * If bit 1 of InterruptType is set, then this is the I/O
1929 * APIC/SAPIC interrupt.
1931 u32 GlobalSystemInterrupt;
1933 /* The actual register address. */
1934 struct acpi_generic_address addr;
1938 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1941 static int try_init_spmi(struct SPMITable *spmi)
1943 unsigned short myaddr;
1945 if (num_addrs >= MAX_SSIF_BMCS)
1948 if (spmi->IPMIlegacy != 1) {
1949 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1953 if (spmi->InterfaceType != 4)
1956 if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1957 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1958 spmi->addr.space_id);
1962 myaddr = spmi->addr.address & 0x7f;
1964 return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI, NULL);
1967 static void spmi_find_bmc(void)
1970 struct SPMITable *spmi;
1979 for (i = 0; ; i++) {
1980 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1981 (struct acpi_table_header **)&spmi);
1982 if (status != AE_OK)
1985 try_init_spmi(spmi);
1989 static void spmi_find_bmc(void) { }
1993 static int dmi_ipmi_probe(struct platform_device *pdev)
1995 u8 type, slave_addr = 0;
2002 rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type);
2006 if (type != IPMI_DMI_TYPE_SSIF)
2009 rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
2011 dev_warn(&pdev->dev, PFX "No i2c-addr property\n");
2015 rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
2017 dev_warn(&pdev->dev, "device has no slave-addr property");
2019 return new_ssif_client(i2c_addr, NULL, 0,
2020 slave_addr, SI_SMBIOS, &pdev->dev);
2023 static int dmi_ipmi_probe(struct platform_device *pdev)
2029 static const struct i2c_device_id ssif_id[] = {
2033 MODULE_DEVICE_TABLE(i2c, ssif_id);
2035 static struct i2c_driver ssif_i2c_driver = {
2036 .class = I2C_CLASS_HWMON,
2040 .probe = ssif_probe,
2041 .remove = ssif_remove,
2042 .alert = ssif_alert,
2043 .id_table = ssif_id,
2044 .detect = ssif_detect
2047 static int ssif_platform_probe(struct platform_device *dev)
2049 return dmi_ipmi_probe(dev);
2052 static int ssif_platform_remove(struct platform_device *dev)
2054 struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2059 mutex_lock(&ssif_infos_mutex);
2060 if (addr_info->added_client)
2061 i2c_unregister_device(addr_info->added_client);
2063 list_del(&addr_info->link);
2065 mutex_unlock(&ssif_infos_mutex);
2069 static struct platform_driver ipmi_driver = {
2071 .name = DEVICE_NAME,
2073 .probe = ssif_platform_probe,
2074 .remove = ssif_platform_remove,
2077 static int init_ipmi_ssif(void)
2085 pr_info("IPMI SSIF Interface driver\n");
2087 /* build list for i2c from addr list */
2088 for (i = 0; i < num_addrs; i++) {
2089 rv = new_ssif_client(addr[i], adapter_name[i],
2090 dbg[i], slave_addrs[i],
2091 SI_HARDCODED, NULL);
2094 "Couldn't add hardcoded device at addr 0x%x\n",
2099 ssif_i2c_driver.driver.acpi_match_table =
2100 ACPI_PTR(ssif_acpi_match);
2106 rv = platform_driver_register(&ipmi_driver);
2108 pr_err(PFX "Unable to register driver: %d\n", rv);
2111 ssif_i2c_driver.address_list = ssif_address_list();
2113 rv = i2c_add_driver(&ssif_i2c_driver);
2119 module_init(init_ipmi_ssif);
2121 static void cleanup_ipmi_ssif(void)
2126 initialized = false;
2128 i2c_del_driver(&ssif_i2c_driver);
2130 platform_driver_unregister(&ipmi_driver);
2132 free_ssif_clients();
2134 module_exit(cleanup_ipmi_ssif);
2136 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2137 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2138 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2139 MODULE_LICENSE("GPL");