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