/*
* ipmi_ssif.c
*
* The interface to the IPMI driver for SMBus access to a SMBus
* compliant device. Called SSIF by the IPMI spec.
*
* Author: Intel Corporation
* Todd Davis <[email protected]>
*
* Rewritten by Corey Minyard <[email protected]> to support the
* non-blocking I2C interface, add support for multi-part
* transactions, add PEC support, and general clenaup.
*
* Copyright 2003 Intel Corporation
* Copyright 2005 MontaVista Software
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*/
/*
* This file holds the "policy" for the interface to the SSIF state
* machine. It does the configuration, handles timers and interrupts,
* and drives the real SSIF state machine.
*/
/*
* TODO: Figure out how to use SMB alerts. This will require a new
* interface into the I2C driver, I believe.
*/
#include <linux/version.h>
#if defined(MODVERSIONS)
#include <linux/modversions.h>
#endif
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/seq_file.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/i2c.h>
#include <linux/ipmi_smi.h>
#include <linux/init.h>
#include <linux/dmi.h>
#include <linux/kthread.h>
#include <linux/acpi.h>
#define PFX "ipmi_ssif: "
#define DEVICE_NAME "ipmi_ssif"
#define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
#define SSIF_IPMI_REQUEST 2
#define SSIF_IPMI_MULTI_PART_REQUEST_START 6
#define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
#define SSIF_IPMI_RESPONSE 3
#define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
/* ssif_debug is a bit-field
* SSIF_DEBUG_MSG - commands and their responses
* SSIF_DEBUG_STATES - message states
* SSIF_DEBUG_TIMING - Measure times between events in the driver
*/
#define SSIF_DEBUG_TIMING 4
#define SSIF_DEBUG_STATE 2
#define SSIF_DEBUG_MSG 1
#define SSIF_NODEBUG 0
#define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
/*
* Timer values
*/
#define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
#define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
/* How many times to we retry sending/receiving the message. */
#define SSIF_SEND_RETRIES 5
#define SSIF_RECV_RETRIES 250
#define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
#define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
#define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
enum ssif_intf_state {
SSIF_NORMAL,
SSIF_GETTING_FLAGS,
SSIF_GETTING_EVENTS,
SSIF_CLEARING_FLAGS,
SSIF_GETTING_MESSAGES,
/* FIXME - add watchdog stuff. */
};
#define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
&& (ssif)->curr_msg == NULL)
/*
* Indexes into stats[] in ssif_info below.
*/
enum ssif_stat_indexes {
/* Number of total messages sent. */
SSIF_STAT_sent_messages = 0,
/*
* Number of message parts sent. Messages may be broken into
* parts if they are long.
*/
SSIF_STAT_sent_messages_parts,
/*
* Number of time a message was retried.
*/
SSIF_STAT_send_retries,
/*
* Number of times the send of a message failed.
*/
SSIF_STAT_send_errors,
/*
* Number of message responses received.
*/
SSIF_STAT_received_messages,
/*
* Number of message fragments received.
*/
SSIF_STAT_received_message_parts,
/*
* Number of times the receive of a message was retried.
*/
SSIF_STAT_receive_retries,
/*
* Number of errors receiving messages.
*/
SSIF_STAT_receive_errors,
/*
* Number of times a flag fetch was requested.
*/
SSIF_STAT_flag_fetches,
/*
* Number of times the hardware didn't follow the state machine.
*/
SSIF_STAT_hosed,
/*
* Number of received events.
*/
SSIF_STAT_events,
/* Number of asyncronous messages received. */
SSIF_STAT_incoming_messages,
/* Number of watchdog pretimeouts. */
SSIF_STAT_watchdog_pretimeouts,
/* Always add statistics before this value, it must be last. */
SSIF_NUM_STATS
};
struct ssif_addr_info {
unsigned short addr;
struct i2c_board_info binfo;
char *adapter_name;
int debug;
int slave_addr;
enum ipmi_addr_src addr_src;
union ipmi_smi_info_union addr_info;
struct mutex clients_mutex;
struct list_head clients;
struct list_head link;
};
struct ssif_info;
typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
unsigned char *data, unsigned int len);
struct ssif_info {
ipmi_smi_t intf;
int intf_num;
spinlock_t lock;
struct ipmi_smi_msg *waiting_msg;
struct ipmi_smi_msg *curr_msg;
enum ssif_intf_state ssif_state;
unsigned long ssif_debug;
struct ipmi_smi_handlers handlers;
enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
union ipmi_smi_info_union addr_info;
/*
* Flags from the last GET_MSG_FLAGS command, used when an ATTN
* is set to hold the flags until we are done handling everything
* from the flags.
*/
#define RECEIVE_MSG_AVAIL 0x01
#define EVENT_MSG_BUFFER_FULL 0x02
#define WDT_PRE_TIMEOUT_INT 0x08
unsigned char msg_flags;
bool has_event_buffer;
/*
* If set to true, this will request events the next time the
* state machine is idle.
*/
bool req_events;
/*
* If set to true, this will request flags the next time the
* state machine is idle.
*/
bool req_flags;
/*
* Used to perform timer operations when run-to-completion
* mode is on. This is a countdown timer.
*/
int rtc_us_timer;
/* Used for sending/receiving data. +1 for the length. */
unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
unsigned int data_len;
/* Temp receive buffer, gets copied into data. */
unsigned char recv[I2C_SMBUS_BLOCK_MAX];
struct i2c_client *client;
ssif_i2c_done done_handler;
/* Thread interface handling */
struct task_struct *thread;
struct completion wake_thread;
bool stopping;
int i2c_read_write;
int i2c_command;
unsigned char *i2c_data;
unsigned int i2c_size;
/* From the device id response. */
struct ipmi_device_id device_id;
struct timer_list retry_timer;
int retries_left;
/* Info from SSIF cmd */
unsigned char max_xmit_msg_size;
unsigned char max_recv_msg_size;
unsigned int multi_support;
int supports_pec;
#define SSIF_NO_MULTI 0
#define SSIF_MULTI_2_PART 1
#define SSIF_MULTI_n_PART 2
unsigned char *multi_data;
unsigned int multi_len;
unsigned int multi_pos;
atomic_t stats[SSIF_NUM_STATS];
};
#define ssif_inc_stat(ssif, stat) \
atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
#define ssif_get_stat(ssif, stat) \
((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
static bool initialized;
static atomic_t next_intf = ATOMIC_INIT(0);
static void return_hosed_msg(struct ssif_info *ssif_info,
struct ipmi_smi_msg *msg);
static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
static int start_send(struct ssif_info *ssif_info,
unsigned char *data,
unsigned int len);
static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
unsigned long *flags)
{
spin_lock_irqsave(&ssif_info->lock, *flags);
return flags;
}
static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
unsigned long *flags)
{
spin_unlock_irqrestore(&ssif_info->lock, *flags);
}
static void deliver_recv_msg(struct ssif_info *ssif_info,
struct ipmi_smi_msg *msg)
{
ipmi_smi_t intf = ssif_info->intf;
if (!intf) {
ipmi_free_smi_msg(msg);
} else if (msg->rsp_size < 0) {
return_hosed_msg(ssif_info, msg);
pr_err(PFX
"Malformed message in deliver_recv_msg: rsp_size = %d\n",
msg->rsp_size);
} else {
ipmi_smi_msg_received(intf, msg);
}
}
static void return_hosed_msg(struct ssif_info *ssif_info,
struct ipmi