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:
819 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
820 /* Error getting event, probably done. */
823 /* Take off the event flag. */
824 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
825 handle_flags(ssif_info, flags);
826 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
827 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
828 pr_warn(PFX "Invalid response getting events: %x %x\n",
829 msg->rsp[0], msg->rsp[1]);
831 /* Take off the event flag. */
832 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
833 handle_flags(ssif_info, flags);
835 handle_flags(ssif_info, flags);
836 ssif_inc_stat(ssif_info, events);
837 deliver_recv_msg(ssif_info, msg);
841 case SSIF_GETTING_MESSAGES:
842 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
843 /* Error getting event, probably done. */
846 /* Take off the msg flag. */
847 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
848 handle_flags(ssif_info, flags);
849 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
850 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
851 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
852 msg->rsp[0], msg->rsp[1]);
855 /* Take off the msg flag. */
856 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
857 handle_flags(ssif_info, flags);
859 ssif_inc_stat(ssif_info, incoming_messages);
860 handle_flags(ssif_info, flags);
861 deliver_recv_msg(ssif_info, msg);
866 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
867 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
868 if (ssif_info->req_events)
869 start_event_fetch(ssif_info, flags);
870 else if (ssif_info->req_flags)
871 start_flag_fetch(ssif_info, flags);
873 start_next_msg(ssif_info, flags);
875 ipmi_ssif_unlock_cond(ssif_info, flags);
877 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
878 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
881 static void msg_written_handler(struct ssif_info *ssif_info, int result,
882 unsigned char *data, unsigned int len)
886 /* We are single-threaded here, so no need for a lock. */
888 ssif_info->retries_left--;
889 if (ssif_info->retries_left > 0) {
890 if (!start_resend(ssif_info)) {
891 ssif_inc_stat(ssif_info, send_retries);
894 /* request failed, just return the error. */
895 ssif_inc_stat(ssif_info, send_errors);
897 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
899 "Out of retries in msg_written_handler\n");
900 msg_done_handler(ssif_info, -EIO, NULL, 0);
904 ssif_inc_stat(ssif_info, send_errors);
907 * Got an error on transmit, let the done routine
910 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
911 pr_info("Error in msg_written_handler: %d\n", result);
913 msg_done_handler(ssif_info, result, NULL, 0);
917 if (ssif_info->multi_data) {
919 * In the middle of a multi-data write. See the comment
920 * in the SSIF_MULTI_n_PART case in the probe function
921 * for details on the intricacies of this.
924 unsigned char *data_to_send;
926 ssif_inc_stat(ssif_info, sent_messages_parts);
928 left = ssif_info->multi_len - ssif_info->multi_pos;
932 ssif_info->multi_data[ssif_info->multi_pos] = left;
933 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
934 ssif_info->multi_pos += left;
937 * Write is finished. Note that we must end
938 * with a write of less than 32 bytes to
939 * complete the transaction, even if it is
942 ssif_info->multi_data = NULL;
944 rv = ssif_i2c_send(ssif_info, msg_written_handler,
946 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
948 I2C_SMBUS_BLOCK_DATA);
950 /* request failed, just return the error. */
951 ssif_inc_stat(ssif_info, send_errors);
953 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
954 pr_info("Error from i2c_non_blocking_op(3)\n");
955 msg_done_handler(ssif_info, -EIO, NULL, 0);
958 /* Ready to request the result. */
959 unsigned long oflags, *flags;
961 ssif_inc_stat(ssif_info, sent_messages);
962 ssif_inc_stat(ssif_info, sent_messages_parts);
964 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
965 if (ssif_info->got_alert) {
966 /* The result is already ready, just start it. */
967 ssif_info->got_alert = false;
968 ipmi_ssif_unlock_cond(ssif_info, flags);
969 start_get(ssif_info);
971 /* Wait a jiffie then request the next message */
972 ssif_info->waiting_alert = true;
973 ssif_info->retries_left = SSIF_RECV_RETRIES;
974 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
975 if (!ssif_info->stopping)
976 mod_timer(&ssif_info->retry_timer,
977 jiffies + SSIF_MSG_PART_JIFFIES);
978 ipmi_ssif_unlock_cond(ssif_info, flags);
983 static int start_resend(struct ssif_info *ssif_info)
988 ssif_info->got_alert = false;
990 if (ssif_info->data_len > 32) {
991 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
992 ssif_info->multi_data = ssif_info->data;
993 ssif_info->multi_len = ssif_info->data_len;
995 * Subtle thing, this is 32, not 33, because we will
996 * overwrite the thing at position 32 (which was just
997 * transmitted) with the new length.
999 ssif_info->multi_pos = 32;
1000 ssif_info->data[0] = 32;
1002 ssif_info->multi_data = NULL;
1003 command = SSIF_IPMI_REQUEST;
1004 ssif_info->data[0] = ssif_info->data_len;
1007 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1008 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1009 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
1010 pr_info("Error from i2c_non_blocking_op(4)\n");
1014 static int start_send(struct ssif_info *ssif_info,
1015 unsigned char *data,
1018 if (len > IPMI_MAX_MSG_LENGTH)
1020 if (len > ssif_info->max_xmit_msg_size)
1023 ssif_info->retries_left = SSIF_SEND_RETRIES;
1024 memcpy(ssif_info->data + 1, data, len);
1025 ssif_info->data_len = len;
1026 return start_resend(ssif_info);
1029 /* Must be called with the message lock held. */
1030 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1032 struct ipmi_smi_msg *msg;
1033 unsigned long oflags;
1036 if (!SSIF_IDLE(ssif_info)) {
1037 ipmi_ssif_unlock_cond(ssif_info, flags);
1041 if (!ssif_info->waiting_msg) {
1042 ssif_info->curr_msg = NULL;
1043 ipmi_ssif_unlock_cond(ssif_info, flags);
1047 ssif_info->curr_msg = ssif_info->waiting_msg;
1048 ssif_info->waiting_msg = NULL;
1049 ipmi_ssif_unlock_cond(ssif_info, flags);
1050 rv = start_send(ssif_info,
1051 ssif_info->curr_msg->data,
1052 ssif_info->curr_msg->data_size);
1054 msg = ssif_info->curr_msg;
1055 ssif_info->curr_msg = NULL;
1056 return_hosed_msg(ssif_info, msg);
1057 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1063 static void sender(void *send_info,
1064 struct ipmi_smi_msg *msg)
1066 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1067 unsigned long oflags, *flags;
1069 BUG_ON(ssif_info->waiting_msg);
1070 ssif_info->waiting_msg = msg;
1072 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1073 start_next_msg(ssif_info, flags);
1075 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1076 struct timespec64 t;
1078 ktime_get_real_ts64(&t);
1079 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1080 msg->data[0], msg->data[1],
1081 (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
1085 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1087 struct ssif_info *ssif_info = send_info;
1089 data->addr_src = ssif_info->addr_source;
1090 data->dev = &ssif_info->client->dev;
1091 data->addr_info = ssif_info->addr_info;
1092 get_device(data->dev);
1098 * Instead of having our own timer to periodically check the message
1099 * flags, we let the message handler drive us.
1101 static void request_events(void *send_info)
1103 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1104 unsigned long oflags, *flags;
1106 if (!ssif_info->has_event_buffer)
1109 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1111 * Request flags first, not events, because the lower layer
1112 * doesn't have a way to send an attention. But make sure
1113 * event checking still happens.
1115 ssif_info->req_events = true;
1116 if (SSIF_IDLE(ssif_info))
1117 start_flag_fetch(ssif_info, flags);
1119 ssif_info->req_flags = true;
1120 ipmi_ssif_unlock_cond(ssif_info, flags);
1124 static int inc_usecount(void *send_info)
1126 struct ssif_info *ssif_info = send_info;
1128 if (!i2c_get_adapter(i2c_adapter_id(ssif_info->client->adapter)))
1131 i2c_use_client(ssif_info->client);
1135 static void dec_usecount(void *send_info)
1137 struct ssif_info *ssif_info = send_info;
1139 i2c_release_client(ssif_info->client);
1140 i2c_put_adapter(ssif_info->client->adapter);
1143 static int ssif_start_processing(void *send_info,
1146 struct ssif_info *ssif_info = send_info;
1148 ssif_info->intf = intf;
1153 #define MAX_SSIF_BMCS 4
1155 static unsigned short addr[MAX_SSIF_BMCS];
1156 static int num_addrs;
1157 module_param_array(addr, ushort, &num_addrs, 0);
1158 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1160 static char *adapter_name[MAX_SSIF_BMCS];
1161 static int num_adapter_names;
1162 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1163 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1165 static int slave_addrs[MAX_SSIF_BMCS];
1166 static int num_slave_addrs;
1167 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1168 MODULE_PARM_DESC(slave_addrs,
1169 "The default IPMB slave address for the controller.");
1171 static bool alerts_broken;
1172 module_param(alerts_broken, bool, 0);
1173 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1176 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1177 * bit 2 enables timing debugging. This is an array indexed by
1180 static int dbg[MAX_SSIF_BMCS];
1182 module_param_array(dbg, int, &num_dbg, 0);
1183 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1185 static bool ssif_dbg_probe;
1186 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1187 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1189 static bool ssif_tryacpi = true;
1190 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1191 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1193 static bool ssif_trydmi = true;
1194 module_param_named(trydmi, ssif_trydmi, bool, 0);
1195 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1197 static DEFINE_MUTEX(ssif_infos_mutex);
1198 static LIST_HEAD(ssif_infos);
1200 static int ssif_remove(struct i2c_client *client)
1202 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1203 struct ssif_addr_info *addr_info;
1210 * After this point, we won't deliver anything asychronously
1211 * to the message handler. We can unregister ourself.
1213 rv = ipmi_unregister_smi(ssif_info->intf);
1215 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1218 ssif_info->intf = NULL;
1220 /* make sure the driver is not looking for flags any more. */
1221 while (ssif_info->ssif_state != SSIF_NORMAL)
1222 schedule_timeout(1);
1224 ssif_info->stopping = true;
1225 del_timer_sync(&ssif_info->retry_timer);
1226 if (ssif_info->thread) {
1227 complete(&ssif_info->wake_thread);
1228 kthread_stop(ssif_info->thread);
1231 list_for_each_entry(addr_info, &ssif_infos, link) {
1232 if (addr_info->client == client) {
1233 addr_info->client = NULL;
1239 * No message can be outstanding now, we have removed the
1240 * upper layer and it permitted us to do so.
1246 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1247 int *resp_len, unsigned char *resp)
1252 retry_cnt = SSIF_SEND_RETRIES;
1254 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1263 retry_cnt = SSIF_RECV_RETRIES;
1264 while (retry_cnt > 0) {
1265 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1269 msleep(SSIF_MSG_MSEC);
1276 /* Validate that the response is correct. */
1278 (resp[0] != (msg[0] | (1 << 2))) ||
1279 (resp[1] != msg[1]))
1290 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1292 unsigned char *resp;
1293 unsigned char msg[3];
1297 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1301 /* Do a Get Device ID command, since it is required. */
1302 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1303 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1304 rv = do_cmd(client, 2, msg, &len, resp);
1308 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1313 static int smi_type_proc_show(struct seq_file *m, void *v)
1315 seq_puts(m, "ssif\n");
1320 static int smi_type_proc_open(struct inode *inode, struct file *file)
1322 return single_open(file, smi_type_proc_show, inode->i_private);
1325 static const struct file_operations smi_type_proc_ops = {
1326 .open = smi_type_proc_open,
1328 .llseek = seq_lseek,
1329 .release = single_release,
1332 static int smi_stats_proc_show(struct seq_file *m, void *v)
1334 struct ssif_info *ssif_info = m->private;
1336 seq_printf(m, "sent_messages: %u\n",
1337 ssif_get_stat(ssif_info, sent_messages));
1338 seq_printf(m, "sent_messages_parts: %u\n",
1339 ssif_get_stat(ssif_info, sent_messages_parts));
1340 seq_printf(m, "send_retries: %u\n",
1341 ssif_get_stat(ssif_info, send_retries));
1342 seq_printf(m, "send_errors: %u\n",
1343 ssif_get_stat(ssif_info, send_errors));
1344 seq_printf(m, "received_messages: %u\n",
1345 ssif_get_stat(ssif_info, received_messages));
1346 seq_printf(m, "received_message_parts: %u\n",
1347 ssif_get_stat(ssif_info, received_message_parts));
1348 seq_printf(m, "receive_retries: %u\n",
1349 ssif_get_stat(ssif_info, receive_retries));
1350 seq_printf(m, "receive_errors: %u\n",
1351 ssif_get_stat(ssif_info, receive_errors));
1352 seq_printf(m, "flag_fetches: %u\n",
1353 ssif_get_stat(ssif_info, flag_fetches));
1354 seq_printf(m, "hosed: %u\n",
1355 ssif_get_stat(ssif_info, hosed));
1356 seq_printf(m, "events: %u\n",
1357 ssif_get_stat(ssif_info, events));
1358 seq_printf(m, "watchdog_pretimeouts: %u\n",
1359 ssif_get_stat(ssif_info, watchdog_pretimeouts));
1360 seq_printf(m, "alerts: %u\n",
1361 ssif_get_stat(ssif_info, alerts));
1365 static int smi_stats_proc_open(struct inode *inode, struct file *file)
1367 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1370 static const struct file_operations smi_stats_proc_ops = {
1371 .open = smi_stats_proc_open,
1373 .llseek = seq_lseek,
1374 .release = single_release,
1377 static int strcmp_nospace(char *s1, char *s2)
1379 while (*s1 && *s2) {
1380 while (isspace(*s1))
1382 while (isspace(*s2))
1394 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1396 bool match_null_name)
1398 struct ssif_addr_info *info, *found = NULL;
1401 list_for_each_entry(info, &ssif_infos, link) {
1402 if (info->binfo.addr == addr) {
1403 if (info->adapter_name || adapter_name) {
1404 if (!info->adapter_name != !adapter_name) {
1405 /* One is NULL and one is not */
1409 strcmp_nospace(info->adapter_name,
1411 /* Names do not match */
1419 if (!found && match_null_name) {
1420 /* Try to get an exact match first, then try with a NULL name */
1421 adapter_name = NULL;
1422 match_null_name = false;
1429 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1432 acpi_handle acpi_handle;
1434 acpi_handle = ACPI_HANDLE(dev);
1436 ssif_info->addr_source = SI_ACPI;
1437 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1444 static int find_slave_address(struct i2c_client *client, int slave_addr)
1446 #ifdef CONFIG_IPMI_DMI_DECODE
1448 slave_addr = ipmi_dmi_get_slave_addr(
1450 i2c_adapter_id(client->adapter),
1458 * Global enables we care about.
1460 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1461 IPMI_BMC_EVT_MSG_INTR)
1463 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1465 unsigned char msg[3];
1466 unsigned char *resp;
1467 struct ssif_info *ssif_info;
1472 struct ssif_addr_info *addr_info = NULL;
1474 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1478 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1484 if (!check_acpi(ssif_info, &client->dev)) {
1485 addr_info = ssif_info_find(client->addr, client->adapter->name,
1488 /* Must have come in through sysfs. */
1489 ssif_info->addr_source = SI_HOTMOD;
1491 ssif_info->addr_source = addr_info->addr_src;
1492 ssif_info->ssif_debug = addr_info->debug;
1493 ssif_info->addr_info = addr_info->addr_info;
1494 addr_info->client = client;
1495 slave_addr = addr_info->slave_addr;
1499 slave_addr = find_slave_address(client, slave_addr);
1501 pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1502 ipmi_addr_src_to_str(ssif_info->addr_source),
1503 client->addr, client->adapter->name, slave_addr);
1506 * Do a Get Device ID command, since it comes back with some
1509 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1510 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1511 rv = do_cmd(client, 2, msg, &len, resp);
1515 rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1519 ssif_info->client = client;
1520 i2c_set_clientdata(client, ssif_info);
1522 /* Now check for system interface capabilities */
1523 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1524 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1525 msg[2] = 0; /* SSIF */
1526 rv = do_cmd(client, 3, msg, &len, resp);
1527 if (!rv && (len >= 3) && (resp[2] == 0)) {
1530 pr_info(PFX "SSIF info too short: %d\n", len);
1534 /* Got a good SSIF response, handle it. */
1535 ssif_info->max_xmit_msg_size = resp[5];
1536 ssif_info->max_recv_msg_size = resp[6];
1537 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1538 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1540 /* Sanitize the data */
1541 switch (ssif_info->multi_support) {
1543 if (ssif_info->max_xmit_msg_size > 32)
1544 ssif_info->max_xmit_msg_size = 32;
1545 if (ssif_info->max_recv_msg_size > 32)
1546 ssif_info->max_recv_msg_size = 32;
1549 case SSIF_MULTI_2_PART:
1550 if (ssif_info->max_xmit_msg_size > 63)
1551 ssif_info->max_xmit_msg_size = 63;
1552 if (ssif_info->max_recv_msg_size > 62)
1553 ssif_info->max_recv_msg_size = 62;
1556 case SSIF_MULTI_n_PART:
1558 * The specification is rather confusing at
1559 * this point, but I think I understand what
1560 * is meant. At least I have a workable
1561 * solution. With multi-part messages, you
1562 * cannot send a message that is a multiple of
1563 * 32-bytes in length, because the start and
1564 * middle messages are 32-bytes and the end
1565 * message must be at least one byte. You
1566 * can't fudge on an extra byte, that would
1567 * screw up things like fru data writes. So
1568 * we limit the length to 63 bytes. That way
1569 * a 32-byte message gets sent as a single
1570 * part. A larger message will be a 32-byte
1571 * start and the next message is always going
1572 * to be 1-31 bytes in length. Not ideal, but
1575 if (ssif_info->max_xmit_msg_size > 63)
1576 ssif_info->max_xmit_msg_size = 63;
1580 /* Data is not sane, just give up. */
1585 /* Assume no multi-part or PEC support */
1586 pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1589 ssif_info->max_xmit_msg_size = 32;
1590 ssif_info->max_recv_msg_size = 32;
1591 ssif_info->multi_support = SSIF_NO_MULTI;
1592 ssif_info->supports_pec = 0;
1595 /* Make sure the NMI timeout is cleared. */
1596 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1597 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1598 msg[2] = WDT_PRE_TIMEOUT_INT;
1599 rv = do_cmd(client, 3, msg, &len, resp);
1600 if (rv || (len < 3) || (resp[2] != 0))
1601 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1604 /* Attempt to enable the event buffer. */
1605 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1606 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1607 rv = do_cmd(client, 2, msg, &len, resp);
1608 if (rv || (len < 4) || (resp[2] != 0)) {
1609 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1611 rv = 0; /* Not fatal */
1615 ssif_info->global_enables = resp[3];
1617 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1618 ssif_info->has_event_buffer = true;
1619 /* buffer is already enabled, nothing to do. */
1623 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1624 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1625 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1626 rv = do_cmd(client, 3, msg, &len, resp);
1627 if (rv || (len < 2)) {
1628 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1630 rv = 0; /* Not fatal */
1635 /* A successful return means the event buffer is supported. */
1636 ssif_info->has_event_buffer = true;
1637 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1640 /* Some systems don't behave well if you enable alerts. */
1644 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1645 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1646 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1647 rv = do_cmd(client, 3, msg, &len, resp);
1648 if (rv || (len < 2)) {
1649 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1651 rv = 0; /* Not fatal */
1656 /* A successful return means the alert is supported. */
1657 ssif_info->supports_alert = true;
1658 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1662 ssif_info->intf_num = atomic_inc_return(&next_intf);
1664 if (ssif_dbg_probe) {
1665 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1669 spin_lock_init(&ssif_info->lock);
1670 ssif_info->ssif_state = SSIF_NORMAL;
1671 setup_timer(&ssif_info->retry_timer, retry_timeout,
1672 (unsigned long)ssif_info);
1674 for (i = 0; i < SSIF_NUM_STATS; i++)
1675 atomic_set(&ssif_info->stats[i], 0);
1677 if (ssif_info->supports_pec)
1678 ssif_info->client->flags |= I2C_CLIENT_PEC;
1680 ssif_info->handlers.owner = THIS_MODULE;
1681 ssif_info->handlers.start_processing = ssif_start_processing;
1682 ssif_info->handlers.get_smi_info = get_smi_info;
1683 ssif_info->handlers.sender = sender;
1684 ssif_info->handlers.request_events = request_events;
1685 ssif_info->handlers.inc_usecount = inc_usecount;
1686 ssif_info->handlers.dec_usecount = dec_usecount;
1689 unsigned int thread_num;
1691 thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1693 ssif_info->client->addr);
1694 init_completion(&ssif_info->wake_thread);
1695 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1696 "kssif%4.4x", thread_num);
1697 if (IS_ERR(ssif_info->thread)) {
1698 rv = PTR_ERR(ssif_info->thread);
1699 dev_notice(&ssif_info->client->dev,
1700 "Could not start kernel thread: error %d\n",
1706 rv = ipmi_register_smi(&ssif_info->handlers,
1708 &ssif_info->device_id,
1709 &ssif_info->client->dev,
1712 pr_err(PFX "Unable to register device: error %d\n", rv);
1716 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1720 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1724 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1725 &smi_stats_proc_ops,
1728 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1735 addr_info->client = NULL;
1737 dev_err(&client->dev, "Unable to start IPMI SSIF: %d\n", rv);
1744 ipmi_unregister_smi(ssif_info->intf);
1748 static int ssif_adapter_handler(struct device *adev, void *opaque)
1750 struct ssif_addr_info *addr_info = opaque;
1752 if (adev->type != &i2c_adapter_type)
1755 addr_info->added_client = i2c_new_device(to_i2c_adapter(adev),
1758 if (!addr_info->adapter_name)
1759 return 1; /* Only try the first I2C adapter by default. */
1763 static int new_ssif_client(int addr, char *adapter_name,
1764 int debug, int slave_addr,
1765 enum ipmi_addr_src addr_src,
1768 struct ssif_addr_info *addr_info;
1771 mutex_lock(&ssif_infos_mutex);
1772 if (ssif_info_find(addr, adapter_name, false)) {
1777 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1784 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1785 if (!addr_info->adapter_name) {
1792 strncpy(addr_info->binfo.type, DEVICE_NAME,
1793 sizeof(addr_info->binfo.type));
1794 addr_info->binfo.addr = addr;
1795 addr_info->binfo.platform_data = addr_info;
1796 addr_info->debug = debug;
1797 addr_info->slave_addr = slave_addr;
1798 addr_info->addr_src = addr_src;
1799 addr_info->dev = dev;
1802 dev_set_drvdata(dev, addr_info);
1804 list_add_tail(&addr_info->link, &ssif_infos);
1807 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1808 /* Otherwise address list will get it */
1811 mutex_unlock(&ssif_infos_mutex);
1815 static void free_ssif_clients(void)
1817 struct ssif_addr_info *info, *tmp;
1819 mutex_lock(&ssif_infos_mutex);
1820 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1821 list_del(&info->link);
1822 kfree(info->adapter_name);
1825 mutex_unlock(&ssif_infos_mutex);
1828 static unsigned short *ssif_address_list(void)
1830 struct ssif_addr_info *info;
1831 unsigned int count = 0, i;
1832 unsigned short *address_list;
1834 list_for_each_entry(info, &ssif_infos, link)
1837 address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1842 list_for_each_entry(info, &ssif_infos, link) {
1843 unsigned short addr = info->binfo.addr;
1846 for (j = 0; j < i; j++) {
1847 if (address_list[j] == addr)
1850 address_list[i] = addr;
1854 address_list[i] = I2C_CLIENT_END;
1856 return address_list;
1860 static const struct acpi_device_id ssif_acpi_match[] = {
1864 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1867 * Once we get an ACPI failure, we don't try any more, because we go
1868 * through the tables sequentially. Once we don't find a table, there
1871 static int acpi_failure;
1874 * Defined in the IPMI 2.0 spec.
1885 s8 CreatorRevision[4];
1888 s16 SpecificationRevision;
1891 * Bit 0 - SCI interrupt supported
1892 * Bit 1 - I/O APIC/SAPIC
1897 * If bit 0 of InterruptType is set, then this is the SCI
1898 * interrupt in the GPEx_STS register.
1905 * If bit 1 of InterruptType is set, then this is the I/O
1906 * APIC/SAPIC interrupt.
1908 u32 GlobalSystemInterrupt;
1910 /* The actual register address. */
1911 struct acpi_generic_address addr;
1915 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1918 static int try_init_spmi(struct SPMITable *spmi)
1920 unsigned short myaddr;
1922 if (num_addrs >= MAX_SSIF_BMCS)
1925 if (spmi->IPMIlegacy != 1) {
1926 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1930 if (spmi->InterfaceType != 4)
1933 if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1934 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1935 spmi->addr.space_id);
1939 myaddr = spmi->addr.address & 0x7f;
1941 return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI, NULL);
1944 static void spmi_find_bmc(void)
1947 struct SPMITable *spmi;
1956 for (i = 0; ; i++) {
1957 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1958 (struct acpi_table_header **)&spmi);
1959 if (status != AE_OK)
1962 try_init_spmi(spmi);
1966 static void spmi_find_bmc(void) { }
1970 static int dmi_ipmi_probe(struct platform_device *pdev)
1972 u8 type, slave_addr = 0;
1979 rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type);
1983 if (type != IPMI_DMI_TYPE_SSIF)
1986 rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
1988 dev_warn(&pdev->dev, PFX "No i2c-addr property\n");
1992 rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
1994 dev_warn(&pdev->dev, "device has no slave-addr property");
1996 return new_ssif_client(i2c_addr, NULL, 0,
1997 slave_addr, SI_SMBIOS, &pdev->dev);
2000 static int dmi_ipmi_probe(struct platform_device *pdev)
2006 static const struct i2c_device_id ssif_id[] = {
2010 MODULE_DEVICE_TABLE(i2c, ssif_id);
2012 static struct i2c_driver ssif_i2c_driver = {
2013 .class = I2C_CLASS_HWMON,
2017 .probe = ssif_probe,
2018 .remove = ssif_remove,
2019 .alert = ssif_alert,
2020 .id_table = ssif_id,
2021 .detect = ssif_detect
2024 static int ssif_platform_probe(struct platform_device *dev)
2026 return dmi_ipmi_probe(dev);
2029 static int ssif_platform_remove(struct platform_device *dev)
2031 struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2036 mutex_lock(&ssif_infos_mutex);
2037 if (addr_info->added_client)
2038 i2c_unregister_device(addr_info->added_client);
2040 list_del(&addr_info->link);
2042 mutex_unlock(&ssif_infos_mutex);
2046 static struct platform_driver ipmi_driver = {
2048 .name = DEVICE_NAME,
2050 .probe = ssif_platform_probe,
2051 .remove = ssif_platform_remove,
2054 static int init_ipmi_ssif(void)
2062 pr_info("IPMI SSIF Interface driver\n");
2064 /* build list for i2c from addr list */
2065 for (i = 0; i < num_addrs; i++) {
2066 rv = new_ssif_client(addr[i], adapter_name[i],
2067 dbg[i], slave_addrs[i],
2068 SI_HARDCODED, NULL);
2071 "Couldn't add hardcoded device at addr 0x%x\n",
2076 ssif_i2c_driver.driver.acpi_match_table =
2077 ACPI_PTR(ssif_acpi_match);
2083 rv = platform_driver_register(&ipmi_driver);
2085 pr_err(PFX "Unable to register driver: %d\n", rv);
2088 ssif_i2c_driver.address_list = ssif_address_list();
2090 rv = i2c_add_driver(&ssif_i2c_driver);
2096 module_init(init_ipmi_ssif);
2098 static void cleanup_ipmi_ssif(void)
2103 initialized = false;
2105 i2c_del_driver(&ssif_i2c_driver);
2107 platform_driver_unregister(&ipmi_driver);
2109 free_ssif_clients();
2111 module_exit(cleanup_ipmi_ssif);
2113 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2114 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2115 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2116 MODULE_LICENSE("GPL");