1 // SPDX-License-Identifier: GPL-2.0+
5 * The interface to the IPMI driver for SMBus access to a SMBus
6 * compliant device. Called SSIF by the IPMI spec.
8 * Author: Intel Corporation
12 * non-blocking I2C interface, add support for multi-part
13 * transactions, add PEC support, and general clenaup.
15 * Copyright 2003 Intel Corporation
16 * Copyright 2005 MontaVista Software
20 * This file holds the "policy" for the interface to the SSIF state
21 * machine. It does the configuration, handles timers and interrupts,
22 * and drives the real SSIF state machine.
25 #define pr_fmt(fmt) "ipmi_ssif: " fmt
26 #define dev_fmt(fmt) "ipmi_ssif: " fmt
28 #if defined(MODVERSIONS)
29 #include <linux/modversions.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/sched.h>
35 #include <linux/seq_file.h>
36 #include <linux/timer.h>
37 #include <linux/delay.h>
38 #include <linux/errno.h>
39 #include <linux/spinlock.h>
40 #include <linux/slab.h>
41 #include <linux/list.h>
42 #include <linux/i2c.h>
43 #include <linux/ipmi_smi.h>
44 #include <linux/init.h>
45 #include <linux/dmi.h>
46 #include <linux/kthread.h>
47 #include <linux/acpi.h>
48 #include <linux/ctype.h>
49 #include <linux/time64.h>
52 #define DEVICE_NAME "ipmi_ssif"
54 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
56 #define SSIF_IPMI_REQUEST 2
57 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
58 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
59 #define SSIF_IPMI_MULTI_PART_REQUEST_END 8
60 #define SSIF_IPMI_RESPONSE 3
61 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
63 /* ssif_debug is a bit-field
64 * SSIF_DEBUG_MSG - commands and their responses
65 * SSIF_DEBUG_STATES - message states
66 * SSIF_DEBUG_TIMING - Measure times between events in the driver
68 #define SSIF_DEBUG_TIMING 4
69 #define SSIF_DEBUG_STATE 2
70 #define SSIF_DEBUG_MSG 1
71 #define SSIF_NODEBUG 0
72 #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
77 #define SSIF_MSG_USEC 60000 /* 60ms between message tries. */
78 #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
80 /* How many times to we retry sending/receiving the message. */
81 #define SSIF_SEND_RETRIES 5
82 #define SSIF_RECV_RETRIES 250
84 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
85 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
86 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
89 * Timeout for the watch, only used for get flag timer.
91 #define SSIF_WATCH_MSG_TIMEOUT msecs_to_jiffies(10)
92 #define SSIF_WATCH_WATCHDOG_TIMEOUT msecs_to_jiffies(250)
94 enum ssif_intf_state {
99 SSIF_GETTING_MESSAGES,
100 /* FIXME - add watchdog stuff. */
103 #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
104 && (ssif)->curr_msg == NULL)
107 * Indexes into stats[] in ssif_info below.
109 enum ssif_stat_indexes {
110 /* Number of total messages sent. */
111 SSIF_STAT_sent_messages = 0,
114 * Number of message parts sent. Messages may be broken into
115 * parts if they are long.
117 SSIF_STAT_sent_messages_parts,
120 * Number of time a message was retried.
122 SSIF_STAT_send_retries,
125 * Number of times the send of a message failed.
127 SSIF_STAT_send_errors,
130 * Number of message responses received.
132 SSIF_STAT_received_messages,
135 * Number of message fragments received.
137 SSIF_STAT_received_message_parts,
140 * Number of times the receive of a message was retried.
142 SSIF_STAT_receive_retries,
145 * Number of errors receiving messages.
147 SSIF_STAT_receive_errors,
150 * Number of times a flag fetch was requested.
152 SSIF_STAT_flag_fetches,
155 * Number of times the hardware didn't follow the state machine.
160 * Number of received events.
164 /* Number of asyncronous messages received. */
165 SSIF_STAT_incoming_messages,
167 /* Number of watchdog pretimeouts. */
168 SSIF_STAT_watchdog_pretimeouts,
170 /* Number of alers received. */
173 /* Always add statistics before this value, it must be last. */
177 struct ssif_addr_info {
178 struct i2c_board_info binfo;
182 enum ipmi_addr_src addr_src;
183 union ipmi_smi_info_union addr_info;
185 struct i2c_client *client;
187 struct mutex clients_mutex;
188 struct list_head clients;
190 struct list_head link;
195 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
196 unsigned char *data, unsigned int len);
199 struct ipmi_smi *intf;
201 struct ipmi_smi_msg *waiting_msg;
202 struct ipmi_smi_msg *curr_msg;
203 enum ssif_intf_state ssif_state;
204 unsigned long ssif_debug;
206 struct ipmi_smi_handlers handlers;
208 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
209 union ipmi_smi_info_union addr_info;
212 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
213 * is set to hold the flags until we are done handling everything
216 #define RECEIVE_MSG_AVAIL 0x01
217 #define EVENT_MSG_BUFFER_FULL 0x02
218 #define WDT_PRE_TIMEOUT_INT 0x08
219 unsigned char msg_flags;
222 bool has_event_buffer;
226 * Used to tell what we should do with alerts. If we are
227 * waiting on a response, read the data immediately.
233 * If set to true, this will request events the next time the
234 * state machine is idle.
239 * If set to true, this will request flags the next time the
240 * state machine is idle.
245 * Used to perform timer operations when run-to-completion
246 * mode is on. This is a countdown timer.
250 /* Used for sending/receiving data. +1 for the length. */
251 unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
252 unsigned int data_len;
254 /* Temp receive buffer, gets copied into data. */
255 unsigned char recv[I2C_SMBUS_BLOCK_MAX];
257 struct i2c_client *client;
258 ssif_i2c_done done_handler;
260 /* Thread interface handling */
261 struct task_struct *thread;
262 struct completion wake_thread;
266 unsigned char *i2c_data;
267 unsigned int i2c_size;
269 struct timer_list retry_timer;
272 long watch_timeout; /* Timeout for flags check, 0 if off. */
273 struct timer_list watch_timer; /* Flag fetch timer. */
275 /* Info from SSIF cmd */
276 unsigned char max_xmit_msg_size;
277 unsigned char max_recv_msg_size;
278 bool cmd8_works; /* See test_multipart_messages() for details. */
279 unsigned int multi_support;
282 #define SSIF_NO_MULTI 0
283 #define SSIF_MULTI_2_PART 1
284 #define SSIF_MULTI_n_PART 2
285 unsigned char *multi_data;
286 unsigned int multi_len;
287 unsigned int multi_pos;
289 atomic_t stats[SSIF_NUM_STATS];
292 #define ssif_inc_stat(ssif, stat) \
293 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
294 #define ssif_get_stat(ssif, stat) \
295 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
297 static bool initialized;
298 static bool platform_registered;
300 static void return_hosed_msg(struct ssif_info *ssif_info,
301 struct ipmi_smi_msg *msg);
302 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
303 static int start_send(struct ssif_info *ssif_info,
307 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
308 unsigned long *flags)
309 __acquires(&ssif_info->lock)
311 spin_lock_irqsave(&ssif_info->lock, *flags);
315 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
316 unsigned long *flags)
317 __releases(&ssif_info->lock)
319 spin_unlock_irqrestore(&ssif_info->lock, *flags);
322 static void deliver_recv_msg(struct ssif_info *ssif_info,
323 struct ipmi_smi_msg *msg)
325 if (msg->rsp_size < 0) {
326 return_hosed_msg(ssif_info, msg);
327 dev_err(&ssif_info->client->dev,
328 "%s: Malformed message: rsp_size = %d\n",
329 __func__, msg->rsp_size);
331 ipmi_smi_msg_received(ssif_info->intf, msg);
335 static void return_hosed_msg(struct ssif_info *ssif_info,
336 struct ipmi_smi_msg *msg)
338 ssif_inc_stat(ssif_info, hosed);
340 /* Make it a response */
341 msg->rsp[0] = msg->data[0] | 4;
342 msg->rsp[1] = msg->data[1];
343 msg->rsp[2] = 0xFF; /* Unknown error. */
346 deliver_recv_msg(ssif_info, msg);
350 * Must be called with the message lock held. This will release the
351 * message lock. Note that the caller will check SSIF_IDLE and start a
352 * new operation, so there is no need to check for new messages to
355 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
357 unsigned char msg[3];
359 ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
360 ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
361 ipmi_ssif_unlock_cond(ssif_info, flags);
363 /* Make sure the watchdog pre-timeout flag is not set at startup. */
364 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
365 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
366 msg[2] = WDT_PRE_TIMEOUT_INT;
368 if (start_send(ssif_info, msg, 3) != 0) {
369 /* Error, just go to normal state. */
370 ssif_info->ssif_state = SSIF_NORMAL;
374 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
378 ssif_info->req_flags = false;
379 ssif_info->ssif_state = SSIF_GETTING_FLAGS;
380 ipmi_ssif_unlock_cond(ssif_info, flags);
382 mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
383 mb[1] = IPMI_GET_MSG_FLAGS_CMD;
384 if (start_send(ssif_info, mb, 2) != 0)
385 ssif_info->ssif_state = SSIF_NORMAL;
388 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
389 struct ipmi_smi_msg *msg)
391 if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
392 unsigned long oflags;
394 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
395 ssif_info->curr_msg = NULL;
396 ssif_info->ssif_state = SSIF_NORMAL;
397 ipmi_ssif_unlock_cond(ssif_info, flags);
398 ipmi_free_smi_msg(msg);
402 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
404 struct ipmi_smi_msg *msg;
406 ssif_info->req_events = false;
408 msg = ipmi_alloc_smi_msg();
410 ssif_info->ssif_state = SSIF_NORMAL;
411 ipmi_ssif_unlock_cond(ssif_info, flags);
415 ssif_info->curr_msg = msg;
416 ssif_info->ssif_state = SSIF_GETTING_EVENTS;
417 ipmi_ssif_unlock_cond(ssif_info, flags);
419 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
420 msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
423 check_start_send(ssif_info, flags, msg);
426 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
427 unsigned long *flags)
429 struct ipmi_smi_msg *msg;
431 msg = ipmi_alloc_smi_msg();
433 ssif_info->ssif_state = SSIF_NORMAL;
434 ipmi_ssif_unlock_cond(ssif_info, flags);
438 ssif_info->curr_msg = msg;
439 ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
440 ipmi_ssif_unlock_cond(ssif_info, flags);
442 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
443 msg->data[1] = IPMI_GET_MSG_CMD;
446 check_start_send(ssif_info, flags, msg);
450 * Must be called with the message lock held. This will release the
451 * message lock. Note that the caller will check SSIF_IDLE and start a
452 * new operation, so there is no need to check for new messages to
455 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
457 if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
458 /* Watchdog pre-timeout */
459 ssif_inc_stat(ssif_info, watchdog_pretimeouts);
460 start_clear_flags(ssif_info, flags);
461 ipmi_smi_watchdog_pretimeout(ssif_info->intf);
462 } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
463 /* Messages available. */
464 start_recv_msg_fetch(ssif_info, flags);
465 else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
466 /* Events available. */
467 start_event_fetch(ssif_info, flags);
469 ssif_info->ssif_state = SSIF_NORMAL;
470 ipmi_ssif_unlock_cond(ssif_info, flags);
474 static int ipmi_ssif_thread(void *data)
476 struct ssif_info *ssif_info = data;
478 while (!kthread_should_stop()) {
481 /* Wait for something to do */
482 result = wait_for_completion_interruptible(
483 &ssif_info->wake_thread);
484 if (ssif_info->stopping)
486 if (result == -ERESTARTSYS)
488 init_completion(&ssif_info->wake_thread);
490 if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
491 result = i2c_smbus_write_block_data(
492 ssif_info->client, ssif_info->i2c_command,
493 ssif_info->i2c_data[0],
494 ssif_info->i2c_data + 1);
495 ssif_info->done_handler(ssif_info, result, NULL, 0);
497 result = i2c_smbus_read_block_data(
498 ssif_info->client, ssif_info->i2c_command,
499 ssif_info->i2c_data);
501 ssif_info->done_handler(ssif_info, result,
504 ssif_info->done_handler(ssif_info, 0,
513 static void ssif_i2c_send(struct ssif_info *ssif_info,
514 ssif_i2c_done handler,
515 int read_write, int command,
516 unsigned char *data, unsigned int size)
518 ssif_info->done_handler = handler;
520 ssif_info->i2c_read_write = read_write;
521 ssif_info->i2c_command = command;
522 ssif_info->i2c_data = data;
523 ssif_info->i2c_size = size;
524 complete(&ssif_info->wake_thread);
528 static void msg_done_handler(struct ssif_info *ssif_info, int result,
529 unsigned char *data, unsigned int len);
531 static void start_get(struct ssif_info *ssif_info)
533 ssif_info->rtc_us_timer = 0;
534 ssif_info->multi_pos = 0;
536 ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
538 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
541 static void retry_timeout(struct timer_list *t)
543 struct ssif_info *ssif_info = from_timer(ssif_info, t, retry_timer);
544 unsigned long oflags, *flags;
547 if (ssif_info->stopping)
550 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
551 waiting = ssif_info->waiting_alert;
552 ssif_info->waiting_alert = false;
553 ipmi_ssif_unlock_cond(ssif_info, flags);
556 start_get(ssif_info);
559 static void watch_timeout(struct timer_list *t)
561 struct ssif_info *ssif_info = from_timer(ssif_info, t, watch_timer);
562 unsigned long oflags, *flags;
564 if (ssif_info->stopping)
567 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
568 if (ssif_info->watch_timeout) {
569 mod_timer(&ssif_info->watch_timer,
570 jiffies + ssif_info->watch_timeout);
571 if (SSIF_IDLE(ssif_info)) {
572 start_flag_fetch(ssif_info, flags); /* Releases lock */
575 ssif_info->req_flags = true;
577 ipmi_ssif_unlock_cond(ssif_info, flags);
580 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
583 struct ssif_info *ssif_info = i2c_get_clientdata(client);
584 unsigned long oflags, *flags;
587 if (type != I2C_PROTOCOL_SMBUS_ALERT)
590 ssif_inc_stat(ssif_info, alerts);
592 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
593 if (ssif_info->waiting_alert) {
594 ssif_info->waiting_alert = false;
595 del_timer(&ssif_info->retry_timer);
597 } else if (ssif_info->curr_msg) {
598 ssif_info->got_alert = true;
600 ipmi_ssif_unlock_cond(ssif_info, flags);
602 start_get(ssif_info);
605 static int start_resend(struct ssif_info *ssif_info);
607 static void msg_done_handler(struct ssif_info *ssif_info, int result,
608 unsigned char *data, unsigned int len)
610 struct ipmi_smi_msg *msg;
611 unsigned long oflags, *flags;
614 * We are single-threaded here, so no need for a lock until we
615 * start messing with driver states or the queues.
619 ssif_info->retries_left--;
620 if (ssif_info->retries_left > 0) {
621 ssif_inc_stat(ssif_info, receive_retries);
623 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
624 ssif_info->waiting_alert = true;
625 ssif_info->rtc_us_timer = SSIF_MSG_USEC;
626 if (!ssif_info->stopping)
627 mod_timer(&ssif_info->retry_timer,
628 jiffies + SSIF_MSG_JIFFIES);
629 ipmi_ssif_unlock_cond(ssif_info, flags);
633 ssif_inc_stat(ssif_info, receive_errors);
635 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
636 dev_dbg(&ssif_info->client->dev,
637 "%s: Error %d\n", __func__, result);
642 if ((len > 1) && (ssif_info->multi_pos == 0)
643 && (data[0] == 0x00) && (data[1] == 0x01)) {
644 /* Start of multi-part read. Start the next transaction. */
647 ssif_inc_stat(ssif_info, received_message_parts);
649 /* Remove the multi-part read marker. */
652 for (i = 0; i < len; i++)
653 ssif_info->data[i] = data[i];
654 ssif_info->multi_len = len;
655 ssif_info->multi_pos = 1;
657 ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
658 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
659 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
661 } else if (ssif_info->multi_pos) {
662 /* Middle of multi-part read. Start the next transaction. */
664 unsigned char blocknum;
668 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
669 dev_dbg(&ssif_info->client->dev,
670 "Middle message with no data\n");
679 if (blocknum != 0xff && len != 31) {
680 /* All blocks but the last must have 31 data bytes. */
682 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
683 dev_dbg(&ssif_info->client->dev,
684 "Received middle message <31\n");
689 if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
690 /* Received message too big, abort the operation. */
692 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
693 dev_dbg(&ssif_info->client->dev,
694 "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 ssif_i2c_send(ssif_info, msg_done_handler,
724 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
726 I2C_SMBUS_BLOCK_DATA);
733 ssif_inc_stat(ssif_info, receive_errors);
735 ssif_inc_stat(ssif_info, received_messages);
736 ssif_inc_stat(ssif_info, received_message_parts);
739 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
740 dev_dbg(&ssif_info->client->dev,
741 "DONE 1: state = %d, result=%d\n",
742 ssif_info->ssif_state, result);
744 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
745 msg = ssif_info->curr_msg;
748 if (len > IPMI_MAX_MSG_LENGTH)
749 len = IPMI_MAX_MSG_LENGTH;
750 memcpy(msg->rsp, data, len);
755 ssif_info->curr_msg = NULL;
758 switch (ssif_info->ssif_state) {
760 ipmi_ssif_unlock_cond(ssif_info, flags);
765 return_hosed_msg(ssif_info, msg);
767 deliver_recv_msg(ssif_info, msg);
770 case SSIF_GETTING_FLAGS:
771 /* We got the flags from the SSIF, now handle them. */
772 if ((result < 0) || (len < 4) || (data[2] != 0)) {
774 * Error fetching flags, or invalid length,
775 * just give up for now.
777 ssif_info->ssif_state = SSIF_NORMAL;
778 ipmi_ssif_unlock_cond(ssif_info, flags);
779 dev_warn(&ssif_info->client->dev,
780 "Error getting flags: %d %d, %x\n",
781 result, len, (len >= 3) ? data[2] : 0);
782 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
783 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
785 * Don't abort here, maybe it was a queued
786 * response to a previous command.
788 ipmi_ssif_unlock_cond(ssif_info, flags);
789 dev_warn(&ssif_info->client->dev,
790 "Invalid response getting flags: %x %x\n",
793 ssif_inc_stat(ssif_info, flag_fetches);
794 ssif_info->msg_flags = data[3];
795 handle_flags(ssif_info, flags);
799 case SSIF_CLEARING_FLAGS:
800 /* We cleared the flags. */
801 if ((result < 0) || (len < 3) || (data[2] != 0)) {
802 /* Error clearing flags */
803 dev_warn(&ssif_info->client->dev,
804 "Error clearing flags: %d %d, %x\n",
805 result, len, (len >= 3) ? data[2] : 0);
806 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
807 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
808 dev_warn(&ssif_info->client->dev,
809 "Invalid response clearing flags: %x %x\n",
812 ssif_info->ssif_state = SSIF_NORMAL;
813 ipmi_ssif_unlock_cond(ssif_info, flags);
816 case SSIF_GETTING_EVENTS:
818 /* Should never happen, but just in case. */
819 dev_warn(&ssif_info->client->dev,
820 "No message set while getting events\n");
821 ipmi_ssif_unlock_cond(ssif_info, flags);
825 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
826 /* Error getting event, probably done. */
829 /* Take off the event flag. */
830 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
831 handle_flags(ssif_info, flags);
832 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
833 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
834 dev_warn(&ssif_info->client->dev,
835 "Invalid response getting events: %x %x\n",
836 msg->rsp[0], msg->rsp[1]);
838 /* Take off the event flag. */
839 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
840 handle_flags(ssif_info, flags);
842 handle_flags(ssif_info, flags);
843 ssif_inc_stat(ssif_info, events);
844 deliver_recv_msg(ssif_info, msg);
848 case SSIF_GETTING_MESSAGES:
850 /* Should never happen, but just in case. */
851 dev_warn(&ssif_info->client->dev,
852 "No message set while getting messages\n");
853 ipmi_ssif_unlock_cond(ssif_info, flags);
857 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
858 /* Error getting event, probably done. */
861 /* Take off the msg flag. */
862 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
863 handle_flags(ssif_info, flags);
864 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
865 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
866 dev_warn(&ssif_info->client->dev,
867 "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 dev_dbg(&ssif_info->client->dev,
902 "DONE 2: state = %d.\n", ssif_info->ssif_state);
905 static void msg_written_handler(struct ssif_info *ssif_info, int result,
906 unsigned char *data, unsigned int len)
908 /* We are single-threaded here, so no need for a lock. */
910 ssif_info->retries_left--;
911 if (ssif_info->retries_left > 0) {
912 if (!start_resend(ssif_info)) {
913 ssif_inc_stat(ssif_info, send_retries);
916 /* request failed, just return the error. */
917 ssif_inc_stat(ssif_info, send_errors);
919 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
920 dev_dbg(&ssif_info->client->dev,
921 "%s: Out of retries\n", __func__);
922 msg_done_handler(ssif_info, -EIO, NULL, 0);
926 ssif_inc_stat(ssif_info, send_errors);
929 * Got an error on transmit, let the done routine
932 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
933 dev_dbg(&ssif_info->client->dev,
934 "%s: Error %d\n", __func__, 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;
950 ssif_inc_stat(ssif_info, sent_messages_parts);
952 left = ssif_info->multi_len - ssif_info->multi_pos;
957 ssif_info->multi_data[ssif_info->multi_pos] = to_write;
958 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
959 ssif_info->multi_pos += to_write;
960 cmd = SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE;
961 if (ssif_info->cmd8_works) {
962 if (left == to_write) {
963 cmd = SSIF_IPMI_MULTI_PART_REQUEST_END;
964 ssif_info->multi_data = NULL;
966 } else if (to_write < 32) {
967 ssif_info->multi_data = NULL;
970 ssif_i2c_send(ssif_info, msg_written_handler,
971 I2C_SMBUS_WRITE, cmd,
972 data_to_send, I2C_SMBUS_BLOCK_DATA);
974 /* Ready to request the result. */
975 unsigned long oflags, *flags;
977 ssif_inc_stat(ssif_info, sent_messages);
978 ssif_inc_stat(ssif_info, sent_messages_parts);
980 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
981 if (ssif_info->got_alert) {
982 /* The result is already ready, just start it. */
983 ssif_info->got_alert = false;
984 ipmi_ssif_unlock_cond(ssif_info, flags);
985 start_get(ssif_info);
987 /* Wait a jiffie then request the next message */
988 ssif_info->waiting_alert = true;
989 ssif_info->retries_left = SSIF_RECV_RETRIES;
990 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
991 if (!ssif_info->stopping)
992 mod_timer(&ssif_info->retry_timer,
993 jiffies + SSIF_MSG_PART_JIFFIES);
994 ipmi_ssif_unlock_cond(ssif_info, flags);
999 static int start_resend(struct ssif_info *ssif_info)
1003 ssif_info->got_alert = false;
1005 if (ssif_info->data_len > 32) {
1006 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
1007 ssif_info->multi_data = ssif_info->data;
1008 ssif_info->multi_len = ssif_info->data_len;
1010 * Subtle thing, this is 32, not 33, because we will
1011 * overwrite the thing at position 32 (which was just
1012 * transmitted) with the new length.
1014 ssif_info->multi_pos = 32;
1015 ssif_info->data[0] = 32;
1017 ssif_info->multi_data = NULL;
1018 command = SSIF_IPMI_REQUEST;
1019 ssif_info->data[0] = ssif_info->data_len;
1022 ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1023 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1027 static int start_send(struct ssif_info *ssif_info,
1028 unsigned char *data,
1031 if (len > IPMI_MAX_MSG_LENGTH)
1033 if (len > ssif_info->max_xmit_msg_size)
1036 ssif_info->retries_left = SSIF_SEND_RETRIES;
1037 memcpy(ssif_info->data + 1, data, len);
1038 ssif_info->data_len = len;
1039 return start_resend(ssif_info);
1042 /* Must be called with the message lock held. */
1043 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1045 struct ipmi_smi_msg *msg;
1046 unsigned long oflags;
1049 if (!SSIF_IDLE(ssif_info)) {
1050 ipmi_ssif_unlock_cond(ssif_info, flags);
1054 if (!ssif_info->waiting_msg) {
1055 ssif_info->curr_msg = NULL;
1056 ipmi_ssif_unlock_cond(ssif_info, flags);
1060 ssif_info->curr_msg = ssif_info->waiting_msg;
1061 ssif_info->waiting_msg = NULL;
1062 ipmi_ssif_unlock_cond(ssif_info, flags);
1063 rv = start_send(ssif_info,
1064 ssif_info->curr_msg->data,
1065 ssif_info->curr_msg->data_size);
1067 msg = ssif_info->curr_msg;
1068 ssif_info->curr_msg = NULL;
1069 return_hosed_msg(ssif_info, msg);
1070 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1076 static void sender(void *send_info,
1077 struct ipmi_smi_msg *msg)
1079 struct ssif_info *ssif_info = send_info;
1080 unsigned long oflags, *flags;
1082 BUG_ON(ssif_info->waiting_msg);
1083 ssif_info->waiting_msg = msg;
1085 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1086 start_next_msg(ssif_info, flags);
1088 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1089 struct timespec64 t;
1091 ktime_get_real_ts64(&t);
1092 dev_dbg(&ssif_info->client->dev,
1093 "**Enqueue %02x %02x: %lld.%6.6ld\n",
1094 msg->data[0], msg->data[1],
1095 (long long)t.tv_sec, (long)t.tv_nsec / NSEC_PER_USEC);
1099 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1101 struct ssif_info *ssif_info = send_info;
1103 data->addr_src = ssif_info->addr_source;
1104 data->dev = &ssif_info->client->dev;
1105 data->addr_info = ssif_info->addr_info;
1106 get_device(data->dev);
1112 * Upper layer wants us to request events.
1114 static void request_events(void *send_info)
1116 struct ssif_info *ssif_info = send_info;
1117 unsigned long oflags, *flags;
1119 if (!ssif_info->has_event_buffer)
1122 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1123 ssif_info->req_events = true;
1124 ipmi_ssif_unlock_cond(ssif_info, flags);
1128 * Upper layer is changing the flag saying whether we need to request
1129 * flags periodically or not.
1131 static void ssif_set_need_watch(void *send_info, unsigned int watch_mask)
1133 struct ssif_info *ssif_info = send_info;
1134 unsigned long oflags, *flags;
1137 if (watch_mask & IPMI_WATCH_MASK_CHECK_MESSAGES)
1138 timeout = SSIF_WATCH_MSG_TIMEOUT;
1139 else if (watch_mask)
1140 timeout = SSIF_WATCH_WATCHDOG_TIMEOUT;
1142 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1143 if (timeout != ssif_info->watch_timeout) {
1144 ssif_info->watch_timeout = timeout;
1145 if (ssif_info->watch_timeout)
1146 mod_timer(&ssif_info->watch_timer,
1147 jiffies + ssif_info->watch_timeout);
1149 ipmi_ssif_unlock_cond(ssif_info, flags);
1152 static int ssif_start_processing(void *send_info,
1153 struct ipmi_smi *intf)
1155 struct ssif_info *ssif_info = send_info;
1157 ssif_info->intf = intf;
1162 #define MAX_SSIF_BMCS 4
1164 static unsigned short addr[MAX_SSIF_BMCS];
1165 static int num_addrs;
1166 module_param_array(addr, ushort, &num_addrs, 0);
1167 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1169 static char *adapter_name[MAX_SSIF_BMCS];
1170 static int num_adapter_names;
1171 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1172 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1174 static int slave_addrs[MAX_SSIF_BMCS];
1175 static int num_slave_addrs;
1176 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1177 MODULE_PARM_DESC(slave_addrs,
1178 "The default IPMB slave address for the controller.");
1180 static bool alerts_broken;
1181 module_param(alerts_broken, bool, 0);
1182 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1185 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1186 * bit 2 enables timing debugging. This is an array indexed by
1189 static int dbg[MAX_SSIF_BMCS];
1191 module_param_array(dbg, int, &num_dbg, 0);
1192 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1194 static bool ssif_dbg_probe;
1195 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1196 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1198 static bool ssif_tryacpi = true;
1199 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1200 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1202 static bool ssif_trydmi = true;
1203 module_param_named(trydmi, ssif_trydmi, bool, 0);
1204 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1206 static DEFINE_MUTEX(ssif_infos_mutex);
1207 static LIST_HEAD(ssif_infos);
1209 #define IPMI_SSIF_ATTR(name) \
1210 static ssize_t ipmi_##name##_show(struct device *dev, \
1211 struct device_attribute *attr, \
1214 struct ssif_info *ssif_info = dev_get_drvdata(dev); \
1216 return sysfs_emit(buf, "%u\n", ssif_get_stat(ssif_info, name));\
1218 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1220 static ssize_t ipmi_type_show(struct device *dev,
1221 struct device_attribute *attr,
1224 return sysfs_emit(buf, "ssif\n");
1226 static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1228 IPMI_SSIF_ATTR(sent_messages);
1229 IPMI_SSIF_ATTR(sent_messages_parts);
1230 IPMI_SSIF_ATTR(send_retries);
1231 IPMI_SSIF_ATTR(send_errors);
1232 IPMI_SSIF_ATTR(received_messages);
1233 IPMI_SSIF_ATTR(received_message_parts);
1234 IPMI_SSIF_ATTR(receive_retries);
1235 IPMI_SSIF_ATTR(receive_errors);
1236 IPMI_SSIF_ATTR(flag_fetches);
1237 IPMI_SSIF_ATTR(hosed);
1238 IPMI_SSIF_ATTR(events);
1239 IPMI_SSIF_ATTR(watchdog_pretimeouts);
1240 IPMI_SSIF_ATTR(alerts);
1242 static struct attribute *ipmi_ssif_dev_attrs[] = {
1243 &dev_attr_type.attr,
1244 &dev_attr_sent_messages.attr,
1245 &dev_attr_sent_messages_parts.attr,
1246 &dev_attr_send_retries.attr,
1247 &dev_attr_send_errors.attr,
1248 &dev_attr_received_messages.attr,
1249 &dev_attr_received_message_parts.attr,
1250 &dev_attr_receive_retries.attr,
1251 &dev_attr_receive_errors.attr,
1252 &dev_attr_flag_fetches.attr,
1253 &dev_attr_hosed.attr,
1254 &dev_attr_events.attr,
1255 &dev_attr_watchdog_pretimeouts.attr,
1256 &dev_attr_alerts.attr,
1260 static const struct attribute_group ipmi_ssif_dev_attr_group = {
1261 .attrs = ipmi_ssif_dev_attrs,
1264 static void shutdown_ssif(void *send_info)
1266 struct ssif_info *ssif_info = send_info;
1268 device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1269 dev_set_drvdata(&ssif_info->client->dev, NULL);
1271 /* make sure the driver is not looking for flags any more. */
1272 while (ssif_info->ssif_state != SSIF_NORMAL)
1273 schedule_timeout(1);
1275 ssif_info->stopping = true;
1276 del_timer_sync(&ssif_info->watch_timer);
1277 del_timer_sync(&ssif_info->retry_timer);
1278 if (ssif_info->thread) {
1279 complete(&ssif_info->wake_thread);
1280 kthread_stop(ssif_info->thread);
1284 static void ssif_remove(struct i2c_client *client)
1286 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1287 struct ssif_addr_info *addr_info;
1293 * After this point, we won't deliver anything asychronously
1294 * to the message handler. We can unregister ourself.
1296 ipmi_unregister_smi(ssif_info->intf);
1298 list_for_each_entry(addr_info, &ssif_infos, link) {
1299 if (addr_info->client == client) {
1300 addr_info->client = NULL;
1308 static int read_response(struct i2c_client *client, unsigned char *resp)
1310 int ret = -ENODEV, retry_cnt = SSIF_RECV_RETRIES;
1312 while (retry_cnt > 0) {
1313 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1317 msleep(SSIF_MSG_MSEC);
1326 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1327 int *resp_len, unsigned char *resp)
1332 retry_cnt = SSIF_SEND_RETRIES;
1334 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1342 ret = read_response(client, resp);
1344 /* Validate that the response is correct. */
1346 (resp[0] != (msg[0] | (1 << 2))) ||
1347 (resp[1] != msg[1]))
1349 else if (ret > IPMI_MAX_MSG_LENGTH) {
1360 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1362 unsigned char *resp;
1363 unsigned char msg[3];
1367 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1371 /* Do a Get Device ID command, since it is required. */
1372 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1373 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1374 rv = do_cmd(client, 2, msg, &len, resp);
1378 strscpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1383 static int strcmp_nospace(char *s1, char *s2)
1385 while (*s1 && *s2) {
1386 while (isspace(*s1))
1388 while (isspace(*s2))
1400 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1402 bool match_null_name)
1404 struct ssif_addr_info *info, *found = NULL;
1407 list_for_each_entry(info, &ssif_infos, link) {
1408 if (info->binfo.addr == addr) {
1409 if (info->addr_src == SI_SMBIOS)
1410 info->adapter_name = kstrdup(adapter_name,
1413 if (info->adapter_name || adapter_name) {
1414 if (!info->adapter_name != !adapter_name) {
1415 /* One is NULL and one is not */
1419 strcmp_nospace(info->adapter_name,
1421 /* Names do not match */
1429 if (!found && match_null_name) {
1430 /* Try to get an exact match first, then try with a NULL name */
1431 adapter_name = NULL;
1432 match_null_name = false;
1439 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1442 acpi_handle acpi_handle;
1444 acpi_handle = ACPI_HANDLE(dev);
1446 ssif_info->addr_source = SI_ACPI;
1447 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1448 request_module("acpi_ipmi");
1455 static int find_slave_address(struct i2c_client *client, int slave_addr)
1457 #ifdef CONFIG_IPMI_DMI_DECODE
1459 slave_addr = ipmi_dmi_get_slave_addr(
1461 i2c_adapter_id(client->adapter),
1468 static int start_multipart_test(struct i2c_client *client,
1469 unsigned char *msg, bool do_middle)
1471 int retry_cnt = SSIF_SEND_RETRIES, ret;
1474 ret = i2c_smbus_write_block_data(client,
1475 SSIF_IPMI_MULTI_PART_REQUEST_START,
1481 dev_err(&client->dev, "Could not write multi-part start, though the BMC said it could handle it. Just limit sends to one part.\n");
1488 ret = i2c_smbus_write_block_data(client,
1489 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1492 dev_err(&client->dev, "Could not write multi-part middle, though the BMC said it could handle it. Just limit sends to one part.\n");
1499 static void test_multipart_messages(struct i2c_client *client,
1500 struct ssif_info *ssif_info,
1501 unsigned char *resp)
1503 unsigned char msg[65];
1507 if (ssif_info->max_xmit_msg_size <= 32)
1510 do_middle = ssif_info->max_xmit_msg_size > 63;
1512 memset(msg, 0, sizeof(msg));
1513 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1514 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1517 * The specification is all messed up dealing with sending
1518 * multi-part messages. Per what the specification says, it
1519 * is impossible to send a message that is a multiple of 32
1520 * bytes, except for 32 itself. It talks about a "start"
1521 * transaction (cmd=6) that must be 32 bytes, "middle"
1522 * transaction (cmd=7) that must be 32 bytes, and an "end"
1523 * transaction. The "end" transaction is shown as cmd=7 in
1524 * the text, but if that's the case there is no way to
1525 * differentiate between a middle and end part except the
1526 * length being less than 32. But there is a table at the far
1527 * end of the section (that I had never noticed until someone
1528 * pointed it out to me) that mentions it as cmd=8.
1530 * After some thought, I think the example is wrong and the
1531 * end transaction should be cmd=8. But some systems don't
1532 * implement cmd=8, they use a zero-length end transaction,
1533 * even though that violates the SMBus specification.
1535 * So, to work around this, this code tests if cmd=8 works.
1536 * If it does, then we use that. If not, it tests zero-
1537 * byte end transactions. If that works, good. If not,
1538 * we only allow 63-byte transactions max.
1541 ret = start_multipart_test(client, msg, do_middle);
1543 goto out_no_multi_part;
1545 ret = i2c_smbus_write_block_data(client,
1546 SSIF_IPMI_MULTI_PART_REQUEST_END,
1550 ret = read_response(client, resp);
1553 /* End transactions work, we are good. */
1554 ssif_info->cmd8_works = true;
1558 ret = start_multipart_test(client, msg, do_middle);
1560 dev_err(&client->dev, "Second multipart test failed.\n");
1561 goto out_no_multi_part;
1564 ret = i2c_smbus_write_block_data(client,
1565 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1568 ret = read_response(client, resp);
1570 /* Zero-size end parts work, use those. */
1573 /* Limit to 63 bytes and use a short middle command to mark the end. */
1574 if (ssif_info->max_xmit_msg_size > 63)
1575 ssif_info->max_xmit_msg_size = 63;
1579 ssif_info->max_xmit_msg_size = 32;
1584 * Global enables we care about.
1586 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1587 IPMI_BMC_EVT_MSG_INTR)
1589 static void ssif_remove_dup(struct i2c_client *client)
1591 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1593 ipmi_unregister_smi(ssif_info->intf);
1597 static int ssif_add_infos(struct i2c_client *client)
1599 struct ssif_addr_info *info;
1601 info = kzalloc(sizeof(*info), GFP_KERNEL);
1604 info->addr_src = SI_ACPI;
1605 info->client = client;
1606 info->adapter_name = kstrdup(client->adapter->name, GFP_KERNEL);
1607 info->binfo.addr = client->addr;
1608 list_add_tail(&info->link, &ssif_infos);
1613 * Prefer ACPI over SMBIOS, if both are available.
1614 * So if we get an ACPI interface and have already registered a SMBIOS
1615 * interface at the same address, remove the SMBIOS and add the ACPI one.
1617 static int ssif_check_and_remove(struct i2c_client *client,
1618 struct ssif_info *ssif_info)
1620 struct ssif_addr_info *info;
1622 list_for_each_entry(info, &ssif_infos, link) {
1625 if (!strcmp(info->adapter_name, client->adapter->name) &&
1626 info->binfo.addr == client->addr) {
1627 if (info->addr_src == SI_ACPI)
1630 if (ssif_info->addr_source == SI_ACPI &&
1631 info->addr_src == SI_SMBIOS) {
1632 dev_info(&client->dev,
1633 "Removing %s-specified SSIF interface in favor of ACPI\n",
1634 ipmi_addr_src_to_str(info->addr_src));
1635 ssif_remove_dup(info->client);
1643 static int ssif_probe(struct i2c_client *client)
1645 unsigned char msg[3];
1646 unsigned char *resp;
1647 struct ssif_info *ssif_info;
1652 struct ssif_addr_info *addr_info = NULL;
1654 mutex_lock(&ssif_infos_mutex);
1655 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1657 mutex_unlock(&ssif_infos_mutex);
1661 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1664 mutex_unlock(&ssif_infos_mutex);
1668 if (!check_acpi(ssif_info, &client->dev)) {
1669 addr_info = ssif_info_find(client->addr, client->adapter->name,
1672 /* Must have come in through sysfs. */
1673 ssif_info->addr_source = SI_HOTMOD;
1675 ssif_info->addr_source = addr_info->addr_src;
1676 ssif_info->ssif_debug = addr_info->debug;
1677 ssif_info->addr_info = addr_info->addr_info;
1678 addr_info->client = client;
1679 slave_addr = addr_info->slave_addr;
1683 ssif_info->client = client;
1684 i2c_set_clientdata(client, ssif_info);
1686 rv = ssif_check_and_remove(client, ssif_info);
1687 /* If rv is 0 and addr source is not SI_ACPI, continue probing */
1688 if (!rv && ssif_info->addr_source == SI_ACPI) {
1689 rv = ssif_add_infos(client);
1691 dev_err(&client->dev, "Out of memory!, exiting ..\n");
1695 dev_err(&client->dev, "Not probing, Interface already present\n");
1699 slave_addr = find_slave_address(client, slave_addr);
1701 dev_info(&client->dev,
1702 "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1703 ipmi_addr_src_to_str(ssif_info->addr_source),
1704 client->addr, client->adapter->name, slave_addr);
1706 /* Now check for system interface capabilities */
1707 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1708 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1709 msg[2] = 0; /* SSIF */
1710 rv = do_cmd(client, 3, msg, &len, resp);
1711 if (!rv && (len >= 3) && (resp[2] == 0)) {
1714 dev_dbg(&ssif_info->client->dev,
1715 "SSIF info too short: %d\n", len);
1719 /* Got a good SSIF response, handle it. */
1720 ssif_info->max_xmit_msg_size = resp[5];
1721 ssif_info->max_recv_msg_size = resp[6];
1722 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1723 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1725 /* Sanitize the data */
1726 switch (ssif_info->multi_support) {
1728 if (ssif_info->max_xmit_msg_size > 32)
1729 ssif_info->max_xmit_msg_size = 32;
1730 if (ssif_info->max_recv_msg_size > 32)
1731 ssif_info->max_recv_msg_size = 32;
1734 case SSIF_MULTI_2_PART:
1735 if (ssif_info->max_xmit_msg_size > 63)
1736 ssif_info->max_xmit_msg_size = 63;
1737 if (ssif_info->max_recv_msg_size > 62)
1738 ssif_info->max_recv_msg_size = 62;
1741 case SSIF_MULTI_n_PART:
1742 /* We take whatever size given, but do some testing. */
1746 /* Data is not sane, just give up. */
1751 /* Assume no multi-part or PEC support */
1752 dev_info(&ssif_info->client->dev,
1753 "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1756 ssif_info->max_xmit_msg_size = 32;
1757 ssif_info->max_recv_msg_size = 32;
1758 ssif_info->multi_support = SSIF_NO_MULTI;
1759 ssif_info->supports_pec = 0;
1762 test_multipart_messages(client, ssif_info, resp);
1764 /* Make sure the NMI timeout is cleared. */
1765 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1766 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1767 msg[2] = WDT_PRE_TIMEOUT_INT;
1768 rv = do_cmd(client, 3, msg, &len, resp);
1769 if (rv || (len < 3) || (resp[2] != 0))
1770 dev_warn(&ssif_info->client->dev,
1771 "Unable to clear message flags: %d %d %2.2x\n",
1774 /* Attempt to enable the event buffer. */
1775 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1776 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1777 rv = do_cmd(client, 2, msg, &len, resp);
1778 if (rv || (len < 4) || (resp[2] != 0)) {
1779 dev_warn(&ssif_info->client->dev,
1780 "Error getting global enables: %d %d %2.2x\n",
1782 rv = 0; /* Not fatal */
1786 ssif_info->global_enables = resp[3];
1788 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1789 ssif_info->has_event_buffer = true;
1790 /* buffer is already enabled, nothing to do. */
1794 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1795 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1796 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1797 rv = do_cmd(client, 3, msg, &len, resp);
1798 if (rv || (len < 2)) {
1799 dev_warn(&ssif_info->client->dev,
1800 "Error setting global enables: %d %d %2.2x\n",
1802 rv = 0; /* Not fatal */
1807 /* A successful return means the event buffer is supported. */
1808 ssif_info->has_event_buffer = true;
1809 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1812 /* Some systems don't behave well if you enable alerts. */
1816 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1817 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1818 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1819 rv = do_cmd(client, 3, msg, &len, resp);
1820 if (rv || (len < 2)) {
1821 dev_warn(&ssif_info->client->dev,
1822 "Error setting global enables: %d %d %2.2x\n",
1824 rv = 0; /* Not fatal */
1829 /* A successful return means the alert is supported. */
1830 ssif_info->supports_alert = true;
1831 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1835 if (ssif_dbg_probe) {
1836 dev_dbg(&ssif_info->client->dev,
1837 "%s: i2c_probe found device at i2c address %x\n",
1838 __func__, client->addr);
1841 spin_lock_init(&ssif_info->lock);
1842 ssif_info->ssif_state = SSIF_NORMAL;
1843 timer_setup(&ssif_info->retry_timer, retry_timeout, 0);
1844 timer_setup(&ssif_info->watch_timer, watch_timeout, 0);
1846 for (i = 0; i < SSIF_NUM_STATS; i++)
1847 atomic_set(&ssif_info->stats[i], 0);
1849 if (ssif_info->supports_pec)
1850 ssif_info->client->flags |= I2C_CLIENT_PEC;
1852 ssif_info->handlers.owner = THIS_MODULE;
1853 ssif_info->handlers.start_processing = ssif_start_processing;
1854 ssif_info->handlers.shutdown = shutdown_ssif;
1855 ssif_info->handlers.get_smi_info = get_smi_info;
1856 ssif_info->handlers.sender = sender;
1857 ssif_info->handlers.request_events = request_events;
1858 ssif_info->handlers.set_need_watch = ssif_set_need_watch;
1861 unsigned int thread_num;
1863 thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1865 ssif_info->client->addr);
1866 init_completion(&ssif_info->wake_thread);
1867 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1868 "kssif%4.4x", thread_num);
1869 if (IS_ERR(ssif_info->thread)) {
1870 rv = PTR_ERR(ssif_info->thread);
1871 dev_notice(&ssif_info->client->dev,
1872 "Could not start kernel thread: error %d\n",
1878 dev_set_drvdata(&ssif_info->client->dev, ssif_info);
1879 rv = device_add_group(&ssif_info->client->dev,
1880 &ipmi_ssif_dev_attr_group);
1882 dev_err(&ssif_info->client->dev,
1883 "Unable to add device attributes: error %d\n",
1888 rv = ipmi_register_smi(&ssif_info->handlers,
1890 &ssif_info->client->dev,
1893 dev_err(&ssif_info->client->dev,
1894 "Unable to register device: error %d\n", rv);
1895 goto out_remove_attr;
1901 addr_info->client = NULL;
1903 dev_err(&ssif_info->client->dev,
1904 "Unable to start IPMI SSIF: %d\n", rv);
1905 i2c_set_clientdata(client, NULL);
1909 mutex_unlock(&ssif_infos_mutex);
1913 device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1914 dev_set_drvdata(&ssif_info->client->dev, NULL);
1918 static int new_ssif_client(int addr, char *adapter_name,
1919 int debug, int slave_addr,
1920 enum ipmi_addr_src addr_src,
1923 struct ssif_addr_info *addr_info;
1926 mutex_lock(&ssif_infos_mutex);
1927 if (ssif_info_find(addr, adapter_name, false)) {
1932 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1939 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1940 if (!addr_info->adapter_name) {
1947 strncpy(addr_info->binfo.type, DEVICE_NAME,
1948 sizeof(addr_info->binfo.type));
1949 addr_info->binfo.addr = addr;
1950 addr_info->binfo.platform_data = addr_info;
1951 addr_info->debug = debug;
1952 addr_info->slave_addr = slave_addr;
1953 addr_info->addr_src = addr_src;
1954 addr_info->dev = dev;
1957 dev_set_drvdata(dev, addr_info);
1959 list_add_tail(&addr_info->link, &ssif_infos);
1961 /* Address list will get it */
1964 mutex_unlock(&ssif_infos_mutex);
1968 static void free_ssif_clients(void)
1970 struct ssif_addr_info *info, *tmp;
1972 mutex_lock(&ssif_infos_mutex);
1973 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1974 list_del(&info->link);
1975 kfree(info->adapter_name);
1978 mutex_unlock(&ssif_infos_mutex);
1981 static unsigned short *ssif_address_list(void)
1983 struct ssif_addr_info *info;
1984 unsigned int count = 0, i = 0;
1985 unsigned short *address_list;
1987 list_for_each_entry(info, &ssif_infos, link)
1990 address_list = kcalloc(count + 1, sizeof(*address_list),
1995 list_for_each_entry(info, &ssif_infos, link) {
1996 unsigned short addr = info->binfo.addr;
1999 for (j = 0; j < i; j++) {
2000 if (address_list[j] == addr)
2004 if (j == i) /* Didn't find it in the list. */
2005 address_list[i++] = addr;
2007 address_list[i] = I2C_CLIENT_END;
2009 return address_list;
2013 static const struct acpi_device_id ssif_acpi_match[] = {
2017 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
2021 static int dmi_ipmi_probe(struct platform_device *pdev)
2030 rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
2032 dev_warn(&pdev->dev, "No i2c-addr property\n");
2036 rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
2040 return new_ssif_client(i2c_addr, NULL, 0,
2041 slave_addr, SI_SMBIOS, &pdev->dev);
2044 static int dmi_ipmi_probe(struct platform_device *pdev)
2050 static const struct i2c_device_id ssif_id[] = {
2054 MODULE_DEVICE_TABLE(i2c, ssif_id);
2056 static struct i2c_driver ssif_i2c_driver = {
2057 .class = I2C_CLASS_HWMON,
2061 .probe_new = ssif_probe,
2062 .remove = ssif_remove,
2063 .alert = ssif_alert,
2064 .id_table = ssif_id,
2065 .detect = ssif_detect
2068 static int ssif_platform_probe(struct platform_device *dev)
2070 return dmi_ipmi_probe(dev);
2073 static int ssif_platform_remove(struct platform_device *dev)
2075 struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2080 mutex_lock(&ssif_infos_mutex);
2081 list_del(&addr_info->link);
2083 mutex_unlock(&ssif_infos_mutex);
2087 static const struct platform_device_id ssif_plat_ids[] = {
2088 { "dmi-ipmi-ssif", 0 },
2092 static struct platform_driver ipmi_driver = {
2094 .name = DEVICE_NAME,
2096 .probe = ssif_platform_probe,
2097 .remove = ssif_platform_remove,
2098 .id_table = ssif_plat_ids
2101 static int __init init_ipmi_ssif(void)
2109 pr_info("IPMI SSIF Interface driver\n");
2111 /* build list for i2c from addr list */
2112 for (i = 0; i < num_addrs; i++) {
2113 rv = new_ssif_client(addr[i], adapter_name[i],
2114 dbg[i], slave_addrs[i],
2115 SI_HARDCODED, NULL);
2117 pr_err("Couldn't add hardcoded device at addr 0x%x\n",
2122 ssif_i2c_driver.driver.acpi_match_table =
2123 ACPI_PTR(ssif_acpi_match);
2126 rv = platform_driver_register(&ipmi_driver);
2128 pr_err("Unable to register driver: %d\n", rv);
2130 platform_registered = true;
2133 ssif_i2c_driver.address_list = ssif_address_list();
2135 rv = i2c_add_driver(&ssif_i2c_driver);
2141 module_init(init_ipmi_ssif);
2143 static void __exit cleanup_ipmi_ssif(void)
2148 initialized = false;
2150 i2c_del_driver(&ssif_i2c_driver);
2152 kfree(ssif_i2c_driver.address_list);
2154 if (ssif_trydmi && platform_registered)
2155 platform_driver_unregister(&ipmi_driver);
2157 free_ssif_clients();
2159 module_exit(cleanup_ipmi_ssif);
2161 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2163 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2164 MODULE_LICENSE("GPL");