Path: blob/master/drivers/char/ipmi/ipmi_si_intf.c
15112 views
/*1* ipmi_si.c2*3* The interface to the IPMI driver for the system interfaces (KCS, SMIC,4* BT).5*6* Author: MontaVista Software, Inc.7* Corey Minyard <[email protected]>8* [email protected]9*10* Copyright 2002 MontaVista Software Inc.11* Copyright 2006 IBM Corp., Christian Krafft <[email protected]>12*13* This program is free software; you can redistribute it and/or modify it14* under the terms of the GNU General Public License as published by the15* Free Software Foundation; either version 2 of the License, or (at your16* option) any later version.17*18*19* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED20* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF21* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.22* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,23* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,24* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS25* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND26* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR27* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE28* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*30* You should have received a copy of the GNU General Public License along31* with this program; if not, write to the Free Software Foundation, Inc.,32* 675 Mass Ave, Cambridge, MA 02139, USA.33*/3435/*36* This file holds the "policy" for the interface to the SMI state37* machine. It does the configuration, handles timers and interrupts,38* and drives the real SMI state machine.39*/4041#include <linux/module.h>42#include <linux/moduleparam.h>43#include <asm/system.h>44#include <linux/sched.h>45#include <linux/seq_file.h>46#include <linux/timer.h>47#include <linux/errno.h>48#include <linux/spinlock.h>49#include <linux/slab.h>50#include <linux/delay.h>51#include <linux/list.h>52#include <linux/pci.h>53#include <linux/ioport.h>54#include <linux/notifier.h>55#include <linux/mutex.h>56#include <linux/kthread.h>57#include <asm/irq.h>58#include <linux/interrupt.h>59#include <linux/rcupdate.h>60#include <linux/ipmi.h>61#include <linux/ipmi_smi.h>62#include <asm/io.h>63#include "ipmi_si_sm.h"64#include <linux/init.h>65#include <linux/dmi.h>66#include <linux/string.h>67#include <linux/ctype.h>68#include <linux/pnp.h>69#include <linux/of_device.h>70#include <linux/of_platform.h>71#include <linux/of_address.h>72#include <linux/of_irq.h>7374#define PFX "ipmi_si: "7576/* Measure times between events in the driver. */77#undef DEBUG_TIMING7879/* Call every 10 ms. */80#define SI_TIMEOUT_TIME_USEC 1000081#define SI_USEC_PER_JIFFY (1000000/HZ)82#define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)83#define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a84short timeout */8586enum si_intf_state {87SI_NORMAL,88SI_GETTING_FLAGS,89SI_GETTING_EVENTS,90SI_CLEARING_FLAGS,91SI_CLEARING_FLAGS_THEN_SET_IRQ,92SI_GETTING_MESSAGES,93SI_ENABLE_INTERRUPTS1,94SI_ENABLE_INTERRUPTS2,95SI_DISABLE_INTERRUPTS1,96SI_DISABLE_INTERRUPTS297/* FIXME - add watchdog stuff. */98};99100/* Some BT-specific defines we need here. */101#define IPMI_BT_INTMASK_REG 2102#define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2103#define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1104105enum si_type {106SI_KCS, SI_SMIC, SI_BT107};108static char *si_to_str[] = { "kcs", "smic", "bt" };109110static char *ipmi_addr_src_to_str[] = { NULL, "hotmod", "hardcoded", "SPMI",111"ACPI", "SMBIOS", "PCI",112"device-tree", "default" };113114#define DEVICE_NAME "ipmi_si"115116static struct platform_driver ipmi_driver;117118/*119* Indexes into stats[] in smi_info below.120*/121enum si_stat_indexes {122/*123* Number of times the driver requested a timer while an operation124* was in progress.125*/126SI_STAT_short_timeouts = 0,127128/*129* Number of times the driver requested a timer while nothing was in130* progress.131*/132SI_STAT_long_timeouts,133134/* Number of times the interface was idle while being polled. */135SI_STAT_idles,136137/* Number of interrupts the driver handled. */138SI_STAT_interrupts,139140/* Number of time the driver got an ATTN from the hardware. */141SI_STAT_attentions,142143/* Number of times the driver requested flags from the hardware. */144SI_STAT_flag_fetches,145146/* Number of times the hardware didn't follow the state machine. */147SI_STAT_hosed_count,148149/* Number of completed messages. */150SI_STAT_complete_transactions,151152/* Number of IPMI events received from the hardware. */153SI_STAT_events,154155/* Number of watchdog pretimeouts. */156SI_STAT_watchdog_pretimeouts,157158/* Number of asyncronous messages received. */159SI_STAT_incoming_messages,160161162/* This *must* remain last, add new values above this. */163SI_NUM_STATS164};165166struct smi_info {167int intf_num;168ipmi_smi_t intf;169struct si_sm_data *si_sm;170struct si_sm_handlers *handlers;171enum si_type si_type;172spinlock_t si_lock;173spinlock_t msg_lock;174struct list_head xmit_msgs;175struct list_head hp_xmit_msgs;176struct ipmi_smi_msg *curr_msg;177enum si_intf_state si_state;178179/*180* Used to handle the various types of I/O that can occur with181* IPMI182*/183struct si_sm_io io;184int (*io_setup)(struct smi_info *info);185void (*io_cleanup)(struct smi_info *info);186int (*irq_setup)(struct smi_info *info);187void (*irq_cleanup)(struct smi_info *info);188unsigned int io_size;189enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */190void (*addr_source_cleanup)(struct smi_info *info);191void *addr_source_data;192193/*194* Per-OEM handler, called from handle_flags(). Returns 1195* when handle_flags() needs to be re-run or 0 indicating it196* set si_state itself.197*/198int (*oem_data_avail_handler)(struct smi_info *smi_info);199200/*201* Flags from the last GET_MSG_FLAGS command, used when an ATTN202* is set to hold the flags until we are done handling everything203* from the flags.204*/205#define RECEIVE_MSG_AVAIL 0x01206#define EVENT_MSG_BUFFER_FULL 0x02207#define WDT_PRE_TIMEOUT_INT 0x08208#define OEM0_DATA_AVAIL 0x20209#define OEM1_DATA_AVAIL 0x40210#define OEM2_DATA_AVAIL 0x80211#define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \212OEM1_DATA_AVAIL | \213OEM2_DATA_AVAIL)214unsigned char msg_flags;215216/* Does the BMC have an event buffer? */217char has_event_buffer;218219/*220* If set to true, this will request events the next time the221* state machine is idle.222*/223atomic_t req_events;224225/*226* If true, run the state machine to completion on every send227* call. Generally used after a panic to make sure stuff goes228* out.229*/230int run_to_completion;231232/* The I/O port of an SI interface. */233int port;234235/*236* The space between start addresses of the two ports. For237* instance, if the first port is 0xca2 and the spacing is 4, then238* the second port is 0xca6.239*/240unsigned int spacing;241242/* zero if no irq; */243int irq;244245/* The timer for this si. */246struct timer_list si_timer;247248/* The time (in jiffies) the last timeout occurred at. */249unsigned long last_timeout_jiffies;250251/* Used to gracefully stop the timer without race conditions. */252atomic_t stop_operation;253254/*255* The driver will disable interrupts when it gets into a256* situation where it cannot handle messages due to lack of257* memory. Once that situation clears up, it will re-enable258* interrupts.259*/260int interrupt_disabled;261262/* From the get device id response... */263struct ipmi_device_id device_id;264265/* Driver model stuff. */266struct device *dev;267struct platform_device *pdev;268269/*270* True if we allocated the device, false if it came from271* someplace else (like PCI).272*/273int dev_registered;274275/* Slave address, could be reported from DMI. */276unsigned char slave_addr;277278/* Counters and things for the proc filesystem. */279atomic_t stats[SI_NUM_STATS];280281struct task_struct *thread;282283struct list_head link;284union ipmi_smi_info_union addr_info;285};286287#define smi_inc_stat(smi, stat) \288atomic_inc(&(smi)->stats[SI_STAT_ ## stat])289#define smi_get_stat(smi, stat) \290((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))291292#define SI_MAX_PARMS 4293294static int force_kipmid[SI_MAX_PARMS];295static int num_force_kipmid;296#ifdef CONFIG_PCI297static int pci_registered;298#endif299#ifdef CONFIG_ACPI300static int pnp_registered;301#endif302303static unsigned int kipmid_max_busy_us[SI_MAX_PARMS];304static int num_max_busy_us;305306static int unload_when_empty = 1;307308static int add_smi(struct smi_info *smi);309static int try_smi_init(struct smi_info *smi);310static void cleanup_one_si(struct smi_info *to_clean);311static void cleanup_ipmi_si(void);312313static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);314static int register_xaction_notifier(struct notifier_block *nb)315{316return atomic_notifier_chain_register(&xaction_notifier_list, nb);317}318319static void deliver_recv_msg(struct smi_info *smi_info,320struct ipmi_smi_msg *msg)321{322/* Deliver the message to the upper layer with the lock323released. */324325if (smi_info->run_to_completion) {326ipmi_smi_msg_received(smi_info->intf, msg);327} else {328spin_unlock(&(smi_info->si_lock));329ipmi_smi_msg_received(smi_info->intf, msg);330spin_lock(&(smi_info->si_lock));331}332}333334static void return_hosed_msg(struct smi_info *smi_info, int cCode)335{336struct ipmi_smi_msg *msg = smi_info->curr_msg;337338if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)339cCode = IPMI_ERR_UNSPECIFIED;340/* else use it as is */341342/* Make it a response */343msg->rsp[0] = msg->data[0] | 4;344msg->rsp[1] = msg->data[1];345msg->rsp[2] = cCode;346msg->rsp_size = 3;347348smi_info->curr_msg = NULL;349deliver_recv_msg(smi_info, msg);350}351352static enum si_sm_result start_next_msg(struct smi_info *smi_info)353{354int rv;355struct list_head *entry = NULL;356#ifdef DEBUG_TIMING357struct timeval t;358#endif359360/*361* No need to save flags, we aleady have interrupts off and we362* already hold the SMI lock.363*/364if (!smi_info->run_to_completion)365spin_lock(&(smi_info->msg_lock));366367/* Pick the high priority queue first. */368if (!list_empty(&(smi_info->hp_xmit_msgs))) {369entry = smi_info->hp_xmit_msgs.next;370} else if (!list_empty(&(smi_info->xmit_msgs))) {371entry = smi_info->xmit_msgs.next;372}373374if (!entry) {375smi_info->curr_msg = NULL;376rv = SI_SM_IDLE;377} else {378int err;379380list_del(entry);381smi_info->curr_msg = list_entry(entry,382struct ipmi_smi_msg,383link);384#ifdef DEBUG_TIMING385do_gettimeofday(&t);386printk(KERN_DEBUG "**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);387#endif388err = atomic_notifier_call_chain(&xaction_notifier_list,3890, smi_info);390if (err & NOTIFY_STOP_MASK) {391rv = SI_SM_CALL_WITHOUT_DELAY;392goto out;393}394err = smi_info->handlers->start_transaction(395smi_info->si_sm,396smi_info->curr_msg->data,397smi_info->curr_msg->data_size);398if (err)399return_hosed_msg(smi_info, err);400401rv = SI_SM_CALL_WITHOUT_DELAY;402}403out:404if (!smi_info->run_to_completion)405spin_unlock(&(smi_info->msg_lock));406407return rv;408}409410static void start_enable_irq(struct smi_info *smi_info)411{412unsigned char msg[2];413414/*415* If we are enabling interrupts, we have to tell the416* BMC to use them.417*/418msg[0] = (IPMI_NETFN_APP_REQUEST << 2);419msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;420421smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);422smi_info->si_state = SI_ENABLE_INTERRUPTS1;423}424425static void start_disable_irq(struct smi_info *smi_info)426{427unsigned char msg[2];428429msg[0] = (IPMI_NETFN_APP_REQUEST << 2);430msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;431432smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);433smi_info->si_state = SI_DISABLE_INTERRUPTS1;434}435436static void start_clear_flags(struct smi_info *smi_info)437{438unsigned char msg[3];439440/* Make sure the watchdog pre-timeout flag is not set at startup. */441msg[0] = (IPMI_NETFN_APP_REQUEST << 2);442msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;443msg[2] = WDT_PRE_TIMEOUT_INT;444445smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);446smi_info->si_state = SI_CLEARING_FLAGS;447}448449/*450* When we have a situtaion where we run out of memory and cannot451* allocate messages, we just leave them in the BMC and run the system452* polled until we can allocate some memory. Once we have some453* memory, we will re-enable the interrupt.454*/455static inline void disable_si_irq(struct smi_info *smi_info)456{457if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {458start_disable_irq(smi_info);459smi_info->interrupt_disabled = 1;460if (!atomic_read(&smi_info->stop_operation))461mod_timer(&smi_info->si_timer,462jiffies + SI_TIMEOUT_JIFFIES);463}464}465466static inline void enable_si_irq(struct smi_info *smi_info)467{468if ((smi_info->irq) && (smi_info->interrupt_disabled)) {469start_enable_irq(smi_info);470smi_info->interrupt_disabled = 0;471}472}473474static void handle_flags(struct smi_info *smi_info)475{476retry:477if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {478/* Watchdog pre-timeout */479smi_inc_stat(smi_info, watchdog_pretimeouts);480481start_clear_flags(smi_info);482smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;483spin_unlock(&(smi_info->si_lock));484ipmi_smi_watchdog_pretimeout(smi_info->intf);485spin_lock(&(smi_info->si_lock));486} else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {487/* Messages available. */488smi_info->curr_msg = ipmi_alloc_smi_msg();489if (!smi_info->curr_msg) {490disable_si_irq(smi_info);491smi_info->si_state = SI_NORMAL;492return;493}494enable_si_irq(smi_info);495496smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);497smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;498smi_info->curr_msg->data_size = 2;499500smi_info->handlers->start_transaction(501smi_info->si_sm,502smi_info->curr_msg->data,503smi_info->curr_msg->data_size);504smi_info->si_state = SI_GETTING_MESSAGES;505} else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {506/* Events available. */507smi_info->curr_msg = ipmi_alloc_smi_msg();508if (!smi_info->curr_msg) {509disable_si_irq(smi_info);510smi_info->si_state = SI_NORMAL;511return;512}513enable_si_irq(smi_info);514515smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);516smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;517smi_info->curr_msg->data_size = 2;518519smi_info->handlers->start_transaction(520smi_info->si_sm,521smi_info->curr_msg->data,522smi_info->curr_msg->data_size);523smi_info->si_state = SI_GETTING_EVENTS;524} else if (smi_info->msg_flags & OEM_DATA_AVAIL &&525smi_info->oem_data_avail_handler) {526if (smi_info->oem_data_avail_handler(smi_info))527goto retry;528} else529smi_info->si_state = SI_NORMAL;530}531532static void handle_transaction_done(struct smi_info *smi_info)533{534struct ipmi_smi_msg *msg;535#ifdef DEBUG_TIMING536struct timeval t;537538do_gettimeofday(&t);539printk(KERN_DEBUG "**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);540#endif541switch (smi_info->si_state) {542case SI_NORMAL:543if (!smi_info->curr_msg)544break;545546smi_info->curr_msg->rsp_size547= smi_info->handlers->get_result(548smi_info->si_sm,549smi_info->curr_msg->rsp,550IPMI_MAX_MSG_LENGTH);551552/*553* Do this here becase deliver_recv_msg() releases the554* lock, and a new message can be put in during the555* time the lock is released.556*/557msg = smi_info->curr_msg;558smi_info->curr_msg = NULL;559deliver_recv_msg(smi_info, msg);560break;561562case SI_GETTING_FLAGS:563{564unsigned char msg[4];565unsigned int len;566567/* We got the flags from the SMI, now handle them. */568len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);569if (msg[2] != 0) {570/* Error fetching flags, just give up for now. */571smi_info->si_state = SI_NORMAL;572} else if (len < 4) {573/*574* Hmm, no flags. That's technically illegal, but575* don't use uninitialized data.576*/577smi_info->si_state = SI_NORMAL;578} else {579smi_info->msg_flags = msg[3];580handle_flags(smi_info);581}582break;583}584585case SI_CLEARING_FLAGS:586case SI_CLEARING_FLAGS_THEN_SET_IRQ:587{588unsigned char msg[3];589590/* We cleared the flags. */591smi_info->handlers->get_result(smi_info->si_sm, msg, 3);592if (msg[2] != 0) {593/* Error clearing flags */594dev_warn(smi_info->dev,595"Error clearing flags: %2.2x\n", msg[2]);596}597if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)598start_enable_irq(smi_info);599else600smi_info->si_state = SI_NORMAL;601break;602}603604case SI_GETTING_EVENTS:605{606smi_info->curr_msg->rsp_size607= smi_info->handlers->get_result(608smi_info->si_sm,609smi_info->curr_msg->rsp,610IPMI_MAX_MSG_LENGTH);611612/*613* Do this here becase deliver_recv_msg() releases the614* lock, and a new message can be put in during the615* time the lock is released.616*/617msg = smi_info->curr_msg;618smi_info->curr_msg = NULL;619if (msg->rsp[2] != 0) {620/* Error getting event, probably done. */621msg->done(msg);622623/* Take off the event flag. */624smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;625handle_flags(smi_info);626} else {627smi_inc_stat(smi_info, events);628629/*630* Do this before we deliver the message631* because delivering the message releases the632* lock and something else can mess with the633* state.634*/635handle_flags(smi_info);636637deliver_recv_msg(smi_info, msg);638}639break;640}641642case SI_GETTING_MESSAGES:643{644smi_info->curr_msg->rsp_size645= smi_info->handlers->get_result(646smi_info->si_sm,647smi_info->curr_msg->rsp,648IPMI_MAX_MSG_LENGTH);649650/*651* Do this here becase deliver_recv_msg() releases the652* lock, and a new message can be put in during the653* time the lock is released.654*/655msg = smi_info->curr_msg;656smi_info->curr_msg = NULL;657if (msg->rsp[2] != 0) {658/* Error getting event, probably done. */659msg->done(msg);660661/* Take off the msg flag. */662smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;663handle_flags(smi_info);664} else {665smi_inc_stat(smi_info, incoming_messages);666667/*668* Do this before we deliver the message669* because delivering the message releases the670* lock and something else can mess with the671* state.672*/673handle_flags(smi_info);674675deliver_recv_msg(smi_info, msg);676}677break;678}679680case SI_ENABLE_INTERRUPTS1:681{682unsigned char msg[4];683684/* We got the flags from the SMI, now handle them. */685smi_info->handlers->get_result(smi_info->si_sm, msg, 4);686if (msg[2] != 0) {687dev_warn(smi_info->dev, "Could not enable interrupts"688", failed get, using polled mode.\n");689smi_info->si_state = SI_NORMAL;690} else {691msg[0] = (IPMI_NETFN_APP_REQUEST << 2);692msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;693msg[2] = (msg[3] |694IPMI_BMC_RCV_MSG_INTR |695IPMI_BMC_EVT_MSG_INTR);696smi_info->handlers->start_transaction(697smi_info->si_sm, msg, 3);698smi_info->si_state = SI_ENABLE_INTERRUPTS2;699}700break;701}702703case SI_ENABLE_INTERRUPTS2:704{705unsigned char msg[4];706707/* We got the flags from the SMI, now handle them. */708smi_info->handlers->get_result(smi_info->si_sm, msg, 4);709if (msg[2] != 0)710dev_warn(smi_info->dev, "Could not enable interrupts"711", failed set, using polled mode.\n");712else713smi_info->interrupt_disabled = 0;714smi_info->si_state = SI_NORMAL;715break;716}717718case SI_DISABLE_INTERRUPTS1:719{720unsigned char msg[4];721722/* We got the flags from the SMI, now handle them. */723smi_info->handlers->get_result(smi_info->si_sm, msg, 4);724if (msg[2] != 0) {725dev_warn(smi_info->dev, "Could not disable interrupts"726", failed get.\n");727smi_info->si_state = SI_NORMAL;728} else {729msg[0] = (IPMI_NETFN_APP_REQUEST << 2);730msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;731msg[2] = (msg[3] &732~(IPMI_BMC_RCV_MSG_INTR |733IPMI_BMC_EVT_MSG_INTR));734smi_info->handlers->start_transaction(735smi_info->si_sm, msg, 3);736smi_info->si_state = SI_DISABLE_INTERRUPTS2;737}738break;739}740741case SI_DISABLE_INTERRUPTS2:742{743unsigned char msg[4];744745/* We got the flags from the SMI, now handle them. */746smi_info->handlers->get_result(smi_info->si_sm, msg, 4);747if (msg[2] != 0) {748dev_warn(smi_info->dev, "Could not disable interrupts"749", failed set.\n");750}751smi_info->si_state = SI_NORMAL;752break;753}754}755}756757/*758* Called on timeouts and events. Timeouts should pass the elapsed759* time, interrupts should pass in zero. Must be called with760* si_lock held and interrupts disabled.761*/762static enum si_sm_result smi_event_handler(struct smi_info *smi_info,763int time)764{765enum si_sm_result si_sm_result;766767restart:768/*769* There used to be a loop here that waited a little while770* (around 25us) before giving up. That turned out to be771* pointless, the minimum delays I was seeing were in the 300us772* range, which is far too long to wait in an interrupt. So773* we just run until the state machine tells us something774* happened or it needs a delay.775*/776si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);777time = 0;778while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)779si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);780781if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {782smi_inc_stat(smi_info, complete_transactions);783784handle_transaction_done(smi_info);785si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);786} else if (si_sm_result == SI_SM_HOSED) {787smi_inc_stat(smi_info, hosed_count);788789/*790* Do the before return_hosed_msg, because that791* releases the lock.792*/793smi_info->si_state = SI_NORMAL;794if (smi_info->curr_msg != NULL) {795/*796* If we were handling a user message, format797* a response to send to the upper layer to798* tell it about the error.799*/800return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);801}802si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);803}804805/*806* We prefer handling attn over new messages. But don't do807* this if there is not yet an upper layer to handle anything.808*/809if (likely(smi_info->intf) && si_sm_result == SI_SM_ATTN) {810unsigned char msg[2];811812smi_inc_stat(smi_info, attentions);813814/*815* Got a attn, send down a get message flags to see816* what's causing it. It would be better to handle817* this in the upper layer, but due to the way818* interrupts work with the SMI, that's not really819* possible.820*/821msg[0] = (IPMI_NETFN_APP_REQUEST << 2);822msg[1] = IPMI_GET_MSG_FLAGS_CMD;823824smi_info->handlers->start_transaction(825smi_info->si_sm, msg, 2);826smi_info->si_state = SI_GETTING_FLAGS;827goto restart;828}829830/* If we are currently idle, try to start the next message. */831if (si_sm_result == SI_SM_IDLE) {832smi_inc_stat(smi_info, idles);833834si_sm_result = start_next_msg(smi_info);835if (si_sm_result != SI_SM_IDLE)836goto restart;837}838839if ((si_sm_result == SI_SM_IDLE)840&& (atomic_read(&smi_info->req_events))) {841/*842* We are idle and the upper layer requested that I fetch843* events, so do so.844*/845atomic_set(&smi_info->req_events, 0);846847smi_info->curr_msg = ipmi_alloc_smi_msg();848if (!smi_info->curr_msg)849goto out;850851smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);852smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;853smi_info->curr_msg->data_size = 2;854855smi_info->handlers->start_transaction(856smi_info->si_sm,857smi_info->curr_msg->data,858smi_info->curr_msg->data_size);859smi_info->si_state = SI_GETTING_EVENTS;860goto restart;861}862out:863return si_sm_result;864}865866static void sender(void *send_info,867struct ipmi_smi_msg *msg,868int priority)869{870struct smi_info *smi_info = send_info;871enum si_sm_result result;872unsigned long flags;873#ifdef DEBUG_TIMING874struct timeval t;875#endif876877if (atomic_read(&smi_info->stop_operation)) {878msg->rsp[0] = msg->data[0] | 4;879msg->rsp[1] = msg->data[1];880msg->rsp[2] = IPMI_ERR_UNSPECIFIED;881msg->rsp_size = 3;882deliver_recv_msg(smi_info, msg);883return;884}885886#ifdef DEBUG_TIMING887do_gettimeofday(&t);888printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);889#endif890891/*892* last_timeout_jiffies is updated here to avoid893* smi_timeout() handler passing very large time_diff894* value to smi_event_handler() that causes895* the send command to abort.896*/897smi_info->last_timeout_jiffies = jiffies;898899mod_timer(&smi_info->si_timer, jiffies + SI_TIMEOUT_JIFFIES);900901if (smi_info->thread)902wake_up_process(smi_info->thread);903904if (smi_info->run_to_completion) {905/*906* If we are running to completion, then throw it in907* the list and run transactions until everything is908* clear. Priority doesn't matter here.909*/910911/*912* Run to completion means we are single-threaded, no913* need for locks.914*/915list_add_tail(&(msg->link), &(smi_info->xmit_msgs));916917result = smi_event_handler(smi_info, 0);918while (result != SI_SM_IDLE) {919udelay(SI_SHORT_TIMEOUT_USEC);920result = smi_event_handler(smi_info,921SI_SHORT_TIMEOUT_USEC);922}923return;924}925926spin_lock_irqsave(&smi_info->msg_lock, flags);927if (priority > 0)928list_add_tail(&msg->link, &smi_info->hp_xmit_msgs);929else930list_add_tail(&msg->link, &smi_info->xmit_msgs);931spin_unlock_irqrestore(&smi_info->msg_lock, flags);932933spin_lock_irqsave(&smi_info->si_lock, flags);934if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL)935start_next_msg(smi_info);936spin_unlock_irqrestore(&smi_info->si_lock, flags);937}938939static void set_run_to_completion(void *send_info, int i_run_to_completion)940{941struct smi_info *smi_info = send_info;942enum si_sm_result result;943944smi_info->run_to_completion = i_run_to_completion;945if (i_run_to_completion) {946result = smi_event_handler(smi_info, 0);947while (result != SI_SM_IDLE) {948udelay(SI_SHORT_TIMEOUT_USEC);949result = smi_event_handler(smi_info,950SI_SHORT_TIMEOUT_USEC);951}952}953}954955/*956* Use -1 in the nsec value of the busy waiting timespec to tell that957* we are spinning in kipmid looking for something and not delaying958* between checks959*/960static inline void ipmi_si_set_not_busy(struct timespec *ts)961{962ts->tv_nsec = -1;963}964static inline int ipmi_si_is_busy(struct timespec *ts)965{966return ts->tv_nsec != -1;967}968969static int ipmi_thread_busy_wait(enum si_sm_result smi_result,970const struct smi_info *smi_info,971struct timespec *busy_until)972{973unsigned int max_busy_us = 0;974975if (smi_info->intf_num < num_max_busy_us)976max_busy_us = kipmid_max_busy_us[smi_info->intf_num];977if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)978ipmi_si_set_not_busy(busy_until);979else if (!ipmi_si_is_busy(busy_until)) {980getnstimeofday(busy_until);981timespec_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);982} else {983struct timespec now;984getnstimeofday(&now);985if (unlikely(timespec_compare(&now, busy_until) > 0)) {986ipmi_si_set_not_busy(busy_until);987return 0;988}989}990return 1;991}992993994/*995* A busy-waiting loop for speeding up IPMI operation.996*997* Lousy hardware makes this hard. This is only enabled for systems998* that are not BT and do not have interrupts. It starts spinning999* when an operation is complete or until max_busy tells it to stop1000* (if that is enabled). See the paragraph on kimid_max_busy_us in1001* Documentation/IPMI.txt for details.1002*/1003static int ipmi_thread(void *data)1004{1005struct smi_info *smi_info = data;1006unsigned long flags;1007enum si_sm_result smi_result;1008struct timespec busy_until;10091010ipmi_si_set_not_busy(&busy_until);1011set_user_nice(current, 19);1012while (!kthread_should_stop()) {1013int busy_wait;10141015spin_lock_irqsave(&(smi_info->si_lock), flags);1016smi_result = smi_event_handler(smi_info, 0);1017spin_unlock_irqrestore(&(smi_info->si_lock), flags);1018busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,1019&busy_until);1020if (smi_result == SI_SM_CALL_WITHOUT_DELAY)1021; /* do nothing */1022else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)1023schedule();1024else if (smi_result == SI_SM_IDLE)1025schedule_timeout_interruptible(100);1026else1027schedule_timeout_interruptible(1);1028}1029return 0;1030}103110321033static void poll(void *send_info)1034{1035struct smi_info *smi_info = send_info;1036unsigned long flags;10371038/*1039* Make sure there is some delay in the poll loop so we can1040* drive time forward and timeout things.1041*/1042udelay(10);1043spin_lock_irqsave(&smi_info->si_lock, flags);1044smi_event_handler(smi_info, 10);1045spin_unlock_irqrestore(&smi_info->si_lock, flags);1046}10471048static void request_events(void *send_info)1049{1050struct smi_info *smi_info = send_info;10511052if (atomic_read(&smi_info->stop_operation) ||1053!smi_info->has_event_buffer)1054return;10551056atomic_set(&smi_info->req_events, 1);1057}10581059static int initialized;10601061static void smi_timeout(unsigned long data)1062{1063struct smi_info *smi_info = (struct smi_info *) data;1064enum si_sm_result smi_result;1065unsigned long flags;1066unsigned long jiffies_now;1067long time_diff;1068long timeout;1069#ifdef DEBUG_TIMING1070struct timeval t;1071#endif10721073spin_lock_irqsave(&(smi_info->si_lock), flags);1074#ifdef DEBUG_TIMING1075do_gettimeofday(&t);1076printk(KERN_DEBUG "**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);1077#endif1078jiffies_now = jiffies;1079time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)1080* SI_USEC_PER_JIFFY);1081smi_result = smi_event_handler(smi_info, time_diff);10821083spin_unlock_irqrestore(&(smi_info->si_lock), flags);10841085smi_info->last_timeout_jiffies = jiffies_now;10861087if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {1088/* Running with interrupts, only do long timeouts. */1089timeout = jiffies + SI_TIMEOUT_JIFFIES;1090smi_inc_stat(smi_info, long_timeouts);1091goto do_mod_timer;1092}10931094/*1095* If the state machine asks for a short delay, then shorten1096* the timer timeout.1097*/1098if (smi_result == SI_SM_CALL_WITH_DELAY) {1099smi_inc_stat(smi_info, short_timeouts);1100timeout = jiffies + 1;1101} else {1102smi_inc_stat(smi_info, long_timeouts);1103timeout = jiffies + SI_TIMEOUT_JIFFIES;1104}11051106do_mod_timer:1107if (smi_result != SI_SM_IDLE)1108mod_timer(&(smi_info->si_timer), timeout);1109}11101111static irqreturn_t si_irq_handler(int irq, void *data)1112{1113struct smi_info *smi_info = data;1114unsigned long flags;1115#ifdef DEBUG_TIMING1116struct timeval t;1117#endif11181119spin_lock_irqsave(&(smi_info->si_lock), flags);11201121smi_inc_stat(smi_info, interrupts);11221123#ifdef DEBUG_TIMING1124do_gettimeofday(&t);1125printk(KERN_DEBUG "**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);1126#endif1127smi_event_handler(smi_info, 0);1128spin_unlock_irqrestore(&(smi_info->si_lock), flags);1129return IRQ_HANDLED;1130}11311132static irqreturn_t si_bt_irq_handler(int irq, void *data)1133{1134struct smi_info *smi_info = data;1135/* We need to clear the IRQ flag for the BT interface. */1136smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,1137IPMI_BT_INTMASK_CLEAR_IRQ_BIT1138| IPMI_BT_INTMASK_ENABLE_IRQ_BIT);1139return si_irq_handler(irq, data);1140}11411142static int smi_start_processing(void *send_info,1143ipmi_smi_t intf)1144{1145struct smi_info *new_smi = send_info;1146int enable = 0;11471148new_smi->intf = intf;11491150/* Try to claim any interrupts. */1151if (new_smi->irq_setup)1152new_smi->irq_setup(new_smi);11531154/* Set up the timer that drives the interface. */1155setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);1156new_smi->last_timeout_jiffies = jiffies;1157mod_timer(&new_smi->si_timer, jiffies + SI_TIMEOUT_JIFFIES);11581159/*1160* Check if the user forcefully enabled the daemon.1161*/1162if (new_smi->intf_num < num_force_kipmid)1163enable = force_kipmid[new_smi->intf_num];1164/*1165* The BT interface is efficient enough to not need a thread,1166* and there is no need for a thread if we have interrupts.1167*/1168else if ((new_smi->si_type != SI_BT) && (!new_smi->irq))1169enable = 1;11701171if (enable) {1172new_smi->thread = kthread_run(ipmi_thread, new_smi,1173"kipmi%d", new_smi->intf_num);1174if (IS_ERR(new_smi->thread)) {1175dev_notice(new_smi->dev, "Could not start"1176" kernel thread due to error %ld, only using"1177" timers to drive the interface\n",1178PTR_ERR(new_smi->thread));1179new_smi->thread = NULL;1180}1181}11821183return 0;1184}11851186static int get_smi_info(void *send_info, struct ipmi_smi_info *data)1187{1188struct smi_info *smi = send_info;11891190data->addr_src = smi->addr_source;1191data->dev = smi->dev;1192data->addr_info = smi->addr_info;1193get_device(smi->dev);11941195return 0;1196}11971198static void set_maintenance_mode(void *send_info, int enable)1199{1200struct smi_info *smi_info = send_info;12011202if (!enable)1203atomic_set(&smi_info->req_events, 0);1204}12051206static struct ipmi_smi_handlers handlers = {1207.owner = THIS_MODULE,1208.start_processing = smi_start_processing,1209.get_smi_info = get_smi_info,1210.sender = sender,1211.request_events = request_events,1212.set_maintenance_mode = set_maintenance_mode,1213.set_run_to_completion = set_run_to_completion,1214.poll = poll,1215};12161217/*1218* There can be 4 IO ports passed in (with or without IRQs), 4 addresses,1219* a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS.1220*/12211222static LIST_HEAD(smi_infos);1223static DEFINE_MUTEX(smi_infos_lock);1224static int smi_num; /* Used to sequence the SMIs */12251226#define DEFAULT_REGSPACING 11227#define DEFAULT_REGSIZE 112281229static int si_trydefaults = 1;1230static char *si_type[SI_MAX_PARMS];1231#define MAX_SI_TYPE_STR 301232static char si_type_str[MAX_SI_TYPE_STR];1233static unsigned long addrs[SI_MAX_PARMS];1234static unsigned int num_addrs;1235static unsigned int ports[SI_MAX_PARMS];1236static unsigned int num_ports;1237static int irqs[SI_MAX_PARMS];1238static unsigned int num_irqs;1239static int regspacings[SI_MAX_PARMS];1240static unsigned int num_regspacings;1241static int regsizes[SI_MAX_PARMS];1242static unsigned int num_regsizes;1243static int regshifts[SI_MAX_PARMS];1244static unsigned int num_regshifts;1245static int slave_addrs[SI_MAX_PARMS]; /* Leaving 0 chooses the default value */1246static unsigned int num_slave_addrs;12471248#define IPMI_IO_ADDR_SPACE 01249#define IPMI_MEM_ADDR_SPACE 11250static char *addr_space_to_str[] = { "i/o", "mem" };12511252static int hotmod_handler(const char *val, struct kernel_param *kp);12531254module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);1255MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"1256" Documentation/IPMI.txt in the kernel sources for the"1257" gory details.");12581259module_param_named(trydefaults, si_trydefaults, bool, 0);1260MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"1261" default scan of the KCS and SMIC interface at the standard"1262" address");1263module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);1264MODULE_PARM_DESC(type, "Defines the type of each interface, each"1265" interface separated by commas. The types are 'kcs',"1266" 'smic', and 'bt'. For example si_type=kcs,bt will set"1267" the first interface to kcs and the second to bt");1268module_param_array(addrs, ulong, &num_addrs, 0);1269MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"1270" addresses separated by commas. Only use if an interface"1271" is in memory. Otherwise, set it to zero or leave"1272" it blank.");1273module_param_array(ports, uint, &num_ports, 0);1274MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"1275" addresses separated by commas. Only use if an interface"1276" is a port. Otherwise, set it to zero or leave"1277" it blank.");1278module_param_array(irqs, int, &num_irqs, 0);1279MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"1280" addresses separated by commas. Only use if an interface"1281" has an interrupt. Otherwise, set it to zero or leave"1282" it blank.");1283module_param_array(regspacings, int, &num_regspacings, 0);1284MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"1285" and each successive register used by the interface. For"1286" instance, if the start address is 0xca2 and the spacing"1287" is 2, then the second address is at 0xca4. Defaults"1288" to 1.");1289module_param_array(regsizes, int, &num_regsizes, 0);1290MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."1291" This should generally be 1, 2, 4, or 8 for an 8-bit,"1292" 16-bit, 32-bit, or 64-bit register. Use this if you"1293" the 8-bit IPMI register has to be read from a larger"1294" register.");1295module_param_array(regshifts, int, &num_regshifts, 0);1296MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."1297" IPMI register, in bits. For instance, if the data"1298" is read from a 32-bit word and the IPMI data is in"1299" bit 8-15, then the shift would be 8");1300module_param_array(slave_addrs, int, &num_slave_addrs, 0);1301MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"1302" the controller. Normally this is 0x20, but can be"1303" overridden by this parm. This is an array indexed"1304" by interface number.");1305module_param_array(force_kipmid, int, &num_force_kipmid, 0);1306MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"1307" disabled(0). Normally the IPMI driver auto-detects"1308" this, but the value may be overridden by this parm.");1309module_param(unload_when_empty, int, 0);1310MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"1311" specified or found, default is 1. Setting to 0"1312" is useful for hot add of devices using hotmod.");1313module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);1314MODULE_PARM_DESC(kipmid_max_busy_us,1315"Max time (in microseconds) to busy-wait for IPMI data before"1316" sleeping. 0 (default) means to wait forever. Set to 100-500"1317" if kipmid is using up a lot of CPU time.");131813191320static void std_irq_cleanup(struct smi_info *info)1321{1322if (info->si_type == SI_BT)1323/* Disable the interrupt in the BT interface. */1324info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);1325free_irq(info->irq, info);1326}13271328static int std_irq_setup(struct smi_info *info)1329{1330int rv;13311332if (!info->irq)1333return 0;13341335if (info->si_type == SI_BT) {1336rv = request_irq(info->irq,1337si_bt_irq_handler,1338IRQF_SHARED | IRQF_DISABLED,1339DEVICE_NAME,1340info);1341if (!rv)1342/* Enable the interrupt in the BT interface. */1343info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,1344IPMI_BT_INTMASK_ENABLE_IRQ_BIT);1345} else1346rv = request_irq(info->irq,1347si_irq_handler,1348IRQF_SHARED | IRQF_DISABLED,1349DEVICE_NAME,1350info);1351if (rv) {1352dev_warn(info->dev, "%s unable to claim interrupt %d,"1353" running polled\n",1354DEVICE_NAME, info->irq);1355info->irq = 0;1356} else {1357info->irq_cleanup = std_irq_cleanup;1358dev_info(info->dev, "Using irq %d\n", info->irq);1359}13601361return rv;1362}13631364static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)1365{1366unsigned int addr = io->addr_data;13671368return inb(addr + (offset * io->regspacing));1369}13701371static void port_outb(struct si_sm_io *io, unsigned int offset,1372unsigned char b)1373{1374unsigned int addr = io->addr_data;13751376outb(b, addr + (offset * io->regspacing));1377}13781379static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)1380{1381unsigned int addr = io->addr_data;13821383return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;1384}13851386static void port_outw(struct si_sm_io *io, unsigned int offset,1387unsigned char b)1388{1389unsigned int addr = io->addr_data;13901391outw(b << io->regshift, addr + (offset * io->regspacing));1392}13931394static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)1395{1396unsigned int addr = io->addr_data;13971398return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;1399}14001401static void port_outl(struct si_sm_io *io, unsigned int offset,1402unsigned char b)1403{1404unsigned int addr = io->addr_data;14051406outl(b << io->regshift, addr+(offset * io->regspacing));1407}14081409static void port_cleanup(struct smi_info *info)1410{1411unsigned int addr = info->io.addr_data;1412int idx;14131414if (addr) {1415for (idx = 0; idx < info->io_size; idx++)1416release_region(addr + idx * info->io.regspacing,1417info->io.regsize);1418}1419}14201421static int port_setup(struct smi_info *info)1422{1423unsigned int addr = info->io.addr_data;1424int idx;14251426if (!addr)1427return -ENODEV;14281429info->io_cleanup = port_cleanup;14301431/*1432* Figure out the actual inb/inw/inl/etc routine to use based1433* upon the register size.1434*/1435switch (info->io.regsize) {1436case 1:1437info->io.inputb = port_inb;1438info->io.outputb = port_outb;1439break;1440case 2:1441info->io.inputb = port_inw;1442info->io.outputb = port_outw;1443break;1444case 4:1445info->io.inputb = port_inl;1446info->io.outputb = port_outl;1447break;1448default:1449dev_warn(info->dev, "Invalid register size: %d\n",1450info->io.regsize);1451return -EINVAL;1452}14531454/*1455* Some BIOSes reserve disjoint I/O regions in their ACPI1456* tables. This causes problems when trying to register the1457* entire I/O region. Therefore we must register each I/O1458* port separately.1459*/1460for (idx = 0; idx < info->io_size; idx++) {1461if (request_region(addr + idx * info->io.regspacing,1462info->io.regsize, DEVICE_NAME) == NULL) {1463/* Undo allocations */1464while (idx--) {1465release_region(addr + idx * info->io.regspacing,1466info->io.regsize);1467}1468return -EIO;1469}1470}1471return 0;1472}14731474static unsigned char intf_mem_inb(struct si_sm_io *io, unsigned int offset)1475{1476return readb((io->addr)+(offset * io->regspacing));1477}14781479static void intf_mem_outb(struct si_sm_io *io, unsigned int offset,1480unsigned char b)1481{1482writeb(b, (io->addr)+(offset * io->regspacing));1483}14841485static unsigned char intf_mem_inw(struct si_sm_io *io, unsigned int offset)1486{1487return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)1488& 0xff;1489}14901491static void intf_mem_outw(struct si_sm_io *io, unsigned int offset,1492unsigned char b)1493{1494writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));1495}14961497static unsigned char intf_mem_inl(struct si_sm_io *io, unsigned int offset)1498{1499return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)1500& 0xff;1501}15021503static void intf_mem_outl(struct si_sm_io *io, unsigned int offset,1504unsigned char b)1505{1506writel(b << io->regshift, (io->addr)+(offset * io->regspacing));1507}15081509#ifdef readq1510static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)1511{1512return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)1513& 0xff;1514}15151516static void mem_outq(struct si_sm_io *io, unsigned int offset,1517unsigned char b)1518{1519writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));1520}1521#endif15221523static void mem_cleanup(struct smi_info *info)1524{1525unsigned long addr = info->io.addr_data;1526int mapsize;15271528if (info->io.addr) {1529iounmap(info->io.addr);15301531mapsize = ((info->io_size * info->io.regspacing)1532- (info->io.regspacing - info->io.regsize));15331534release_mem_region(addr, mapsize);1535}1536}15371538static int mem_setup(struct smi_info *info)1539{1540unsigned long addr = info->io.addr_data;1541int mapsize;15421543if (!addr)1544return -ENODEV;15451546info->io_cleanup = mem_cleanup;15471548/*1549* Figure out the actual readb/readw/readl/etc routine to use based1550* upon the register size.1551*/1552switch (info->io.regsize) {1553case 1:1554info->io.inputb = intf_mem_inb;1555info->io.outputb = intf_mem_outb;1556break;1557case 2:1558info->io.inputb = intf_mem_inw;1559info->io.outputb = intf_mem_outw;1560break;1561case 4:1562info->io.inputb = intf_mem_inl;1563info->io.outputb = intf_mem_outl;1564break;1565#ifdef readq1566case 8:1567info->io.inputb = mem_inq;1568info->io.outputb = mem_outq;1569break;1570#endif1571default:1572dev_warn(info->dev, "Invalid register size: %d\n",1573info->io.regsize);1574return -EINVAL;1575}15761577/*1578* Calculate the total amount of memory to claim. This is an1579* unusual looking calculation, but it avoids claiming any1580* more memory than it has to. It will claim everything1581* between the first address to the end of the last full1582* register.1583*/1584mapsize = ((info->io_size * info->io.regspacing)1585- (info->io.regspacing - info->io.regsize));15861587if (request_mem_region(addr, mapsize, DEVICE_NAME) == NULL)1588return -EIO;15891590info->io.addr = ioremap(addr, mapsize);1591if (info->io.addr == NULL) {1592release_mem_region(addr, mapsize);1593return -EIO;1594}1595return 0;1596}15971598/*1599* Parms come in as <op1>[:op2[:op3...]]. ops are:1600* add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]1601* Options are:1602* rsp=<regspacing>1603* rsi=<regsize>1604* rsh=<regshift>1605* irq=<irq>1606* ipmb=<ipmb addr>1607*/1608enum hotmod_op { HM_ADD, HM_REMOVE };1609struct hotmod_vals {1610char *name;1611int val;1612};1613static struct hotmod_vals hotmod_ops[] = {1614{ "add", HM_ADD },1615{ "remove", HM_REMOVE },1616{ NULL }1617};1618static struct hotmod_vals hotmod_si[] = {1619{ "kcs", SI_KCS },1620{ "smic", SI_SMIC },1621{ "bt", SI_BT },1622{ NULL }1623};1624static struct hotmod_vals hotmod_as[] = {1625{ "mem", IPMI_MEM_ADDR_SPACE },1626{ "i/o", IPMI_IO_ADDR_SPACE },1627{ NULL }1628};16291630static int parse_str(struct hotmod_vals *v, int *val, char *name, char **curr)1631{1632char *s;1633int i;16341635s = strchr(*curr, ',');1636if (!s) {1637printk(KERN_WARNING PFX "No hotmod %s given.\n", name);1638return -EINVAL;1639}1640*s = '\0';1641s++;1642for (i = 0; hotmod_ops[i].name; i++) {1643if (strcmp(*curr, v[i].name) == 0) {1644*val = v[i].val;1645*curr = s;1646return 0;1647}1648}16491650printk(KERN_WARNING PFX "Invalid hotmod %s '%s'\n", name, *curr);1651return -EINVAL;1652}16531654static int check_hotmod_int_op(const char *curr, const char *option,1655const char *name, int *val)1656{1657char *n;16581659if (strcmp(curr, name) == 0) {1660if (!option) {1661printk(KERN_WARNING PFX1662"No option given for '%s'\n",1663curr);1664return -EINVAL;1665}1666*val = simple_strtoul(option, &n, 0);1667if ((*n != '\0') || (*option == '\0')) {1668printk(KERN_WARNING PFX1669"Bad option given for '%s'\n",1670curr);1671return -EINVAL;1672}1673return 1;1674}1675return 0;1676}16771678static struct smi_info *smi_info_alloc(void)1679{1680struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);16811682if (info) {1683spin_lock_init(&info->si_lock);1684spin_lock_init(&info->msg_lock);1685}1686return info;1687}16881689static int hotmod_handler(const char *val, struct kernel_param *kp)1690{1691char *str = kstrdup(val, GFP_KERNEL);1692int rv;1693char *next, *curr, *s, *n, *o;1694enum hotmod_op op;1695enum si_type si_type;1696int addr_space;1697unsigned long addr;1698int regspacing;1699int regsize;1700int regshift;1701int irq;1702int ipmb;1703int ival;1704int len;1705struct smi_info *info;17061707if (!str)1708return -ENOMEM;17091710/* Kill any trailing spaces, as we can get a "\n" from echo. */1711len = strlen(str);1712ival = len - 1;1713while ((ival >= 0) && isspace(str[ival])) {1714str[ival] = '\0';1715ival--;1716}17171718for (curr = str; curr; curr = next) {1719regspacing = 1;1720regsize = 1;1721regshift = 0;1722irq = 0;1723ipmb = 0; /* Choose the default if not specified */17241725next = strchr(curr, ':');1726if (next) {1727*next = '\0';1728next++;1729}17301731rv = parse_str(hotmod_ops, &ival, "operation", &curr);1732if (rv)1733break;1734op = ival;17351736rv = parse_str(hotmod_si, &ival, "interface type", &curr);1737if (rv)1738break;1739si_type = ival;17401741rv = parse_str(hotmod_as, &addr_space, "address space", &curr);1742if (rv)1743break;17441745s = strchr(curr, ',');1746if (s) {1747*s = '\0';1748s++;1749}1750addr = simple_strtoul(curr, &n, 0);1751if ((*n != '\0') || (*curr == '\0')) {1752printk(KERN_WARNING PFX "Invalid hotmod address"1753" '%s'\n", curr);1754break;1755}17561757while (s) {1758curr = s;1759s = strchr(curr, ',');1760if (s) {1761*s = '\0';1762s++;1763}1764o = strchr(curr, '=');1765if (o) {1766*o = '\0';1767o++;1768}1769rv = check_hotmod_int_op(curr, o, "rsp", ®spacing);1770if (rv < 0)1771goto out;1772else if (rv)1773continue;1774rv = check_hotmod_int_op(curr, o, "rsi", ®size);1775if (rv < 0)1776goto out;1777else if (rv)1778continue;1779rv = check_hotmod_int_op(curr, o, "rsh", ®shift);1780if (rv < 0)1781goto out;1782else if (rv)1783continue;1784rv = check_hotmod_int_op(curr, o, "irq", &irq);1785if (rv < 0)1786goto out;1787else if (rv)1788continue;1789rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);1790if (rv < 0)1791goto out;1792else if (rv)1793continue;17941795rv = -EINVAL;1796printk(KERN_WARNING PFX1797"Invalid hotmod option '%s'\n",1798curr);1799goto out;1800}18011802if (op == HM_ADD) {1803info = smi_info_alloc();1804if (!info) {1805rv = -ENOMEM;1806goto out;1807}18081809info->addr_source = SI_HOTMOD;1810info->si_type = si_type;1811info->io.addr_data = addr;1812info->io.addr_type = addr_space;1813if (addr_space == IPMI_MEM_ADDR_SPACE)1814info->io_setup = mem_setup;1815else1816info->io_setup = port_setup;18171818info->io.addr = NULL;1819info->io.regspacing = regspacing;1820if (!info->io.regspacing)1821info->io.regspacing = DEFAULT_REGSPACING;1822info->io.regsize = regsize;1823if (!info->io.regsize)1824info->io.regsize = DEFAULT_REGSPACING;1825info->io.regshift = regshift;1826info->irq = irq;1827if (info->irq)1828info->irq_setup = std_irq_setup;1829info->slave_addr = ipmb;18301831if (!add_smi(info)) {1832if (try_smi_init(info))1833cleanup_one_si(info);1834} else {1835kfree(info);1836}1837} else {1838/* remove */1839struct smi_info *e, *tmp_e;18401841mutex_lock(&smi_infos_lock);1842list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {1843if (e->io.addr_type != addr_space)1844continue;1845if (e->si_type != si_type)1846continue;1847if (e->io.addr_data == addr)1848cleanup_one_si(e);1849}1850mutex_unlock(&smi_infos_lock);1851}1852}1853rv = len;1854out:1855kfree(str);1856return rv;1857}18581859static int __devinit hardcode_find_bmc(void)1860{1861int ret = -ENODEV;1862int i;1863struct smi_info *info;18641865for (i = 0; i < SI_MAX_PARMS; i++) {1866if (!ports[i] && !addrs[i])1867continue;18681869info = smi_info_alloc();1870if (!info)1871return -ENOMEM;18721873info->addr_source = SI_HARDCODED;1874printk(KERN_INFO PFX "probing via hardcoded address\n");18751876if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {1877info->si_type = SI_KCS;1878} else if (strcmp(si_type[i], "smic") == 0) {1879info->si_type = SI_SMIC;1880} else if (strcmp(si_type[i], "bt") == 0) {1881info->si_type = SI_BT;1882} else {1883printk(KERN_WARNING PFX "Interface type specified "1884"for interface %d, was invalid: %s\n",1885i, si_type[i]);1886kfree(info);1887continue;1888}18891890if (ports[i]) {1891/* An I/O port */1892info->io_setup = port_setup;1893info->io.addr_data = ports[i];1894info->io.addr_type = IPMI_IO_ADDR_SPACE;1895} else if (addrs[i]) {1896/* A memory port */1897info->io_setup = mem_setup;1898info->io.addr_data = addrs[i];1899info->io.addr_type = IPMI_MEM_ADDR_SPACE;1900} else {1901printk(KERN_WARNING PFX "Interface type specified "1902"for interface %d, but port and address were "1903"not set or set to zero.\n", i);1904kfree(info);1905continue;1906}19071908info->io.addr = NULL;1909info->io.regspacing = regspacings[i];1910if (!info->io.regspacing)1911info->io.regspacing = DEFAULT_REGSPACING;1912info->io.regsize = regsizes[i];1913if (!info->io.regsize)1914info->io.regsize = DEFAULT_REGSPACING;1915info->io.regshift = regshifts[i];1916info->irq = irqs[i];1917if (info->irq)1918info->irq_setup = std_irq_setup;1919info->slave_addr = slave_addrs[i];19201921if (!add_smi(info)) {1922if (try_smi_init(info))1923cleanup_one_si(info);1924ret = 0;1925} else {1926kfree(info);1927}1928}1929return ret;1930}19311932#ifdef CONFIG_ACPI19331934#include <linux/acpi.h>19351936/*1937* Once we get an ACPI failure, we don't try any more, because we go1938* through the tables sequentially. Once we don't find a table, there1939* are no more.1940*/1941static int acpi_failure;19421943/* For GPE-type interrupts. */1944static u32 ipmi_acpi_gpe(acpi_handle gpe_device,1945u32 gpe_number, void *context)1946{1947struct smi_info *smi_info = context;1948unsigned long flags;1949#ifdef DEBUG_TIMING1950struct timeval t;1951#endif19521953spin_lock_irqsave(&(smi_info->si_lock), flags);19541955smi_inc_stat(smi_info, interrupts);19561957#ifdef DEBUG_TIMING1958do_gettimeofday(&t);1959printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);1960#endif1961smi_event_handler(smi_info, 0);1962spin_unlock_irqrestore(&(smi_info->si_lock), flags);19631964return ACPI_INTERRUPT_HANDLED;1965}19661967static void acpi_gpe_irq_cleanup(struct smi_info *info)1968{1969if (!info->irq)1970return;19711972acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);1973}19741975static int acpi_gpe_irq_setup(struct smi_info *info)1976{1977acpi_status status;19781979if (!info->irq)1980return 0;19811982/* FIXME - is level triggered right? */1983status = acpi_install_gpe_handler(NULL,1984info->irq,1985ACPI_GPE_LEVEL_TRIGGERED,1986&ipmi_acpi_gpe,1987info);1988if (status != AE_OK) {1989dev_warn(info->dev, "%s unable to claim ACPI GPE %d,"1990" running polled\n", DEVICE_NAME, info->irq);1991info->irq = 0;1992return -EINVAL;1993} else {1994info->irq_cleanup = acpi_gpe_irq_cleanup;1995dev_info(info->dev, "Using ACPI GPE %d\n", info->irq);1996return 0;1997}1998}19992000/*2001* Defined at2002* http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf2003*/2004struct SPMITable {2005s8 Signature[4];2006u32 Length;2007u8 Revision;2008u8 Checksum;2009s8 OEMID[6];2010s8 OEMTableID[8];2011s8 OEMRevision[4];2012s8 CreatorID[4];2013s8 CreatorRevision[4];2014u8 InterfaceType;2015u8 IPMIlegacy;2016s16 SpecificationRevision;20172018/*2019* Bit 0 - SCI interrupt supported2020* Bit 1 - I/O APIC/SAPIC2021*/2022u8 InterruptType;20232024/*2025* If bit 0 of InterruptType is set, then this is the SCI2026* interrupt in the GPEx_STS register.2027*/2028u8 GPE;20292030s16 Reserved;20312032/*2033* If bit 1 of InterruptType is set, then this is the I/O2034* APIC/SAPIC interrupt.2035*/2036u32 GlobalSystemInterrupt;20372038/* The actual register address. */2039struct acpi_generic_address addr;20402041u8 UID[4];20422043s8 spmi_id[1]; /* A '\0' terminated array starts here. */2044};20452046static int __devinit try_init_spmi(struct SPMITable *spmi)2047{2048struct smi_info *info;20492050if (spmi->IPMIlegacy != 1) {2051printk(KERN_INFO PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);2052return -ENODEV;2053}20542055info = smi_info_alloc();2056if (!info) {2057printk(KERN_ERR PFX "Could not allocate SI data (3)\n");2058return -ENOMEM;2059}20602061info->addr_source = SI_SPMI;2062printk(KERN_INFO PFX "probing via SPMI\n");20632064/* Figure out the interface type. */2065switch (spmi->InterfaceType) {2066case 1: /* KCS */2067info->si_type = SI_KCS;2068break;2069case 2: /* SMIC */2070info->si_type = SI_SMIC;2071break;2072case 3: /* BT */2073info->si_type = SI_BT;2074break;2075default:2076printk(KERN_INFO PFX "Unknown ACPI/SPMI SI type %d\n",2077spmi->InterfaceType);2078kfree(info);2079return -EIO;2080}20812082if (spmi->InterruptType & 1) {2083/* We've got a GPE interrupt. */2084info->irq = spmi->GPE;2085info->irq_setup = acpi_gpe_irq_setup;2086} else if (spmi->InterruptType & 2) {2087/* We've got an APIC/SAPIC interrupt. */2088info->irq = spmi->GlobalSystemInterrupt;2089info->irq_setup = std_irq_setup;2090} else {2091/* Use the default interrupt setting. */2092info->irq = 0;2093info->irq_setup = NULL;2094}20952096if (spmi->addr.bit_width) {2097/* A (hopefully) properly formed register bit width. */2098info->io.regspacing = spmi->addr.bit_width / 8;2099} else {2100info->io.regspacing = DEFAULT_REGSPACING;2101}2102info->io.regsize = info->io.regspacing;2103info->io.regshift = spmi->addr.bit_offset;21042105if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {2106info->io_setup = mem_setup;2107info->io.addr_type = IPMI_MEM_ADDR_SPACE;2108} else if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_IO) {2109info->io_setup = port_setup;2110info->io.addr_type = IPMI_IO_ADDR_SPACE;2111} else {2112kfree(info);2113printk(KERN_WARNING PFX "Unknown ACPI I/O Address type\n");2114return -EIO;2115}2116info->io.addr_data = spmi->addr.address;21172118pr_info("ipmi_si: SPMI: %s %#lx regsize %d spacing %d irq %d\n",2119(info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",2120info->io.addr_data, info->io.regsize, info->io.regspacing,2121info->irq);21222123if (add_smi(info))2124kfree(info);21252126return 0;2127}21282129static void __devinit spmi_find_bmc(void)2130{2131acpi_status status;2132struct SPMITable *spmi;2133int i;21342135if (acpi_disabled)2136return;21372138if (acpi_failure)2139return;21402141for (i = 0; ; i++) {2142status = acpi_get_table(ACPI_SIG_SPMI, i+1,2143(struct acpi_table_header **)&spmi);2144if (status != AE_OK)2145return;21462147try_init_spmi(spmi);2148}2149}21502151static int __devinit ipmi_pnp_probe(struct pnp_dev *dev,2152const struct pnp_device_id *dev_id)2153{2154struct acpi_device *acpi_dev;2155struct smi_info *info;2156struct resource *res, *res_second;2157acpi_handle handle;2158acpi_status status;2159unsigned long long tmp;21602161acpi_dev = pnp_acpi_device(dev);2162if (!acpi_dev)2163return -ENODEV;21642165info = smi_info_alloc();2166if (!info)2167return -ENOMEM;21682169info->addr_source = SI_ACPI;2170printk(KERN_INFO PFX "probing via ACPI\n");21712172handle = acpi_dev->handle;2173info->addr_info.acpi_info.acpi_handle = handle;21742175/* _IFT tells us the interface type: KCS, BT, etc */2176status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);2177if (ACPI_FAILURE(status))2178goto err_free;21792180switch (tmp) {2181case 1:2182info->si_type = SI_KCS;2183break;2184case 2:2185info->si_type = SI_SMIC;2186break;2187case 3:2188info->si_type = SI_BT;2189break;2190default:2191dev_info(&dev->dev, "unknown IPMI type %lld\n", tmp);2192goto err_free;2193}21942195res = pnp_get_resource(dev, IORESOURCE_IO, 0);2196if (res) {2197info->io_setup = port_setup;2198info->io.addr_type = IPMI_IO_ADDR_SPACE;2199} else {2200res = pnp_get_resource(dev, IORESOURCE_MEM, 0);2201if (res) {2202info->io_setup = mem_setup;2203info->io.addr_type = IPMI_MEM_ADDR_SPACE;2204}2205}2206if (!res) {2207dev_err(&dev->dev, "no I/O or memory address\n");2208goto err_free;2209}2210info->io.addr_data = res->start;22112212info->io.regspacing = DEFAULT_REGSPACING;2213res_second = pnp_get_resource(dev,2214(info->io.addr_type == IPMI_IO_ADDR_SPACE) ?2215IORESOURCE_IO : IORESOURCE_MEM,22161);2217if (res_second) {2218if (res_second->start > info->io.addr_data)2219info->io.regspacing = res_second->start - info->io.addr_data;2220}2221info->io.regsize = DEFAULT_REGSPACING;2222info->io.regshift = 0;22232224/* If _GPE exists, use it; otherwise use standard interrupts */2225status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);2226if (ACPI_SUCCESS(status)) {2227info->irq = tmp;2228info->irq_setup = acpi_gpe_irq_setup;2229} else if (pnp_irq_valid(dev, 0)) {2230info->irq = pnp_irq(dev, 0);2231info->irq_setup = std_irq_setup;2232}22332234info->dev = &dev->dev;2235pnp_set_drvdata(dev, info);22362237dev_info(info->dev, "%pR regsize %d spacing %d irq %d\n",2238res, info->io.regsize, info->io.regspacing,2239info->irq);22402241if (add_smi(info))2242goto err_free;22432244return 0;22452246err_free:2247kfree(info);2248return -EINVAL;2249}22502251static void __devexit ipmi_pnp_remove(struct pnp_dev *dev)2252{2253struct smi_info *info = pnp_get_drvdata(dev);22542255cleanup_one_si(info);2256}22572258static const struct pnp_device_id pnp_dev_table[] = {2259{"IPI0001", 0},2260{"", 0},2261};22622263static struct pnp_driver ipmi_pnp_driver = {2264.name = DEVICE_NAME,2265.probe = ipmi_pnp_probe,2266.remove = __devexit_p(ipmi_pnp_remove),2267.id_table = pnp_dev_table,2268};2269#endif22702271#ifdef CONFIG_DMI2272struct dmi_ipmi_data {2273u8 type;2274u8 addr_space;2275unsigned long base_addr;2276u8 irq;2277u8 offset;2278u8 slave_addr;2279};22802281static int __devinit decode_dmi(const struct dmi_header *dm,2282struct dmi_ipmi_data *dmi)2283{2284const u8 *data = (const u8 *)dm;2285unsigned long base_addr;2286u8 reg_spacing;2287u8 len = dm->length;22882289dmi->type = data[4];22902291memcpy(&base_addr, data+8, sizeof(unsigned long));2292if (len >= 0x11) {2293if (base_addr & 1) {2294/* I/O */2295base_addr &= 0xFFFE;2296dmi->addr_space = IPMI_IO_ADDR_SPACE;2297} else2298/* Memory */2299dmi->addr_space = IPMI_MEM_ADDR_SPACE;23002301/* If bit 4 of byte 0x10 is set, then the lsb for the address2302is odd. */2303dmi->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);23042305dmi->irq = data[0x11];23062307/* The top two bits of byte 0x10 hold the register spacing. */2308reg_spacing = (data[0x10] & 0xC0) >> 6;2309switch (reg_spacing) {2310case 0x00: /* Byte boundaries */2311dmi->offset = 1;2312break;2313case 0x01: /* 32-bit boundaries */2314dmi->offset = 4;2315break;2316case 0x02: /* 16-byte boundaries */2317dmi->offset = 16;2318break;2319default:2320/* Some other interface, just ignore it. */2321return -EIO;2322}2323} else {2324/* Old DMI spec. */2325/*2326* Note that technically, the lower bit of the base2327* address should be 1 if the address is I/O and 0 if2328* the address is in memory. So many systems get that2329* wrong (and all that I have seen are I/O) so we just2330* ignore that bit and assume I/O. Systems that use2331* memory should use the newer spec, anyway.2332*/2333dmi->base_addr = base_addr & 0xfffe;2334dmi->addr_space = IPMI_IO_ADDR_SPACE;2335dmi->offset = 1;2336}23372338dmi->slave_addr = data[6];23392340return 0;2341}23422343static void __devinit try_init_dmi(struct dmi_ipmi_data *ipmi_data)2344{2345struct smi_info *info;23462347info = smi_info_alloc();2348if (!info) {2349printk(KERN_ERR PFX "Could not allocate SI data\n");2350return;2351}23522353info->addr_source = SI_SMBIOS;2354printk(KERN_INFO PFX "probing via SMBIOS\n");23552356switch (ipmi_data->type) {2357case 0x01: /* KCS */2358info->si_type = SI_KCS;2359break;2360case 0x02: /* SMIC */2361info->si_type = SI_SMIC;2362break;2363case 0x03: /* BT */2364info->si_type = SI_BT;2365break;2366default:2367kfree(info);2368return;2369}23702371switch (ipmi_data->addr_space) {2372case IPMI_MEM_ADDR_SPACE:2373info->io_setup = mem_setup;2374info->io.addr_type = IPMI_MEM_ADDR_SPACE;2375break;23762377case IPMI_IO_ADDR_SPACE:2378info->io_setup = port_setup;2379info->io.addr_type = IPMI_IO_ADDR_SPACE;2380break;23812382default:2383kfree(info);2384printk(KERN_WARNING PFX "Unknown SMBIOS I/O Address type: %d\n",2385ipmi_data->addr_space);2386return;2387}2388info->io.addr_data = ipmi_data->base_addr;23892390info->io.regspacing = ipmi_data->offset;2391if (!info->io.regspacing)2392info->io.regspacing = DEFAULT_REGSPACING;2393info->io.regsize = DEFAULT_REGSPACING;2394info->io.regshift = 0;23952396info->slave_addr = ipmi_data->slave_addr;23972398info->irq = ipmi_data->irq;2399if (info->irq)2400info->irq_setup = std_irq_setup;24012402pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",2403(info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",2404info->io.addr_data, info->io.regsize, info->io.regspacing,2405info->irq);24062407if (add_smi(info))2408kfree(info);2409}24102411static void __devinit dmi_find_bmc(void)2412{2413const struct dmi_device *dev = NULL;2414struct dmi_ipmi_data data;2415int rv;24162417while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {2418memset(&data, 0, sizeof(data));2419rv = decode_dmi((const struct dmi_header *) dev->device_data,2420&data);2421if (!rv)2422try_init_dmi(&data);2423}2424}2425#endif /* CONFIG_DMI */24262427#ifdef CONFIG_PCI24282429#define PCI_ERMC_CLASSCODE 0x0C07002430#define PCI_ERMC_CLASSCODE_MASK 0xffffff002431#define PCI_ERMC_CLASSCODE_TYPE_MASK 0xff2432#define PCI_ERMC_CLASSCODE_TYPE_SMIC 0x002433#define PCI_ERMC_CLASSCODE_TYPE_KCS 0x012434#define PCI_ERMC_CLASSCODE_TYPE_BT 0x0224352436#define PCI_HP_VENDOR_ID 0x103C2437#define PCI_MMC_DEVICE_ID 0x121A2438#define PCI_MMC_ADDR_CW 0x1024392440static void ipmi_pci_cleanup(struct smi_info *info)2441{2442struct pci_dev *pdev = info->addr_source_data;24432444pci_disable_device(pdev);2445}24462447static int __devinit ipmi_pci_probe(struct pci_dev *pdev,2448const struct pci_device_id *ent)2449{2450int rv;2451int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;2452struct smi_info *info;24532454info = smi_info_alloc();2455if (!info)2456return -ENOMEM;24572458info->addr_source = SI_PCI;2459dev_info(&pdev->dev, "probing via PCI");24602461switch (class_type) {2462case PCI_ERMC_CLASSCODE_TYPE_SMIC:2463info->si_type = SI_SMIC;2464break;24652466case PCI_ERMC_CLASSCODE_TYPE_KCS:2467info->si_type = SI_KCS;2468break;24692470case PCI_ERMC_CLASSCODE_TYPE_BT:2471info->si_type = SI_BT;2472break;24732474default:2475kfree(info);2476dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);2477return -ENOMEM;2478}24792480rv = pci_enable_device(pdev);2481if (rv) {2482dev_err(&pdev->dev, "couldn't enable PCI device\n");2483kfree(info);2484return rv;2485}24862487info->addr_source_cleanup = ipmi_pci_cleanup;2488info->addr_source_data = pdev;24892490if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {2491info->io_setup = port_setup;2492info->io.addr_type = IPMI_IO_ADDR_SPACE;2493} else {2494info->io_setup = mem_setup;2495info->io.addr_type = IPMI_MEM_ADDR_SPACE;2496}2497info->io.addr_data = pci_resource_start(pdev, 0);24982499info->io.regspacing = DEFAULT_REGSPACING;2500info->io.regsize = DEFAULT_REGSPACING;2501info->io.regshift = 0;25022503info->irq = pdev->irq;2504if (info->irq)2505info->irq_setup = std_irq_setup;25062507info->dev = &pdev->dev;2508pci_set_drvdata(pdev, info);25092510dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",2511&pdev->resource[0], info->io.regsize, info->io.regspacing,2512info->irq);25132514if (add_smi(info))2515kfree(info);25162517return 0;2518}25192520static void __devexit ipmi_pci_remove(struct pci_dev *pdev)2521{2522struct smi_info *info = pci_get_drvdata(pdev);2523cleanup_one_si(info);2524}25252526#ifdef CONFIG_PM2527static int ipmi_pci_suspend(struct pci_dev *pdev, pm_message_t state)2528{2529return 0;2530}25312532static int ipmi_pci_resume(struct pci_dev *pdev)2533{2534return 0;2535}2536#endif25372538static struct pci_device_id ipmi_pci_devices[] = {2539{ PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },2540{ PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE_MASK) },2541{ 0, }2542};2543MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);25442545static struct pci_driver ipmi_pci_driver = {2546.name = DEVICE_NAME,2547.id_table = ipmi_pci_devices,2548.probe = ipmi_pci_probe,2549.remove = __devexit_p(ipmi_pci_remove),2550#ifdef CONFIG_PM2551.suspend = ipmi_pci_suspend,2552.resume = ipmi_pci_resume,2553#endif2554};2555#endif /* CONFIG_PCI */25562557static struct of_device_id ipmi_match[];2558static int __devinit ipmi_probe(struct platform_device *dev)2559{2560#ifdef CONFIG_OF2561const struct of_device_id *match;2562struct smi_info *info;2563struct resource resource;2564const __be32 *regsize, *regspacing, *regshift;2565struct device_node *np = dev->dev.of_node;2566int ret;2567int proplen;25682569dev_info(&dev->dev, "probing via device tree\n");25702571match = of_match_device(ipmi_match, &dev->dev);2572if (!match)2573return -EINVAL;25742575ret = of_address_to_resource(np, 0, &resource);2576if (ret) {2577dev_warn(&dev->dev, PFX "invalid address from OF\n");2578return ret;2579}25802581regsize = of_get_property(np, "reg-size", &proplen);2582if (regsize && proplen != 4) {2583dev_warn(&dev->dev, PFX "invalid regsize from OF\n");2584return -EINVAL;2585}25862587regspacing = of_get_property(np, "reg-spacing", &proplen);2588if (regspacing && proplen != 4) {2589dev_warn(&dev->dev, PFX "invalid regspacing from OF\n");2590return -EINVAL;2591}25922593regshift = of_get_property(np, "reg-shift", &proplen);2594if (regshift && proplen != 4) {2595dev_warn(&dev->dev, PFX "invalid regshift from OF\n");2596return -EINVAL;2597}25982599info = smi_info_alloc();26002601if (!info) {2602dev_err(&dev->dev,2603"could not allocate memory for OF probe\n");2604return -ENOMEM;2605}26062607info->si_type = (enum si_type) match->data;2608info->addr_source = SI_DEVICETREE;2609info->irq_setup = std_irq_setup;26102611if (resource.flags & IORESOURCE_IO) {2612info->io_setup = port_setup;2613info->io.addr_type = IPMI_IO_ADDR_SPACE;2614} else {2615info->io_setup = mem_setup;2616info->io.addr_type = IPMI_MEM_ADDR_SPACE;2617}26182619info->io.addr_data = resource.start;26202621info->io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;2622info->io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;2623info->io.regshift = regshift ? be32_to_cpup(regshift) : 0;26242625info->irq = irq_of_parse_and_map(dev->dev.of_node, 0);2626info->dev = &dev->dev;26272628dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",2629info->io.addr_data, info->io.regsize, info->io.regspacing,2630info->irq);26312632dev_set_drvdata(&dev->dev, info);26332634if (add_smi(info)) {2635kfree(info);2636return -EBUSY;2637}2638#endif2639return 0;2640}26412642static int __devexit ipmi_remove(struct platform_device *dev)2643{2644#ifdef CONFIG_OF2645cleanup_one_si(dev_get_drvdata(&dev->dev));2646#endif2647return 0;2648}26492650static struct of_device_id ipmi_match[] =2651{2652{ .type = "ipmi", .compatible = "ipmi-kcs",2653.data = (void *)(unsigned long) SI_KCS },2654{ .type = "ipmi", .compatible = "ipmi-smic",2655.data = (void *)(unsigned long) SI_SMIC },2656{ .type = "ipmi", .compatible = "ipmi-bt",2657.data = (void *)(unsigned long) SI_BT },2658{},2659};26602661static struct platform_driver ipmi_driver = {2662.driver = {2663.name = DEVICE_NAME,2664.owner = THIS_MODULE,2665.of_match_table = ipmi_match,2666},2667.probe = ipmi_probe,2668.remove = __devexit_p(ipmi_remove),2669};26702671static int wait_for_msg_done(struct smi_info *smi_info)2672{2673enum si_sm_result smi_result;26742675smi_result = smi_info->handlers->event(smi_info->si_sm, 0);2676for (;;) {2677if (smi_result == SI_SM_CALL_WITH_DELAY ||2678smi_result == SI_SM_CALL_WITH_TICK_DELAY) {2679schedule_timeout_uninterruptible(1);2680smi_result = smi_info->handlers->event(2681smi_info->si_sm, 100);2682} else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {2683smi_result = smi_info->handlers->event(2684smi_info->si_sm, 0);2685} else2686break;2687}2688if (smi_result == SI_SM_HOSED)2689/*2690* We couldn't get the state machine to run, so whatever's at2691* the port is probably not an IPMI SMI interface.2692*/2693return -ENODEV;26942695return 0;2696}26972698static int try_get_dev_id(struct smi_info *smi_info)2699{2700unsigned char msg[2];2701unsigned char *resp;2702unsigned long resp_len;2703int rv = 0;27042705resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);2706if (!resp)2707return -ENOMEM;27082709/*2710* Do a Get Device ID command, since it comes back with some2711* useful info.2712*/2713msg[0] = IPMI_NETFN_APP_REQUEST << 2;2714msg[1] = IPMI_GET_DEVICE_ID_CMD;2715smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);27162717rv = wait_for_msg_done(smi_info);2718if (rv)2719goto out;27202721resp_len = smi_info->handlers->get_result(smi_info->si_sm,2722resp, IPMI_MAX_MSG_LENGTH);27232724/* Check and record info from the get device id, in case we need it. */2725rv = ipmi_demangle_device_id(resp, resp_len, &smi_info->device_id);27262727out:2728kfree(resp);2729return rv;2730}27312732static int try_enable_event_buffer(struct smi_info *smi_info)2733{2734unsigned char msg[3];2735unsigned char *resp;2736unsigned long resp_len;2737int rv = 0;27382739resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);2740if (!resp)2741return -ENOMEM;27422743msg[0] = IPMI_NETFN_APP_REQUEST << 2;2744msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;2745smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);27462747rv = wait_for_msg_done(smi_info);2748if (rv) {2749printk(KERN_WARNING PFX "Error getting response from get"2750" global enables command, the event buffer is not"2751" enabled.\n");2752goto out;2753}27542755resp_len = smi_info->handlers->get_result(smi_info->si_sm,2756resp, IPMI_MAX_MSG_LENGTH);27572758if (resp_len < 4 ||2759resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||2760resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||2761resp[2] != 0) {2762printk(KERN_WARNING PFX "Invalid return from get global"2763" enables command, cannot enable the event buffer.\n");2764rv = -EINVAL;2765goto out;2766}27672768if (resp[3] & IPMI_BMC_EVT_MSG_BUFF)2769/* buffer is already enabled, nothing to do. */2770goto out;27712772msg[0] = IPMI_NETFN_APP_REQUEST << 2;2773msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;2774msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;2775smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);27762777rv = wait_for_msg_done(smi_info);2778if (rv) {2779printk(KERN_WARNING PFX "Error getting response from set"2780" global, enables command, the event buffer is not"2781" enabled.\n");2782goto out;2783}27842785resp_len = smi_info->handlers->get_result(smi_info->si_sm,2786resp, IPMI_MAX_MSG_LENGTH);27872788if (resp_len < 3 ||2789resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||2790resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {2791printk(KERN_WARNING PFX "Invalid return from get global,"2792"enables command, not enable the event buffer.\n");2793rv = -EINVAL;2794goto out;2795}27962797if (resp[2] != 0)2798/*2799* An error when setting the event buffer bit means2800* that the event buffer is not supported.2801*/2802rv = -ENOENT;2803out:2804kfree(resp);2805return rv;2806}28072808static int smi_type_proc_show(struct seq_file *m, void *v)2809{2810struct smi_info *smi = m->private;28112812return seq_printf(m, "%s\n", si_to_str[smi->si_type]);2813}28142815static int smi_type_proc_open(struct inode *inode, struct file *file)2816{2817return single_open(file, smi_type_proc_show, PDE(inode)->data);2818}28192820static const struct file_operations smi_type_proc_ops = {2821.open = smi_type_proc_open,2822.read = seq_read,2823.llseek = seq_lseek,2824.release = single_release,2825};28262827static int smi_si_stats_proc_show(struct seq_file *m, void *v)2828{2829struct smi_info *smi = m->private;28302831seq_printf(m, "interrupts_enabled: %d\n",2832smi->irq && !smi->interrupt_disabled);2833seq_printf(m, "short_timeouts: %u\n",2834smi_get_stat(smi, short_timeouts));2835seq_printf(m, "long_timeouts: %u\n",2836smi_get_stat(smi, long_timeouts));2837seq_printf(m, "idles: %u\n",2838smi_get_stat(smi, idles));2839seq_printf(m, "interrupts: %u\n",2840smi_get_stat(smi, interrupts));2841seq_printf(m, "attentions: %u\n",2842smi_get_stat(smi, attentions));2843seq_printf(m, "flag_fetches: %u\n",2844smi_get_stat(smi, flag_fetches));2845seq_printf(m, "hosed_count: %u\n",2846smi_get_stat(smi, hosed_count));2847seq_printf(m, "complete_transactions: %u\n",2848smi_get_stat(smi, complete_transactions));2849seq_printf(m, "events: %u\n",2850smi_get_stat(smi, events));2851seq_printf(m, "watchdog_pretimeouts: %u\n",2852smi_get_stat(smi, watchdog_pretimeouts));2853seq_printf(m, "incoming_messages: %u\n",2854smi_get_stat(smi, incoming_messages));2855return 0;2856}28572858static int smi_si_stats_proc_open(struct inode *inode, struct file *file)2859{2860return single_open(file, smi_si_stats_proc_show, PDE(inode)->data);2861}28622863static const struct file_operations smi_si_stats_proc_ops = {2864.open = smi_si_stats_proc_open,2865.read = seq_read,2866.llseek = seq_lseek,2867.release = single_release,2868};28692870static int smi_params_proc_show(struct seq_file *m, void *v)2871{2872struct smi_info *smi = m->private;28732874return seq_printf(m,2875"%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",2876si_to_str[smi->si_type],2877addr_space_to_str[smi->io.addr_type],2878smi->io.addr_data,2879smi->io.regspacing,2880smi->io.regsize,2881smi->io.regshift,2882smi->irq,2883smi->slave_addr);2884}28852886static int smi_params_proc_open(struct inode *inode, struct file *file)2887{2888return single_open(file, smi_params_proc_show, PDE(inode)->data);2889}28902891static const struct file_operations smi_params_proc_ops = {2892.open = smi_params_proc_open,2893.read = seq_read,2894.llseek = seq_lseek,2895.release = single_release,2896};28972898/*2899* oem_data_avail_to_receive_msg_avail2900* @info - smi_info structure with msg_flags set2901*2902* Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL2903* Returns 1 indicating need to re-run handle_flags().2904*/2905static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)2906{2907smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |2908RECEIVE_MSG_AVAIL);2909return 1;2910}29112912/*2913* setup_dell_poweredge_oem_data_handler2914* @info - smi_info.device_id must be populated2915*2916* Systems that match, but have firmware version < 1.40 may assert2917* OEM0_DATA_AVAIL on their own, without being told via Set Flags that2918* it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL2919* upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags2920* as RECEIVE_MSG_AVAIL instead.2921*2922* As Dell has no plans to release IPMI 1.5 firmware that *ever*2923* assert the OEM[012] bits, and if it did, the driver would have to2924* change to handle that properly, we don't actually check for the2925* firmware version.2926* Device ID = 0x20 BMC on PowerEdge 8G servers2927* Device Revision = 0x802928* Firmware Revision1 = 0x01 BMC version 1.402929* Firmware Revision2 = 0x40 BCD encoded2930* IPMI Version = 0x51 IPMI 1.52931* Manufacturer ID = A2 02 00 Dell IANA2932*2933* Additionally, PowerEdge systems with IPMI < 1.5 may also assert2934* OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.2935*2936*/2937#define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x202938#define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x802939#define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x512940#define DELL_IANA_MFR_ID 0x0002a22941static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)2942{2943struct ipmi_device_id *id = &smi_info->device_id;2944if (id->manufacturer_id == DELL_IANA_MFR_ID) {2945if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&2946id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&2947id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {2948smi_info->oem_data_avail_handler =2949oem_data_avail_to_receive_msg_avail;2950} else if (ipmi_version_major(id) < 1 ||2951(ipmi_version_major(id) == 1 &&2952ipmi_version_minor(id) < 5)) {2953smi_info->oem_data_avail_handler =2954oem_data_avail_to_receive_msg_avail;2955}2956}2957}29582959#define CANNOT_RETURN_REQUESTED_LENGTH 0xCA2960static void return_hosed_msg_badsize(struct smi_info *smi_info)2961{2962struct ipmi_smi_msg *msg = smi_info->curr_msg;29632964/* Make it a response */2965msg->rsp[0] = msg->data[0] | 4;2966msg->rsp[1] = msg->data[1];2967msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;2968msg->rsp_size = 3;2969smi_info->curr_msg = NULL;2970deliver_recv_msg(smi_info, msg);2971}29722973/*2974* dell_poweredge_bt_xaction_handler2975* @info - smi_info.device_id must be populated2976*2977* Dell PowerEdge servers with the BT interface (x6xx and 1750) will2978* not respond to a Get SDR command if the length of the data2979* requested is exactly 0x3A, which leads to command timeouts and no2980* data returned. This intercepts such commands, and causes userspace2981* callers to try again with a different-sized buffer, which succeeds.2982*/29832984#define STORAGE_NETFN 0x0A2985#define STORAGE_CMD_GET_SDR 0x232986static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,2987unsigned long unused,2988void *in)2989{2990struct smi_info *smi_info = in;2991unsigned char *data = smi_info->curr_msg->data;2992unsigned int size = smi_info->curr_msg->data_size;2993if (size >= 8 &&2994(data[0]>>2) == STORAGE_NETFN &&2995data[1] == STORAGE_CMD_GET_SDR &&2996data[7] == 0x3A) {2997return_hosed_msg_badsize(smi_info);2998return NOTIFY_STOP;2999}3000return NOTIFY_DONE;3001}30023003static struct notifier_block dell_poweredge_bt_xaction_notifier = {3004.notifier_call = dell_poweredge_bt_xaction_handler,3005};30063007/*3008* setup_dell_poweredge_bt_xaction_handler3009* @info - smi_info.device_id must be filled in already3010*3011* Fills in smi_info.device_id.start_transaction_pre_hook3012* when we know what function to use there.3013*/3014static void3015setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)3016{3017struct ipmi_device_id *id = &smi_info->device_id;3018if (id->manufacturer_id == DELL_IANA_MFR_ID &&3019smi_info->si_type == SI_BT)3020register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);3021}30223023/*3024* setup_oem_data_handler3025* @info - smi_info.device_id must be filled in already3026*3027* Fills in smi_info.device_id.oem_data_available_handler3028* when we know what function to use there.3029*/30303031static void setup_oem_data_handler(struct smi_info *smi_info)3032{3033setup_dell_poweredge_oem_data_handler(smi_info);3034}30353036static void setup_xaction_handlers(struct smi_info *smi_info)3037{3038setup_dell_poweredge_bt_xaction_handler(smi_info);3039}30403041static inline void wait_for_timer_and_thread(struct smi_info *smi_info)3042{3043if (smi_info->intf) {3044/*3045* The timer and thread are only running if the3046* interface has been started up and registered.3047*/3048if (smi_info->thread != NULL)3049kthread_stop(smi_info->thread);3050del_timer_sync(&smi_info->si_timer);3051}3052}30533054static __devinitdata struct ipmi_default_vals3055{3056int type;3057int port;3058} ipmi_defaults[] =3059{3060{ .type = SI_KCS, .port = 0xca2 },3061{ .type = SI_SMIC, .port = 0xca9 },3062{ .type = SI_BT, .port = 0xe4 },3063{ .port = 0 }3064};30653066static void __devinit default_find_bmc(void)3067{3068struct smi_info *info;3069int i;30703071for (i = 0; ; i++) {3072if (!ipmi_defaults[i].port)3073break;3074#ifdef CONFIG_PPC3075if (check_legacy_ioport(ipmi_defaults[i].port))3076continue;3077#endif3078info = smi_info_alloc();3079if (!info)3080return;30813082info->addr_source = SI_DEFAULT;30833084info->si_type = ipmi_defaults[i].type;3085info->io_setup = port_setup;3086info->io.addr_data = ipmi_defaults[i].port;3087info->io.addr_type = IPMI_IO_ADDR_SPACE;30883089info->io.addr = NULL;3090info->io.regspacing = DEFAULT_REGSPACING;3091info->io.regsize = DEFAULT_REGSPACING;3092info->io.regshift = 0;30933094if (add_smi(info) == 0) {3095if ((try_smi_init(info)) == 0) {3096/* Found one... */3097printk(KERN_INFO PFX "Found default %s"3098" state machine at %s address 0x%lx\n",3099si_to_str[info->si_type],3100addr_space_to_str[info->io.addr_type],3101info->io.addr_data);3102} else3103cleanup_one_si(info);3104} else {3105kfree(info);3106}3107}3108}31093110static int is_new_interface(struct smi_info *info)3111{3112struct smi_info *e;31133114list_for_each_entry(e, &smi_infos, link) {3115if (e->io.addr_type != info->io.addr_type)3116continue;3117if (e->io.addr_data == info->io.addr_data)3118return 0;3119}31203121return 1;3122}31233124static int add_smi(struct smi_info *new_smi)3125{3126int rv = 0;31273128printk(KERN_INFO PFX "Adding %s-specified %s state machine",3129ipmi_addr_src_to_str[new_smi->addr_source],3130si_to_str[new_smi->si_type]);3131mutex_lock(&smi_infos_lock);3132if (!is_new_interface(new_smi)) {3133printk(KERN_CONT " duplicate interface\n");3134rv = -EBUSY;3135goto out_err;3136}31373138printk(KERN_CONT "\n");31393140/* So we know not to free it unless we have allocated one. */3141new_smi->intf = NULL;3142new_smi->si_sm = NULL;3143new_smi->handlers = NULL;31443145list_add_tail(&new_smi->link, &smi_infos);31463147out_err:3148mutex_unlock(&smi_infos_lock);3149return rv;3150}31513152static int try_smi_init(struct smi_info *new_smi)3153{3154int rv = 0;3155int i;31563157printk(KERN_INFO PFX "Trying %s-specified %s state"3158" machine at %s address 0x%lx, slave address 0x%x,"3159" irq %d\n",3160ipmi_addr_src_to_str[new_smi->addr_source],3161si_to_str[new_smi->si_type],3162addr_space_to_str[new_smi->io.addr_type],3163new_smi->io.addr_data,3164new_smi->slave_addr, new_smi->irq);31653166switch (new_smi->si_type) {3167case SI_KCS:3168new_smi->handlers = &kcs_smi_handlers;3169break;31703171case SI_SMIC:3172new_smi->handlers = &smic_smi_handlers;3173break;31743175case SI_BT:3176new_smi->handlers = &bt_smi_handlers;3177break;31783179default:3180/* No support for anything else yet. */3181rv = -EIO;3182goto out_err;3183}31843185/* Allocate the state machine's data and initialize it. */3186new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);3187if (!new_smi->si_sm) {3188printk(KERN_ERR PFX3189"Could not allocate state machine memory\n");3190rv = -ENOMEM;3191goto out_err;3192}3193new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,3194&new_smi->io);31953196/* Now that we know the I/O size, we can set up the I/O. */3197rv = new_smi->io_setup(new_smi);3198if (rv) {3199printk(KERN_ERR PFX "Could not set up I/O space\n");3200goto out_err;3201}32023203/* Do low-level detection first. */3204if (new_smi->handlers->detect(new_smi->si_sm)) {3205if (new_smi->addr_source)3206printk(KERN_INFO PFX "Interface detection failed\n");3207rv = -ENODEV;3208goto out_err;3209}32103211/*3212* Attempt a get device id command. If it fails, we probably3213* don't have a BMC here.3214*/3215rv = try_get_dev_id(new_smi);3216if (rv) {3217if (new_smi->addr_source)3218printk(KERN_INFO PFX "There appears to be no BMC"3219" at this location\n");3220goto out_err;3221}32223223setup_oem_data_handler(new_smi);3224setup_xaction_handlers(new_smi);32253226INIT_LIST_HEAD(&(new_smi->xmit_msgs));3227INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));3228new_smi->curr_msg = NULL;3229atomic_set(&new_smi->req_events, 0);3230new_smi->run_to_completion = 0;3231for (i = 0; i < SI_NUM_STATS; i++)3232atomic_set(&new_smi->stats[i], 0);32333234new_smi->interrupt_disabled = 1;3235atomic_set(&new_smi->stop_operation, 0);3236new_smi->intf_num = smi_num;3237smi_num++;32383239rv = try_enable_event_buffer(new_smi);3240if (rv == 0)3241new_smi->has_event_buffer = 1;32423243/*3244* Start clearing the flags before we enable interrupts or the3245* timer to avoid racing with the timer.3246*/3247start_clear_flags(new_smi);3248/* IRQ is defined to be set when non-zero. */3249if (new_smi->irq)3250new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;32513252if (!new_smi->dev) {3253/*3254* If we don't already have a device from something3255* else (like PCI), then register a new one.3256*/3257new_smi->pdev = platform_device_alloc("ipmi_si",3258new_smi->intf_num);3259if (!new_smi->pdev) {3260printk(KERN_ERR PFX3261"Unable to allocate platform device\n");3262goto out_err;3263}3264new_smi->dev = &new_smi->pdev->dev;3265new_smi->dev->driver = &ipmi_driver.driver;32663267rv = platform_device_add(new_smi->pdev);3268if (rv) {3269printk(KERN_ERR PFX3270"Unable to register system interface device:"3271" %d\n",3272rv);3273goto out_err;3274}3275new_smi->dev_registered = 1;3276}32773278rv = ipmi_register_smi(&handlers,3279new_smi,3280&new_smi->device_id,3281new_smi->dev,3282"bmc",3283new_smi->slave_addr);3284if (rv) {3285dev_err(new_smi->dev, "Unable to register device: error %d\n",3286rv);3287goto out_err_stop_timer;3288}32893290rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",3291&smi_type_proc_ops,3292new_smi);3293if (rv) {3294dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);3295goto out_err_stop_timer;3296}32973298rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",3299&smi_si_stats_proc_ops,3300new_smi);3301if (rv) {3302dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);3303goto out_err_stop_timer;3304}33053306rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",3307&smi_params_proc_ops,3308new_smi);3309if (rv) {3310dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);3311goto out_err_stop_timer;3312}33133314dev_info(new_smi->dev, "IPMI %s interface initialized\n",3315si_to_str[new_smi->si_type]);33163317return 0;33183319out_err_stop_timer:3320atomic_inc(&new_smi->stop_operation);3321wait_for_timer_and_thread(new_smi);33223323out_err:3324new_smi->interrupt_disabled = 1;33253326if (new_smi->intf) {3327ipmi_unregister_smi(new_smi->intf);3328new_smi->intf = NULL;3329}33303331if (new_smi->irq_cleanup) {3332new_smi->irq_cleanup(new_smi);3333new_smi->irq_cleanup = NULL;3334}33353336/*3337* Wait until we know that we are out of any interrupt3338* handlers might have been running before we freed the3339* interrupt.3340*/3341synchronize_sched();33423343if (new_smi->si_sm) {3344if (new_smi->handlers)3345new_smi->handlers->cleanup(new_smi->si_sm);3346kfree(new_smi->si_sm);3347new_smi->si_sm = NULL;3348}3349if (new_smi->addr_source_cleanup) {3350new_smi->addr_source_cleanup(new_smi);3351new_smi->addr_source_cleanup = NULL;3352}3353if (new_smi->io_cleanup) {3354new_smi->io_cleanup(new_smi);3355new_smi->io_cleanup = NULL;3356}33573358if (new_smi->dev_registered) {3359platform_device_unregister(new_smi->pdev);3360new_smi->dev_registered = 0;3361}33623363return rv;3364}33653366static int __devinit init_ipmi_si(void)3367{3368int i;3369char *str;3370int rv;3371struct smi_info *e;3372enum ipmi_addr_src type = SI_INVALID;33733374if (initialized)3375return 0;3376initialized = 1;33773378rv = platform_driver_register(&ipmi_driver);3379if (rv) {3380printk(KERN_ERR PFX "Unable to register driver: %d\n", rv);3381return rv;3382}338333843385/* Parse out the si_type string into its components. */3386str = si_type_str;3387if (*str != '\0') {3388for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {3389si_type[i] = str;3390str = strchr(str, ',');3391if (str) {3392*str = '\0';3393str++;3394} else {3395break;3396}3397}3398}33993400printk(KERN_INFO "IPMI System Interface driver.\n");34013402/* If the user gave us a device, they presumably want us to use it */3403if (!hardcode_find_bmc())3404return 0;34053406#ifdef CONFIG_PCI3407rv = pci_register_driver(&ipmi_pci_driver);3408if (rv)3409printk(KERN_ERR PFX "Unable to register PCI driver: %d\n", rv);3410else3411pci_registered = 1;3412#endif34133414#ifdef CONFIG_ACPI3415pnp_register_driver(&ipmi_pnp_driver);3416pnp_registered = 1;3417#endif34183419#ifdef CONFIG_DMI3420dmi_find_bmc();3421#endif34223423#ifdef CONFIG_ACPI3424spmi_find_bmc();3425#endif34263427/* We prefer devices with interrupts, but in the case of a machine3428with multiple BMCs we assume that there will be several instances3429of a given type so if we succeed in registering a type then also3430try to register everything else of the same type */34313432mutex_lock(&smi_infos_lock);3433list_for_each_entry(e, &smi_infos, link) {3434/* Try to register a device if it has an IRQ and we either3435haven't successfully registered a device yet or this3436device has the same type as one we successfully registered */3437if (e->irq && (!type || e->addr_source == type)) {3438if (!try_smi_init(e)) {3439type = e->addr_source;3440}3441}3442}34433444/* type will only have been set if we successfully registered an si */3445if (type) {3446mutex_unlock(&smi_infos_lock);3447return 0;3448}34493450/* Fall back to the preferred device */34513452list_for_each_entry(e, &smi_infos, link) {3453if (!e->irq && (!type || e->addr_source == type)) {3454if (!try_smi_init(e)) {3455type = e->addr_source;3456}3457}3458}3459mutex_unlock(&smi_infos_lock);34603461if (type)3462return 0;34633464if (si_trydefaults) {3465mutex_lock(&smi_infos_lock);3466if (list_empty(&smi_infos)) {3467/* No BMC was found, try defaults. */3468mutex_unlock(&smi_infos_lock);3469default_find_bmc();3470} else3471mutex_unlock(&smi_infos_lock);3472}34733474mutex_lock(&smi_infos_lock);3475if (unload_when_empty && list_empty(&smi_infos)) {3476mutex_unlock(&smi_infos_lock);3477cleanup_ipmi_si();3478printk(KERN_WARNING PFX3479"Unable to find any System Interface(s)\n");3480return -ENODEV;3481} else {3482mutex_unlock(&smi_infos_lock);3483return 0;3484}3485}3486module_init(init_ipmi_si);34873488static void cleanup_one_si(struct smi_info *to_clean)3489{3490int rv = 0;3491unsigned long flags;34923493if (!to_clean)3494return;34953496list_del(&to_clean->link);34973498/* Tell the driver that we are shutting down. */3499atomic_inc(&to_clean->stop_operation);35003501/*3502* Make sure the timer and thread are stopped and will not run3503* again.3504*/3505wait_for_timer_and_thread(to_clean);35063507/*3508* Timeouts are stopped, now make sure the interrupts are off3509* for the device. A little tricky with locks to make sure3510* there are no races.3511*/3512spin_lock_irqsave(&to_clean->si_lock, flags);3513while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {3514spin_unlock_irqrestore(&to_clean->si_lock, flags);3515poll(to_clean);3516schedule_timeout_uninterruptible(1);3517spin_lock_irqsave(&to_clean->si_lock, flags);3518}3519disable_si_irq(to_clean);3520spin_unlock_irqrestore(&to_clean->si_lock, flags);3521while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {3522poll(to_clean);3523schedule_timeout_uninterruptible(1);3524}35253526/* Clean up interrupts and make sure that everything is done. */3527if (to_clean->irq_cleanup)3528to_clean->irq_cleanup(to_clean);3529while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {3530poll(to_clean);3531schedule_timeout_uninterruptible(1);3532}35333534if (to_clean->intf)3535rv = ipmi_unregister_smi(to_clean->intf);35363537if (rv) {3538printk(KERN_ERR PFX "Unable to unregister device: errno=%d\n",3539rv);3540}35413542if (to_clean->handlers)3543to_clean->handlers->cleanup(to_clean->si_sm);35443545kfree(to_clean->si_sm);35463547if (to_clean->addr_source_cleanup)3548to_clean->addr_source_cleanup(to_clean);3549if (to_clean->io_cleanup)3550to_clean->io_cleanup(to_clean);35513552if (to_clean->dev_registered)3553platform_device_unregister(to_clean->pdev);35543555kfree(to_clean);3556}35573558static void cleanup_ipmi_si(void)3559{3560struct smi_info *e, *tmp_e;35613562if (!initialized)3563return;35643565#ifdef CONFIG_PCI3566if (pci_registered)3567pci_unregister_driver(&ipmi_pci_driver);3568#endif3569#ifdef CONFIG_ACPI3570if (pnp_registered)3571pnp_unregister_driver(&ipmi_pnp_driver);3572#endif35733574platform_driver_unregister(&ipmi_driver);35753576mutex_lock(&smi_infos_lock);3577list_for_each_entry_safe(e, tmp_e, &smi_infos, link)3578cleanup_one_si(e);3579mutex_unlock(&smi_infos_lock);3580}3581module_exit(cleanup_ipmi_si);35823583MODULE_LICENSE("GPL");3584MODULE_AUTHOR("Corey Minyard <[email protected]>");3585MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"3586" system interfaces.");358735883589