sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / char / ipmi / ipmi_ssif.c
blobcca6e5bc1cea3c01831b66223f09b22fe7cf4c74
1 /*
2 * ipmi_ssif.c
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>
36 #endif
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/sched.h>
41 #include <linux/seq_file.h>
42 #include <linux/timer.h>
43 #include <linux/delay.h>
44 #include <linux/errno.h>
45 #include <linux/spinlock.h>
46 #include <linux/slab.h>
47 #include <linux/list.h>
48 #include <linux/i2c.h>
49 #include <linux/ipmi_smi.h>
50 #include <linux/init.h>
51 #include <linux/dmi.h>
52 #include <linux/kthread.h>
53 #include <linux/acpi.h>
54 #include <linux/ctype.h>
55 #include <linux/time64.h>
57 #define PFX "ipmi_ssif: "
58 #define DEVICE_NAME "ipmi_ssif"
60 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
62 #define SSIF_IPMI_REQUEST 2
63 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
64 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
65 #define SSIF_IPMI_RESPONSE 3
66 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
68 /* ssif_debug is a bit-field
69 * SSIF_DEBUG_MSG - commands and their responses
70 * SSIF_DEBUG_STATES - message states
71 * SSIF_DEBUG_TIMING - Measure times between events in the driver
73 #define SSIF_DEBUG_TIMING 4
74 #define SSIF_DEBUG_STATE 2
75 #define SSIF_DEBUG_MSG 1
76 #define SSIF_NODEBUG 0
77 #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
80 * Timer values
82 #define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
83 #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
85 /* How many times to we retry sending/receiving the message. */
86 #define SSIF_SEND_RETRIES 5
87 #define SSIF_RECV_RETRIES 250
89 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
90 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
91 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
93 enum ssif_intf_state {
94 SSIF_NORMAL,
95 SSIF_GETTING_FLAGS,
96 SSIF_GETTING_EVENTS,
97 SSIF_CLEARING_FLAGS,
98 SSIF_GETTING_MESSAGES,
99 /* FIXME - add watchdog stuff. */
102 #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
103 && (ssif)->curr_msg == NULL)
106 * Indexes into stats[] in ssif_info below.
108 enum ssif_stat_indexes {
109 /* Number of total messages sent. */
110 SSIF_STAT_sent_messages = 0,
113 * Number of message parts sent. Messages may be broken into
114 * parts if they are long.
116 SSIF_STAT_sent_messages_parts,
119 * Number of time a message was retried.
121 SSIF_STAT_send_retries,
124 * Number of times the send of a message failed.
126 SSIF_STAT_send_errors,
129 * Number of message responses received.
131 SSIF_STAT_received_messages,
134 * Number of message fragments received.
136 SSIF_STAT_received_message_parts,
139 * Number of times the receive of a message was retried.
141 SSIF_STAT_receive_retries,
144 * Number of errors receiving messages.
146 SSIF_STAT_receive_errors,
149 * Number of times a flag fetch was requested.
151 SSIF_STAT_flag_fetches,
154 * Number of times the hardware didn't follow the state machine.
156 SSIF_STAT_hosed,
159 * Number of received events.
161 SSIF_STAT_events,
163 /* Number of asyncronous messages received. */
164 SSIF_STAT_incoming_messages,
166 /* Number of watchdog pretimeouts. */
167 SSIF_STAT_watchdog_pretimeouts,
169 /* Number of alers received. */
170 SSIF_STAT_alerts,
172 /* Always add statistics before this value, it must be last. */
173 SSIF_NUM_STATS
176 struct ssif_addr_info {
177 struct i2c_board_info binfo;
178 char *adapter_name;
179 int debug;
180 int slave_addr;
181 enum ipmi_addr_src addr_src;
182 union ipmi_smi_info_union addr_info;
184 struct mutex clients_mutex;
185 struct list_head clients;
187 struct list_head link;
190 struct ssif_info;
192 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
193 unsigned char *data, unsigned int len);
195 struct ssif_info {
196 ipmi_smi_t intf;
197 int intf_num;
198 spinlock_t lock;
199 struct ipmi_smi_msg *waiting_msg;
200 struct ipmi_smi_msg *curr_msg;
201 enum ssif_intf_state ssif_state;
202 unsigned long ssif_debug;
204 struct ipmi_smi_handlers handlers;
206 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
207 union ipmi_smi_info_union addr_info;
210 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
211 * is set to hold the flags until we are done handling everything
212 * from the flags.
214 #define RECEIVE_MSG_AVAIL 0x01
215 #define EVENT_MSG_BUFFER_FULL 0x02
216 #define WDT_PRE_TIMEOUT_INT 0x08
217 unsigned char msg_flags;
219 u8 global_enables;
220 bool has_event_buffer;
221 bool supports_alert;
224 * Used to tell what we should do with alerts. If we are
225 * waiting on a response, read the data immediately.
227 bool got_alert;
228 bool waiting_alert;
231 * If set to true, this will request events the next time the
232 * state machine is idle.
234 bool req_events;
237 * If set to true, this will request flags the next time the
238 * state machine is idle.
240 bool req_flags;
243 * Used to perform timer operations when run-to-completion
244 * mode is on. This is a countdown timer.
246 int rtc_us_timer;
248 /* Used for sending/receiving data. +1 for the length. */
249 unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
250 unsigned int data_len;
252 /* Temp receive buffer, gets copied into data. */
253 unsigned char recv[I2C_SMBUS_BLOCK_MAX];
255 struct i2c_client *client;
256 ssif_i2c_done done_handler;
258 /* Thread interface handling */
259 struct task_struct *thread;
260 struct completion wake_thread;
261 bool stopping;
262 int i2c_read_write;
263 int i2c_command;
264 unsigned char *i2c_data;
265 unsigned int i2c_size;
267 /* From the device id response. */
268 struct ipmi_device_id device_id;
270 struct timer_list retry_timer;
271 int retries_left;
273 /* Info from SSIF cmd */
274 unsigned char max_xmit_msg_size;
275 unsigned char max_recv_msg_size;
276 unsigned int multi_support;
277 int supports_pec;
279 #define SSIF_NO_MULTI 0
280 #define SSIF_MULTI_2_PART 1
281 #define SSIF_MULTI_n_PART 2
282 unsigned char *multi_data;
283 unsigned int multi_len;
284 unsigned int multi_pos;
286 atomic_t stats[SSIF_NUM_STATS];
289 #define ssif_inc_stat(ssif, stat) \
290 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
291 #define ssif_get_stat(ssif, stat) \
292 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
294 static bool initialized;
296 static atomic_t next_intf = ATOMIC_INIT(0);
298 static void return_hosed_msg(struct ssif_info *ssif_info,
299 struct ipmi_smi_msg *msg);
300 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
301 static int start_send(struct ssif_info *ssif_info,
302 unsigned char *data,
303 unsigned int len);
305 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
306 unsigned long *flags)
308 spin_lock_irqsave(&ssif_info->lock, *flags);
309 return flags;
312 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
313 unsigned long *flags)
315 spin_unlock_irqrestore(&ssif_info->lock, *flags);
318 static void deliver_recv_msg(struct ssif_info *ssif_info,
319 struct ipmi_smi_msg *msg)
321 ipmi_smi_t intf = ssif_info->intf;
323 if (!intf) {
324 ipmi_free_smi_msg(msg);
325 } else if (msg->rsp_size < 0) {
326 return_hosed_msg(ssif_info, msg);
327 pr_err(PFX
328 "Malformed message in deliver_recv_msg: rsp_size = %d\n",
329 msg->rsp_size);
330 } else {
331 ipmi_smi_msg_received(intf, msg);
335 static void return_hosed_msg(struct ssif_info *ssif_info,
336 struct ipmi_smi_msg *msg)
338 ssif_inc_stat(ssif_info, hosed);
340 /* Make it a response */
341 msg->rsp[0] = msg->data[0] | 4;
342 msg->rsp[1] = msg->data[1];
343 msg->rsp[2] = 0xFF; /* Unknown error. */
344 msg->rsp_size = 3;
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
353 * start in here.
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)
376 unsigned char mb[2];
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();
409 if (!msg) {
410 ssif_info->ssif_state = SSIF_NORMAL;
411 return;
414 ssif_info->curr_msg = msg;
415 ssif_info->ssif_state = SSIF_GETTING_EVENTS;
416 ipmi_ssif_unlock_cond(ssif_info, flags);
418 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
419 msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
420 msg->data_size = 2;
422 check_start_send(ssif_info, flags, msg);
425 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
426 unsigned long *flags)
428 struct ipmi_smi_msg *msg;
430 msg = ipmi_alloc_smi_msg();
431 if (!msg) {
432 ssif_info->ssif_state = SSIF_NORMAL;
433 return;
436 ssif_info->curr_msg = msg;
437 ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
438 ipmi_ssif_unlock_cond(ssif_info, flags);
440 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
441 msg->data[1] = IPMI_GET_MSG_CMD;
442 msg->data_size = 2;
444 check_start_send(ssif_info, flags, msg);
448 * Must be called with the message lock held. This will release the
449 * message lock. Note that the caller will check SSIF_IDLE and start a
450 * new operation, so there is no need to check for new messages to
451 * start in here.
453 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
455 if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
456 ipmi_smi_t intf = ssif_info->intf;
457 /* Watchdog pre-timeout */
458 ssif_inc_stat(ssif_info, watchdog_pretimeouts);
459 start_clear_flags(ssif_info, flags);
460 if (intf)
461 ipmi_smi_watchdog_pretimeout(intf);
462 } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
463 /* Messages available. */
464 start_recv_msg_fetch(ssif_info, flags);
465 else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
466 /* Events available. */
467 start_event_fetch(ssif_info, flags);
468 else {
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()) {
479 int result;
481 /* Wait for something to do */
482 result = wait_for_completion_interruptible(
483 &ssif_info->wake_thread);
484 if (ssif_info->stopping)
485 break;
486 if (result == -ERESTARTSYS)
487 continue;
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);
496 } else {
497 result = i2c_smbus_read_block_data(
498 ssif_info->client, ssif_info->i2c_command,
499 ssif_info->i2c_data);
500 if (result < 0)
501 ssif_info->done_handler(ssif_info, result,
502 NULL, 0);
503 else
504 ssif_info->done_handler(ssif_info, 0,
505 ssif_info->i2c_data,
506 result);
510 return 0;
513 static int ssif_i2c_send(struct ssif_info *ssif_info,
514 ssif_i2c_done handler,
515 int read_write, int command,
516 unsigned char *data, unsigned int size)
518 ssif_info->done_handler = handler;
520 ssif_info->i2c_read_write = read_write;
521 ssif_info->i2c_command = command;
522 ssif_info->i2c_data = data;
523 ssif_info->i2c_size = size;
524 complete(&ssif_info->wake_thread);
525 return 0;
529 static void msg_done_handler(struct ssif_info *ssif_info, int result,
530 unsigned char *data, unsigned int len);
532 static void start_get(struct ssif_info *ssif_info)
534 int rv;
536 ssif_info->rtc_us_timer = 0;
537 ssif_info->multi_pos = 0;
539 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
540 SSIF_IPMI_RESPONSE,
541 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
542 if (rv < 0) {
543 /* request failed, just return the error. */
544 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
545 pr_info("Error from i2c_non_blocking_op(5)\n");
547 msg_done_handler(ssif_info, -EIO, NULL, 0);
551 static void retry_timeout(unsigned long data)
553 struct ssif_info *ssif_info = (void *) data;
554 unsigned long oflags, *flags;
555 bool waiting;
557 if (ssif_info->stopping)
558 return;
560 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
561 waiting = ssif_info->waiting_alert;
562 ssif_info->waiting_alert = false;
563 ipmi_ssif_unlock_cond(ssif_info, flags);
565 if (waiting)
566 start_get(ssif_info);
570 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
571 unsigned int data)
573 struct ssif_info *ssif_info = i2c_get_clientdata(client);
574 unsigned long oflags, *flags;
575 bool do_get = false;
577 if (type != I2C_PROTOCOL_SMBUS_ALERT)
578 return;
580 ssif_inc_stat(ssif_info, alerts);
582 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
583 if (ssif_info->waiting_alert) {
584 ssif_info->waiting_alert = false;
585 del_timer(&ssif_info->retry_timer);
586 do_get = true;
587 } else if (ssif_info->curr_msg) {
588 ssif_info->got_alert = true;
590 ipmi_ssif_unlock_cond(ssif_info, flags);
591 if (do_get)
592 start_get(ssif_info);
595 static int start_resend(struct ssif_info *ssif_info);
597 static void msg_done_handler(struct ssif_info *ssif_info, int result,
598 unsigned char *data, unsigned int len)
600 struct ipmi_smi_msg *msg;
601 unsigned long oflags, *flags;
602 int rv;
605 * We are single-threaded here, so no need for a lock until we
606 * start messing with driver states or the queues.
609 if (result < 0) {
610 ssif_info->retries_left--;
611 if (ssif_info->retries_left > 0) {
612 ssif_inc_stat(ssif_info, receive_retries);
614 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
615 ssif_info->waiting_alert = true;
616 ssif_info->rtc_us_timer = SSIF_MSG_USEC;
617 mod_timer(&ssif_info->retry_timer,
618 jiffies + SSIF_MSG_JIFFIES);
619 ipmi_ssif_unlock_cond(ssif_info, flags);
620 return;
623 ssif_inc_stat(ssif_info, receive_errors);
625 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
626 pr_info("Error in msg_done_handler: %d\n", result);
627 len = 0;
628 goto continue_op;
631 if ((len > 1) && (ssif_info->multi_pos == 0)
632 && (data[0] == 0x00) && (data[1] == 0x01)) {
633 /* Start of multi-part read. Start the next transaction. */
634 int i;
636 ssif_inc_stat(ssif_info, received_message_parts);
638 /* Remove the multi-part read marker. */
639 len -= 2;
640 for (i = 0; i < len; i++)
641 ssif_info->data[i] = data[i+2];
642 ssif_info->multi_len = len;
643 ssif_info->multi_pos = 1;
645 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
646 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
647 ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
648 if (rv < 0) {
649 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
650 pr_info("Error from i2c_non_blocking_op(1)\n");
652 result = -EIO;
653 } else
654 return;
655 } else if (ssif_info->multi_pos) {
656 /* Middle of multi-part read. Start the next transaction. */
657 int i;
658 unsigned char blocknum;
660 if (len == 0) {
661 result = -EIO;
662 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
663 pr_info(PFX "Middle message with no data\n");
665 goto continue_op;
668 blocknum = data[0];
670 if (ssif_info->multi_len + len - 1 > IPMI_MAX_MSG_LENGTH) {
671 /* Received message too big, abort the operation. */
672 result = -E2BIG;
673 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
674 pr_info("Received message too big\n");
676 goto continue_op;
679 /* Remove the blocknum from the data. */
680 len--;
681 for (i = 0; i < len; i++)
682 ssif_info->data[i + ssif_info->multi_len] = data[i + 1];
683 ssif_info->multi_len += len;
684 if (blocknum == 0xff) {
685 /* End of read */
686 len = ssif_info->multi_len;
687 data = ssif_info->data;
688 } else if (blocknum + 1 != ssif_info->multi_pos) {
690 * Out of sequence block, just abort. Block
691 * numbers start at zero for the second block,
692 * but multi_pos starts at one, so the +1.
694 result = -EIO;
695 } else {
696 ssif_inc_stat(ssif_info, received_message_parts);
698 ssif_info->multi_pos++;
700 rv = ssif_i2c_send(ssif_info, msg_done_handler,
701 I2C_SMBUS_READ,
702 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
703 ssif_info->recv,
704 I2C_SMBUS_BLOCK_DATA);
705 if (rv < 0) {
706 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
707 pr_info(PFX
708 "Error from ssif_i2c_send\n");
710 result = -EIO;
711 } else
712 return;
716 if (result < 0) {
717 ssif_inc_stat(ssif_info, receive_errors);
718 } else {
719 ssif_inc_stat(ssif_info, received_messages);
720 ssif_inc_stat(ssif_info, received_message_parts);
724 continue_op:
725 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
726 pr_info(PFX "DONE 1: state = %d, result=%d.\n",
727 ssif_info->ssif_state, result);
729 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
730 msg = ssif_info->curr_msg;
731 if (msg) {
732 msg->rsp_size = len;
733 if (msg->rsp_size > IPMI_MAX_MSG_LENGTH)
734 msg->rsp_size = IPMI_MAX_MSG_LENGTH;
735 memcpy(msg->rsp, data, msg->rsp_size);
736 ssif_info->curr_msg = NULL;
739 switch (ssif_info->ssif_state) {
740 case SSIF_NORMAL:
741 ipmi_ssif_unlock_cond(ssif_info, flags);
742 if (!msg)
743 break;
745 if (result < 0)
746 return_hosed_msg(ssif_info, msg);
747 else
748 deliver_recv_msg(ssif_info, msg);
749 break;
751 case SSIF_GETTING_FLAGS:
752 /* We got the flags from the SSIF, now handle them. */
753 if ((result < 0) || (len < 4) || (data[2] != 0)) {
755 * Error fetching flags, or invalid length,
756 * just give up for now.
758 ssif_info->ssif_state = SSIF_NORMAL;
759 ipmi_ssif_unlock_cond(ssif_info, flags);
760 pr_warn(PFX "Error getting flags: %d %d, %x\n",
761 result, len, data[2]);
762 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
763 || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
764 pr_warn(PFX "Invalid response getting flags: %x %x\n",
765 data[0], data[1]);
766 } else {
767 ssif_inc_stat(ssif_info, flag_fetches);
768 ssif_info->msg_flags = data[3];
769 handle_flags(ssif_info, flags);
771 break;
773 case SSIF_CLEARING_FLAGS:
774 /* We cleared the flags. */
775 if ((result < 0) || (len < 3) || (data[2] != 0)) {
776 /* Error clearing flags */
777 pr_warn(PFX "Error clearing flags: %d %d, %x\n",
778 result, len, data[2]);
779 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
780 || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
781 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
782 data[0], data[1]);
784 ssif_info->ssif_state = SSIF_NORMAL;
785 ipmi_ssif_unlock_cond(ssif_info, flags);
786 break;
788 case SSIF_GETTING_EVENTS:
789 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
790 /* Error getting event, probably done. */
791 msg->done(msg);
793 /* Take off the event flag. */
794 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
795 handle_flags(ssif_info, flags);
796 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
797 || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
798 pr_warn(PFX "Invalid response getting events: %x %x\n",
799 msg->rsp[0], msg->rsp[1]);
800 msg->done(msg);
801 /* Take off the event flag. */
802 ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
803 handle_flags(ssif_info, flags);
804 } else {
805 handle_flags(ssif_info, flags);
806 ssif_inc_stat(ssif_info, events);
807 deliver_recv_msg(ssif_info, msg);
809 break;
811 case SSIF_GETTING_MESSAGES:
812 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
813 /* Error getting event, probably done. */
814 msg->done(msg);
816 /* Take off the msg flag. */
817 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
818 handle_flags(ssif_info, flags);
819 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
820 || msg->rsp[1] != IPMI_GET_MSG_CMD) {
821 pr_warn(PFX "Invalid response clearing flags: %x %x\n",
822 msg->rsp[0], msg->rsp[1]);
823 msg->done(msg);
825 /* Take off the msg flag. */
826 ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
827 handle_flags(ssif_info, flags);
828 } else {
829 ssif_inc_stat(ssif_info, incoming_messages);
830 handle_flags(ssif_info, flags);
831 deliver_recv_msg(ssif_info, msg);
833 break;
836 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
837 if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
838 if (ssif_info->req_events)
839 start_event_fetch(ssif_info, flags);
840 else if (ssif_info->req_flags)
841 start_flag_fetch(ssif_info, flags);
842 else
843 start_next_msg(ssif_info, flags);
844 } else
845 ipmi_ssif_unlock_cond(ssif_info, flags);
847 if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
848 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
851 static void msg_written_handler(struct ssif_info *ssif_info, int result,
852 unsigned char *data, unsigned int len)
854 int rv;
856 /* We are single-threaded here, so no need for a lock. */
857 if (result < 0) {
858 ssif_info->retries_left--;
859 if (ssif_info->retries_left > 0) {
860 if (!start_resend(ssif_info)) {
861 ssif_inc_stat(ssif_info, send_retries);
862 return;
864 /* request failed, just return the error. */
865 ssif_inc_stat(ssif_info, send_errors);
867 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
868 pr_info(PFX
869 "Out of retries in msg_written_handler\n");
870 msg_done_handler(ssif_info, -EIO, NULL, 0);
871 return;
874 ssif_inc_stat(ssif_info, send_errors);
877 * Got an error on transmit, let the done routine
878 * handle it.
880 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
881 pr_info("Error in msg_written_handler: %d\n", result);
883 msg_done_handler(ssif_info, result, NULL, 0);
884 return;
887 if (ssif_info->multi_data) {
889 * In the middle of a multi-data write. See the comment
890 * in the SSIF_MULTI_n_PART case in the probe function
891 * for details on the intricacies of this.
893 int left;
895 ssif_inc_stat(ssif_info, sent_messages_parts);
897 left = ssif_info->multi_len - ssif_info->multi_pos;
898 if (left > 32)
899 left = 32;
900 /* Length byte. */
901 ssif_info->multi_data[ssif_info->multi_pos] = left;
902 ssif_info->multi_pos += left;
903 if (left < 32)
905 * Write is finished. Note that we must end
906 * with a write of less than 32 bytes to
907 * complete the transaction, even if it is
908 * zero bytes.
910 ssif_info->multi_data = NULL;
912 rv = ssif_i2c_send(ssif_info, msg_written_handler,
913 I2C_SMBUS_WRITE,
914 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
915 ssif_info->multi_data + ssif_info->multi_pos,
916 I2C_SMBUS_BLOCK_DATA);
917 if (rv < 0) {
918 /* request failed, just return the error. */
919 ssif_inc_stat(ssif_info, send_errors);
921 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
922 pr_info("Error from i2c_non_blocking_op(3)\n");
923 msg_done_handler(ssif_info, -EIO, NULL, 0);
925 } else {
926 /* Ready to request the result. */
927 unsigned long oflags, *flags;
929 ssif_inc_stat(ssif_info, sent_messages);
930 ssif_inc_stat(ssif_info, sent_messages_parts);
932 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
933 if (ssif_info->got_alert) {
934 /* The result is already ready, just start it. */
935 ssif_info->got_alert = false;
936 ipmi_ssif_unlock_cond(ssif_info, flags);
937 start_get(ssif_info);
938 } else {
939 /* Wait a jiffie then request the next message */
940 ssif_info->waiting_alert = true;
941 ssif_info->retries_left = SSIF_RECV_RETRIES;
942 ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
943 mod_timer(&ssif_info->retry_timer,
944 jiffies + SSIF_MSG_PART_JIFFIES);
945 ipmi_ssif_unlock_cond(ssif_info, flags);
950 static int start_resend(struct ssif_info *ssif_info)
952 int rv;
953 int command;
955 ssif_info->got_alert = false;
957 if (ssif_info->data_len > 32) {
958 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
959 ssif_info->multi_data = ssif_info->data;
960 ssif_info->multi_len = ssif_info->data_len;
962 * Subtle thing, this is 32, not 33, because we will
963 * overwrite the thing at position 32 (which was just
964 * transmitted) with the new length.
966 ssif_info->multi_pos = 32;
967 ssif_info->data[0] = 32;
968 } else {
969 ssif_info->multi_data = NULL;
970 command = SSIF_IPMI_REQUEST;
971 ssif_info->data[0] = ssif_info->data_len;
974 rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
975 command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
976 if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
977 pr_info("Error from i2c_non_blocking_op(4)\n");
978 return rv;
981 static int start_send(struct ssif_info *ssif_info,
982 unsigned char *data,
983 unsigned int len)
985 if (len > IPMI_MAX_MSG_LENGTH)
986 return -E2BIG;
987 if (len > ssif_info->max_xmit_msg_size)
988 return -E2BIG;
990 ssif_info->retries_left = SSIF_SEND_RETRIES;
991 memcpy(ssif_info->data + 1, data, len);
992 ssif_info->data_len = len;
993 return start_resend(ssif_info);
996 /* Must be called with the message lock held. */
997 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
999 struct ipmi_smi_msg *msg;
1000 unsigned long oflags;
1002 restart:
1003 if (!SSIF_IDLE(ssif_info)) {
1004 ipmi_ssif_unlock_cond(ssif_info, flags);
1005 return;
1008 if (!ssif_info->waiting_msg) {
1009 ssif_info->curr_msg = NULL;
1010 ipmi_ssif_unlock_cond(ssif_info, flags);
1011 } else {
1012 int rv;
1014 ssif_info->curr_msg = ssif_info->waiting_msg;
1015 ssif_info->waiting_msg = NULL;
1016 ipmi_ssif_unlock_cond(ssif_info, flags);
1017 rv = start_send(ssif_info,
1018 ssif_info->curr_msg->data,
1019 ssif_info->curr_msg->data_size);
1020 if (rv) {
1021 msg = ssif_info->curr_msg;
1022 ssif_info->curr_msg = NULL;
1023 return_hosed_msg(ssif_info, msg);
1024 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1025 goto restart;
1030 static void sender(void *send_info,
1031 struct ipmi_smi_msg *msg)
1033 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1034 unsigned long oflags, *flags;
1036 BUG_ON(ssif_info->waiting_msg);
1037 ssif_info->waiting_msg = msg;
1039 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1040 start_next_msg(ssif_info, flags);
1042 if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1043 struct timespec64 t;
1045 ktime_get_real_ts64(&t);
1046 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1047 msg->data[0], msg->data[1],
1048 (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
1052 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1054 struct ssif_info *ssif_info = send_info;
1056 data->addr_src = ssif_info->addr_source;
1057 data->dev = &ssif_info->client->dev;
1058 data->addr_info = ssif_info->addr_info;
1059 get_device(data->dev);
1061 return 0;
1065 * Instead of having our own timer to periodically check the message
1066 * flags, we let the message handler drive us.
1068 static void request_events(void *send_info)
1070 struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1071 unsigned long oflags, *flags;
1073 if (!ssif_info->has_event_buffer)
1074 return;
1076 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1078 * Request flags first, not events, because the lower layer
1079 * doesn't have a way to send an attention. But make sure
1080 * event checking still happens.
1082 ssif_info->req_events = true;
1083 if (SSIF_IDLE(ssif_info))
1084 start_flag_fetch(ssif_info, flags);
1085 else {
1086 ssif_info->req_flags = true;
1087 ipmi_ssif_unlock_cond(ssif_info, flags);
1091 static int inc_usecount(void *send_info)
1093 struct ssif_info *ssif_info = send_info;
1095 if (!i2c_get_adapter(ssif_info->client->adapter->nr))
1096 return -ENODEV;
1098 i2c_use_client(ssif_info->client);
1099 return 0;
1102 static void dec_usecount(void *send_info)
1104 struct ssif_info *ssif_info = send_info;
1106 i2c_release_client(ssif_info->client);
1107 i2c_put_adapter(ssif_info->client->adapter);
1110 static int ssif_start_processing(void *send_info,
1111 ipmi_smi_t intf)
1113 struct ssif_info *ssif_info = send_info;
1115 ssif_info->intf = intf;
1117 return 0;
1120 #define MAX_SSIF_BMCS 4
1122 static unsigned short addr[MAX_SSIF_BMCS];
1123 static int num_addrs;
1124 module_param_array(addr, ushort, &num_addrs, 0);
1125 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1127 static char *adapter_name[MAX_SSIF_BMCS];
1128 static int num_adapter_names;
1129 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1130 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1132 static int slave_addrs[MAX_SSIF_BMCS];
1133 static int num_slave_addrs;
1134 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1135 MODULE_PARM_DESC(slave_addrs,
1136 "The default IPMB slave address for the controller.");
1138 static bool alerts_broken;
1139 module_param(alerts_broken, bool, 0);
1140 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1143 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1144 * bit 2 enables timing debugging. This is an array indexed by
1145 * interface number"
1147 static int dbg[MAX_SSIF_BMCS];
1148 static int num_dbg;
1149 module_param_array(dbg, int, &num_dbg, 0);
1150 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1152 static bool ssif_dbg_probe;
1153 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1154 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1156 static bool ssif_tryacpi = true;
1157 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1158 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1160 static bool ssif_trydmi = true;
1161 module_param_named(trydmi, ssif_trydmi, bool, 0);
1162 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1164 static DEFINE_MUTEX(ssif_infos_mutex);
1165 static LIST_HEAD(ssif_infos);
1167 static int ssif_remove(struct i2c_client *client)
1169 struct ssif_info *ssif_info = i2c_get_clientdata(client);
1170 int rv;
1172 if (!ssif_info)
1173 return 0;
1176 * After this point, we won't deliver anything asychronously
1177 * to the message handler. We can unregister ourself.
1179 rv = ipmi_unregister_smi(ssif_info->intf);
1180 if (rv) {
1181 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1182 return rv;
1184 ssif_info->intf = NULL;
1186 /* make sure the driver is not looking for flags any more. */
1187 while (ssif_info->ssif_state != SSIF_NORMAL)
1188 schedule_timeout(1);
1190 ssif_info->stopping = true;
1191 del_timer_sync(&ssif_info->retry_timer);
1192 if (ssif_info->thread) {
1193 complete(&ssif_info->wake_thread);
1194 kthread_stop(ssif_info->thread);
1198 * No message can be outstanding now, we have removed the
1199 * upper layer and it permitted us to do so.
1201 kfree(ssif_info);
1202 return 0;
1205 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1206 int *resp_len, unsigned char *resp)
1208 int retry_cnt;
1209 int ret;
1211 retry_cnt = SSIF_SEND_RETRIES;
1212 retry1:
1213 ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1214 if (ret) {
1215 retry_cnt--;
1216 if (retry_cnt > 0)
1217 goto retry1;
1218 return -ENODEV;
1221 ret = -ENODEV;
1222 retry_cnt = SSIF_RECV_RETRIES;
1223 while (retry_cnt > 0) {
1224 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1225 resp);
1226 if (ret > 0)
1227 break;
1228 msleep(SSIF_MSG_MSEC);
1229 retry_cnt--;
1230 if (retry_cnt <= 0)
1231 break;
1234 if (ret > 0) {
1235 /* Validate that the response is correct. */
1236 if (ret < 3 ||
1237 (resp[0] != (msg[0] | (1 << 2))) ||
1238 (resp[1] != msg[1]))
1239 ret = -EINVAL;
1240 else {
1241 *resp_len = ret;
1242 ret = 0;
1246 return ret;
1249 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1251 unsigned char *resp;
1252 unsigned char msg[3];
1253 int rv;
1254 int len;
1256 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1257 if (!resp)
1258 return -ENOMEM;
1260 /* Do a Get Device ID command, since it is required. */
1261 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1262 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1263 rv = do_cmd(client, 2, msg, &len, resp);
1264 if (rv)
1265 rv = -ENODEV;
1266 else
1267 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1268 kfree(resp);
1269 return rv;
1272 static int smi_type_proc_show(struct seq_file *m, void *v)
1274 seq_puts(m, "ssif\n");
1276 return 0;
1279 static int smi_type_proc_open(struct inode *inode, struct file *file)
1281 return single_open(file, smi_type_proc_show, inode->i_private);
1284 static const struct file_operations smi_type_proc_ops = {
1285 .open = smi_type_proc_open,
1286 .read = seq_read,
1287 .llseek = seq_lseek,
1288 .release = single_release,
1291 static int smi_stats_proc_show(struct seq_file *m, void *v)
1293 struct ssif_info *ssif_info = m->private;
1295 seq_printf(m, "sent_messages: %u\n",
1296 ssif_get_stat(ssif_info, sent_messages));
1297 seq_printf(m, "sent_messages_parts: %u\n",
1298 ssif_get_stat(ssif_info, sent_messages_parts));
1299 seq_printf(m, "send_retries: %u\n",
1300 ssif_get_stat(ssif_info, send_retries));
1301 seq_printf(m, "send_errors: %u\n",
1302 ssif_get_stat(ssif_info, send_errors));
1303 seq_printf(m, "received_messages: %u\n",
1304 ssif_get_stat(ssif_info, received_messages));
1305 seq_printf(m, "received_message_parts: %u\n",
1306 ssif_get_stat(ssif_info, received_message_parts));
1307 seq_printf(m, "receive_retries: %u\n",
1308 ssif_get_stat(ssif_info, receive_retries));
1309 seq_printf(m, "receive_errors: %u\n",
1310 ssif_get_stat(ssif_info, receive_errors));
1311 seq_printf(m, "flag_fetches: %u\n",
1312 ssif_get_stat(ssif_info, flag_fetches));
1313 seq_printf(m, "hosed: %u\n",
1314 ssif_get_stat(ssif_info, hosed));
1315 seq_printf(m, "events: %u\n",
1316 ssif_get_stat(ssif_info, events));
1317 seq_printf(m, "watchdog_pretimeouts: %u\n",
1318 ssif_get_stat(ssif_info, watchdog_pretimeouts));
1319 seq_printf(m, "alerts: %u\n",
1320 ssif_get_stat(ssif_info, alerts));
1321 return 0;
1324 static int smi_stats_proc_open(struct inode *inode, struct file *file)
1326 return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1329 static const struct file_operations smi_stats_proc_ops = {
1330 .open = smi_stats_proc_open,
1331 .read = seq_read,
1332 .llseek = seq_lseek,
1333 .release = single_release,
1336 static int strcmp_nospace(char *s1, char *s2)
1338 while (*s1 && *s2) {
1339 while (isspace(*s1))
1340 s1++;
1341 while (isspace(*s2))
1342 s2++;
1343 if (*s1 > *s2)
1344 return 1;
1345 if (*s1 < *s2)
1346 return -1;
1347 s1++;
1348 s2++;
1350 return 0;
1353 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1354 char *adapter_name,
1355 bool match_null_name)
1357 struct ssif_addr_info *info, *found = NULL;
1359 restart:
1360 list_for_each_entry(info, &ssif_infos, link) {
1361 if (info->binfo.addr == addr) {
1362 if (info->adapter_name || adapter_name) {
1363 if (!info->adapter_name != !adapter_name) {
1364 /* One is NULL and one is not */
1365 continue;
1367 if (adapter_name &&
1368 strcmp_nospace(info->adapter_name,
1369 adapter_name))
1370 /* Names do not match */
1371 continue;
1373 found = info;
1374 break;
1378 if (!found && match_null_name) {
1379 /* Try to get an exact match first, then try with a NULL name */
1380 adapter_name = NULL;
1381 match_null_name = false;
1382 goto restart;
1385 return found;
1388 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1390 #ifdef CONFIG_ACPI
1391 acpi_handle acpi_handle;
1393 acpi_handle = ACPI_HANDLE(dev);
1394 if (acpi_handle) {
1395 ssif_info->addr_source = SI_ACPI;
1396 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1397 return true;
1399 #endif
1400 return false;
1403 static int find_slave_address(struct i2c_client *client, int slave_addr)
1405 struct ssif_addr_info *info;
1407 if (slave_addr)
1408 return slave_addr;
1411 * Came in without a slave address, search around to see if
1412 * the other sources have a slave address. This lets us pick
1413 * up an SMBIOS slave address when using ACPI.
1415 list_for_each_entry(info, &ssif_infos, link) {
1416 if (info->binfo.addr != client->addr)
1417 continue;
1418 if (info->adapter_name && client->adapter->name &&
1419 strcmp_nospace(info->adapter_name,
1420 client->adapter->name))
1421 continue;
1422 if (info->slave_addr) {
1423 slave_addr = info->slave_addr;
1424 break;
1428 return slave_addr;
1432 * Global enables we care about.
1434 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1435 IPMI_BMC_EVT_MSG_INTR)
1437 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1439 unsigned char msg[3];
1440 unsigned char *resp;
1441 struct ssif_info *ssif_info;
1442 int rv = 0;
1443 int len;
1444 int i;
1445 u8 slave_addr = 0;
1446 struct ssif_addr_info *addr_info = NULL;
1449 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1450 if (!resp)
1451 return -ENOMEM;
1453 ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1454 if (!ssif_info) {
1455 kfree(resp);
1456 return -ENOMEM;
1459 if (!check_acpi(ssif_info, &client->dev)) {
1460 addr_info = ssif_info_find(client->addr, client->adapter->name,
1461 true);
1462 if (!addr_info) {
1463 /* Must have come in through sysfs. */
1464 ssif_info->addr_source = SI_HOTMOD;
1465 } else {
1466 ssif_info->addr_source = addr_info->addr_src;
1467 ssif_info->ssif_debug = addr_info->debug;
1468 ssif_info->addr_info = addr_info->addr_info;
1469 slave_addr = addr_info->slave_addr;
1473 slave_addr = find_slave_address(client, slave_addr);
1475 pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1476 ipmi_addr_src_to_str(ssif_info->addr_source),
1477 client->addr, client->adapter->name, slave_addr);
1480 * Do a Get Device ID command, since it comes back with some
1481 * useful info.
1483 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1484 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1485 rv = do_cmd(client, 2, msg, &len, resp);
1486 if (rv)
1487 goto out;
1489 rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1490 if (rv)
1491 goto out;
1493 ssif_info->client = client;
1494 i2c_set_clientdata(client, ssif_info);
1496 /* Now check for system interface capabilities */
1497 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1498 msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1499 msg[2] = 0; /* SSIF */
1500 rv = do_cmd(client, 3, msg, &len, resp);
1501 if (!rv && (len >= 3) && (resp[2] == 0)) {
1502 if (len < 7) {
1503 if (ssif_dbg_probe)
1504 pr_info(PFX "SSIF info too short: %d\n", len);
1505 goto no_support;
1508 /* Got a good SSIF response, handle it. */
1509 ssif_info->max_xmit_msg_size = resp[5];
1510 ssif_info->max_recv_msg_size = resp[6];
1511 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1512 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1514 /* Sanitize the data */
1515 switch (ssif_info->multi_support) {
1516 case SSIF_NO_MULTI:
1517 if (ssif_info->max_xmit_msg_size > 32)
1518 ssif_info->max_xmit_msg_size = 32;
1519 if (ssif_info->max_recv_msg_size > 32)
1520 ssif_info->max_recv_msg_size = 32;
1521 break;
1523 case SSIF_MULTI_2_PART:
1524 if (ssif_info->max_xmit_msg_size > 63)
1525 ssif_info->max_xmit_msg_size = 63;
1526 if (ssif_info->max_recv_msg_size > 62)
1527 ssif_info->max_recv_msg_size = 62;
1528 break;
1530 case SSIF_MULTI_n_PART:
1532 * The specification is rather confusing at
1533 * this point, but I think I understand what
1534 * is meant. At least I have a workable
1535 * solution. With multi-part messages, you
1536 * cannot send a message that is a multiple of
1537 * 32-bytes in length, because the start and
1538 * middle messages are 32-bytes and the end
1539 * message must be at least one byte. You
1540 * can't fudge on an extra byte, that would
1541 * screw up things like fru data writes. So
1542 * we limit the length to 63 bytes. That way
1543 * a 32-byte message gets sent as a single
1544 * part. A larger message will be a 32-byte
1545 * start and the next message is always going
1546 * to be 1-31 bytes in length. Not ideal, but
1547 * it should work.
1549 if (ssif_info->max_xmit_msg_size > 63)
1550 ssif_info->max_xmit_msg_size = 63;
1551 break;
1553 default:
1554 /* Data is not sane, just give up. */
1555 goto no_support;
1557 } else {
1558 no_support:
1559 /* Assume no multi-part or PEC support */
1560 pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1561 rv, len, resp[2]);
1563 ssif_info->max_xmit_msg_size = 32;
1564 ssif_info->max_recv_msg_size = 32;
1565 ssif_info->multi_support = SSIF_NO_MULTI;
1566 ssif_info->supports_pec = 0;
1569 /* Make sure the NMI timeout is cleared. */
1570 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1571 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1572 msg[2] = WDT_PRE_TIMEOUT_INT;
1573 rv = do_cmd(client, 3, msg, &len, resp);
1574 if (rv || (len < 3) || (resp[2] != 0))
1575 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1576 rv, len, resp[2]);
1578 /* Attempt to enable the event buffer. */
1579 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1580 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1581 rv = do_cmd(client, 2, msg, &len, resp);
1582 if (rv || (len < 4) || (resp[2] != 0)) {
1583 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1584 rv, len, resp[2]);
1585 rv = 0; /* Not fatal */
1586 goto found;
1589 ssif_info->global_enables = resp[3];
1591 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1592 ssif_info->has_event_buffer = true;
1593 /* buffer is already enabled, nothing to do. */
1594 goto found;
1597 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1598 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1599 msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1600 rv = do_cmd(client, 3, msg, &len, resp);
1601 if (rv || (len < 2)) {
1602 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1603 rv, len, resp[2]);
1604 rv = 0; /* Not fatal */
1605 goto found;
1608 if (resp[2] == 0) {
1609 /* A successful return means the event buffer is supported. */
1610 ssif_info->has_event_buffer = true;
1611 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1614 /* Some systems don't behave well if you enable alerts. */
1615 if (alerts_broken)
1616 goto found;
1618 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1619 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1620 msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1621 rv = do_cmd(client, 3, msg, &len, resp);
1622 if (rv || (len < 2)) {
1623 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1624 rv, len, resp[2]);
1625 rv = 0; /* Not fatal */
1626 goto found;
1629 if (resp[2] == 0) {
1630 /* A successful return means the alert is supported. */
1631 ssif_info->supports_alert = true;
1632 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1635 found:
1636 ssif_info->intf_num = atomic_inc_return(&next_intf);
1638 if (ssif_dbg_probe) {
1639 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1640 client->addr);
1643 spin_lock_init(&ssif_info->lock);
1644 ssif_info->ssif_state = SSIF_NORMAL;
1645 init_timer(&ssif_info->retry_timer);
1646 ssif_info->retry_timer.data = (unsigned long) ssif_info;
1647 ssif_info->retry_timer.function = retry_timeout;
1649 for (i = 0; i < SSIF_NUM_STATS; i++)
1650 atomic_set(&ssif_info->stats[i], 0);
1652 if (ssif_info->supports_pec)
1653 ssif_info->client->flags |= I2C_CLIENT_PEC;
1655 ssif_info->handlers.owner = THIS_MODULE;
1656 ssif_info->handlers.start_processing = ssif_start_processing;
1657 ssif_info->handlers.get_smi_info = get_smi_info;
1658 ssif_info->handlers.sender = sender;
1659 ssif_info->handlers.request_events = request_events;
1660 ssif_info->handlers.inc_usecount = inc_usecount;
1661 ssif_info->handlers.dec_usecount = dec_usecount;
1664 unsigned int thread_num;
1666 thread_num = ((ssif_info->client->adapter->nr << 8) |
1667 ssif_info->client->addr);
1668 init_completion(&ssif_info->wake_thread);
1669 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1670 "kssif%4.4x", thread_num);
1671 if (IS_ERR(ssif_info->thread)) {
1672 rv = PTR_ERR(ssif_info->thread);
1673 dev_notice(&ssif_info->client->dev,
1674 "Could not start kernel thread: error %d\n",
1675 rv);
1676 goto out;
1680 rv = ipmi_register_smi(&ssif_info->handlers,
1681 ssif_info,
1682 &ssif_info->device_id,
1683 &ssif_info->client->dev,
1684 slave_addr);
1685 if (rv) {
1686 pr_err(PFX "Unable to register device: error %d\n", rv);
1687 goto out;
1690 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1691 &smi_type_proc_ops,
1692 ssif_info);
1693 if (rv) {
1694 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1695 goto out_err_unreg;
1698 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1699 &smi_stats_proc_ops,
1700 ssif_info);
1701 if (rv) {
1702 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1703 goto out_err_unreg;
1706 out:
1707 if (rv)
1708 kfree(ssif_info);
1709 kfree(resp);
1710 return rv;
1712 out_err_unreg:
1713 ipmi_unregister_smi(ssif_info->intf);
1714 goto out;
1717 static int ssif_adapter_handler(struct device *adev, void *opaque)
1719 struct ssif_addr_info *addr_info = opaque;
1721 if (adev->type != &i2c_adapter_type)
1722 return 0;
1724 i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo);
1726 if (!addr_info->adapter_name)
1727 return 1; /* Only try the first I2C adapter by default. */
1728 return 0;
1731 static int new_ssif_client(int addr, char *adapter_name,
1732 int debug, int slave_addr,
1733 enum ipmi_addr_src addr_src)
1735 struct ssif_addr_info *addr_info;
1736 int rv = 0;
1738 mutex_lock(&ssif_infos_mutex);
1739 if (ssif_info_find(addr, adapter_name, false)) {
1740 rv = -EEXIST;
1741 goto out_unlock;
1744 addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1745 if (!addr_info) {
1746 rv = -ENOMEM;
1747 goto out_unlock;
1750 if (adapter_name) {
1751 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1752 if (!addr_info->adapter_name) {
1753 kfree(addr_info);
1754 rv = -ENOMEM;
1755 goto out_unlock;
1759 strncpy(addr_info->binfo.type, DEVICE_NAME,
1760 sizeof(addr_info->binfo.type));
1761 addr_info->binfo.addr = addr;
1762 addr_info->binfo.platform_data = addr_info;
1763 addr_info->debug = debug;
1764 addr_info->slave_addr = slave_addr;
1765 addr_info->addr_src = addr_src;
1767 list_add_tail(&addr_info->link, &ssif_infos);
1769 if (initialized)
1770 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1771 /* Otherwise address list will get it */
1773 out_unlock:
1774 mutex_unlock(&ssif_infos_mutex);
1775 return rv;
1778 static void free_ssif_clients(void)
1780 struct ssif_addr_info *info, *tmp;
1782 mutex_lock(&ssif_infos_mutex);
1783 list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1784 list_del(&info->link);
1785 kfree(info->adapter_name);
1786 kfree(info);
1788 mutex_unlock(&ssif_infos_mutex);
1791 static unsigned short *ssif_address_list(void)
1793 struct ssif_addr_info *info;
1794 unsigned int count = 0, i;
1795 unsigned short *address_list;
1797 list_for_each_entry(info, &ssif_infos, link)
1798 count++;
1800 address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1801 if (!address_list)
1802 return NULL;
1804 i = 0;
1805 list_for_each_entry(info, &ssif_infos, link) {
1806 unsigned short addr = info->binfo.addr;
1807 int j;
1809 for (j = 0; j < i; j++) {
1810 if (address_list[j] == addr)
1811 goto skip_addr;
1813 address_list[i] = addr;
1814 skip_addr:
1815 i++;
1817 address_list[i] = I2C_CLIENT_END;
1819 return address_list;
1822 #ifdef CONFIG_ACPI
1823 static const struct acpi_device_id ssif_acpi_match[] = {
1824 { "IPI0001", 0 },
1825 { },
1827 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1830 * Once we get an ACPI failure, we don't try any more, because we go
1831 * through the tables sequentially. Once we don't find a table, there
1832 * are no more.
1834 static int acpi_failure;
1837 * Defined in the IPMI 2.0 spec.
1839 struct SPMITable {
1840 s8 Signature[4];
1841 u32 Length;
1842 u8 Revision;
1843 u8 Checksum;
1844 s8 OEMID[6];
1845 s8 OEMTableID[8];
1846 s8 OEMRevision[4];
1847 s8 CreatorID[4];
1848 s8 CreatorRevision[4];
1849 u8 InterfaceType;
1850 u8 IPMIlegacy;
1851 s16 SpecificationRevision;
1854 * Bit 0 - SCI interrupt supported
1855 * Bit 1 - I/O APIC/SAPIC
1857 u8 InterruptType;
1860 * If bit 0 of InterruptType is set, then this is the SCI
1861 * interrupt in the GPEx_STS register.
1863 u8 GPE;
1865 s16 Reserved;
1868 * If bit 1 of InterruptType is set, then this is the I/O
1869 * APIC/SAPIC interrupt.
1871 u32 GlobalSystemInterrupt;
1873 /* The actual register address. */
1874 struct acpi_generic_address addr;
1876 u8 UID[4];
1878 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
1881 static int try_init_spmi(struct SPMITable *spmi)
1883 unsigned short myaddr;
1885 if (num_addrs >= MAX_SSIF_BMCS)
1886 return -1;
1888 if (spmi->IPMIlegacy != 1) {
1889 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1890 return -ENODEV;
1893 if (spmi->InterfaceType != 4)
1894 return -ENODEV;
1896 if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1897 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1898 spmi->addr.space_id);
1899 return -EIO;
1902 myaddr = spmi->addr.address & 0x7f;
1904 return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI);
1907 static void spmi_find_bmc(void)
1909 acpi_status status;
1910 struct SPMITable *spmi;
1911 int i;
1913 if (acpi_disabled)
1914 return;
1916 if (acpi_failure)
1917 return;
1919 for (i = 0; ; i++) {
1920 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1921 (struct acpi_table_header **)&spmi);
1922 if (status != AE_OK)
1923 return;
1925 try_init_spmi(spmi);
1928 #else
1929 static void spmi_find_bmc(void) { }
1930 #endif
1932 #ifdef CONFIG_DMI
1933 static int decode_dmi(const struct dmi_device *dmi_dev)
1935 struct dmi_header *dm = dmi_dev->device_data;
1936 u8 *data = (u8 *) dm;
1937 u8 len = dm->length;
1938 unsigned short myaddr;
1939 int slave_addr;
1941 if (num_addrs >= MAX_SSIF_BMCS)
1942 return -1;
1944 if (len < 9)
1945 return -1;
1947 if (data[0x04] != 4) /* Not SSIF */
1948 return -1;
1950 if ((data[8] >> 1) == 0) {
1952 * Some broken systems put the I2C address in
1953 * the slave address field. We try to
1954 * accommodate them here.
1956 myaddr = data[6] >> 1;
1957 slave_addr = 0;
1958 } else {
1959 myaddr = data[8] >> 1;
1960 slave_addr = data[6];
1963 return new_ssif_client(myaddr, NULL, 0, slave_addr, SI_SMBIOS);
1966 static void dmi_iterator(void)
1968 const struct dmi_device *dev = NULL;
1970 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
1971 decode_dmi(dev);
1973 #else
1974 static void dmi_iterator(void) { }
1975 #endif
1977 static const struct i2c_device_id ssif_id[] = {
1978 { DEVICE_NAME, 0 },
1981 MODULE_DEVICE_TABLE(i2c, ssif_id);
1983 static struct i2c_driver ssif_i2c_driver = {
1984 .class = I2C_CLASS_HWMON,
1985 .driver = {
1986 .name = DEVICE_NAME
1988 .probe = ssif_probe,
1989 .remove = ssif_remove,
1990 .alert = ssif_alert,
1991 .id_table = ssif_id,
1992 .detect = ssif_detect
1995 static int init_ipmi_ssif(void)
1997 int i;
1998 int rv;
2000 if (initialized)
2001 return 0;
2003 pr_info("IPMI SSIF Interface driver\n");
2005 /* build list for i2c from addr list */
2006 for (i = 0; i < num_addrs; i++) {
2007 rv = new_ssif_client(addr[i], adapter_name[i],
2008 dbg[i], slave_addrs[i],
2009 SI_HARDCODED);
2010 if (rv)
2011 pr_err(PFX
2012 "Couldn't add hardcoded device at addr 0x%x\n",
2013 addr[i]);
2016 if (ssif_tryacpi)
2017 ssif_i2c_driver.driver.acpi_match_table =
2018 ACPI_PTR(ssif_acpi_match);
2019 if (ssif_trydmi)
2020 dmi_iterator();
2021 if (ssif_tryacpi)
2022 spmi_find_bmc();
2024 ssif_i2c_driver.address_list = ssif_address_list();
2026 rv = i2c_add_driver(&ssif_i2c_driver);
2027 if (!rv)
2028 initialized = true;
2030 return rv;
2032 module_init(init_ipmi_ssif);
2034 static void cleanup_ipmi_ssif(void)
2036 if (!initialized)
2037 return;
2039 initialized = false;
2041 i2c_del_driver(&ssif_i2c_driver);
2043 free_ssif_clients();
2045 module_exit(cleanup_ipmi_ssif);
2047 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2048 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2049 MODULE_LICENSE("GPL");