Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * ipmi_ssif.c
0004  *
0005  * The interface to the IPMI driver for SMBus access to a SMBus
0006  * compliant device.  Called SSIF by the IPMI spec.
0007  *
0008  * Author: Intel Corporation
0009  *         Todd Davis <todd.c.davis@intel.com>
0010  *
0011  * Rewritten by Corey Minyard <minyard@acm.org> to support the
0012  * non-blocking I2C interface, add support for multi-part
0013  * transactions, add PEC support, and general clenaup.
0014  *
0015  * Copyright 2003 Intel Corporation
0016  * Copyright 2005 MontaVista Software
0017  */
0018 
0019 /*
0020  * This file holds the "policy" for the interface to the SSIF state
0021  * machine.  It does the configuration, handles timers and interrupts,
0022  * and drives the real SSIF state machine.
0023  */
0024 
0025 #define pr_fmt(fmt) "ipmi_ssif: " fmt
0026 #define dev_fmt(fmt) "ipmi_ssif: " fmt
0027 
0028 #if defined(MODVERSIONS)
0029 #include <linux/modversions.h>
0030 #endif
0031 
0032 #include <linux/module.h>
0033 #include <linux/moduleparam.h>
0034 #include <linux/sched.h>
0035 #include <linux/seq_file.h>
0036 #include <linux/timer.h>
0037 #include <linux/delay.h>
0038 #include <linux/errno.h>
0039 #include <linux/spinlock.h>
0040 #include <linux/slab.h>
0041 #include <linux/list.h>
0042 #include <linux/i2c.h>
0043 #include <linux/ipmi_smi.h>
0044 #include <linux/init.h>
0045 #include <linux/dmi.h>
0046 #include <linux/kthread.h>
0047 #include <linux/acpi.h>
0048 #include <linux/ctype.h>
0049 #include <linux/time64.h>
0050 #include "ipmi_dmi.h"
0051 
0052 #define DEVICE_NAME "ipmi_ssif"
0053 
0054 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD  0x57
0055 
0056 #define SSIF_IPMI_REQUEST           2
0057 #define SSIF_IPMI_MULTI_PART_REQUEST_START  6
0058 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
0059 #define SSIF_IPMI_MULTI_PART_REQUEST_END    8
0060 #define SSIF_IPMI_RESPONSE          3
0061 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE    9
0062 
0063 /* ssif_debug is a bit-field
0064  *  SSIF_DEBUG_MSG -    commands and their responses
0065  *  SSIF_DEBUG_STATES - message states
0066  *  SSIF_DEBUG_TIMING -  Measure times between events in the driver
0067  */
0068 #define SSIF_DEBUG_TIMING   4
0069 #define SSIF_DEBUG_STATE    2
0070 #define SSIF_DEBUG_MSG      1
0071 #define SSIF_NODEBUG        0
0072 #define SSIF_DEFAULT_DEBUG  (SSIF_NODEBUG)
0073 
0074 /*
0075  * Timer values
0076  */
0077 #define SSIF_MSG_USEC       20000   /* 20ms between message tries. */
0078 #define SSIF_MSG_PART_USEC  5000    /* 5ms for a message part */
0079 
0080 /* How many times to we retry sending/receiving the message. */
0081 #define SSIF_SEND_RETRIES   5
0082 #define SSIF_RECV_RETRIES   250
0083 
0084 #define SSIF_MSG_MSEC       (SSIF_MSG_USEC / 1000)
0085 #define SSIF_MSG_JIFFIES    ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
0086 #define SSIF_MSG_PART_JIFFIES   ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
0087 
0088 /*
0089  * Timeout for the watch, only used for get flag timer.
0090  */
0091 #define SSIF_WATCH_MSG_TIMEOUT      msecs_to_jiffies(10)
0092 #define SSIF_WATCH_WATCHDOG_TIMEOUT msecs_to_jiffies(250)
0093 
0094 enum ssif_intf_state {
0095     SSIF_NORMAL,
0096     SSIF_GETTING_FLAGS,
0097     SSIF_GETTING_EVENTS,
0098     SSIF_CLEARING_FLAGS,
0099     SSIF_GETTING_MESSAGES,
0100     /* FIXME - add watchdog stuff. */
0101 };
0102 
0103 #define SSIF_IDLE(ssif)  ((ssif)->ssif_state == SSIF_NORMAL \
0104               && (ssif)->curr_msg == NULL)
0105 
0106 /*
0107  * Indexes into stats[] in ssif_info below.
0108  */
0109 enum ssif_stat_indexes {
0110     /* Number of total messages sent. */
0111     SSIF_STAT_sent_messages = 0,
0112 
0113     /*
0114      * Number of message parts sent.  Messages may be broken into
0115      * parts if they are long.
0116      */
0117     SSIF_STAT_sent_messages_parts,
0118 
0119     /*
0120      * Number of time a message was retried.
0121      */
0122     SSIF_STAT_send_retries,
0123 
0124     /*
0125      * Number of times the send of a message failed.
0126      */
0127     SSIF_STAT_send_errors,
0128 
0129     /*
0130      * Number of message responses received.
0131      */
0132     SSIF_STAT_received_messages,
0133 
0134     /*
0135      * Number of message fragments received.
0136      */
0137     SSIF_STAT_received_message_parts,
0138 
0139     /*
0140      * Number of times the receive of a message was retried.
0141      */
0142     SSIF_STAT_receive_retries,
0143 
0144     /*
0145      * Number of errors receiving messages.
0146      */
0147     SSIF_STAT_receive_errors,
0148 
0149     /*
0150      * Number of times a flag fetch was requested.
0151      */
0152     SSIF_STAT_flag_fetches,
0153 
0154     /*
0155      * Number of times the hardware didn't follow the state machine.
0156      */
0157     SSIF_STAT_hosed,
0158 
0159     /*
0160      * Number of received events.
0161      */
0162     SSIF_STAT_events,
0163 
0164     /* Number of asyncronous messages received. */
0165     SSIF_STAT_incoming_messages,
0166 
0167     /* Number of watchdog pretimeouts. */
0168     SSIF_STAT_watchdog_pretimeouts,
0169 
0170     /* Number of alers received. */
0171     SSIF_STAT_alerts,
0172 
0173     /* Always add statistics before this value, it must be last. */
0174     SSIF_NUM_STATS
0175 };
0176 
0177 struct ssif_addr_info {
0178     struct i2c_board_info binfo;
0179     char *adapter_name;
0180     int debug;
0181     int slave_addr;
0182     enum ipmi_addr_src addr_src;
0183     union ipmi_smi_info_union addr_info;
0184     struct device *dev;
0185     struct i2c_client *client;
0186 
0187     struct mutex clients_mutex;
0188     struct list_head clients;
0189 
0190     struct list_head link;
0191 };
0192 
0193 struct ssif_info;
0194 
0195 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
0196                  unsigned char *data, unsigned int len);
0197 
0198 struct ssif_info {
0199     struct ipmi_smi     *intf;
0200     spinlock_t      lock;
0201     struct ipmi_smi_msg *waiting_msg;
0202     struct ipmi_smi_msg *curr_msg;
0203     enum ssif_intf_state ssif_state;
0204     unsigned long       ssif_debug;
0205 
0206     struct ipmi_smi_handlers handlers;
0207 
0208     enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
0209     union ipmi_smi_info_union addr_info;
0210 
0211     /*
0212      * Flags from the last GET_MSG_FLAGS command, used when an ATTN
0213      * is set to hold the flags until we are done handling everything
0214      * from the flags.
0215      */
0216 #define RECEIVE_MSG_AVAIL   0x01
0217 #define EVENT_MSG_BUFFER_FULL   0x02
0218 #define WDT_PRE_TIMEOUT_INT 0x08
0219     unsigned char       msg_flags;
0220 
0221     u8          global_enables;
0222     bool            has_event_buffer;
0223     bool            supports_alert;
0224 
0225     /*
0226      * Used to tell what we should do with alerts.  If we are
0227      * waiting on a response, read the data immediately.
0228      */
0229     bool            got_alert;
0230     bool            waiting_alert;
0231 
0232     /*
0233      * If set to true, this will request events the next time the
0234      * state machine is idle.
0235      */
0236     bool                req_events;
0237 
0238     /*
0239      * If set to true, this will request flags the next time the
0240      * state machine is idle.
0241      */
0242     bool                req_flags;
0243 
0244     /*
0245      * Used to perform timer operations when run-to-completion
0246      * mode is on.  This is a countdown timer.
0247      */
0248     int                 rtc_us_timer;
0249 
0250     /* Used for sending/receiving data.  +1 for the length. */
0251     unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
0252     unsigned int  data_len;
0253 
0254     /* Temp receive buffer, gets copied into data. */
0255     unsigned char recv[I2C_SMBUS_BLOCK_MAX];
0256 
0257     struct i2c_client *client;
0258     ssif_i2c_done done_handler;
0259 
0260     /* Thread interface handling */
0261     struct task_struct *thread;
0262     struct completion wake_thread;
0263     bool stopping;
0264     int i2c_read_write;
0265     int i2c_command;
0266     unsigned char *i2c_data;
0267     unsigned int i2c_size;
0268 
0269     struct timer_list retry_timer;
0270     int retries_left;
0271 
0272     long watch_timeout;     /* Timeout for flags check, 0 if off. */
0273     struct timer_list watch_timer;  /* Flag fetch timer. */
0274 
0275     /* Info from SSIF cmd */
0276     unsigned char max_xmit_msg_size;
0277     unsigned char max_recv_msg_size;
0278     bool cmd8_works; /* See test_multipart_messages() for details. */
0279     unsigned int  multi_support;
0280     int           supports_pec;
0281 
0282 #define SSIF_NO_MULTI       0
0283 #define SSIF_MULTI_2_PART   1
0284 #define SSIF_MULTI_n_PART   2
0285     unsigned char *multi_data;
0286     unsigned int  multi_len;
0287     unsigned int  multi_pos;
0288 
0289     atomic_t stats[SSIF_NUM_STATS];
0290 };
0291 
0292 #define ssif_inc_stat(ssif, stat) \
0293     atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
0294 #define ssif_get_stat(ssif, stat) \
0295     ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
0296 
0297 static bool initialized;
0298 static bool platform_registered;
0299 
0300 static void return_hosed_msg(struct ssif_info *ssif_info,
0301                  struct ipmi_smi_msg *msg);
0302 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
0303 static int start_send(struct ssif_info *ssif_info,
0304               unsigned char   *data,
0305               unsigned int    len);
0306 
0307 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
0308                       unsigned long *flags)
0309     __acquires(&ssif_info->lock)
0310 {
0311     spin_lock_irqsave(&ssif_info->lock, *flags);
0312     return flags;
0313 }
0314 
0315 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
0316                   unsigned long *flags)
0317     __releases(&ssif_info->lock)
0318 {
0319     spin_unlock_irqrestore(&ssif_info->lock, *flags);
0320 }
0321 
0322 static void deliver_recv_msg(struct ssif_info *ssif_info,
0323                  struct ipmi_smi_msg *msg)
0324 {
0325     if (msg->rsp_size < 0) {
0326         return_hosed_msg(ssif_info, msg);
0327         dev_err(&ssif_info->client->dev,
0328             "%s: Malformed message: rsp_size = %d\n",
0329                __func__, msg->rsp_size);
0330     } else {
0331         ipmi_smi_msg_received(ssif_info->intf, msg);
0332     }
0333 }
0334 
0335 static void return_hosed_msg(struct ssif_info *ssif_info,
0336                  struct ipmi_smi_msg *msg)
0337 {
0338     ssif_inc_stat(ssif_info, hosed);
0339 
0340     /* Make it a response */
0341     msg->rsp[0] = msg->data[0] | 4;
0342     msg->rsp[1] = msg->data[1];
0343     msg->rsp[2] = 0xFF; /* Unknown error. */
0344     msg->rsp_size = 3;
0345 
0346     deliver_recv_msg(ssif_info, msg);
0347 }
0348 
0349 /*
0350  * Must be called with the message lock held.  This will release the
0351  * message lock.  Note that the caller will check SSIF_IDLE and start a
0352  * new operation, so there is no need to check for new messages to
0353  * start in here.
0354  */
0355 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
0356 {
0357     unsigned char msg[3];
0358 
0359     ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
0360     ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
0361     ipmi_ssif_unlock_cond(ssif_info, flags);
0362 
0363     /* Make sure the watchdog pre-timeout flag is not set at startup. */
0364     msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
0365     msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
0366     msg[2] = WDT_PRE_TIMEOUT_INT;
0367 
0368     if (start_send(ssif_info, msg, 3) != 0) {
0369         /* Error, just go to normal state. */
0370         ssif_info->ssif_state = SSIF_NORMAL;
0371     }
0372 }
0373 
0374 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
0375 {
0376     unsigned char mb[2];
0377 
0378     ssif_info->req_flags = false;
0379     ssif_info->ssif_state = SSIF_GETTING_FLAGS;
0380     ipmi_ssif_unlock_cond(ssif_info, flags);
0381 
0382     mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
0383     mb[1] = IPMI_GET_MSG_FLAGS_CMD;
0384     if (start_send(ssif_info, mb, 2) != 0)
0385         ssif_info->ssif_state = SSIF_NORMAL;
0386 }
0387 
0388 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
0389                  struct ipmi_smi_msg *msg)
0390 {
0391     if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
0392         unsigned long oflags;
0393 
0394         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
0395         ssif_info->curr_msg = NULL;
0396         ssif_info->ssif_state = SSIF_NORMAL;
0397         ipmi_ssif_unlock_cond(ssif_info, flags);
0398         ipmi_free_smi_msg(msg);
0399     }
0400 }
0401 
0402 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
0403 {
0404     struct ipmi_smi_msg *msg;
0405 
0406     ssif_info->req_events = false;
0407 
0408     msg = ipmi_alloc_smi_msg();
0409     if (!msg) {
0410         ssif_info->ssif_state = SSIF_NORMAL;
0411         ipmi_ssif_unlock_cond(ssif_info, flags);
0412         return;
0413     }
0414 
0415     ssif_info->curr_msg = msg;
0416     ssif_info->ssif_state = SSIF_GETTING_EVENTS;
0417     ipmi_ssif_unlock_cond(ssif_info, flags);
0418 
0419     msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
0420     msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
0421     msg->data_size = 2;
0422 
0423     check_start_send(ssif_info, flags, msg);
0424 }
0425 
0426 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
0427                  unsigned long *flags)
0428 {
0429     struct ipmi_smi_msg *msg;
0430 
0431     msg = ipmi_alloc_smi_msg();
0432     if (!msg) {
0433         ssif_info->ssif_state = SSIF_NORMAL;
0434         ipmi_ssif_unlock_cond(ssif_info, flags);
0435         return;
0436     }
0437 
0438     ssif_info->curr_msg = msg;
0439     ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
0440     ipmi_ssif_unlock_cond(ssif_info, flags);
0441 
0442     msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
0443     msg->data[1] = IPMI_GET_MSG_CMD;
0444     msg->data_size = 2;
0445 
0446     check_start_send(ssif_info, flags, msg);
0447 }
0448 
0449 /*
0450  * Must be called with the message lock held.  This will release the
0451  * message lock.  Note that the caller will check SSIF_IDLE and start a
0452  * new operation, so there is no need to check for new messages to
0453  * start in here.
0454  */
0455 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
0456 {
0457     if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
0458         /* Watchdog pre-timeout */
0459         ssif_inc_stat(ssif_info, watchdog_pretimeouts);
0460         start_clear_flags(ssif_info, flags);
0461         ipmi_smi_watchdog_pretimeout(ssif_info->intf);
0462     } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
0463         /* Messages available. */
0464         start_recv_msg_fetch(ssif_info, flags);
0465     else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
0466         /* Events available. */
0467         start_event_fetch(ssif_info, flags);
0468     else {
0469         ssif_info->ssif_state = SSIF_NORMAL;
0470         ipmi_ssif_unlock_cond(ssif_info, flags);
0471     }
0472 }
0473 
0474 static int ipmi_ssif_thread(void *data)
0475 {
0476     struct ssif_info *ssif_info = data;
0477 
0478     while (!kthread_should_stop()) {
0479         int result;
0480 
0481         /* Wait for something to do */
0482         result = wait_for_completion_interruptible(
0483                         &ssif_info->wake_thread);
0484         if (ssif_info->stopping)
0485             break;
0486         if (result == -ERESTARTSYS)
0487             continue;
0488         init_completion(&ssif_info->wake_thread);
0489 
0490         if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
0491             result = i2c_smbus_write_block_data(
0492                 ssif_info->client, ssif_info->i2c_command,
0493                 ssif_info->i2c_data[0],
0494                 ssif_info->i2c_data + 1);
0495             ssif_info->done_handler(ssif_info, result, NULL, 0);
0496         } else {
0497             result = i2c_smbus_read_block_data(
0498                 ssif_info->client, ssif_info->i2c_command,
0499                 ssif_info->i2c_data);
0500             if (result < 0)
0501                 ssif_info->done_handler(ssif_info, result,
0502                             NULL, 0);
0503             else
0504                 ssif_info->done_handler(ssif_info, 0,
0505                             ssif_info->i2c_data,
0506                             result);
0507         }
0508     }
0509 
0510     return 0;
0511 }
0512 
0513 static void ssif_i2c_send(struct ssif_info *ssif_info,
0514             ssif_i2c_done handler,
0515             int read_write, int command,
0516             unsigned char *data, unsigned int size)
0517 {
0518     ssif_info->done_handler = handler;
0519 
0520     ssif_info->i2c_read_write = read_write;
0521     ssif_info->i2c_command = command;
0522     ssif_info->i2c_data = data;
0523     ssif_info->i2c_size = size;
0524     complete(&ssif_info->wake_thread);
0525 }
0526 
0527 
0528 static void msg_done_handler(struct ssif_info *ssif_info, int result,
0529                  unsigned char *data, unsigned int len);
0530 
0531 static void start_get(struct ssif_info *ssif_info)
0532 {
0533     ssif_info->rtc_us_timer = 0;
0534     ssif_info->multi_pos = 0;
0535 
0536     ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
0537           SSIF_IPMI_RESPONSE,
0538           ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
0539 }
0540 
0541 static void retry_timeout(struct timer_list *t)
0542 {
0543     struct ssif_info *ssif_info = from_timer(ssif_info, t, retry_timer);
0544     unsigned long oflags, *flags;
0545     bool waiting;
0546 
0547     if (ssif_info->stopping)
0548         return;
0549 
0550     flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
0551     waiting = ssif_info->waiting_alert;
0552     ssif_info->waiting_alert = false;
0553     ipmi_ssif_unlock_cond(ssif_info, flags);
0554 
0555     if (waiting)
0556         start_get(ssif_info);
0557 }
0558 
0559 static void watch_timeout(struct timer_list *t)
0560 {
0561     struct ssif_info *ssif_info = from_timer(ssif_info, t, watch_timer);
0562     unsigned long oflags, *flags;
0563 
0564     if (ssif_info->stopping)
0565         return;
0566 
0567     flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
0568     if (ssif_info->watch_timeout) {
0569         mod_timer(&ssif_info->watch_timer,
0570               jiffies + ssif_info->watch_timeout);
0571         if (SSIF_IDLE(ssif_info)) {
0572             start_flag_fetch(ssif_info, flags); /* Releases lock */
0573             return;
0574         }
0575         ssif_info->req_flags = true;
0576     }
0577     ipmi_ssif_unlock_cond(ssif_info, flags);
0578 }
0579 
0580 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
0581                unsigned int data)
0582 {
0583     struct ssif_info *ssif_info = i2c_get_clientdata(client);
0584     unsigned long oflags, *flags;
0585     bool do_get = false;
0586 
0587     if (type != I2C_PROTOCOL_SMBUS_ALERT)
0588         return;
0589 
0590     ssif_inc_stat(ssif_info, alerts);
0591 
0592     flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
0593     if (ssif_info->waiting_alert) {
0594         ssif_info->waiting_alert = false;
0595         del_timer(&ssif_info->retry_timer);
0596         do_get = true;
0597     } else if (ssif_info->curr_msg) {
0598         ssif_info->got_alert = true;
0599     }
0600     ipmi_ssif_unlock_cond(ssif_info, flags);
0601     if (do_get)
0602         start_get(ssif_info);
0603 }
0604 
0605 static int start_resend(struct ssif_info *ssif_info);
0606 
0607 static void msg_done_handler(struct ssif_info *ssif_info, int result,
0608                  unsigned char *data, unsigned int len)
0609 {
0610     struct ipmi_smi_msg *msg;
0611     unsigned long oflags, *flags;
0612 
0613     /*
0614      * We are single-threaded here, so no need for a lock until we
0615      * start messing with driver states or the queues.
0616      */
0617 
0618     if (result < 0) {
0619         ssif_info->retries_left--;
0620         if (ssif_info->retries_left > 0) {
0621             ssif_inc_stat(ssif_info, receive_retries);
0622 
0623             flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
0624             ssif_info->waiting_alert = true;
0625             ssif_info->rtc_us_timer = SSIF_MSG_USEC;
0626             if (!ssif_info->stopping)
0627                 mod_timer(&ssif_info->retry_timer,
0628                       jiffies + SSIF_MSG_JIFFIES);
0629             ipmi_ssif_unlock_cond(ssif_info, flags);
0630             return;
0631         }
0632 
0633         ssif_inc_stat(ssif_info, receive_errors);
0634 
0635         if  (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
0636             dev_dbg(&ssif_info->client->dev,
0637                 "%s: Error %d\n", __func__, result);
0638         len = 0;
0639         goto continue_op;
0640     }
0641 
0642     if ((len > 1) && (ssif_info->multi_pos == 0)
0643                 && (data[0] == 0x00) && (data[1] == 0x01)) {
0644         /* Start of multi-part read.  Start the next transaction. */
0645         int i;
0646 
0647         ssif_inc_stat(ssif_info, received_message_parts);
0648 
0649         /* Remove the multi-part read marker. */
0650         len -= 2;
0651         data += 2;
0652         for (i = 0; i < len; i++)
0653             ssif_info->data[i] = data[i];
0654         ssif_info->multi_len = len;
0655         ssif_info->multi_pos = 1;
0656 
0657         ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
0658              SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
0659              ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
0660         return;
0661     } else if (ssif_info->multi_pos) {
0662         /* Middle of multi-part read.  Start the next transaction. */
0663         int i;
0664         unsigned char blocknum;
0665 
0666         if (len == 0) {
0667             result = -EIO;
0668             if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
0669                 dev_dbg(&ssif_info->client->dev,
0670                     "Middle message with no data\n");
0671 
0672             goto continue_op;
0673         }
0674 
0675         blocknum = data[0];
0676         len--;
0677         data++;
0678 
0679         if (blocknum != 0xff && len != 31) {
0680             /* All blocks but the last must have 31 data bytes. */
0681             result = -EIO;
0682             if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
0683                 dev_dbg(&ssif_info->client->dev,
0684                     "Received middle message <31\n");
0685 
0686             goto continue_op;
0687         }
0688 
0689         if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
0690             /* Received message too big, abort the operation. */
0691             result = -E2BIG;
0692             if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
0693                 dev_dbg(&ssif_info->client->dev,
0694                     "Received message too big\n");
0695 
0696             goto continue_op;
0697         }
0698 
0699         for (i = 0; i < len; i++)
0700             ssif_info->data[i + ssif_info->multi_len] = data[i];
0701         ssif_info->multi_len += len;
0702         if (blocknum == 0xff) {
0703             /* End of read */
0704             len = ssif_info->multi_len;
0705             data = ssif_info->data;
0706         } else if (blocknum + 1 != ssif_info->multi_pos) {
0707             /*
0708              * Out of sequence block, just abort.  Block
0709              * numbers start at zero for the second block,
0710              * but multi_pos starts at one, so the +1.
0711              */
0712             if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
0713                 dev_dbg(&ssif_info->client->dev,
0714                     "Received message out of sequence, expected %u, got %u\n",
0715                     ssif_info->multi_pos - 1, blocknum);
0716             result = -EIO;
0717         } else {
0718             ssif_inc_stat(ssif_info, received_message_parts);
0719 
0720             ssif_info->multi_pos++;
0721 
0722             ssif_i2c_send(ssif_info, msg_done_handler,
0723                   I2C_SMBUS_READ,
0724                   SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
0725                   ssif_info->recv,
0726                   I2C_SMBUS_BLOCK_DATA);
0727             return;
0728         }
0729     }
0730 
0731  continue_op:
0732     if (result < 0) {
0733         ssif_inc_stat(ssif_info, receive_errors);
0734     } else {
0735         ssif_inc_stat(ssif_info, received_messages);
0736         ssif_inc_stat(ssif_info, received_message_parts);
0737     }
0738 
0739     if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
0740         dev_dbg(&ssif_info->client->dev,
0741             "DONE 1: state = %d, result=%d\n",
0742             ssif_info->ssif_state, result);
0743 
0744     flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
0745     msg = ssif_info->curr_msg;
0746     if (msg) {
0747         if (data) {
0748             if (len > IPMI_MAX_MSG_LENGTH)
0749                 len = IPMI_MAX_MSG_LENGTH;
0750             memcpy(msg->rsp, data, len);
0751         } else {
0752             len = 0;
0753         }
0754         msg->rsp_size = len;
0755         ssif_info->curr_msg = NULL;
0756     }
0757 
0758     switch (ssif_info->ssif_state) {
0759     case SSIF_NORMAL:
0760         ipmi_ssif_unlock_cond(ssif_info, flags);
0761         if (!msg)
0762             break;
0763 
0764         if (result < 0)
0765             return_hosed_msg(ssif_info, msg);
0766         else
0767             deliver_recv_msg(ssif_info, msg);
0768         break;
0769 
0770     case SSIF_GETTING_FLAGS:
0771         /* We got the flags from the SSIF, now handle them. */
0772         if ((result < 0) || (len < 4) || (data[2] != 0)) {
0773             /*
0774              * Error fetching flags, or invalid length,
0775              * just give up for now.
0776              */
0777             ssif_info->ssif_state = SSIF_NORMAL;
0778             ipmi_ssif_unlock_cond(ssif_info, flags);
0779             dev_warn(&ssif_info->client->dev,
0780                  "Error getting flags: %d %d, %x\n",
0781                  result, len, (len >= 3) ? data[2] : 0);
0782         } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
0783                || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
0784             /*
0785              * Don't abort here, maybe it was a queued
0786              * response to a previous command.
0787              */
0788             ipmi_ssif_unlock_cond(ssif_info, flags);
0789             dev_warn(&ssif_info->client->dev,
0790                  "Invalid response getting flags: %x %x\n",
0791                  data[0], data[1]);
0792         } else {
0793             ssif_inc_stat(ssif_info, flag_fetches);
0794             ssif_info->msg_flags = data[3];
0795             handle_flags(ssif_info, flags);
0796         }
0797         break;
0798 
0799     case SSIF_CLEARING_FLAGS:
0800         /* We cleared the flags. */
0801         if ((result < 0) || (len < 3) || (data[2] != 0)) {
0802             /* Error clearing flags */
0803             dev_warn(&ssif_info->client->dev,
0804                  "Error clearing flags: %d %d, %x\n",
0805                  result, len, (len >= 3) ? data[2] : 0);
0806         } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
0807                || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
0808             dev_warn(&ssif_info->client->dev,
0809                  "Invalid response clearing flags: %x %x\n",
0810                  data[0], data[1]);
0811         }
0812         ssif_info->ssif_state = SSIF_NORMAL;
0813         ipmi_ssif_unlock_cond(ssif_info, flags);
0814         break;
0815 
0816     case SSIF_GETTING_EVENTS:
0817         if (!msg) {
0818             /* Should never happen, but just in case. */
0819             dev_warn(&ssif_info->client->dev,
0820                  "No message set while getting events\n");
0821             ipmi_ssif_unlock_cond(ssif_info, flags);
0822             break;
0823         }
0824 
0825         if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
0826             /* Error getting event, probably done. */
0827             msg->done(msg);
0828 
0829             /* Take off the event flag. */
0830             ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
0831             handle_flags(ssif_info, flags);
0832         } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
0833                || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
0834             dev_warn(&ssif_info->client->dev,
0835                  "Invalid response getting events: %x %x\n",
0836                  msg->rsp[0], msg->rsp[1]);
0837             msg->done(msg);
0838             /* Take off the event flag. */
0839             ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
0840             handle_flags(ssif_info, flags);
0841         } else {
0842             handle_flags(ssif_info, flags);
0843             ssif_inc_stat(ssif_info, events);
0844             deliver_recv_msg(ssif_info, msg);
0845         }
0846         break;
0847 
0848     case SSIF_GETTING_MESSAGES:
0849         if (!msg) {
0850             /* Should never happen, but just in case. */
0851             dev_warn(&ssif_info->client->dev,
0852                  "No message set while getting messages\n");
0853             ipmi_ssif_unlock_cond(ssif_info, flags);
0854             break;
0855         }
0856 
0857         if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
0858             /* Error getting event, probably done. */
0859             msg->done(msg);
0860 
0861             /* Take off the msg flag. */
0862             ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
0863             handle_flags(ssif_info, flags);
0864         } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
0865                || msg->rsp[1] != IPMI_GET_MSG_CMD) {
0866             dev_warn(&ssif_info->client->dev,
0867                  "Invalid response clearing flags: %x %x\n",
0868                  msg->rsp[0], msg->rsp[1]);
0869             msg->done(msg);
0870 
0871             /* Take off the msg flag. */
0872             ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
0873             handle_flags(ssif_info, flags);
0874         } else {
0875             ssif_inc_stat(ssif_info, incoming_messages);
0876             handle_flags(ssif_info, flags);
0877             deliver_recv_msg(ssif_info, msg);
0878         }
0879         break;
0880 
0881     default:
0882         /* Should never happen, but just in case. */
0883         dev_warn(&ssif_info->client->dev,
0884              "Invalid state in message done handling: %d\n",
0885              ssif_info->ssif_state);
0886         ipmi_ssif_unlock_cond(ssif_info, flags);
0887     }
0888 
0889     flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
0890     if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
0891         if (ssif_info->req_events)
0892             start_event_fetch(ssif_info, flags);
0893         else if (ssif_info->req_flags)
0894             start_flag_fetch(ssif_info, flags);
0895         else
0896             start_next_msg(ssif_info, flags);
0897     } else
0898         ipmi_ssif_unlock_cond(ssif_info, flags);
0899 
0900     if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
0901         dev_dbg(&ssif_info->client->dev,
0902             "DONE 2: state = %d.\n", ssif_info->ssif_state);
0903 }
0904 
0905 static void msg_written_handler(struct ssif_info *ssif_info, int result,
0906                 unsigned char *data, unsigned int len)
0907 {
0908     /* We are single-threaded here, so no need for a lock. */
0909     if (result < 0) {
0910         ssif_info->retries_left--;
0911         if (ssif_info->retries_left > 0) {
0912             if (!start_resend(ssif_info)) {
0913                 ssif_inc_stat(ssif_info, send_retries);
0914                 return;
0915             }
0916             /* request failed, just return the error. */
0917             ssif_inc_stat(ssif_info, send_errors);
0918 
0919             if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
0920                 dev_dbg(&ssif_info->client->dev,
0921                     "%s: Out of retries\n", __func__);
0922             msg_done_handler(ssif_info, -EIO, NULL, 0);
0923             return;
0924         }
0925 
0926         ssif_inc_stat(ssif_info, send_errors);
0927 
0928         /*
0929          * Got an error on transmit, let the done routine
0930          * handle it.
0931          */
0932         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
0933             dev_dbg(&ssif_info->client->dev,
0934                 "%s: Error  %d\n", __func__, result);
0935 
0936         msg_done_handler(ssif_info, result, NULL, 0);
0937         return;
0938     }
0939 
0940     if (ssif_info->multi_data) {
0941         /*
0942          * In the middle of a multi-data write.  See the comment
0943          * in the SSIF_MULTI_n_PART case in the probe function
0944          * for details on the intricacies of this.
0945          */
0946         int left, to_write;
0947         unsigned char *data_to_send;
0948         unsigned char cmd;
0949 
0950         ssif_inc_stat(ssif_info, sent_messages_parts);
0951 
0952         left = ssif_info->multi_len - ssif_info->multi_pos;
0953         to_write = left;
0954         if (to_write > 32)
0955             to_write = 32;
0956         /* Length byte. */
0957         ssif_info->multi_data[ssif_info->multi_pos] = to_write;
0958         data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
0959         ssif_info->multi_pos += to_write;
0960         cmd = SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE;
0961         if (ssif_info->cmd8_works) {
0962             if (left == to_write) {
0963                 cmd = SSIF_IPMI_MULTI_PART_REQUEST_END;
0964                 ssif_info->multi_data = NULL;
0965             }
0966         } else if (to_write < 32) {
0967             ssif_info->multi_data = NULL;
0968         }
0969 
0970         ssif_i2c_send(ssif_info, msg_written_handler,
0971               I2C_SMBUS_WRITE, cmd,
0972               data_to_send, I2C_SMBUS_BLOCK_DATA);
0973     } else {
0974         /* Ready to request the result. */
0975         unsigned long oflags, *flags;
0976 
0977         ssif_inc_stat(ssif_info, sent_messages);
0978         ssif_inc_stat(ssif_info, sent_messages_parts);
0979 
0980         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
0981         if (ssif_info->got_alert) {
0982             /* The result is already ready, just start it. */
0983             ssif_info->got_alert = false;
0984             ipmi_ssif_unlock_cond(ssif_info, flags);
0985             start_get(ssif_info);
0986         } else {
0987             /* Wait a jiffie then request the next message */
0988             ssif_info->waiting_alert = true;
0989             ssif_info->retries_left = SSIF_RECV_RETRIES;
0990             ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
0991             if (!ssif_info->stopping)
0992                 mod_timer(&ssif_info->retry_timer,
0993                       jiffies + SSIF_MSG_PART_JIFFIES);
0994             ipmi_ssif_unlock_cond(ssif_info, flags);
0995         }
0996     }
0997 }
0998 
0999 static int start_resend(struct ssif_info *ssif_info)
1000 {
1001     int command;
1002 
1003     ssif_info->got_alert = false;
1004 
1005     if (ssif_info->data_len > 32) {
1006         command = SSIF_IPMI_MULTI_PART_REQUEST_START;
1007         ssif_info->multi_data = ssif_info->data;
1008         ssif_info->multi_len = ssif_info->data_len;
1009         /*
1010          * Subtle thing, this is 32, not 33, because we will
1011          * overwrite the thing at position 32 (which was just
1012          * transmitted) with the new length.
1013          */
1014         ssif_info->multi_pos = 32;
1015         ssif_info->data[0] = 32;
1016     } else {
1017         ssif_info->multi_data = NULL;
1018         command = SSIF_IPMI_REQUEST;
1019         ssif_info->data[0] = ssif_info->data_len;
1020     }
1021 
1022     ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1023            command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1024     return 0;
1025 }
1026 
1027 static int start_send(struct ssif_info *ssif_info,
1028               unsigned char   *data,
1029               unsigned int    len)
1030 {
1031     if (len > IPMI_MAX_MSG_LENGTH)
1032         return -E2BIG;
1033     if (len > ssif_info->max_xmit_msg_size)
1034         return -E2BIG;
1035 
1036     ssif_info->retries_left = SSIF_SEND_RETRIES;
1037     memcpy(ssif_info->data + 1, data, len);
1038     ssif_info->data_len = len;
1039     return start_resend(ssif_info);
1040 }
1041 
1042 /* Must be called with the message lock held. */
1043 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1044 {
1045     struct ipmi_smi_msg *msg;
1046     unsigned long oflags;
1047 
1048  restart:
1049     if (!SSIF_IDLE(ssif_info)) {
1050         ipmi_ssif_unlock_cond(ssif_info, flags);
1051         return;
1052     }
1053 
1054     if (!ssif_info->waiting_msg) {
1055         ssif_info->curr_msg = NULL;
1056         ipmi_ssif_unlock_cond(ssif_info, flags);
1057     } else {
1058         int rv;
1059 
1060         ssif_info->curr_msg = ssif_info->waiting_msg;
1061         ssif_info->waiting_msg = NULL;
1062         ipmi_ssif_unlock_cond(ssif_info, flags);
1063         rv = start_send(ssif_info,
1064                 ssif_info->curr_msg->data,
1065                 ssif_info->curr_msg->data_size);
1066         if (rv) {
1067             msg = ssif_info->curr_msg;
1068             ssif_info->curr_msg = NULL;
1069             return_hosed_msg(ssif_info, msg);
1070             flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1071             goto restart;
1072         }
1073     }
1074 }
1075 
1076 static void sender(void                *send_info,
1077            struct ipmi_smi_msg *msg)
1078 {
1079     struct ssif_info *ssif_info = send_info;
1080     unsigned long oflags, *flags;
1081 
1082     BUG_ON(ssif_info->waiting_msg);
1083     ssif_info->waiting_msg = msg;
1084 
1085     flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1086     start_next_msg(ssif_info, flags);
1087 
1088     if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1089         struct timespec64 t;
1090 
1091         ktime_get_real_ts64(&t);
1092         dev_dbg(&ssif_info->client->dev,
1093             "**Enqueue %02x %02x: %lld.%6.6ld\n",
1094             msg->data[0], msg->data[1],
1095             (long long)t.tv_sec, (long)t.tv_nsec / NSEC_PER_USEC);
1096     }
1097 }
1098 
1099 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1100 {
1101     struct ssif_info *ssif_info = send_info;
1102 
1103     data->addr_src = ssif_info->addr_source;
1104     data->dev = &ssif_info->client->dev;
1105     data->addr_info = ssif_info->addr_info;
1106     get_device(data->dev);
1107 
1108     return 0;
1109 }
1110 
1111 /*
1112  * Upper layer wants us to request events.
1113  */
1114 static void request_events(void *send_info)
1115 {
1116     struct ssif_info *ssif_info = send_info;
1117     unsigned long oflags, *flags;
1118 
1119     if (!ssif_info->has_event_buffer)
1120         return;
1121 
1122     flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1123     ssif_info->req_events = true;
1124     ipmi_ssif_unlock_cond(ssif_info, flags);
1125 }
1126 
1127 /*
1128  * Upper layer is changing the flag saying whether we need to request
1129  * flags periodically or not.
1130  */
1131 static void ssif_set_need_watch(void *send_info, unsigned int watch_mask)
1132 {
1133     struct ssif_info *ssif_info = send_info;
1134     unsigned long oflags, *flags;
1135     long timeout = 0;
1136 
1137     if (watch_mask & IPMI_WATCH_MASK_CHECK_MESSAGES)
1138         timeout = SSIF_WATCH_MSG_TIMEOUT;
1139     else if (watch_mask)
1140         timeout = SSIF_WATCH_WATCHDOG_TIMEOUT;
1141 
1142     flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1143     if (timeout != ssif_info->watch_timeout) {
1144         ssif_info->watch_timeout = timeout;
1145         if (ssif_info->watch_timeout)
1146             mod_timer(&ssif_info->watch_timer,
1147                   jiffies + ssif_info->watch_timeout);
1148     }
1149     ipmi_ssif_unlock_cond(ssif_info, flags);
1150 }
1151 
1152 static int ssif_start_processing(void            *send_info,
1153                  struct ipmi_smi *intf)
1154 {
1155     struct ssif_info *ssif_info = send_info;
1156 
1157     ssif_info->intf = intf;
1158 
1159     return 0;
1160 }
1161 
1162 #define MAX_SSIF_BMCS 4
1163 
1164 static unsigned short addr[MAX_SSIF_BMCS];
1165 static int num_addrs;
1166 module_param_array(addr, ushort, &num_addrs, 0);
1167 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1168 
1169 static char *adapter_name[MAX_SSIF_BMCS];
1170 static int num_adapter_names;
1171 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1172 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC.  By default all devices are scanned.");
1173 
1174 static int slave_addrs[MAX_SSIF_BMCS];
1175 static int num_slave_addrs;
1176 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1177 MODULE_PARM_DESC(slave_addrs,
1178          "The default IPMB slave address for the controller.");
1179 
1180 static bool alerts_broken;
1181 module_param(alerts_broken, bool, 0);
1182 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1183 
1184 /*
1185  * Bit 0 enables message debugging, bit 1 enables state debugging, and
1186  * bit 2 enables timing debugging.  This is an array indexed by
1187  * interface number"
1188  */
1189 static int dbg[MAX_SSIF_BMCS];
1190 static int num_dbg;
1191 module_param_array(dbg, int, &num_dbg, 0);
1192 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1193 
1194 static bool ssif_dbg_probe;
1195 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1196 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1197 
1198 static bool ssif_tryacpi = true;
1199 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1200 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1201 
1202 static bool ssif_trydmi = true;
1203 module_param_named(trydmi, ssif_trydmi, bool, 0);
1204 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1205 
1206 static DEFINE_MUTEX(ssif_infos_mutex);
1207 static LIST_HEAD(ssif_infos);
1208 
1209 #define IPMI_SSIF_ATTR(name) \
1210 static ssize_t ipmi_##name##_show(struct device *dev,           \
1211                   struct device_attribute *attr,    \
1212                   char *buf)                \
1213 {                                   \
1214     struct ssif_info *ssif_info = dev_get_drvdata(dev);     \
1215                                     \
1216     return sysfs_emit(buf, "%u\n", ssif_get_stat(ssif_info, name));\
1217 }                                   \
1218 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1219 
1220 static ssize_t ipmi_type_show(struct device *dev,
1221                   struct device_attribute *attr,
1222                   char *buf)
1223 {
1224     return sysfs_emit(buf, "ssif\n");
1225 }
1226 static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1227 
1228 IPMI_SSIF_ATTR(sent_messages);
1229 IPMI_SSIF_ATTR(sent_messages_parts);
1230 IPMI_SSIF_ATTR(send_retries);
1231 IPMI_SSIF_ATTR(send_errors);
1232 IPMI_SSIF_ATTR(received_messages);
1233 IPMI_SSIF_ATTR(received_message_parts);
1234 IPMI_SSIF_ATTR(receive_retries);
1235 IPMI_SSIF_ATTR(receive_errors);
1236 IPMI_SSIF_ATTR(flag_fetches);
1237 IPMI_SSIF_ATTR(hosed);
1238 IPMI_SSIF_ATTR(events);
1239 IPMI_SSIF_ATTR(watchdog_pretimeouts);
1240 IPMI_SSIF_ATTR(alerts);
1241 
1242 static struct attribute *ipmi_ssif_dev_attrs[] = {
1243     &dev_attr_type.attr,
1244     &dev_attr_sent_messages.attr,
1245     &dev_attr_sent_messages_parts.attr,
1246     &dev_attr_send_retries.attr,
1247     &dev_attr_send_errors.attr,
1248     &dev_attr_received_messages.attr,
1249     &dev_attr_received_message_parts.attr,
1250     &dev_attr_receive_retries.attr,
1251     &dev_attr_receive_errors.attr,
1252     &dev_attr_flag_fetches.attr,
1253     &dev_attr_hosed.attr,
1254     &dev_attr_events.attr,
1255     &dev_attr_watchdog_pretimeouts.attr,
1256     &dev_attr_alerts.attr,
1257     NULL
1258 };
1259 
1260 static const struct attribute_group ipmi_ssif_dev_attr_group = {
1261     .attrs      = ipmi_ssif_dev_attrs,
1262 };
1263 
1264 static void shutdown_ssif(void *send_info)
1265 {
1266     struct ssif_info *ssif_info = send_info;
1267 
1268     device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1269     dev_set_drvdata(&ssif_info->client->dev, NULL);
1270 
1271     /* make sure the driver is not looking for flags any more. */
1272     while (ssif_info->ssif_state != SSIF_NORMAL)
1273         schedule_timeout(1);
1274 
1275     ssif_info->stopping = true;
1276     del_timer_sync(&ssif_info->watch_timer);
1277     del_timer_sync(&ssif_info->retry_timer);
1278     if (ssif_info->thread) {
1279         complete(&ssif_info->wake_thread);
1280         kthread_stop(ssif_info->thread);
1281     }
1282 }
1283 
1284 static int ssif_remove(struct i2c_client *client)
1285 {
1286     struct ssif_info *ssif_info = i2c_get_clientdata(client);
1287     struct ssif_addr_info *addr_info;
1288 
1289     if (!ssif_info)
1290         return 0;
1291 
1292     /*
1293      * After this point, we won't deliver anything asychronously
1294      * to the message handler.  We can unregister ourself.
1295      */
1296     ipmi_unregister_smi(ssif_info->intf);
1297 
1298     list_for_each_entry(addr_info, &ssif_infos, link) {
1299         if (addr_info->client == client) {
1300             addr_info->client = NULL;
1301             break;
1302         }
1303     }
1304 
1305     kfree(ssif_info);
1306 
1307     return 0;
1308 }
1309 
1310 static int read_response(struct i2c_client *client, unsigned char *resp)
1311 {
1312     int ret = -ENODEV, retry_cnt = SSIF_RECV_RETRIES;
1313 
1314     while (retry_cnt > 0) {
1315         ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1316                         resp);
1317         if (ret > 0)
1318             break;
1319         msleep(SSIF_MSG_MSEC);
1320         retry_cnt--;
1321         if (retry_cnt <= 0)
1322             break;
1323     }
1324 
1325     return ret;
1326 }
1327 
1328 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1329           int *resp_len, unsigned char *resp)
1330 {
1331     int retry_cnt;
1332     int ret;
1333 
1334     retry_cnt = SSIF_SEND_RETRIES;
1335  retry1:
1336     ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1337     if (ret) {
1338         retry_cnt--;
1339         if (retry_cnt > 0)
1340             goto retry1;
1341         return -ENODEV;
1342     }
1343 
1344     ret = read_response(client, resp);
1345     if (ret > 0) {
1346         /* Validate that the response is correct. */
1347         if (ret < 3 ||
1348             (resp[0] != (msg[0] | (1 << 2))) ||
1349             (resp[1] != msg[1]))
1350             ret = -EINVAL;
1351         else if (ret > IPMI_MAX_MSG_LENGTH) {
1352             ret = -E2BIG;
1353         } else {
1354             *resp_len = ret;
1355             ret = 0;
1356         }
1357     }
1358 
1359     return ret;
1360 }
1361 
1362 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1363 {
1364     unsigned char *resp;
1365     unsigned char msg[3];
1366     int           rv;
1367     int           len;
1368 
1369     resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1370     if (!resp)
1371         return -ENOMEM;
1372 
1373     /* Do a Get Device ID command, since it is required. */
1374     msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1375     msg[1] = IPMI_GET_DEVICE_ID_CMD;
1376     rv = do_cmd(client, 2, msg, &len, resp);
1377     if (rv)
1378         rv = -ENODEV;
1379     else
1380         strscpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1381     kfree(resp);
1382     return rv;
1383 }
1384 
1385 static int strcmp_nospace(char *s1, char *s2)
1386 {
1387     while (*s1 && *s2) {
1388         while (isspace(*s1))
1389             s1++;
1390         while (isspace(*s2))
1391             s2++;
1392         if (*s1 > *s2)
1393             return 1;
1394         if (*s1 < *s2)
1395             return -1;
1396         s1++;
1397         s2++;
1398     }
1399     return 0;
1400 }
1401 
1402 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1403                          char *adapter_name,
1404                          bool match_null_name)
1405 {
1406     struct ssif_addr_info *info, *found = NULL;
1407 
1408 restart:
1409     list_for_each_entry(info, &ssif_infos, link) {
1410         if (info->binfo.addr == addr) {
1411             if (info->addr_src == SI_SMBIOS)
1412                 info->adapter_name = kstrdup(adapter_name,
1413                                  GFP_KERNEL);
1414 
1415             if (info->adapter_name || adapter_name) {
1416                 if (!info->adapter_name != !adapter_name) {
1417                     /* One is NULL and one is not */
1418                     continue;
1419                 }
1420                 if (adapter_name &&
1421                     strcmp_nospace(info->adapter_name,
1422                            adapter_name))
1423                     /* Names do not match */
1424                     continue;
1425             }
1426             found = info;
1427             break;
1428         }
1429     }
1430 
1431     if (!found && match_null_name) {
1432         /* Try to get an exact match first, then try with a NULL name */
1433         adapter_name = NULL;
1434         match_null_name = false;
1435         goto restart;
1436     }
1437 
1438     return found;
1439 }
1440 
1441 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1442 {
1443 #ifdef CONFIG_ACPI
1444     acpi_handle acpi_handle;
1445 
1446     acpi_handle = ACPI_HANDLE(dev);
1447     if (acpi_handle) {
1448         ssif_info->addr_source = SI_ACPI;
1449         ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1450         request_module("acpi_ipmi");
1451         return true;
1452     }
1453 #endif
1454     return false;
1455 }
1456 
1457 static int find_slave_address(struct i2c_client *client, int slave_addr)
1458 {
1459 #ifdef CONFIG_IPMI_DMI_DECODE
1460     if (!slave_addr)
1461         slave_addr = ipmi_dmi_get_slave_addr(
1462             SI_TYPE_INVALID,
1463             i2c_adapter_id(client->adapter),
1464             client->addr);
1465 #endif
1466 
1467     return slave_addr;
1468 }
1469 
1470 static int start_multipart_test(struct i2c_client *client,
1471                 unsigned char *msg, bool do_middle)
1472 {
1473     int retry_cnt = SSIF_SEND_RETRIES, ret;
1474 
1475 retry_write:
1476     ret = i2c_smbus_write_block_data(client,
1477                      SSIF_IPMI_MULTI_PART_REQUEST_START,
1478                      32, msg);
1479     if (ret) {
1480         retry_cnt--;
1481         if (retry_cnt > 0)
1482             goto retry_write;
1483         dev_err(&client->dev, "Could not write multi-part start, though the BMC said it could handle it.  Just limit sends to one part.\n");
1484         return ret;
1485     }
1486 
1487     if (!do_middle)
1488         return 0;
1489 
1490     ret = i2c_smbus_write_block_data(client,
1491                      SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1492                      32, msg + 32);
1493     if (ret) {
1494         dev_err(&client->dev, "Could not write multi-part middle, though the BMC said it could handle it.  Just limit sends to one part.\n");
1495         return ret;
1496     }
1497 
1498     return 0;
1499 }
1500 
1501 static void test_multipart_messages(struct i2c_client *client,
1502                     struct ssif_info *ssif_info,
1503                     unsigned char *resp)
1504 {
1505     unsigned char msg[65];
1506     int ret;
1507     bool do_middle;
1508 
1509     if (ssif_info->max_xmit_msg_size <= 32)
1510         return;
1511 
1512     do_middle = ssif_info->max_xmit_msg_size > 63;
1513 
1514     memset(msg, 0, sizeof(msg));
1515     msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1516     msg[1] = IPMI_GET_DEVICE_ID_CMD;
1517 
1518     /*
1519      * The specification is all messed up dealing with sending
1520      * multi-part messages.  Per what the specification says, it
1521      * is impossible to send a message that is a multiple of 32
1522      * bytes, except for 32 itself.  It talks about a "start"
1523      * transaction (cmd=6) that must be 32 bytes, "middle"
1524      * transaction (cmd=7) that must be 32 bytes, and an "end"
1525      * transaction.  The "end" transaction is shown as cmd=7 in
1526      * the text, but if that's the case there is no way to
1527      * differentiate between a middle and end part except the
1528      * length being less than 32.  But there is a table at the far
1529      * end of the section (that I had never noticed until someone
1530      * pointed it out to me) that mentions it as cmd=8.
1531      *
1532      * After some thought, I think the example is wrong and the
1533      * end transaction should be cmd=8.  But some systems don't
1534      * implement cmd=8, they use a zero-length end transaction,
1535      * even though that violates the SMBus specification.
1536      *
1537      * So, to work around this, this code tests if cmd=8 works.
1538      * If it does, then we use that.  If not, it tests zero-
1539      * byte end transactions.  If that works, good.  If not,
1540      * we only allow 63-byte transactions max.
1541      */
1542 
1543     ret = start_multipart_test(client, msg, do_middle);
1544     if (ret)
1545         goto out_no_multi_part;
1546 
1547     ret = i2c_smbus_write_block_data(client,
1548                      SSIF_IPMI_MULTI_PART_REQUEST_END,
1549                      1, msg + 64);
1550 
1551     if (!ret)
1552         ret = read_response(client, resp);
1553 
1554     if (ret > 0) {
1555         /* End transactions work, we are good. */
1556         ssif_info->cmd8_works = true;
1557         return;
1558     }
1559 
1560     ret = start_multipart_test(client, msg, do_middle);
1561     if (ret) {
1562         dev_err(&client->dev, "Second multipart test failed.\n");
1563         goto out_no_multi_part;
1564     }
1565 
1566     ret = i2c_smbus_write_block_data(client,
1567                      SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
1568                      0, msg + 64);
1569     if (!ret)
1570         ret = read_response(client, resp);
1571     if (ret > 0)
1572         /* Zero-size end parts work, use those. */
1573         return;
1574 
1575     /* Limit to 63 bytes and use a short middle command to mark the end. */
1576     if (ssif_info->max_xmit_msg_size > 63)
1577         ssif_info->max_xmit_msg_size = 63;
1578     return;
1579 
1580 out_no_multi_part:
1581     ssif_info->max_xmit_msg_size = 32;
1582     return;
1583 }
1584 
1585 /*
1586  * Global enables we care about.
1587  */
1588 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1589                  IPMI_BMC_EVT_MSG_INTR)
1590 
1591 static void ssif_remove_dup(struct i2c_client *client)
1592 {
1593     struct ssif_info *ssif_info = i2c_get_clientdata(client);
1594 
1595     ipmi_unregister_smi(ssif_info->intf);
1596     kfree(ssif_info);
1597 }
1598 
1599 static int ssif_add_infos(struct i2c_client *client)
1600 {
1601     struct ssif_addr_info *info;
1602 
1603     info = kzalloc(sizeof(*info), GFP_KERNEL);
1604     if (!info)
1605         return -ENOMEM;
1606     info->addr_src = SI_ACPI;
1607     info->client = client;
1608     info->adapter_name = kstrdup(client->adapter->name, GFP_KERNEL);
1609     info->binfo.addr = client->addr;
1610     list_add_tail(&info->link, &ssif_infos);
1611     return 0;
1612 }
1613 
1614 /*
1615  * Prefer ACPI over SMBIOS, if both are available.
1616  * So if we get an ACPI interface and have already registered a SMBIOS
1617  * interface at the same address, remove the SMBIOS and add the ACPI one.
1618  */
1619 static int ssif_check_and_remove(struct i2c_client *client,
1620                   struct ssif_info *ssif_info)
1621 {
1622     struct ssif_addr_info *info;
1623 
1624     list_for_each_entry(info, &ssif_infos, link) {
1625         if (!info->client)
1626             return 0;
1627         if (!strcmp(info->adapter_name, client->adapter->name) &&
1628             info->binfo.addr == client->addr) {
1629             if (info->addr_src == SI_ACPI)
1630                 return -EEXIST;
1631 
1632             if (ssif_info->addr_source == SI_ACPI &&
1633                 info->addr_src == SI_SMBIOS) {
1634                 dev_info(&client->dev,
1635                      "Removing %s-specified SSIF interface in favor of ACPI\n",
1636                      ipmi_addr_src_to_str(info->addr_src));
1637                 ssif_remove_dup(info->client);
1638                 return 0;
1639             }
1640         }
1641     }
1642     return 0;
1643 }
1644 
1645 static int ssif_probe(struct i2c_client *client)
1646 {
1647     unsigned char     msg[3];
1648     unsigned char     *resp;
1649     struct ssif_info   *ssif_info;
1650     int               rv = 0;
1651     int               len = 0;
1652     int               i;
1653     u8        slave_addr = 0;
1654     struct ssif_addr_info *addr_info = NULL;
1655 
1656     mutex_lock(&ssif_infos_mutex);
1657     resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1658     if (!resp) {
1659         mutex_unlock(&ssif_infos_mutex);
1660         return -ENOMEM;
1661     }
1662 
1663     ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1664     if (!ssif_info) {
1665         kfree(resp);
1666         mutex_unlock(&ssif_infos_mutex);
1667         return -ENOMEM;
1668     }
1669 
1670     if (!check_acpi(ssif_info, &client->dev)) {
1671         addr_info = ssif_info_find(client->addr, client->adapter->name,
1672                        true);
1673         if (!addr_info) {
1674             /* Must have come in through sysfs. */
1675             ssif_info->addr_source = SI_HOTMOD;
1676         } else {
1677             ssif_info->addr_source = addr_info->addr_src;
1678             ssif_info->ssif_debug = addr_info->debug;
1679             ssif_info->addr_info = addr_info->addr_info;
1680             addr_info->client = client;
1681             slave_addr = addr_info->slave_addr;
1682         }
1683     }
1684 
1685     ssif_info->client = client;
1686     i2c_set_clientdata(client, ssif_info);
1687 
1688     rv = ssif_check_and_remove(client, ssif_info);
1689     /* If rv is 0 and addr source is not SI_ACPI, continue probing */
1690     if (!rv && ssif_info->addr_source == SI_ACPI) {
1691         rv = ssif_add_infos(client);
1692         if (rv) {
1693             dev_err(&client->dev, "Out of memory!, exiting ..\n");
1694             goto out;
1695         }
1696     } else if (rv) {
1697         dev_err(&client->dev, "Not probing, Interface already present\n");
1698         goto out;
1699     }
1700 
1701     slave_addr = find_slave_address(client, slave_addr);
1702 
1703     dev_info(&client->dev,
1704          "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1705         ipmi_addr_src_to_str(ssif_info->addr_source),
1706         client->addr, client->adapter->name, slave_addr);
1707 
1708     /* Now check for system interface capabilities */
1709     msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1710     msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1711     msg[2] = 0; /* SSIF */
1712     rv = do_cmd(client, 3, msg, &len, resp);
1713     if (!rv && (len >= 3) && (resp[2] == 0)) {
1714         if (len < 7) {
1715             if (ssif_dbg_probe)
1716                 dev_dbg(&ssif_info->client->dev,
1717                     "SSIF info too short: %d\n", len);
1718             goto no_support;
1719         }
1720 
1721         /* Got a good SSIF response, handle it. */
1722         ssif_info->max_xmit_msg_size = resp[5];
1723         ssif_info->max_recv_msg_size = resp[6];
1724         ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1725         ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1726 
1727         /* Sanitize the data */
1728         switch (ssif_info->multi_support) {
1729         case SSIF_NO_MULTI:
1730             if (ssif_info->max_xmit_msg_size > 32)
1731                 ssif_info->max_xmit_msg_size = 32;
1732             if (ssif_info->max_recv_msg_size > 32)
1733                 ssif_info->max_recv_msg_size = 32;
1734             break;
1735 
1736         case SSIF_MULTI_2_PART:
1737             if (ssif_info->max_xmit_msg_size > 63)
1738                 ssif_info->max_xmit_msg_size = 63;
1739             if (ssif_info->max_recv_msg_size > 62)
1740                 ssif_info->max_recv_msg_size = 62;
1741             break;
1742 
1743         case SSIF_MULTI_n_PART:
1744             /* We take whatever size given, but do some testing. */
1745             break;
1746 
1747         default:
1748             /* Data is not sane, just give up. */
1749             goto no_support;
1750         }
1751     } else {
1752  no_support:
1753         /* Assume no multi-part or PEC support */
1754         dev_info(&ssif_info->client->dev,
1755              "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1756             rv, len, resp[2]);
1757 
1758         ssif_info->max_xmit_msg_size = 32;
1759         ssif_info->max_recv_msg_size = 32;
1760         ssif_info->multi_support = SSIF_NO_MULTI;
1761         ssif_info->supports_pec = 0;
1762     }
1763 
1764     test_multipart_messages(client, ssif_info, resp);
1765 
1766     /* Make sure the NMI timeout is cleared. */
1767     msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1768     msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1769     msg[2] = WDT_PRE_TIMEOUT_INT;
1770     rv = do_cmd(client, 3, msg, &len, resp);
1771     if (rv || (len < 3) || (resp[2] != 0))
1772         dev_warn(&ssif_info->client->dev,
1773              "Unable to clear message flags: %d %d %2.2x\n",
1774              rv, len, resp[2]);
1775 
1776     /* Attempt to enable the event buffer. */
1777     msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1778     msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1779     rv = do_cmd(client, 2, msg, &len, resp);
1780     if (rv || (len < 4) || (resp[2] != 0)) {
1781         dev_warn(&ssif_info->client->dev,
1782              "Error getting global enables: %d %d %2.2x\n",
1783              rv, len, resp[2]);
1784         rv = 0; /* Not fatal */
1785         goto found;
1786     }
1787 
1788     ssif_info->global_enables = resp[3];
1789 
1790     if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1791         ssif_info->has_event_buffer = true;
1792         /* buffer is already enabled, nothing to do. */
1793         goto found;
1794     }
1795 
1796     msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1797     msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1798     msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1799     rv = do_cmd(client, 3, msg, &len, resp);
1800     if (rv || (len < 2)) {
1801         dev_warn(&ssif_info->client->dev,
1802              "Error setting global enables: %d %d %2.2x\n",
1803              rv, len, resp[2]);
1804         rv = 0; /* Not fatal */
1805         goto found;
1806     }
1807 
1808     if (resp[2] == 0) {
1809         /* A successful return means the event buffer is supported. */
1810         ssif_info->has_event_buffer = true;
1811         ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1812     }
1813 
1814     /* Some systems don't behave well if you enable alerts. */
1815     if (alerts_broken)
1816         goto found;
1817 
1818     msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1819     msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1820     msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1821     rv = do_cmd(client, 3, msg, &len, resp);
1822     if (rv || (len < 2)) {
1823         dev_warn(&ssif_info->client->dev,
1824              "Error setting global enables: %d %d %2.2x\n",
1825              rv, len, resp[2]);
1826         rv = 0; /* Not fatal */
1827         goto found;
1828     }
1829 
1830     if (resp[2] == 0) {
1831         /* A successful return means the alert is supported. */
1832         ssif_info->supports_alert = true;
1833         ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1834     }
1835 
1836  found:
1837     if (ssif_dbg_probe) {
1838         dev_dbg(&ssif_info->client->dev,
1839                "%s: i2c_probe found device at i2c address %x\n",
1840                __func__, client->addr);
1841     }
1842 
1843     spin_lock_init(&ssif_info->lock);
1844     ssif_info->ssif_state = SSIF_NORMAL;
1845     timer_setup(&ssif_info->retry_timer, retry_timeout, 0);
1846     timer_setup(&ssif_info->watch_timer, watch_timeout, 0);
1847 
1848     for (i = 0; i < SSIF_NUM_STATS; i++)
1849         atomic_set(&ssif_info->stats[i], 0);
1850 
1851     if (ssif_info->supports_pec)
1852         ssif_info->client->flags |= I2C_CLIENT_PEC;
1853 
1854     ssif_info->handlers.owner = THIS_MODULE;
1855     ssif_info->handlers.start_processing = ssif_start_processing;
1856     ssif_info->handlers.shutdown = shutdown_ssif;
1857     ssif_info->handlers.get_smi_info = get_smi_info;
1858     ssif_info->handlers.sender = sender;
1859     ssif_info->handlers.request_events = request_events;
1860     ssif_info->handlers.set_need_watch = ssif_set_need_watch;
1861 
1862     {
1863         unsigned int thread_num;
1864 
1865         thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1866                    << 8) |
1867                   ssif_info->client->addr);
1868         init_completion(&ssif_info->wake_thread);
1869         ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1870                            "kssif%4.4x", thread_num);
1871         if (IS_ERR(ssif_info->thread)) {
1872             rv = PTR_ERR(ssif_info->thread);
1873             dev_notice(&ssif_info->client->dev,
1874                    "Could not start kernel thread: error %d\n",
1875                    rv);
1876             goto out;
1877         }
1878     }
1879 
1880     dev_set_drvdata(&ssif_info->client->dev, ssif_info);
1881     rv = device_add_group(&ssif_info->client->dev,
1882                   &ipmi_ssif_dev_attr_group);
1883     if (rv) {
1884         dev_err(&ssif_info->client->dev,
1885             "Unable to add device attributes: error %d\n",
1886             rv);
1887         goto out;
1888     }
1889 
1890     rv = ipmi_register_smi(&ssif_info->handlers,
1891                    ssif_info,
1892                    &ssif_info->client->dev,
1893                    slave_addr);
1894     if (rv) {
1895         dev_err(&ssif_info->client->dev,
1896             "Unable to register device: error %d\n", rv);
1897         goto out_remove_attr;
1898     }
1899 
1900  out:
1901     if (rv) {
1902         if (addr_info)
1903             addr_info->client = NULL;
1904 
1905         dev_err(&ssif_info->client->dev,
1906             "Unable to start IPMI SSIF: %d\n", rv);
1907         i2c_set_clientdata(client, NULL);
1908         kfree(ssif_info);
1909     }
1910     kfree(resp);
1911     mutex_unlock(&ssif_infos_mutex);
1912     return rv;
1913 
1914 out_remove_attr:
1915     device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1916     dev_set_drvdata(&ssif_info->client->dev, NULL);
1917     goto out;
1918 }
1919 
1920 static int new_ssif_client(int addr, char *adapter_name,
1921                int debug, int slave_addr,
1922                enum ipmi_addr_src addr_src,
1923                struct device *dev)
1924 {
1925     struct ssif_addr_info *addr_info;
1926     int rv = 0;
1927 
1928     mutex_lock(&ssif_infos_mutex);
1929     if (ssif_info_find(addr, adapter_name, false)) {
1930         rv = -EEXIST;
1931         goto out_unlock;
1932     }
1933 
1934     addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1935     if (!addr_info) {
1936         rv = -ENOMEM;
1937         goto out_unlock;
1938     }
1939 
1940     if (adapter_name) {
1941         addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1942         if (!addr_info->adapter_name) {
1943             kfree(addr_info);
1944             rv = -ENOMEM;
1945             goto out_unlock;
1946         }
1947     }
1948 
1949     strncpy(addr_info->binfo.type, DEVICE_NAME,
1950         sizeof(addr_info->binfo.type));
1951     addr_info->binfo.addr = addr;
1952     addr_info->binfo.platform_data = addr_info;
1953     addr_info->debug = debug;
1954     addr_info->slave_addr = slave_addr;
1955     addr_info->addr_src = addr_src;
1956     addr_info->dev = dev;
1957 
1958     if (dev)
1959         dev_set_drvdata(dev, addr_info);
1960 
1961     list_add_tail(&addr_info->link, &ssif_infos);
1962 
1963     /* Address list will get it */
1964 
1965 out_unlock:
1966     mutex_unlock(&ssif_infos_mutex);
1967     return rv;
1968 }
1969 
1970 static void free_ssif_clients(void)
1971 {
1972     struct ssif_addr_info *info, *tmp;
1973 
1974     mutex_lock(&ssif_infos_mutex);
1975     list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1976         list_del(&info->link);
1977         kfree(info->adapter_name);
1978         kfree(info);
1979     }
1980     mutex_unlock(&ssif_infos_mutex);
1981 }
1982 
1983 static unsigned short *ssif_address_list(void)
1984 {
1985     struct ssif_addr_info *info;
1986     unsigned int count = 0, i = 0;
1987     unsigned short *address_list;
1988 
1989     list_for_each_entry(info, &ssif_infos, link)
1990         count++;
1991 
1992     address_list = kcalloc(count + 1, sizeof(*address_list),
1993                    GFP_KERNEL);
1994     if (!address_list)
1995         return NULL;
1996 
1997     list_for_each_entry(info, &ssif_infos, link) {
1998         unsigned short addr = info->binfo.addr;
1999         int j;
2000 
2001         for (j = 0; j < i; j++) {
2002             if (address_list[j] == addr)
2003                 /* Found a dup. */
2004                 break;
2005         }
2006         if (j == i) /* Didn't find it in the list. */
2007             address_list[i++] = addr;
2008     }
2009     address_list[i] = I2C_CLIENT_END;
2010 
2011     return address_list;
2012 }
2013 
2014 #ifdef CONFIG_ACPI
2015 static const struct acpi_device_id ssif_acpi_match[] = {
2016     { "IPI0001", 0 },
2017     { },
2018 };
2019 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
2020 #endif
2021 
2022 #ifdef CONFIG_DMI
2023 static int dmi_ipmi_probe(struct platform_device *pdev)
2024 {
2025     u8 slave_addr = 0;
2026     u16 i2c_addr;
2027     int rv;
2028 
2029     if (!ssif_trydmi)
2030         return -ENODEV;
2031 
2032     rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
2033     if (rv) {
2034         dev_warn(&pdev->dev, "No i2c-addr property\n");
2035         return -ENODEV;
2036     }
2037 
2038     rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
2039     if (rv)
2040         slave_addr = 0x20;
2041 
2042     return new_ssif_client(i2c_addr, NULL, 0,
2043                    slave_addr, SI_SMBIOS, &pdev->dev);
2044 }
2045 #else
2046 static int dmi_ipmi_probe(struct platform_device *pdev)
2047 {
2048     return -ENODEV;
2049 }
2050 #endif
2051 
2052 static const struct i2c_device_id ssif_id[] = {
2053     { DEVICE_NAME, 0 },
2054     { }
2055 };
2056 MODULE_DEVICE_TABLE(i2c, ssif_id);
2057 
2058 static struct i2c_driver ssif_i2c_driver = {
2059     .class      = I2C_CLASS_HWMON,
2060     .driver     = {
2061         .name           = DEVICE_NAME
2062     },
2063     .probe_new  = ssif_probe,
2064     .remove     = ssif_remove,
2065     .alert      = ssif_alert,
2066     .id_table   = ssif_id,
2067     .detect     = ssif_detect
2068 };
2069 
2070 static int ssif_platform_probe(struct platform_device *dev)
2071 {
2072     return dmi_ipmi_probe(dev);
2073 }
2074 
2075 static int ssif_platform_remove(struct platform_device *dev)
2076 {
2077     struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2078 
2079     if (!addr_info)
2080         return 0;
2081 
2082     mutex_lock(&ssif_infos_mutex);
2083     list_del(&addr_info->link);
2084     kfree(addr_info);
2085     mutex_unlock(&ssif_infos_mutex);
2086     return 0;
2087 }
2088 
2089 static const struct platform_device_id ssif_plat_ids[] = {
2090     { "dmi-ipmi-ssif", 0 },
2091     { }
2092 };
2093 
2094 static struct platform_driver ipmi_driver = {
2095     .driver = {
2096         .name = DEVICE_NAME,
2097     },
2098     .probe      = ssif_platform_probe,
2099     .remove     = ssif_platform_remove,
2100     .id_table       = ssif_plat_ids
2101 };
2102 
2103 static int init_ipmi_ssif(void)
2104 {
2105     int i;
2106     int rv;
2107 
2108     if (initialized)
2109         return 0;
2110 
2111     pr_info("IPMI SSIF Interface driver\n");
2112 
2113     /* build list for i2c from addr list */
2114     for (i = 0; i < num_addrs; i++) {
2115         rv = new_ssif_client(addr[i], adapter_name[i],
2116                      dbg[i], slave_addrs[i],
2117                      SI_HARDCODED, NULL);
2118         if (rv)
2119             pr_err("Couldn't add hardcoded device at addr 0x%x\n",
2120                    addr[i]);
2121     }
2122 
2123     if (ssif_tryacpi)
2124         ssif_i2c_driver.driver.acpi_match_table =
2125             ACPI_PTR(ssif_acpi_match);
2126 
2127     if (ssif_trydmi) {
2128         rv = platform_driver_register(&ipmi_driver);
2129         if (rv)
2130             pr_err("Unable to register driver: %d\n", rv);
2131         else
2132             platform_registered = true;
2133     }
2134 
2135     ssif_i2c_driver.address_list = ssif_address_list();
2136 
2137     rv = i2c_add_driver(&ssif_i2c_driver);
2138     if (!rv)
2139         initialized = true;
2140 
2141     return rv;
2142 }
2143 module_init(init_ipmi_ssif);
2144 
2145 static void cleanup_ipmi_ssif(void)
2146 {
2147     if (!initialized)
2148         return;
2149 
2150     initialized = false;
2151 
2152     i2c_del_driver(&ssif_i2c_driver);
2153 
2154     kfree(ssif_i2c_driver.address_list);
2155 
2156     if (ssif_trydmi && platform_registered)
2157         platform_driver_unregister(&ipmi_driver);
2158 
2159     free_ssif_clients();
2160 }
2161 module_exit(cleanup_ipmi_ssif);
2162 
2163 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2164 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2165 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2166 MODULE_LICENSE("GPL");