Merge tag 'pci-v4.15-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[linux-2.6-microblaze.git] / drivers / char / ipmi / ipmi_ssif.c
1 /*
2  * ipmi_ssif.c
3  *
4  * The interface to the IPMI driver for SMBus access to a SMBus
5  * compliant device.  Called SSIF by the IPMI spec.
6  *
7  * Author: Intel Corporation
8  *         Todd Davis <todd.c.davis@intel.com>
9  *
10  * Rewritten by Corey Minyard <minyard@acm.org> to support the
11  * non-blocking I2C interface, add support for multi-part
12  * transactions, add PEC support, and general clenaup.
13  *
14  * Copyright 2003 Intel Corporation
15  * Copyright 2005 MontaVista Software
16  *
17  *  This program is free software; you can redistribute it and/or modify it
18  *  under the terms of the GNU General Public License as published by the
19  *  Free Software Foundation; either version 2 of the License, or (at your
20  *  option) any later version.
21  */
22
23 /*
24  * This file holds the "policy" for the interface to the SSIF state
25  * machine.  It does the configuration, handles timers and interrupts,
26  * and drives the real SSIF state machine.
27  */
28
29 /*
30  * TODO: Figure out how to use SMB alerts.  This will require a new
31  * interface into the I2C driver, I believe.
32  */
33
34 #if defined(MODVERSIONS)
35 #include <linux/modversions.h>
36 #endif
37
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/sched.h>
41 #include <linux/seq_file.h>
42 #include <linux/timer.h>
43 #include <linux/delay.h>
44 #include <linux/errno.h>
45 #include <linux/spinlock.h>
46 #include <linux/slab.h>
47 #include <linux/list.h>
48 #include <linux/i2c.h>
49 #include <linux/ipmi_smi.h>
50 #include <linux/init.h>
51 #include <linux/dmi.h>
52 #include <linux/kthread.h>
53 #include <linux/acpi.h>
54 #include <linux/ctype.h>
55 #include <linux/time64.h>
56 #include "ipmi_dmi.h"
57
58 #define PFX "ipmi_ssif: "
59 #define DEVICE_NAME "ipmi_ssif"
60
61 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD      0x57
62
63 #define SSIF_IPMI_REQUEST                       2
64 #define SSIF_IPMI_MULTI_PART_REQUEST_START      6
65 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE     7
66 #define SSIF_IPMI_RESPONSE                      3
67 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE    9
68
69 /* ssif_debug is a bit-field
70  *      SSIF_DEBUG_MSG -        commands and their responses
71  *      SSIF_DEBUG_STATES -     message states
72  *      SSIF_DEBUG_TIMING -      Measure times between events in the driver
73  */
74 #define SSIF_DEBUG_TIMING       4
75 #define SSIF_DEBUG_STATE        2
76 #define SSIF_DEBUG_MSG          1
77 #define SSIF_NODEBUG            0
78 #define SSIF_DEFAULT_DEBUG      (SSIF_NODEBUG)
79
80 /*
81  * Timer values
82  */
83 #define SSIF_MSG_USEC           20000   /* 20ms between message tries. */
84 #define SSIF_MSG_PART_USEC      5000    /* 5ms for a message part */
85
86 /* How many times to we retry sending/receiving the message. */
87 #define SSIF_SEND_RETRIES       5
88 #define SSIF_RECV_RETRIES       250
89
90 #define SSIF_MSG_MSEC           (SSIF_MSG_USEC / 1000)
91 #define SSIF_MSG_JIFFIES        ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
92 #define SSIF_MSG_PART_JIFFIES   ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
93
94 enum ssif_intf_state {
95         SSIF_NORMAL,
96         SSIF_GETTING_FLAGS,
97         SSIF_GETTING_EVENTS,
98         SSIF_CLEARING_FLAGS,
99         SSIF_GETTING_MESSAGES,
100         /* FIXME - add watchdog stuff. */
101 };
102
103 #define SSIF_IDLE(ssif)  ((ssif)->ssif_state == SSIF_NORMAL \
104                           && (ssif)->curr_msg == NULL)
105
106 /*
107  * Indexes into stats[] in ssif_info below.
108  */
109 enum ssif_stat_indexes {
110         /* Number of total messages sent. */
111         SSIF_STAT_sent_messages = 0,
112
113         /*
114          * Number of message parts sent.  Messages may be broken into
115          * parts if they are long.
116          */
117         SSIF_STAT_sent_messages_parts,
118
119         /*
120          * Number of time a message was retried.
121          */
122         SSIF_STAT_send_retries,
123
124         /*
125          * Number of times the send of a message failed.
126          */
127         SSIF_STAT_send_errors,
128
129         /*
130          * Number of message responses received.
131          */
132         SSIF_STAT_received_messages,
133
134         /*
135          * Number of message fragments received.
136          */
137         SSIF_STAT_received_message_parts,
138
139         /*
140          * Number of times the receive of a message was retried.
141          */
142         SSIF_STAT_receive_retries,
143
144         /*
145          * Number of errors receiving messages.
146          */
147         SSIF_STAT_receive_errors,
148
149         /*
150          * Number of times a flag fetch was requested.
151          */
152         SSIF_STAT_flag_fetches,
153
154         /*
155          * Number of times the hardware didn't follow the state machine.
156          */
157         SSIF_STAT_hosed,
158
159         /*
160          * Number of received events.
161          */
162         SSIF_STAT_events,
163
164         /* Number of asyncronous messages received. */
165         SSIF_STAT_incoming_messages,
166
167         /* Number of watchdog pretimeouts. */
168         SSIF_STAT_watchdog_pretimeouts,
169
170         /* Number of alers received. */
171         SSIF_STAT_alerts,
172
173         /* Always add statistics before this value, it must be last. */
174         SSIF_NUM_STATS
175 };
176
177 struct ssif_addr_info {
178         struct i2c_board_info binfo;
179         char *adapter_name;
180         int debug;
181         int slave_addr;
182         enum ipmi_addr_src addr_src;
183         union ipmi_smi_info_union addr_info;
184         struct device *dev;
185         struct i2c_client *client;
186
187         struct mutex clients_mutex;
188         struct list_head clients;
189
190         struct list_head link;
191 };
192
193 struct ssif_info;
194
195 typedef void (*ssif_i2c_done)(struct ssif_info *ssif_info, int result,
196                              unsigned char *data, unsigned int len);
197
198 struct ssif_info {
199         ipmi_smi_t          intf;
200         int                 intf_num;
201         spinlock_t          lock;
202         struct ipmi_smi_msg *waiting_msg;
203         struct ipmi_smi_msg *curr_msg;
204         enum ssif_intf_state ssif_state;
205         unsigned long       ssif_debug;
206
207         struct ipmi_smi_handlers handlers;
208
209         enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
210         union ipmi_smi_info_union addr_info;
211
212         /*
213          * Flags from the last GET_MSG_FLAGS command, used when an ATTN
214          * is set to hold the flags until we are done handling everything
215          * from the flags.
216          */
217 #define RECEIVE_MSG_AVAIL       0x01
218 #define EVENT_MSG_BUFFER_FULL   0x02
219 #define WDT_PRE_TIMEOUT_INT     0x08
220         unsigned char       msg_flags;
221
222         u8                  global_enables;
223         bool                has_event_buffer;
224         bool                supports_alert;
225
226         /*
227          * Used to tell what we should do with alerts.  If we are
228          * waiting on a response, read the data immediately.
229          */
230         bool                got_alert;
231         bool                waiting_alert;
232
233         /*
234          * If set to true, this will request events the next time the
235          * state machine is idle.
236          */
237         bool                req_events;
238
239         /*
240          * If set to true, this will request flags the next time the
241          * state machine is idle.
242          */
243         bool                req_flags;
244
245         /*
246          * Used to perform timer operations when run-to-completion
247          * mode is on.  This is a countdown timer.
248          */
249         int                 rtc_us_timer;
250
251         /* Used for sending/receiving data.  +1 for the length. */
252         unsigned char data[IPMI_MAX_MSG_LENGTH + 1];
253         unsigned int  data_len;
254
255         /* Temp receive buffer, gets copied into data. */
256         unsigned char recv[I2C_SMBUS_BLOCK_MAX];
257
258         struct i2c_client *client;
259         ssif_i2c_done done_handler;
260
261         /* Thread interface handling */
262         struct task_struct *thread;
263         struct completion wake_thread;
264         bool stopping;
265         int i2c_read_write;
266         int i2c_command;
267         unsigned char *i2c_data;
268         unsigned int i2c_size;
269
270         /* From the device id response. */
271         struct ipmi_device_id device_id;
272
273         struct timer_list retry_timer;
274         int retries_left;
275
276         /* Info from SSIF cmd */
277         unsigned char max_xmit_msg_size;
278         unsigned char max_recv_msg_size;
279         unsigned int  multi_support;
280         int           supports_pec;
281
282 #define SSIF_NO_MULTI           0
283 #define SSIF_MULTI_2_PART       1
284 #define SSIF_MULTI_n_PART       2
285         unsigned char *multi_data;
286         unsigned int  multi_len;
287         unsigned int  multi_pos;
288
289         atomic_t stats[SSIF_NUM_STATS];
290 };
291
292 #define ssif_inc_stat(ssif, stat) \
293         atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
294 #define ssif_get_stat(ssif, stat) \
295         ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
296
297 static bool initialized;
298
299 static atomic_t next_intf = ATOMIC_INIT(0);
300
301 static void return_hosed_msg(struct ssif_info *ssif_info,
302                              struct ipmi_smi_msg *msg);
303 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags);
304 static int start_send(struct ssif_info *ssif_info,
305                       unsigned char   *data,
306                       unsigned int    len);
307
308 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info *ssif_info,
309                                           unsigned long *flags)
310 {
311         spin_lock_irqsave(&ssif_info->lock, *flags);
312         return flags;
313 }
314
315 static void ipmi_ssif_unlock_cond(struct ssif_info *ssif_info,
316                                   unsigned long *flags)
317 {
318         spin_unlock_irqrestore(&ssif_info->lock, *flags);
319 }
320
321 static void deliver_recv_msg(struct ssif_info *ssif_info,
322                              struct ipmi_smi_msg *msg)
323 {
324         ipmi_smi_t    intf = ssif_info->intf;
325
326         if (!intf) {
327                 ipmi_free_smi_msg(msg);
328         } else if (msg->rsp_size < 0) {
329                 return_hosed_msg(ssif_info, msg);
330                 pr_err(PFX
331                        "Malformed message in deliver_recv_msg: rsp_size = %d\n",
332                        msg->rsp_size);
333         } else {
334                 ipmi_smi_msg_received(intf, msg);
335         }
336 }
337
338 static void return_hosed_msg(struct ssif_info *ssif_info,
339                              struct ipmi_smi_msg *msg)
340 {
341         ssif_inc_stat(ssif_info, hosed);
342
343         /* Make it a response */
344         msg->rsp[0] = msg->data[0] | 4;
345         msg->rsp[1] = msg->data[1];
346         msg->rsp[2] = 0xFF; /* Unknown error. */
347         msg->rsp_size = 3;
348
349         deliver_recv_msg(ssif_info, msg);
350 }
351
352 /*
353  * Must be called with the message lock held.  This will release the
354  * message lock.  Note that the caller will check SSIF_IDLE and start a
355  * new operation, so there is no need to check for new messages to
356  * start in here.
357  */
358 static void start_clear_flags(struct ssif_info *ssif_info, unsigned long *flags)
359 {
360         unsigned char msg[3];
361
362         ssif_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
363         ssif_info->ssif_state = SSIF_CLEARING_FLAGS;
364         ipmi_ssif_unlock_cond(ssif_info, flags);
365
366         /* Make sure the watchdog pre-timeout flag is not set at startup. */
367         msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
368         msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
369         msg[2] = WDT_PRE_TIMEOUT_INT;
370
371         if (start_send(ssif_info, msg, 3) != 0) {
372                 /* Error, just go to normal state. */
373                 ssif_info->ssif_state = SSIF_NORMAL;
374         }
375 }
376
377 static void start_flag_fetch(struct ssif_info *ssif_info, unsigned long *flags)
378 {
379         unsigned char mb[2];
380
381         ssif_info->req_flags = false;
382         ssif_info->ssif_state = SSIF_GETTING_FLAGS;
383         ipmi_ssif_unlock_cond(ssif_info, flags);
384
385         mb[0] = (IPMI_NETFN_APP_REQUEST << 2);
386         mb[1] = IPMI_GET_MSG_FLAGS_CMD;
387         if (start_send(ssif_info, mb, 2) != 0)
388                 ssif_info->ssif_state = SSIF_NORMAL;
389 }
390
391 static void check_start_send(struct ssif_info *ssif_info, unsigned long *flags,
392                              struct ipmi_smi_msg *msg)
393 {
394         if (start_send(ssif_info, msg->data, msg->data_size) != 0) {
395                 unsigned long oflags;
396
397                 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
398                 ssif_info->curr_msg = NULL;
399                 ssif_info->ssif_state = SSIF_NORMAL;
400                 ipmi_ssif_unlock_cond(ssif_info, flags);
401                 ipmi_free_smi_msg(msg);
402         }
403 }
404
405 static void start_event_fetch(struct ssif_info *ssif_info, unsigned long *flags)
406 {
407         struct ipmi_smi_msg *msg;
408
409         ssif_info->req_events = false;
410
411         msg = ipmi_alloc_smi_msg();
412         if (!msg) {
413                 ssif_info->ssif_state = SSIF_NORMAL;
414                 ipmi_ssif_unlock_cond(ssif_info, flags);
415                 return;
416         }
417
418         ssif_info->curr_msg = msg;
419         ssif_info->ssif_state = SSIF_GETTING_EVENTS;
420         ipmi_ssif_unlock_cond(ssif_info, flags);
421
422         msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
423         msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
424         msg->data_size = 2;
425
426         check_start_send(ssif_info, flags, msg);
427 }
428
429 static void start_recv_msg_fetch(struct ssif_info *ssif_info,
430                                  unsigned long *flags)
431 {
432         struct ipmi_smi_msg *msg;
433
434         msg = ipmi_alloc_smi_msg();
435         if (!msg) {
436                 ssif_info->ssif_state = SSIF_NORMAL;
437                 ipmi_ssif_unlock_cond(ssif_info, flags);
438                 return;
439         }
440
441         ssif_info->curr_msg = msg;
442         ssif_info->ssif_state = SSIF_GETTING_MESSAGES;
443         ipmi_ssif_unlock_cond(ssif_info, flags);
444
445         msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
446         msg->data[1] = IPMI_GET_MSG_CMD;
447         msg->data_size = 2;
448
449         check_start_send(ssif_info, flags, msg);
450 }
451
452 /*
453  * Must be called with the message lock held.  This will release the
454  * message lock.  Note that the caller will check SSIF_IDLE and start a
455  * new operation, so there is no need to check for new messages to
456  * start in here.
457  */
458 static void handle_flags(struct ssif_info *ssif_info, unsigned long *flags)
459 {
460         if (ssif_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
461                 ipmi_smi_t intf = ssif_info->intf;
462                 /* Watchdog pre-timeout */
463                 ssif_inc_stat(ssif_info, watchdog_pretimeouts);
464                 start_clear_flags(ssif_info, flags);
465                 if (intf)
466                         ipmi_smi_watchdog_pretimeout(intf);
467         } else if (ssif_info->msg_flags & RECEIVE_MSG_AVAIL)
468                 /* Messages available. */
469                 start_recv_msg_fetch(ssif_info, flags);
470         else if (ssif_info->msg_flags & EVENT_MSG_BUFFER_FULL)
471                 /* Events available. */
472                 start_event_fetch(ssif_info, flags);
473         else {
474                 ssif_info->ssif_state = SSIF_NORMAL;
475                 ipmi_ssif_unlock_cond(ssif_info, flags);
476         }
477 }
478
479 static int ipmi_ssif_thread(void *data)
480 {
481         struct ssif_info *ssif_info = data;
482
483         while (!kthread_should_stop()) {
484                 int result;
485
486                 /* Wait for something to do */
487                 result = wait_for_completion_interruptible(
488                                                 &ssif_info->wake_thread);
489                 if (ssif_info->stopping)
490                         break;
491                 if (result == -ERESTARTSYS)
492                         continue;
493                 init_completion(&ssif_info->wake_thread);
494
495                 if (ssif_info->i2c_read_write == I2C_SMBUS_WRITE) {
496                         result = i2c_smbus_write_block_data(
497                                 ssif_info->client, ssif_info->i2c_command,
498                                 ssif_info->i2c_data[0],
499                                 ssif_info->i2c_data + 1);
500                         ssif_info->done_handler(ssif_info, result, NULL, 0);
501                 } else {
502                         result = i2c_smbus_read_block_data(
503                                 ssif_info->client, ssif_info->i2c_command,
504                                 ssif_info->i2c_data);
505                         if (result < 0)
506                                 ssif_info->done_handler(ssif_info, result,
507                                                         NULL, 0);
508                         else
509                                 ssif_info->done_handler(ssif_info, 0,
510                                                         ssif_info->i2c_data,
511                                                         result);
512                 }
513         }
514
515         return 0;
516 }
517
518 static int ssif_i2c_send(struct ssif_info *ssif_info,
519                         ssif_i2c_done handler,
520                         int read_write, int command,
521                         unsigned char *data, unsigned int size)
522 {
523         ssif_info->done_handler = handler;
524
525         ssif_info->i2c_read_write = read_write;
526         ssif_info->i2c_command = command;
527         ssif_info->i2c_data = data;
528         ssif_info->i2c_size = size;
529         complete(&ssif_info->wake_thread);
530         return 0;
531 }
532
533
534 static void msg_done_handler(struct ssif_info *ssif_info, int result,
535                              unsigned char *data, unsigned int len);
536
537 static void start_get(struct ssif_info *ssif_info)
538 {
539         int rv;
540
541         ssif_info->rtc_us_timer = 0;
542         ssif_info->multi_pos = 0;
543
544         rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
545                           SSIF_IPMI_RESPONSE,
546                           ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
547         if (rv < 0) {
548                 /* request failed, just return the error. */
549                 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
550                         pr_info("Error from i2c_non_blocking_op(5)\n");
551
552                 msg_done_handler(ssif_info, -EIO, NULL, 0);
553         }
554 }
555
556 static void retry_timeout(unsigned long data)
557 {
558         struct ssif_info *ssif_info = (void *) data;
559         unsigned long oflags, *flags;
560         bool waiting;
561
562         if (ssif_info->stopping)
563                 return;
564
565         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
566         waiting = ssif_info->waiting_alert;
567         ssif_info->waiting_alert = false;
568         ipmi_ssif_unlock_cond(ssif_info, flags);
569
570         if (waiting)
571                 start_get(ssif_info);
572 }
573
574
575 static void ssif_alert(struct i2c_client *client, enum i2c_alert_protocol type,
576                        unsigned int data)
577 {
578         struct ssif_info *ssif_info = i2c_get_clientdata(client);
579         unsigned long oflags, *flags;
580         bool do_get = false;
581
582         if (type != I2C_PROTOCOL_SMBUS_ALERT)
583                 return;
584
585         ssif_inc_stat(ssif_info, alerts);
586
587         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
588         if (ssif_info->waiting_alert) {
589                 ssif_info->waiting_alert = false;
590                 del_timer(&ssif_info->retry_timer);
591                 do_get = true;
592         } else if (ssif_info->curr_msg) {
593                 ssif_info->got_alert = true;
594         }
595         ipmi_ssif_unlock_cond(ssif_info, flags);
596         if (do_get)
597                 start_get(ssif_info);
598 }
599
600 static int start_resend(struct ssif_info *ssif_info);
601
602 static void msg_done_handler(struct ssif_info *ssif_info, int result,
603                              unsigned char *data, unsigned int len)
604 {
605         struct ipmi_smi_msg *msg;
606         unsigned long oflags, *flags;
607         int rv;
608
609         /*
610          * We are single-threaded here, so no need for a lock until we
611          * start messing with driver states or the queues.
612          */
613
614         if (result < 0) {
615                 ssif_info->retries_left--;
616                 if (ssif_info->retries_left > 0) {
617                         ssif_inc_stat(ssif_info, receive_retries);
618
619                         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
620                         ssif_info->waiting_alert = true;
621                         ssif_info->rtc_us_timer = SSIF_MSG_USEC;
622                         mod_timer(&ssif_info->retry_timer,
623                                   jiffies + SSIF_MSG_JIFFIES);
624                         ipmi_ssif_unlock_cond(ssif_info, flags);
625                         return;
626                 }
627
628                 ssif_inc_stat(ssif_info, receive_errors);
629
630                 if  (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
631                         pr_info("Error in msg_done_handler: %d\n", result);
632                 len = 0;
633                 goto continue_op;
634         }
635
636         if ((len > 1) && (ssif_info->multi_pos == 0)
637                                 && (data[0] == 0x00) && (data[1] == 0x01)) {
638                 /* Start of multi-part read.  Start the next transaction. */
639                 int i;
640
641                 ssif_inc_stat(ssif_info, received_message_parts);
642
643                 /* Remove the multi-part read marker. */
644                 len -= 2;
645                 for (i = 0; i < len; i++)
646                         ssif_info->data[i] = data[i+2];
647                 ssif_info->multi_len = len;
648                 ssif_info->multi_pos = 1;
649
650                 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
651                                   SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
652                                   ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
653                 if (rv < 0) {
654                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
655                                 pr_info("Error from i2c_non_blocking_op(1)\n");
656
657                         result = -EIO;
658                 } else
659                         return;
660         } else if (ssif_info->multi_pos) {
661                 /* Middle of multi-part read.  Start the next transaction. */
662                 int i;
663                 unsigned char blocknum;
664
665                 if (len == 0) {
666                         result = -EIO;
667                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
668                                 pr_info(PFX "Middle message with no data\n");
669
670                         goto continue_op;
671                 }
672
673                 blocknum = data[0];
674
675                 if (ssif_info->multi_len + len - 1 > IPMI_MAX_MSG_LENGTH) {
676                         /* Received message too big, abort the operation. */
677                         result = -E2BIG;
678                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
679                                 pr_info("Received message too big\n");
680
681                         goto continue_op;
682                 }
683
684                 /* Remove the blocknum from the data. */
685                 len--;
686                 for (i = 0; i < len; i++)
687                         ssif_info->data[i + ssif_info->multi_len] = data[i + 1];
688                 ssif_info->multi_len += len;
689                 if (blocknum == 0xff) {
690                         /* End of read */
691                         len = ssif_info->multi_len;
692                         data = ssif_info->data;
693                 } else if (blocknum + 1 != ssif_info->multi_pos) {
694                         /*
695                          * Out of sequence block, just abort.  Block
696                          * numbers start at zero for the second block,
697                          * but multi_pos starts at one, so the +1.
698                          */
699                         result = -EIO;
700                 } else {
701                         ssif_inc_stat(ssif_info, received_message_parts);
702
703                         ssif_info->multi_pos++;
704
705                         rv = ssif_i2c_send(ssif_info, msg_done_handler,
706                                            I2C_SMBUS_READ,
707                                            SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
708                                            ssif_info->recv,
709                                            I2C_SMBUS_BLOCK_DATA);
710                         if (rv < 0) {
711                                 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
712                                         pr_info(PFX
713                                                 "Error from ssif_i2c_send\n");
714
715                                 result = -EIO;
716                         } else
717                                 return;
718                 }
719         }
720
721         if (result < 0) {
722                 ssif_inc_stat(ssif_info, receive_errors);
723         } else {
724                 ssif_inc_stat(ssif_info, received_messages);
725                 ssif_inc_stat(ssif_info, received_message_parts);
726         }
727
728
729  continue_op:
730         if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
731                 pr_info(PFX "DONE 1: state = %d, result=%d.\n",
732                         ssif_info->ssif_state, result);
733
734         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
735         msg = ssif_info->curr_msg;
736         if (msg) {
737                 msg->rsp_size = len;
738                 if (msg->rsp_size > IPMI_MAX_MSG_LENGTH)
739                         msg->rsp_size = IPMI_MAX_MSG_LENGTH;
740                 memcpy(msg->rsp, data, msg->rsp_size);
741                 ssif_info->curr_msg = NULL;
742         }
743
744         switch (ssif_info->ssif_state) {
745         case SSIF_NORMAL:
746                 ipmi_ssif_unlock_cond(ssif_info, flags);
747                 if (!msg)
748                         break;
749
750                 if (result < 0)
751                         return_hosed_msg(ssif_info, msg);
752                 else
753                         deliver_recv_msg(ssif_info, msg);
754                 break;
755
756         case SSIF_GETTING_FLAGS:
757                 /* We got the flags from the SSIF, now handle them. */
758                 if ((result < 0) || (len < 4) || (data[2] != 0)) {
759                         /*
760                          * Error fetching flags, or invalid length,
761                          * just give up for now.
762                          */
763                         ssif_info->ssif_state = SSIF_NORMAL;
764                         ipmi_ssif_unlock_cond(ssif_info, flags);
765                         pr_warn(PFX "Error getting flags: %d %d, %x\n",
766                                result, len, data[2]);
767                 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
768                            || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
769                         /*
770                          * Don't abort here, maybe it was a queued
771                          * response to a previous command.
772                          */
773                         ipmi_ssif_unlock_cond(ssif_info, flags);
774                         pr_warn(PFX "Invalid response getting flags: %x %x\n",
775                                 data[0], data[1]);
776                 } else {
777                         ssif_inc_stat(ssif_info, flag_fetches);
778                         ssif_info->msg_flags = data[3];
779                         handle_flags(ssif_info, flags);
780                 }
781                 break;
782
783         case SSIF_CLEARING_FLAGS:
784                 /* We cleared the flags. */
785                 if ((result < 0) || (len < 3) || (data[2] != 0)) {
786                         /* Error clearing flags */
787                         pr_warn(PFX "Error clearing flags: %d %d, %x\n",
788                                result, len, data[2]);
789                 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
790                            || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
791                         pr_warn(PFX "Invalid response clearing flags: %x %x\n",
792                                 data[0], data[1]);
793                 }
794                 ssif_info->ssif_state = SSIF_NORMAL;
795                 ipmi_ssif_unlock_cond(ssif_info, flags);
796                 break;
797
798         case SSIF_GETTING_EVENTS:
799                 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
800                         /* Error getting event, probably done. */
801                         msg->done(msg);
802
803                         /* Take off the event flag. */
804                         ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
805                         handle_flags(ssif_info, flags);
806                 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
807                            || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
808                         pr_warn(PFX "Invalid response getting events: %x %x\n",
809                                 msg->rsp[0], msg->rsp[1]);
810                         msg->done(msg);
811                         /* Take off the event flag. */
812                         ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
813                         handle_flags(ssif_info, flags);
814                 } else {
815                         handle_flags(ssif_info, flags);
816                         ssif_inc_stat(ssif_info, events);
817                         deliver_recv_msg(ssif_info, msg);
818                 }
819                 break;
820
821         case SSIF_GETTING_MESSAGES:
822                 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
823                         /* Error getting event, probably done. */
824                         msg->done(msg);
825
826                         /* Take off the msg flag. */
827                         ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
828                         handle_flags(ssif_info, flags);
829                 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
830                            || msg->rsp[1] != IPMI_GET_MSG_CMD) {
831                         pr_warn(PFX "Invalid response clearing flags: %x %x\n",
832                                 msg->rsp[0], msg->rsp[1]);
833                         msg->done(msg);
834
835                         /* Take off the msg flag. */
836                         ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
837                         handle_flags(ssif_info, flags);
838                 } else {
839                         ssif_inc_stat(ssif_info, incoming_messages);
840                         handle_flags(ssif_info, flags);
841                         deliver_recv_msg(ssif_info, msg);
842                 }
843                 break;
844         }
845
846         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
847         if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
848                 if (ssif_info->req_events)
849                         start_event_fetch(ssif_info, flags);
850                 else if (ssif_info->req_flags)
851                         start_flag_fetch(ssif_info, flags);
852                 else
853                         start_next_msg(ssif_info, flags);
854         } else
855                 ipmi_ssif_unlock_cond(ssif_info, flags);
856
857         if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
858                 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
859 }
860
861 static void msg_written_handler(struct ssif_info *ssif_info, int result,
862                                 unsigned char *data, unsigned int len)
863 {
864         int rv;
865
866         /* We are single-threaded here, so no need for a lock. */
867         if (result < 0) {
868                 ssif_info->retries_left--;
869                 if (ssif_info->retries_left > 0) {
870                         if (!start_resend(ssif_info)) {
871                                 ssif_inc_stat(ssif_info, send_retries);
872                                 return;
873                         }
874                         /* request failed, just return the error. */
875                         ssif_inc_stat(ssif_info, send_errors);
876
877                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
878                                 pr_info(PFX
879                                         "Out of retries in msg_written_handler\n");
880                         msg_done_handler(ssif_info, -EIO, NULL, 0);
881                         return;
882                 }
883
884                 ssif_inc_stat(ssif_info, send_errors);
885
886                 /*
887                  * Got an error on transmit, let the done routine
888                  * handle it.
889                  */
890                 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
891                         pr_info("Error in msg_written_handler: %d\n", result);
892
893                 msg_done_handler(ssif_info, result, NULL, 0);
894                 return;
895         }
896
897         if (ssif_info->multi_data) {
898                 /*
899                  * In the middle of a multi-data write.  See the comment
900                  * in the SSIF_MULTI_n_PART case in the probe function
901                  * for details on the intricacies of this.
902                  */
903                 int left;
904                 unsigned char *data_to_send;
905
906                 ssif_inc_stat(ssif_info, sent_messages_parts);
907
908                 left = ssif_info->multi_len - ssif_info->multi_pos;
909                 if (left > 32)
910                         left = 32;
911                 /* Length byte. */
912                 ssif_info->multi_data[ssif_info->multi_pos] = left;
913                 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
914                 ssif_info->multi_pos += left;
915                 if (left < 32)
916                         /*
917                          * Write is finished.  Note that we must end
918                          * with a write of less than 32 bytes to
919                          * complete the transaction, even if it is
920                          * zero bytes.
921                          */
922                         ssif_info->multi_data = NULL;
923
924                 rv = ssif_i2c_send(ssif_info, msg_written_handler,
925                                   I2C_SMBUS_WRITE,
926                                   SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
927                                   data_to_send,
928                                   I2C_SMBUS_BLOCK_DATA);
929                 if (rv < 0) {
930                         /* request failed, just return the error. */
931                         ssif_inc_stat(ssif_info, send_errors);
932
933                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
934                                 pr_info("Error from i2c_non_blocking_op(3)\n");
935                         msg_done_handler(ssif_info, -EIO, NULL, 0);
936                 }
937         } else {
938                 /* Ready to request the result. */
939                 unsigned long oflags, *flags;
940
941                 ssif_inc_stat(ssif_info, sent_messages);
942                 ssif_inc_stat(ssif_info, sent_messages_parts);
943
944                 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
945                 if (ssif_info->got_alert) {
946                         /* The result is already ready, just start it. */
947                         ssif_info->got_alert = false;
948                         ipmi_ssif_unlock_cond(ssif_info, flags);
949                         start_get(ssif_info);
950                 } else {
951                         /* Wait a jiffie then request the next message */
952                         ssif_info->waiting_alert = true;
953                         ssif_info->retries_left = SSIF_RECV_RETRIES;
954                         ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
955                         mod_timer(&ssif_info->retry_timer,
956                                   jiffies + SSIF_MSG_PART_JIFFIES);
957                         ipmi_ssif_unlock_cond(ssif_info, flags);
958                 }
959         }
960 }
961
962 static int start_resend(struct ssif_info *ssif_info)
963 {
964         int rv;
965         int command;
966
967         ssif_info->got_alert = false;
968
969         if (ssif_info->data_len > 32) {
970                 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
971                 ssif_info->multi_data = ssif_info->data;
972                 ssif_info->multi_len = ssif_info->data_len;
973                 /*
974                  * Subtle thing, this is 32, not 33, because we will
975                  * overwrite the thing at position 32 (which was just
976                  * transmitted) with the new length.
977                  */
978                 ssif_info->multi_pos = 32;
979                 ssif_info->data[0] = 32;
980         } else {
981                 ssif_info->multi_data = NULL;
982                 command = SSIF_IPMI_REQUEST;
983                 ssif_info->data[0] = ssif_info->data_len;
984         }
985
986         rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
987                           command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
988         if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
989                 pr_info("Error from i2c_non_blocking_op(4)\n");
990         return rv;
991 }
992
993 static int start_send(struct ssif_info *ssif_info,
994                       unsigned char   *data,
995                       unsigned int    len)
996 {
997         if (len > IPMI_MAX_MSG_LENGTH)
998                 return -E2BIG;
999         if (len > ssif_info->max_xmit_msg_size)
1000                 return -E2BIG;
1001
1002         ssif_info->retries_left = SSIF_SEND_RETRIES;
1003         memcpy(ssif_info->data + 1, data, len);
1004         ssif_info->data_len = len;
1005         return start_resend(ssif_info);
1006 }
1007
1008 /* Must be called with the message lock held. */
1009 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1010 {
1011         struct ipmi_smi_msg *msg;
1012         unsigned long oflags;
1013
1014  restart:
1015         if (!SSIF_IDLE(ssif_info)) {
1016                 ipmi_ssif_unlock_cond(ssif_info, flags);
1017                 return;
1018         }
1019
1020         if (!ssif_info->waiting_msg) {
1021                 ssif_info->curr_msg = NULL;
1022                 ipmi_ssif_unlock_cond(ssif_info, flags);
1023         } else {
1024                 int rv;
1025
1026                 ssif_info->curr_msg = ssif_info->waiting_msg;
1027                 ssif_info->waiting_msg = NULL;
1028                 ipmi_ssif_unlock_cond(ssif_info, flags);
1029                 rv = start_send(ssif_info,
1030                                 ssif_info->curr_msg->data,
1031                                 ssif_info->curr_msg->data_size);
1032                 if (rv) {
1033                         msg = ssif_info->curr_msg;
1034                         ssif_info->curr_msg = NULL;
1035                         return_hosed_msg(ssif_info, msg);
1036                         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1037                         goto restart;
1038                 }
1039         }
1040 }
1041
1042 static void sender(void                *send_info,
1043                    struct ipmi_smi_msg *msg)
1044 {
1045         struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1046         unsigned long oflags, *flags;
1047
1048         BUG_ON(ssif_info->waiting_msg);
1049         ssif_info->waiting_msg = msg;
1050
1051         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1052         start_next_msg(ssif_info, flags);
1053
1054         if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1055                 struct timespec64 t;
1056
1057                 ktime_get_real_ts64(&t);
1058                 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1059                        msg->data[0], msg->data[1],
1060                        (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
1061         }
1062 }
1063
1064 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1065 {
1066         struct ssif_info *ssif_info = send_info;
1067
1068         data->addr_src = ssif_info->addr_source;
1069         data->dev = &ssif_info->client->dev;
1070         data->addr_info = ssif_info->addr_info;
1071         get_device(data->dev);
1072
1073         return 0;
1074 }
1075
1076 /*
1077  * Instead of having our own timer to periodically check the message
1078  * flags, we let the message handler drive us.
1079  */
1080 static void request_events(void *send_info)
1081 {
1082         struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1083         unsigned long oflags, *flags;
1084
1085         if (!ssif_info->has_event_buffer)
1086                 return;
1087
1088         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1089         /*
1090          * Request flags first, not events, because the lower layer
1091          * doesn't have a way to send an attention.  But make sure
1092          * event checking still happens.
1093          */
1094         ssif_info->req_events = true;
1095         if (SSIF_IDLE(ssif_info))
1096                 start_flag_fetch(ssif_info, flags);
1097         else {
1098                 ssif_info->req_flags = true;
1099                 ipmi_ssif_unlock_cond(ssif_info, flags);
1100         }
1101 }
1102
1103 static int inc_usecount(void *send_info)
1104 {
1105         struct ssif_info *ssif_info = send_info;
1106
1107         if (!i2c_get_adapter(i2c_adapter_id(ssif_info->client->adapter)))
1108                 return -ENODEV;
1109
1110         i2c_use_client(ssif_info->client);
1111         return 0;
1112 }
1113
1114 static void dec_usecount(void *send_info)
1115 {
1116         struct ssif_info *ssif_info = send_info;
1117
1118         i2c_release_client(ssif_info->client);
1119         i2c_put_adapter(ssif_info->client->adapter);
1120 }
1121
1122 static int ssif_start_processing(void *send_info,
1123                                  ipmi_smi_t intf)
1124 {
1125         struct ssif_info *ssif_info = send_info;
1126
1127         ssif_info->intf = intf;
1128
1129         return 0;
1130 }
1131
1132 #define MAX_SSIF_BMCS 4
1133
1134 static unsigned short addr[MAX_SSIF_BMCS];
1135 static int num_addrs;
1136 module_param_array(addr, ushort, &num_addrs, 0);
1137 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1138
1139 static char *adapter_name[MAX_SSIF_BMCS];
1140 static int num_adapter_names;
1141 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1142 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC.  By default all devices are scanned.");
1143
1144 static int slave_addrs[MAX_SSIF_BMCS];
1145 static int num_slave_addrs;
1146 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1147 MODULE_PARM_DESC(slave_addrs,
1148                  "The default IPMB slave address for the controller.");
1149
1150 static bool alerts_broken;
1151 module_param(alerts_broken, bool, 0);
1152 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1153
1154 /*
1155  * Bit 0 enables message debugging, bit 1 enables state debugging, and
1156  * bit 2 enables timing debugging.  This is an array indexed by
1157  * interface number"
1158  */
1159 static int dbg[MAX_SSIF_BMCS];
1160 static int num_dbg;
1161 module_param_array(dbg, int, &num_dbg, 0);
1162 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1163
1164 static bool ssif_dbg_probe;
1165 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1166 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1167
1168 static bool ssif_tryacpi = true;
1169 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1170 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1171
1172 static bool ssif_trydmi = true;
1173 module_param_named(trydmi, ssif_trydmi, bool, 0);
1174 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1175
1176 static DEFINE_MUTEX(ssif_infos_mutex);
1177 static LIST_HEAD(ssif_infos);
1178
1179 static int ssif_remove(struct i2c_client *client)
1180 {
1181         struct ssif_info *ssif_info = i2c_get_clientdata(client);
1182         struct ssif_addr_info *addr_info;
1183         int rv;
1184
1185         if (!ssif_info)
1186                 return 0;
1187
1188         /*
1189          * After this point, we won't deliver anything asychronously
1190          * to the message handler.  We can unregister ourself.
1191          */
1192         rv = ipmi_unregister_smi(ssif_info->intf);
1193         if (rv) {
1194                 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1195                 return rv;
1196         }
1197         ssif_info->intf = NULL;
1198
1199         /* make sure the driver is not looking for flags any more. */
1200         while (ssif_info->ssif_state != SSIF_NORMAL)
1201                 schedule_timeout(1);
1202
1203         ssif_info->stopping = true;
1204         del_timer_sync(&ssif_info->retry_timer);
1205         if (ssif_info->thread) {
1206                 complete(&ssif_info->wake_thread);
1207                 kthread_stop(ssif_info->thread);
1208         }
1209
1210         list_for_each_entry(addr_info, &ssif_infos, link) {
1211                 if (addr_info->client == client) {
1212                         addr_info->client = NULL;
1213                         break;
1214                 }
1215         }
1216
1217         /*
1218          * No message can be outstanding now, we have removed the
1219          * upper layer and it permitted us to do so.
1220          */
1221         kfree(ssif_info);
1222         return 0;
1223 }
1224
1225 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1226                   int *resp_len, unsigned char *resp)
1227 {
1228         int retry_cnt;
1229         int ret;
1230
1231         retry_cnt = SSIF_SEND_RETRIES;
1232  retry1:
1233         ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1234         if (ret) {
1235                 retry_cnt--;
1236                 if (retry_cnt > 0)
1237                         goto retry1;
1238                 return -ENODEV;
1239         }
1240
1241         ret = -ENODEV;
1242         retry_cnt = SSIF_RECV_RETRIES;
1243         while (retry_cnt > 0) {
1244                 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1245                                                 resp);
1246                 if (ret > 0)
1247                         break;
1248                 msleep(SSIF_MSG_MSEC);
1249                 retry_cnt--;
1250                 if (retry_cnt <= 0)
1251                         break;
1252         }
1253
1254         if (ret > 0) {
1255                 /* Validate that the response is correct. */
1256                 if (ret < 3 ||
1257                     (resp[0] != (msg[0] | (1 << 2))) ||
1258                     (resp[1] != msg[1]))
1259                         ret = -EINVAL;
1260                 else {
1261                         *resp_len = ret;
1262                         ret = 0;
1263                 }
1264         }
1265
1266         return ret;
1267 }
1268
1269 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1270 {
1271         unsigned char *resp;
1272         unsigned char msg[3];
1273         int           rv;
1274         int           len;
1275
1276         resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1277         if (!resp)
1278                 return -ENOMEM;
1279
1280         /* Do a Get Device ID command, since it is required. */
1281         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1282         msg[1] = IPMI_GET_DEVICE_ID_CMD;
1283         rv = do_cmd(client, 2, msg, &len, resp);
1284         if (rv)
1285                 rv = -ENODEV;
1286         else
1287                 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1288         kfree(resp);
1289         return rv;
1290 }
1291
1292 static int smi_type_proc_show(struct seq_file *m, void *v)
1293 {
1294         seq_puts(m, "ssif\n");
1295
1296         return 0;
1297 }
1298
1299 static int smi_type_proc_open(struct inode *inode, struct file *file)
1300 {
1301         return single_open(file, smi_type_proc_show, inode->i_private);
1302 }
1303
1304 static const struct file_operations smi_type_proc_ops = {
1305         .open           = smi_type_proc_open,
1306         .read           = seq_read,
1307         .llseek         = seq_lseek,
1308         .release        = single_release,
1309 };
1310
1311 static int smi_stats_proc_show(struct seq_file *m, void *v)
1312 {
1313         struct ssif_info *ssif_info = m->private;
1314
1315         seq_printf(m, "sent_messages:          %u\n",
1316                    ssif_get_stat(ssif_info, sent_messages));
1317         seq_printf(m, "sent_messages_parts:    %u\n",
1318                    ssif_get_stat(ssif_info, sent_messages_parts));
1319         seq_printf(m, "send_retries:           %u\n",
1320                    ssif_get_stat(ssif_info, send_retries));
1321         seq_printf(m, "send_errors:            %u\n",
1322                    ssif_get_stat(ssif_info, send_errors));
1323         seq_printf(m, "received_messages:      %u\n",
1324                    ssif_get_stat(ssif_info, received_messages));
1325         seq_printf(m, "received_message_parts: %u\n",
1326                    ssif_get_stat(ssif_info, received_message_parts));
1327         seq_printf(m, "receive_retries:        %u\n",
1328                    ssif_get_stat(ssif_info, receive_retries));
1329         seq_printf(m, "receive_errors:         %u\n",
1330                    ssif_get_stat(ssif_info, receive_errors));
1331         seq_printf(m, "flag_fetches:           %u\n",
1332                    ssif_get_stat(ssif_info, flag_fetches));
1333         seq_printf(m, "hosed:                  %u\n",
1334                    ssif_get_stat(ssif_info, hosed));
1335         seq_printf(m, "events:                 %u\n",
1336                    ssif_get_stat(ssif_info, events));
1337         seq_printf(m, "watchdog_pretimeouts:   %u\n",
1338                    ssif_get_stat(ssif_info, watchdog_pretimeouts));
1339         seq_printf(m, "alerts:                 %u\n",
1340                    ssif_get_stat(ssif_info, alerts));
1341         return 0;
1342 }
1343
1344 static int smi_stats_proc_open(struct inode *inode, struct file *file)
1345 {
1346         return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1347 }
1348
1349 static const struct file_operations smi_stats_proc_ops = {
1350         .open           = smi_stats_proc_open,
1351         .read           = seq_read,
1352         .llseek         = seq_lseek,
1353         .release        = single_release,
1354 };
1355
1356 static int strcmp_nospace(char *s1, char *s2)
1357 {
1358         while (*s1 && *s2) {
1359                 while (isspace(*s1))
1360                         s1++;
1361                 while (isspace(*s2))
1362                         s2++;
1363                 if (*s1 > *s2)
1364                         return 1;
1365                 if (*s1 < *s2)
1366                         return -1;
1367                 s1++;
1368                 s2++;
1369         }
1370         return 0;
1371 }
1372
1373 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1374                                              char *adapter_name,
1375                                              bool match_null_name)
1376 {
1377         struct ssif_addr_info *info, *found = NULL;
1378
1379 restart:
1380         list_for_each_entry(info, &ssif_infos, link) {
1381                 if (info->binfo.addr == addr) {
1382                         if (info->adapter_name || adapter_name) {
1383                                 if (!info->adapter_name != !adapter_name) {
1384                                         /* One is NULL and one is not */
1385                                         continue;
1386                                 }
1387                                 if (adapter_name &&
1388                                     strcmp_nospace(info->adapter_name,
1389                                                    adapter_name))
1390                                         /* Names do not match */
1391                                         continue;
1392                         }
1393                         found = info;
1394                         break;
1395                 }
1396         }
1397
1398         if (!found && match_null_name) {
1399                 /* Try to get an exact match first, then try with a NULL name */
1400                 adapter_name = NULL;
1401                 match_null_name = false;
1402                 goto restart;
1403         }
1404
1405         return found;
1406 }
1407
1408 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1409 {
1410 #ifdef CONFIG_ACPI
1411         acpi_handle acpi_handle;
1412
1413         acpi_handle = ACPI_HANDLE(dev);
1414         if (acpi_handle) {
1415                 ssif_info->addr_source = SI_ACPI;
1416                 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1417                 return true;
1418         }
1419 #endif
1420         return false;
1421 }
1422
1423 static int find_slave_address(struct i2c_client *client, int slave_addr)
1424 {
1425 #ifdef CONFIG_IPMI_DMI_DECODE
1426         if (!slave_addr)
1427                 slave_addr = ipmi_dmi_get_slave_addr(
1428                         IPMI_DMI_TYPE_SSIF,
1429                         i2c_adapter_id(client->adapter),
1430                         client->addr);
1431 #endif
1432
1433         return slave_addr;
1434 }
1435
1436 /*
1437  * Global enables we care about.
1438  */
1439 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1440                              IPMI_BMC_EVT_MSG_INTR)
1441
1442 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1443 {
1444         unsigned char     msg[3];
1445         unsigned char     *resp;
1446         struct ssif_info   *ssif_info;
1447         int               rv = 0;
1448         int               len;
1449         int               i;
1450         u8                slave_addr = 0;
1451         struct ssif_addr_info *addr_info = NULL;
1452
1453         resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1454         if (!resp)
1455                 return -ENOMEM;
1456
1457         ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1458         if (!ssif_info) {
1459                 kfree(resp);
1460                 return -ENOMEM;
1461         }
1462
1463         if (!check_acpi(ssif_info, &client->dev)) {
1464                 addr_info = ssif_info_find(client->addr, client->adapter->name,
1465                                            true);
1466                 if (!addr_info) {
1467                         /* Must have come in through sysfs. */
1468                         ssif_info->addr_source = SI_HOTMOD;
1469                 } else {
1470                         ssif_info->addr_source = addr_info->addr_src;
1471                         ssif_info->ssif_debug = addr_info->debug;
1472                         ssif_info->addr_info = addr_info->addr_info;
1473                         addr_info->client = client;
1474                         slave_addr = addr_info->slave_addr;
1475                 }
1476         }
1477
1478         slave_addr = find_slave_address(client, slave_addr);
1479
1480         pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1481                ipmi_addr_src_to_str(ssif_info->addr_source),
1482                client->addr, client->adapter->name, slave_addr);
1483
1484         /*
1485          * Do a Get Device ID command, since it comes back with some
1486          * useful info.
1487          */
1488         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1489         msg[1] = IPMI_GET_DEVICE_ID_CMD;
1490         rv = do_cmd(client, 2, msg, &len, resp);
1491         if (rv)
1492                 goto out;
1493
1494         rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1495         if (rv)
1496                 goto out;
1497
1498         ssif_info->client = client;
1499         i2c_set_clientdata(client, ssif_info);
1500
1501         /* Now check for system interface capabilities */
1502         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1503         msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1504         msg[2] = 0; /* SSIF */
1505         rv = do_cmd(client, 3, msg, &len, resp);
1506         if (!rv && (len >= 3) && (resp[2] == 0)) {
1507                 if (len < 7) {
1508                         if (ssif_dbg_probe)
1509                                 pr_info(PFX "SSIF info too short: %d\n", len);
1510                         goto no_support;
1511                 }
1512
1513                 /* Got a good SSIF response, handle it. */
1514                 ssif_info->max_xmit_msg_size = resp[5];
1515                 ssif_info->max_recv_msg_size = resp[6];
1516                 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1517                 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1518
1519                 /* Sanitize the data */
1520                 switch (ssif_info->multi_support) {
1521                 case SSIF_NO_MULTI:
1522                         if (ssif_info->max_xmit_msg_size > 32)
1523                                 ssif_info->max_xmit_msg_size = 32;
1524                         if (ssif_info->max_recv_msg_size > 32)
1525                                 ssif_info->max_recv_msg_size = 32;
1526                         break;
1527
1528                 case SSIF_MULTI_2_PART:
1529                         if (ssif_info->max_xmit_msg_size > 63)
1530                                 ssif_info->max_xmit_msg_size = 63;
1531                         if (ssif_info->max_recv_msg_size > 62)
1532                                 ssif_info->max_recv_msg_size = 62;
1533                         break;
1534
1535                 case SSIF_MULTI_n_PART:
1536                         /*
1537                          * The specification is rather confusing at
1538                          * this point, but I think I understand what
1539                          * is meant.  At least I have a workable
1540                          * solution.  With multi-part messages, you
1541                          * cannot send a message that is a multiple of
1542                          * 32-bytes in length, because the start and
1543                          * middle messages are 32-bytes and the end
1544                          * message must be at least one byte.  You
1545                          * can't fudge on an extra byte, that would
1546                          * screw up things like fru data writes.  So
1547                          * we limit the length to 63 bytes.  That way
1548                          * a 32-byte message gets sent as a single
1549                          * part.  A larger message will be a 32-byte
1550                          * start and the next message is always going
1551                          * to be 1-31 bytes in length.  Not ideal, but
1552                          * it should work.
1553                          */
1554                         if (ssif_info->max_xmit_msg_size > 63)
1555                                 ssif_info->max_xmit_msg_size = 63;
1556                         break;
1557
1558                 default:
1559                         /* Data is not sane, just give up. */
1560                         goto no_support;
1561                 }
1562         } else {
1563  no_support:
1564                 /* Assume no multi-part or PEC support */
1565                 pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1566                        rv, len, resp[2]);
1567
1568                 ssif_info->max_xmit_msg_size = 32;
1569                 ssif_info->max_recv_msg_size = 32;
1570                 ssif_info->multi_support = SSIF_NO_MULTI;
1571                 ssif_info->supports_pec = 0;
1572         }
1573
1574         /* Make sure the NMI timeout is cleared. */
1575         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1576         msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1577         msg[2] = WDT_PRE_TIMEOUT_INT;
1578         rv = do_cmd(client, 3, msg, &len, resp);
1579         if (rv || (len < 3) || (resp[2] != 0))
1580                 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1581                         rv, len, resp[2]);
1582
1583         /* Attempt to enable the event buffer. */
1584         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1585         msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1586         rv = do_cmd(client, 2, msg, &len, resp);
1587         if (rv || (len < 4) || (resp[2] != 0)) {
1588                 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1589                         rv, len, resp[2]);
1590                 rv = 0; /* Not fatal */
1591                 goto found;
1592         }
1593
1594         ssif_info->global_enables = resp[3];
1595
1596         if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1597                 ssif_info->has_event_buffer = true;
1598                 /* buffer is already enabled, nothing to do. */
1599                 goto found;
1600         }
1601
1602         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1603         msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1604         msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1605         rv = do_cmd(client, 3, msg, &len, resp);
1606         if (rv || (len < 2)) {
1607                 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1608                         rv, len, resp[2]);
1609                 rv = 0; /* Not fatal */
1610                 goto found;
1611         }
1612
1613         if (resp[2] == 0) {
1614                 /* A successful return means the event buffer is supported. */
1615                 ssif_info->has_event_buffer = true;
1616                 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1617         }
1618
1619         /* Some systems don't behave well if you enable alerts. */
1620         if (alerts_broken)
1621                 goto found;
1622
1623         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1624         msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1625         msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1626         rv = do_cmd(client, 3, msg, &len, resp);
1627         if (rv || (len < 2)) {
1628                 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1629                         rv, len, resp[2]);
1630                 rv = 0; /* Not fatal */
1631                 goto found;
1632         }
1633
1634         if (resp[2] == 0) {
1635                 /* A successful return means the alert is supported. */
1636                 ssif_info->supports_alert = true;
1637                 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1638         }
1639
1640  found:
1641         ssif_info->intf_num = atomic_inc_return(&next_intf);
1642
1643         if (ssif_dbg_probe) {
1644                 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1645                         client->addr);
1646         }
1647
1648         spin_lock_init(&ssif_info->lock);
1649         ssif_info->ssif_state = SSIF_NORMAL;
1650         setup_timer(&ssif_info->retry_timer, retry_timeout,
1651                     (unsigned long)ssif_info);
1652
1653         for (i = 0; i < SSIF_NUM_STATS; i++)
1654                 atomic_set(&ssif_info->stats[i], 0);
1655
1656         if (ssif_info->supports_pec)
1657                 ssif_info->client->flags |= I2C_CLIENT_PEC;
1658
1659         ssif_info->handlers.owner = THIS_MODULE;
1660         ssif_info->handlers.start_processing = ssif_start_processing;
1661         ssif_info->handlers.get_smi_info = get_smi_info;
1662         ssif_info->handlers.sender = sender;
1663         ssif_info->handlers.request_events = request_events;
1664         ssif_info->handlers.inc_usecount = inc_usecount;
1665         ssif_info->handlers.dec_usecount = dec_usecount;
1666
1667         {
1668                 unsigned int thread_num;
1669
1670                 thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1671                                << 8) |
1672                               ssif_info->client->addr);
1673                 init_completion(&ssif_info->wake_thread);
1674                 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1675                                                "kssif%4.4x", thread_num);
1676                 if (IS_ERR(ssif_info->thread)) {
1677                         rv = PTR_ERR(ssif_info->thread);
1678                         dev_notice(&ssif_info->client->dev,
1679                                    "Could not start kernel thread: error %d\n",
1680                                    rv);
1681                         goto out;
1682                 }
1683         }
1684
1685         rv = ipmi_register_smi(&ssif_info->handlers,
1686                                ssif_info,
1687                                &ssif_info->device_id,
1688                                &ssif_info->client->dev,
1689                                slave_addr);
1690          if (rv) {
1691                 pr_err(PFX "Unable to register device: error %d\n", rv);
1692                 goto out;
1693         }
1694
1695         rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1696                                      &smi_type_proc_ops,
1697                                      ssif_info);
1698         if (rv) {
1699                 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1700                 goto out_err_unreg;
1701         }
1702
1703         rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1704                                      &smi_stats_proc_ops,
1705                                      ssif_info);
1706         if (rv) {
1707                 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1708                 goto out_err_unreg;
1709         }
1710
1711  out:
1712         if (rv) {
1713                 /*
1714                  * Note that if addr_info->client is assigned, we
1715                  * leave it.  The i2c client hangs around even if we
1716                  * return a failure here, and the failure here is not
1717                  * propagated back to the i2c code.  This seems to be
1718                  * design intent, strange as it may be.  But if we
1719                  * don't leave it, ssif_platform_remove will not remove
1720                  * the client like it should.
1721                  */
1722                 dev_err(&client->dev, "Unable to start IPMI SSIF: %d\n", rv);
1723                 kfree(ssif_info);
1724         }
1725         kfree(resp);
1726         return rv;
1727
1728  out_err_unreg:
1729         ipmi_unregister_smi(ssif_info->intf);
1730         goto out;
1731 }
1732
1733 static int ssif_adapter_handler(struct device *adev, void *opaque)
1734 {
1735         struct ssif_addr_info *addr_info = opaque;
1736
1737         if (adev->type != &i2c_adapter_type)
1738                 return 0;
1739
1740         i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo);
1741
1742         if (!addr_info->adapter_name)
1743                 return 1; /* Only try the first I2C adapter by default. */
1744         return 0;
1745 }
1746
1747 static int new_ssif_client(int addr, char *adapter_name,
1748                            int debug, int slave_addr,
1749                            enum ipmi_addr_src addr_src,
1750                            struct device *dev)
1751 {
1752         struct ssif_addr_info *addr_info;
1753         int rv = 0;
1754
1755         mutex_lock(&ssif_infos_mutex);
1756         if (ssif_info_find(addr, adapter_name, false)) {
1757                 rv = -EEXIST;
1758                 goto out_unlock;
1759         }
1760
1761         addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1762         if (!addr_info) {
1763                 rv = -ENOMEM;
1764                 goto out_unlock;
1765         }
1766
1767         if (adapter_name) {
1768                 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1769                 if (!addr_info->adapter_name) {
1770                         kfree(addr_info);
1771                         rv = -ENOMEM;
1772                         goto out_unlock;
1773                 }
1774         }
1775
1776         strncpy(addr_info->binfo.type, DEVICE_NAME,
1777                 sizeof(addr_info->binfo.type));
1778         addr_info->binfo.addr = addr;
1779         addr_info->binfo.platform_data = addr_info;
1780         addr_info->debug = debug;
1781         addr_info->slave_addr = slave_addr;
1782         addr_info->addr_src = addr_src;
1783         addr_info->dev = dev;
1784
1785         if (dev)
1786                 dev_set_drvdata(dev, addr_info);
1787
1788         list_add_tail(&addr_info->link, &ssif_infos);
1789
1790         if (initialized)
1791                 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1792         /* Otherwise address list will get it */
1793
1794 out_unlock:
1795         mutex_unlock(&ssif_infos_mutex);
1796         return rv;
1797 }
1798
1799 static void free_ssif_clients(void)
1800 {
1801         struct ssif_addr_info *info, *tmp;
1802
1803         mutex_lock(&ssif_infos_mutex);
1804         list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1805                 list_del(&info->link);
1806                 kfree(info->adapter_name);
1807                 kfree(info);
1808         }
1809         mutex_unlock(&ssif_infos_mutex);
1810 }
1811
1812 static unsigned short *ssif_address_list(void)
1813 {
1814         struct ssif_addr_info *info;
1815         unsigned int count = 0, i;
1816         unsigned short *address_list;
1817
1818         list_for_each_entry(info, &ssif_infos, link)
1819                 count++;
1820
1821         address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1822         if (!address_list)
1823                 return NULL;
1824
1825         i = 0;
1826         list_for_each_entry(info, &ssif_infos, link) {
1827                 unsigned short addr = info->binfo.addr;
1828                 int j;
1829
1830                 for (j = 0; j < i; j++) {
1831                         if (address_list[j] == addr)
1832                                 goto skip_addr;
1833                 }
1834                 address_list[i] = addr;
1835 skip_addr:
1836                 i++;
1837         }
1838         address_list[i] = I2C_CLIENT_END;
1839
1840         return address_list;
1841 }
1842
1843 #ifdef CONFIG_ACPI
1844 static const struct acpi_device_id ssif_acpi_match[] = {
1845         { "IPI0001", 0 },
1846         { },
1847 };
1848 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1849
1850 /*
1851  * Once we get an ACPI failure, we don't try any more, because we go
1852  * through the tables sequentially.  Once we don't find a table, there
1853  * are no more.
1854  */
1855 static int acpi_failure;
1856
1857 /*
1858  * Defined in the IPMI 2.0 spec.
1859  */
1860 struct SPMITable {
1861         s8      Signature[4];
1862         u32     Length;
1863         u8      Revision;
1864         u8      Checksum;
1865         s8      OEMID[6];
1866         s8      OEMTableID[8];
1867         s8      OEMRevision[4];
1868         s8      CreatorID[4];
1869         s8      CreatorRevision[4];
1870         u8      InterfaceType;
1871         u8      IPMIlegacy;
1872         s16     SpecificationRevision;
1873
1874         /*
1875          * Bit 0 - SCI interrupt supported
1876          * Bit 1 - I/O APIC/SAPIC
1877          */
1878         u8      InterruptType;
1879
1880         /*
1881          * If bit 0 of InterruptType is set, then this is the SCI
1882          * interrupt in the GPEx_STS register.
1883          */
1884         u8      GPE;
1885
1886         s16     Reserved;
1887
1888         /*
1889          * If bit 1 of InterruptType is set, then this is the I/O
1890          * APIC/SAPIC interrupt.
1891          */
1892         u32     GlobalSystemInterrupt;
1893
1894         /* The actual register address. */
1895         struct acpi_generic_address addr;
1896
1897         u8      UID[4];
1898
1899         s8      spmi_id[1]; /* A '\0' terminated array starts here. */
1900 };
1901
1902 static int try_init_spmi(struct SPMITable *spmi)
1903 {
1904         unsigned short myaddr;
1905
1906         if (num_addrs >= MAX_SSIF_BMCS)
1907                 return -1;
1908
1909         if (spmi->IPMIlegacy != 1) {
1910                 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1911                 return -ENODEV;
1912         }
1913
1914         if (spmi->InterfaceType != 4)
1915                 return -ENODEV;
1916
1917         if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1918                 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1919                         spmi->addr.space_id);
1920                 return -EIO;
1921         }
1922
1923         myaddr = spmi->addr.address & 0x7f;
1924
1925         return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI, NULL);
1926 }
1927
1928 static void spmi_find_bmc(void)
1929 {
1930         acpi_status      status;
1931         struct SPMITable *spmi;
1932         int              i;
1933
1934         if (acpi_disabled)
1935                 return;
1936
1937         if (acpi_failure)
1938                 return;
1939
1940         for (i = 0; ; i++) {
1941                 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1942                                         (struct acpi_table_header **)&spmi);
1943                 if (status != AE_OK)
1944                         return;
1945
1946                 try_init_spmi(spmi);
1947         }
1948 }
1949 #else
1950 static void spmi_find_bmc(void) { }
1951 #endif
1952
1953 #ifdef CONFIG_DMI
1954 static int dmi_ipmi_probe(struct platform_device *pdev)
1955 {
1956         u8 type, slave_addr = 0;
1957         u16 i2c_addr;
1958         int rv;
1959
1960         if (!ssif_trydmi)
1961                 return -ENODEV;
1962
1963         rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type);
1964         if (rv)
1965                 return -ENODEV;
1966
1967         if (type != IPMI_DMI_TYPE_SSIF)
1968                 return -ENODEV;
1969
1970         rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
1971         if (rv) {
1972                 dev_warn(&pdev->dev, PFX "No i2c-addr property\n");
1973                 return -ENODEV;
1974         }
1975
1976         rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
1977         if (rv)
1978                 dev_warn(&pdev->dev, "device has no slave-addr property");
1979
1980         return new_ssif_client(i2c_addr, NULL, 0,
1981                                slave_addr, SI_SMBIOS, &pdev->dev);
1982 }
1983 #else
1984 static int dmi_ipmi_probe(struct platform_device *pdev)
1985 {
1986         return -ENODEV;
1987 }
1988 #endif
1989
1990 static const struct i2c_device_id ssif_id[] = {
1991         { DEVICE_NAME, 0 },
1992         { }
1993 };
1994 MODULE_DEVICE_TABLE(i2c, ssif_id);
1995
1996 static struct i2c_driver ssif_i2c_driver = {
1997         .class          = I2C_CLASS_HWMON,
1998         .driver         = {
1999                 .name                   = DEVICE_NAME
2000         },
2001         .probe          = ssif_probe,
2002         .remove         = ssif_remove,
2003         .alert          = ssif_alert,
2004         .id_table       = ssif_id,
2005         .detect         = ssif_detect
2006 };
2007
2008 static int ssif_platform_probe(struct platform_device *dev)
2009 {
2010         return dmi_ipmi_probe(dev);
2011 }
2012
2013 static int ssif_platform_remove(struct platform_device *dev)
2014 {
2015         struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2016
2017         if (!addr_info)
2018                 return 0;
2019
2020         mutex_lock(&ssif_infos_mutex);
2021         if (addr_info->client)
2022                 i2c_unregister_device(addr_info->client);
2023
2024         list_del(&addr_info->link);
2025         kfree(addr_info);
2026         mutex_unlock(&ssif_infos_mutex);
2027         return 0;
2028 }
2029
2030 static struct platform_driver ipmi_driver = {
2031         .driver = {
2032                 .name = DEVICE_NAME,
2033         },
2034         .probe          = ssif_platform_probe,
2035         .remove         = ssif_platform_remove,
2036 };
2037
2038 static int init_ipmi_ssif(void)
2039 {
2040         int i;
2041         int rv;
2042
2043         if (initialized)
2044                 return 0;
2045
2046         pr_info("IPMI SSIF Interface driver\n");
2047
2048         /* build list for i2c from addr list */
2049         for (i = 0; i < num_addrs; i++) {
2050                 rv = new_ssif_client(addr[i], adapter_name[i],
2051                                      dbg[i], slave_addrs[i],
2052                                      SI_HARDCODED, NULL);
2053                 if (rv)
2054                         pr_err(PFX
2055                                "Couldn't add hardcoded device at addr 0x%x\n",
2056                                addr[i]);
2057         }
2058
2059         if (ssif_tryacpi)
2060                 ssif_i2c_driver.driver.acpi_match_table =
2061                         ACPI_PTR(ssif_acpi_match);
2062
2063         if (ssif_tryacpi)
2064                 spmi_find_bmc();
2065
2066         if (ssif_trydmi) {
2067                 rv = platform_driver_register(&ipmi_driver);
2068                 if (rv)
2069                         pr_err(PFX "Unable to register driver: %d\n", rv);
2070         }
2071
2072         ssif_i2c_driver.address_list = ssif_address_list();
2073
2074         rv = i2c_add_driver(&ssif_i2c_driver);
2075         if (!rv)
2076                 initialized = true;
2077
2078         return rv;
2079 }
2080 module_init(init_ipmi_ssif);
2081
2082 static void cleanup_ipmi_ssif(void)
2083 {
2084         if (!initialized)
2085                 return;
2086
2087         initialized = false;
2088
2089         i2c_del_driver(&ssif_i2c_driver);
2090
2091         platform_driver_unregister(&ipmi_driver);
2092
2093         free_ssif_clients();
2094 }
2095 module_exit(cleanup_ipmi_ssif);
2096
2097 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2098 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2099 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2100 MODULE_LICENSE("GPL");