GNU Linux-libre 4.4.283-gnu1
[releases.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                         if (!ssif_info->stopping)
617                                 mod_timer(&ssif_info->retry_timer,
618                                           jiffies + SSIF_MSG_JIFFIES);
619                         ipmi_ssif_unlock_cond(ssif_info, flags);
620                         return;
621                 }
622
623                 ssif_inc_stat(ssif_info, receive_errors);
624
625                 if  (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
626                         pr_info("Error in msg_done_handler: %d\n", result);
627                 len = 0;
628                 goto continue_op;
629         }
630
631         if ((len > 1) && (ssif_info->multi_pos == 0)
632                                 && (data[0] == 0x00) && (data[1] == 0x01)) {
633                 /* Start of multi-part read.  Start the next transaction. */
634                 int i;
635
636                 ssif_inc_stat(ssif_info, received_message_parts);
637
638                 /* Remove the multi-part read marker. */
639                 len -= 2;
640                 data += 2;
641                 for (i = 0; i < len; i++)
642                         ssif_info->data[i] = data[i];
643                 ssif_info->multi_len = len;
644                 ssif_info->multi_pos = 1;
645
646                 rv = ssif_i2c_send(ssif_info, msg_done_handler, I2C_SMBUS_READ,
647                                   SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
648                                   ssif_info->recv, I2C_SMBUS_BLOCK_DATA);
649                 if (rv < 0) {
650                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
651                                 pr_info("Error from i2c_non_blocking_op(1)\n");
652
653                         result = -EIO;
654                 } else
655                         return;
656         } else if (ssif_info->multi_pos) {
657                 /* Middle of multi-part read.  Start the next transaction. */
658                 int i;
659                 unsigned char blocknum;
660
661                 if (len == 0) {
662                         result = -EIO;
663                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
664                                 pr_info(PFX "Middle message with no data\n");
665
666                         goto continue_op;
667                 }
668
669                 blocknum = data[0];
670                 len--;
671                 data++;
672
673                 if (blocknum != 0xff && len != 31) {
674                     /* All blocks but the last must have 31 data bytes. */
675                         result = -EIO;
676                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
677                                 pr_info("Received middle message <31\n");
678
679                         goto continue_op;
680                 }
681
682                 if (ssif_info->multi_len + len > IPMI_MAX_MSG_LENGTH) {
683                         /* Received message too big, abort the operation. */
684                         result = -E2BIG;
685                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
686                                 pr_info("Received message too big\n");
687
688                         goto continue_op;
689                 }
690
691                 for (i = 0; i < len; i++)
692                         ssif_info->data[i + ssif_info->multi_len] = data[i];
693                 ssif_info->multi_len += len;
694                 if (blocknum == 0xff) {
695                         /* End of read */
696                         len = ssif_info->multi_len;
697                         data = ssif_info->data;
698                 } else if (blocknum + 1 != ssif_info->multi_pos) {
699                         /*
700                          * Out of sequence block, just abort.  Block
701                          * numbers start at zero for the second block,
702                          * but multi_pos starts at one, so the +1.
703                          */
704                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
705                                 dev_dbg(&ssif_info->client->dev,
706                                         "Received message out of sequence, expected %u, got %u\n",
707                                         ssif_info->multi_pos - 1, blocknum);
708                         result = -EIO;
709                 } else {
710                         ssif_inc_stat(ssif_info, received_message_parts);
711
712                         ssif_info->multi_pos++;
713
714                         rv = ssif_i2c_send(ssif_info, msg_done_handler,
715                                            I2C_SMBUS_READ,
716                                            SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE,
717                                            ssif_info->recv,
718                                            I2C_SMBUS_BLOCK_DATA);
719                         if (rv < 0) {
720                                 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
721                                         pr_info(PFX
722                                                 "Error from ssif_i2c_send\n");
723
724                                 result = -EIO;
725                         } else
726                                 return;
727                 }
728         }
729
730  continue_op:
731         if (result < 0) {
732                 ssif_inc_stat(ssif_info, receive_errors);
733         } else {
734                 ssif_inc_stat(ssif_info, received_messages);
735                 ssif_inc_stat(ssif_info, received_message_parts);
736         }
737
738         if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
739                 pr_info(PFX "DONE 1: state = %d, result=%d.\n",
740                         ssif_info->ssif_state, result);
741
742         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
743         msg = ssif_info->curr_msg;
744         if (msg) {
745                 if (data) {
746                         if (len > IPMI_MAX_MSG_LENGTH)
747                                 len = IPMI_MAX_MSG_LENGTH;
748                         memcpy(msg->rsp, data, len);
749                 } else {
750                         len = 0;
751                 }
752                 msg->rsp_size = len;
753                 ssif_info->curr_msg = NULL;
754         }
755
756         switch (ssif_info->ssif_state) {
757         case SSIF_NORMAL:
758                 ipmi_ssif_unlock_cond(ssif_info, flags);
759                 if (!msg)
760                         break;
761
762                 if (result < 0)
763                         return_hosed_msg(ssif_info, msg);
764                 else
765                         deliver_recv_msg(ssif_info, msg);
766                 break;
767
768         case SSIF_GETTING_FLAGS:
769                 /* We got the flags from the SSIF, now handle them. */
770                 if ((result < 0) || (len < 4) || (data[2] != 0)) {
771                         /*
772                          * Error fetching flags, or invalid length,
773                          * just give up for now.
774                          */
775                         ssif_info->ssif_state = SSIF_NORMAL;
776                         ipmi_ssif_unlock_cond(ssif_info, flags);
777                         pr_warn(PFX "Error getting flags: %d %d, %x\n",
778                                result, len, (len >= 3) ? data[2] : 0);
779                 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
780                            || data[1] != IPMI_GET_MSG_FLAGS_CMD) {
781                         /*
782                          * Don't abort here, maybe it was a queued
783                          * response to a previous command.
784                          */
785                         ipmi_ssif_unlock_cond(ssif_info, flags);
786                         pr_warn(PFX "Invalid response getting flags: %x %x\n",
787                                 data[0], data[1]);
788                 } else {
789                         ssif_inc_stat(ssif_info, flag_fetches);
790                         ssif_info->msg_flags = data[3];
791                         handle_flags(ssif_info, flags);
792                 }
793                 break;
794
795         case SSIF_CLEARING_FLAGS:
796                 /* We cleared the flags. */
797                 if ((result < 0) || (len < 3) || (data[2] != 0)) {
798                         /* Error clearing flags */
799                         pr_warn(PFX "Error clearing flags: %d %d, %x\n",
800                                result, len, (len >= 3) ? data[2] : 0);
801                 } else if (data[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
802                            || data[1] != IPMI_CLEAR_MSG_FLAGS_CMD) {
803                         pr_warn(PFX "Invalid response clearing flags: %x %x\n",
804                                 data[0], data[1]);
805                 }
806                 ssif_info->ssif_state = SSIF_NORMAL;
807                 ipmi_ssif_unlock_cond(ssif_info, flags);
808                 break;
809
810         case SSIF_GETTING_EVENTS:
811                 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
812                         /* Error getting event, probably done. */
813                         msg->done(msg);
814
815                         /* Take off the event flag. */
816                         ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
817                         handle_flags(ssif_info, flags);
818                 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
819                            || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
820                         pr_warn(PFX "Invalid response getting events: %x %x\n",
821                                 msg->rsp[0], msg->rsp[1]);
822                         msg->done(msg);
823                         /* Take off the event flag. */
824                         ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
825                         handle_flags(ssif_info, flags);
826                 } else {
827                         handle_flags(ssif_info, flags);
828                         ssif_inc_stat(ssif_info, events);
829                         deliver_recv_msg(ssif_info, msg);
830                 }
831                 break;
832
833         case SSIF_GETTING_MESSAGES:
834                 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
835                         /* Error getting event, probably done. */
836                         msg->done(msg);
837
838                         /* Take off the msg flag. */
839                         ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
840                         handle_flags(ssif_info, flags);
841                 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
842                            || msg->rsp[1] != IPMI_GET_MSG_CMD) {
843                         pr_warn(PFX "Invalid response clearing flags: %x %x\n",
844                                 msg->rsp[0], msg->rsp[1]);
845                         msg->done(msg);
846
847                         /* Take off the msg flag. */
848                         ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
849                         handle_flags(ssif_info, flags);
850                 } else {
851                         ssif_inc_stat(ssif_info, incoming_messages);
852                         handle_flags(ssif_info, flags);
853                         deliver_recv_msg(ssif_info, msg);
854                 }
855                 break;
856         }
857
858         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
859         if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
860                 if (ssif_info->req_events)
861                         start_event_fetch(ssif_info, flags);
862                 else if (ssif_info->req_flags)
863                         start_flag_fetch(ssif_info, flags);
864                 else
865                         start_next_msg(ssif_info, flags);
866         } else
867                 ipmi_ssif_unlock_cond(ssif_info, flags);
868
869         if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
870                 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
871 }
872
873 static void msg_written_handler(struct ssif_info *ssif_info, int result,
874                                 unsigned char *data, unsigned int len)
875 {
876         int rv;
877
878         /* We are single-threaded here, so no need for a lock. */
879         if (result < 0) {
880                 ssif_info->retries_left--;
881                 if (ssif_info->retries_left > 0) {
882                         if (!start_resend(ssif_info)) {
883                                 ssif_inc_stat(ssif_info, send_retries);
884                                 return;
885                         }
886                         /* request failed, just return the error. */
887                         ssif_inc_stat(ssif_info, send_errors);
888
889                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
890                                 pr_info(PFX
891                                         "Out of retries in msg_written_handler\n");
892                         msg_done_handler(ssif_info, -EIO, NULL, 0);
893                         return;
894                 }
895
896                 ssif_inc_stat(ssif_info, send_errors);
897
898                 /*
899                  * Got an error on transmit, let the done routine
900                  * handle it.
901                  */
902                 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
903                         pr_info("Error in msg_written_handler: %d\n", result);
904
905                 msg_done_handler(ssif_info, result, NULL, 0);
906                 return;
907         }
908
909         if (ssif_info->multi_data) {
910                 /*
911                  * In the middle of a multi-data write.  See the comment
912                  * in the SSIF_MULTI_n_PART case in the probe function
913                  * for details on the intricacies of this.
914                  */
915                 int left;
916                 unsigned char *data_to_send;
917
918                 ssif_inc_stat(ssif_info, sent_messages_parts);
919
920                 left = ssif_info->multi_len - ssif_info->multi_pos;
921                 if (left > 32)
922                         left = 32;
923                 /* Length byte. */
924                 ssif_info->multi_data[ssif_info->multi_pos] = left;
925                 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
926                 ssif_info->multi_pos += left;
927                 if (left < 32)
928                         /*
929                          * Write is finished.  Note that we must end
930                          * with a write of less than 32 bytes to
931                          * complete the transaction, even if it is
932                          * zero bytes.
933                          */
934                         ssif_info->multi_data = NULL;
935
936                 rv = ssif_i2c_send(ssif_info, msg_written_handler,
937                                   I2C_SMBUS_WRITE,
938                                   SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
939                                   data_to_send,
940                                   I2C_SMBUS_BLOCK_DATA);
941                 if (rv < 0) {
942                         /* request failed, just return the error. */
943                         ssif_inc_stat(ssif_info, send_errors);
944
945                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
946                                 pr_info("Error from i2c_non_blocking_op(3)\n");
947                         msg_done_handler(ssif_info, -EIO, NULL, 0);
948                 }
949         } else {
950                 unsigned long oflags, *flags;
951                 bool got_alert;
952
953                 ssif_inc_stat(ssif_info, sent_messages);
954                 ssif_inc_stat(ssif_info, sent_messages_parts);
955
956                 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
957                 got_alert = ssif_info->got_alert;
958                 if (got_alert) {
959                         ssif_info->got_alert = false;
960                         ssif_info->waiting_alert = false;
961                 }
962
963                 if (got_alert) {
964                         ipmi_ssif_unlock_cond(ssif_info, flags);
965                         /* The alert already happened, try now. */
966                         retry_timeout((unsigned long) ssif_info);
967                 } else {
968                         /* Wait a jiffie then request the next message */
969                         ssif_info->waiting_alert = true;
970                         ssif_info->retries_left = SSIF_RECV_RETRIES;
971                         ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
972                         if (!ssif_info->stopping)
973                                 mod_timer(&ssif_info->retry_timer,
974                                           jiffies + SSIF_MSG_PART_JIFFIES);
975                         ipmi_ssif_unlock_cond(ssif_info, flags);
976                 }
977         }
978 }
979
980 static int start_resend(struct ssif_info *ssif_info)
981 {
982         int rv;
983         int command;
984
985         ssif_info->got_alert = false;
986
987         if (ssif_info->data_len > 32) {
988                 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
989                 ssif_info->multi_data = ssif_info->data;
990                 ssif_info->multi_len = ssif_info->data_len;
991                 /*
992                  * Subtle thing, this is 32, not 33, because we will
993                  * overwrite the thing at position 32 (which was just
994                  * transmitted) with the new length.
995                  */
996                 ssif_info->multi_pos = 32;
997                 ssif_info->data[0] = 32;
998         } else {
999                 ssif_info->multi_data = NULL;
1000                 command = SSIF_IPMI_REQUEST;
1001                 ssif_info->data[0] = ssif_info->data_len;
1002         }
1003
1004         rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1005                           command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1006         if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
1007                 pr_info("Error from i2c_non_blocking_op(4)\n");
1008         return rv;
1009 }
1010
1011 static int start_send(struct ssif_info *ssif_info,
1012                       unsigned char   *data,
1013                       unsigned int    len)
1014 {
1015         if (len > IPMI_MAX_MSG_LENGTH)
1016                 return -E2BIG;
1017         if (len > ssif_info->max_xmit_msg_size)
1018                 return -E2BIG;
1019
1020         ssif_info->retries_left = SSIF_SEND_RETRIES;
1021         memcpy(ssif_info->data + 1, data, len);
1022         ssif_info->data_len = len;
1023         return start_resend(ssif_info);
1024 }
1025
1026 /* Must be called with the message lock held. */
1027 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1028 {
1029         struct ipmi_smi_msg *msg;
1030         unsigned long oflags;
1031
1032  restart:
1033         if (!SSIF_IDLE(ssif_info)) {
1034                 ipmi_ssif_unlock_cond(ssif_info, flags);
1035                 return;
1036         }
1037
1038         if (!ssif_info->waiting_msg) {
1039                 ssif_info->curr_msg = NULL;
1040                 ipmi_ssif_unlock_cond(ssif_info, flags);
1041         } else {
1042                 int rv;
1043
1044                 ssif_info->curr_msg = ssif_info->waiting_msg;
1045                 ssif_info->waiting_msg = NULL;
1046                 ipmi_ssif_unlock_cond(ssif_info, flags);
1047                 rv = start_send(ssif_info,
1048                                 ssif_info->curr_msg->data,
1049                                 ssif_info->curr_msg->data_size);
1050                 if (rv) {
1051                         msg = ssif_info->curr_msg;
1052                         ssif_info->curr_msg = NULL;
1053                         return_hosed_msg(ssif_info, msg);
1054                         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1055                         goto restart;
1056                 }
1057         }
1058 }
1059
1060 static void sender(void                *send_info,
1061                    struct ipmi_smi_msg *msg)
1062 {
1063         struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1064         unsigned long oflags, *flags;
1065
1066         BUG_ON(ssif_info->waiting_msg);
1067         ssif_info->waiting_msg = msg;
1068
1069         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1070         start_next_msg(ssif_info, flags);
1071
1072         if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1073                 struct timespec64 t;
1074
1075                 ktime_get_real_ts64(&t);
1076                 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1077                        msg->data[0], msg->data[1],
1078                        (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
1079         }
1080 }
1081
1082 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1083 {
1084         struct ssif_info *ssif_info = send_info;
1085
1086         data->addr_src = ssif_info->addr_source;
1087         data->dev = &ssif_info->client->dev;
1088         data->addr_info = ssif_info->addr_info;
1089         get_device(data->dev);
1090
1091         return 0;
1092 }
1093
1094 /*
1095  * Instead of having our own timer to periodically check the message
1096  * flags, we let the message handler drive us.
1097  */
1098 static void request_events(void *send_info)
1099 {
1100         struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1101         unsigned long oflags, *flags;
1102
1103         if (!ssif_info->has_event_buffer)
1104                 return;
1105
1106         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1107         /*
1108          * Request flags first, not events, because the lower layer
1109          * doesn't have a way to send an attention.  But make sure
1110          * event checking still happens.
1111          */
1112         ssif_info->req_events = true;
1113         if (SSIF_IDLE(ssif_info))
1114                 start_flag_fetch(ssif_info, flags);
1115         else {
1116                 ssif_info->req_flags = true;
1117                 ipmi_ssif_unlock_cond(ssif_info, flags);
1118         }
1119 }
1120
1121 static int inc_usecount(void *send_info)
1122 {
1123         struct ssif_info *ssif_info = send_info;
1124
1125         if (!i2c_get_adapter(ssif_info->client->adapter->nr))
1126                 return -ENODEV;
1127
1128         i2c_use_client(ssif_info->client);
1129         return 0;
1130 }
1131
1132 static void dec_usecount(void *send_info)
1133 {
1134         struct ssif_info *ssif_info = send_info;
1135
1136         i2c_release_client(ssif_info->client);
1137         i2c_put_adapter(ssif_info->client->adapter);
1138 }
1139
1140 static int ssif_start_processing(void *send_info,
1141                                  ipmi_smi_t intf)
1142 {
1143         struct ssif_info *ssif_info = send_info;
1144
1145         ssif_info->intf = intf;
1146
1147         return 0;
1148 }
1149
1150 #define MAX_SSIF_BMCS 4
1151
1152 static unsigned short addr[MAX_SSIF_BMCS];
1153 static int num_addrs;
1154 module_param_array(addr, ushort, &num_addrs, 0);
1155 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1156
1157 static char *adapter_name[MAX_SSIF_BMCS];
1158 static int num_adapter_names;
1159 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1160 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC.  By default all devices are scanned.");
1161
1162 static int slave_addrs[MAX_SSIF_BMCS];
1163 static int num_slave_addrs;
1164 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1165 MODULE_PARM_DESC(slave_addrs,
1166                  "The default IPMB slave address for the controller.");
1167
1168 static bool alerts_broken;
1169 module_param(alerts_broken, bool, 0);
1170 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1171
1172 /*
1173  * Bit 0 enables message debugging, bit 1 enables state debugging, and
1174  * bit 2 enables timing debugging.  This is an array indexed by
1175  * interface number"
1176  */
1177 static int dbg[MAX_SSIF_BMCS];
1178 static int num_dbg;
1179 module_param_array(dbg, int, &num_dbg, 0);
1180 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1181
1182 static bool ssif_dbg_probe;
1183 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1184 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1185
1186 static int use_thread;
1187 module_param(use_thread, int, 0);
1188 MODULE_PARM_DESC(use_thread, "Use the thread interface.");
1189
1190 static bool ssif_tryacpi = true;
1191 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1192 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1193
1194 static bool ssif_trydmi = true;
1195 module_param_named(trydmi, ssif_trydmi, bool, 0);
1196 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1197
1198 static DEFINE_MUTEX(ssif_infos_mutex);
1199 static LIST_HEAD(ssif_infos);
1200
1201 static int ssif_remove(struct i2c_client *client)
1202 {
1203         struct ssif_info *ssif_info = i2c_get_clientdata(client);
1204         int rv;
1205
1206         if (!ssif_info)
1207                 return 0;
1208
1209         /*
1210          * After this point, we won't deliver anything asychronously
1211          * to the message handler.  We can unregister ourself.
1212          */
1213         rv = ipmi_unregister_smi(ssif_info->intf);
1214         if (rv) {
1215                 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1216                 return rv;
1217         }
1218         ssif_info->intf = NULL;
1219
1220         /* make sure the driver is not looking for flags any more. */
1221         while (ssif_info->ssif_state != SSIF_NORMAL)
1222                 schedule_timeout(1);
1223
1224         ssif_info->stopping = true;
1225         del_timer_sync(&ssif_info->retry_timer);
1226         if (ssif_info->thread) {
1227                 complete(&ssif_info->wake_thread);
1228                 kthread_stop(ssif_info->thread);
1229         }
1230
1231         /*
1232          * No message can be outstanding now, we have removed the
1233          * upper layer and it permitted us to do so.
1234          */
1235         kfree(ssif_info);
1236         return 0;
1237 }
1238
1239 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1240                   int *resp_len, unsigned char *resp)
1241 {
1242         int retry_cnt;
1243         int ret;
1244
1245         retry_cnt = SSIF_SEND_RETRIES;
1246  retry1:
1247         ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1248         if (ret) {
1249                 retry_cnt--;
1250                 if (retry_cnt > 0)
1251                         goto retry1;
1252                 return -ENODEV;
1253         }
1254
1255         ret = -ENODEV;
1256         retry_cnt = SSIF_RECV_RETRIES;
1257         while (retry_cnt > 0) {
1258                 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1259                                                 resp);
1260                 if (ret > 0)
1261                         break;
1262                 msleep(SSIF_MSG_MSEC);
1263                 retry_cnt--;
1264                 if (retry_cnt <= 0)
1265                         break;
1266         }
1267
1268         if (ret > 0) {
1269                 /* Validate that the response is correct. */
1270                 if (ret < 3 ||
1271                     (resp[0] != (msg[0] | (1 << 2))) ||
1272                     (resp[1] != msg[1]))
1273                         ret = -EINVAL;
1274                 else {
1275                         *resp_len = ret;
1276                         ret = 0;
1277                 }
1278         }
1279
1280         return ret;
1281 }
1282
1283 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1284 {
1285         unsigned char *resp;
1286         unsigned char msg[3];
1287         int           rv;
1288         int           len;
1289
1290         resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1291         if (!resp)
1292                 return -ENOMEM;
1293
1294         /* Do a Get Device ID command, since it is required. */
1295         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1296         msg[1] = IPMI_GET_DEVICE_ID_CMD;
1297         rv = do_cmd(client, 2, msg, &len, resp);
1298         if (rv)
1299                 rv = -ENODEV;
1300         else
1301                 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1302         kfree(resp);
1303         return rv;
1304 }
1305
1306 static int smi_type_proc_show(struct seq_file *m, void *v)
1307 {
1308         seq_puts(m, "ssif\n");
1309
1310         return 0;
1311 }
1312
1313 static int smi_type_proc_open(struct inode *inode, struct file *file)
1314 {
1315         return single_open(file, smi_type_proc_show, inode->i_private);
1316 }
1317
1318 static const struct file_operations smi_type_proc_ops = {
1319         .open           = smi_type_proc_open,
1320         .read           = seq_read,
1321         .llseek         = seq_lseek,
1322         .release        = single_release,
1323 };
1324
1325 static int smi_stats_proc_show(struct seq_file *m, void *v)
1326 {
1327         struct ssif_info *ssif_info = m->private;
1328
1329         seq_printf(m, "sent_messages:          %u\n",
1330                    ssif_get_stat(ssif_info, sent_messages));
1331         seq_printf(m, "sent_messages_parts:    %u\n",
1332                    ssif_get_stat(ssif_info, sent_messages_parts));
1333         seq_printf(m, "send_retries:           %u\n",
1334                    ssif_get_stat(ssif_info, send_retries));
1335         seq_printf(m, "send_errors:            %u\n",
1336                    ssif_get_stat(ssif_info, send_errors));
1337         seq_printf(m, "received_messages:      %u\n",
1338                    ssif_get_stat(ssif_info, received_messages));
1339         seq_printf(m, "received_message_parts: %u\n",
1340                    ssif_get_stat(ssif_info, received_message_parts));
1341         seq_printf(m, "receive_retries:        %u\n",
1342                    ssif_get_stat(ssif_info, receive_retries));
1343         seq_printf(m, "receive_errors:         %u\n",
1344                    ssif_get_stat(ssif_info, receive_errors));
1345         seq_printf(m, "flag_fetches:           %u\n",
1346                    ssif_get_stat(ssif_info, flag_fetches));
1347         seq_printf(m, "hosed:                  %u\n",
1348                    ssif_get_stat(ssif_info, hosed));
1349         seq_printf(m, "events:                 %u\n",
1350                    ssif_get_stat(ssif_info, events));
1351         seq_printf(m, "watchdog_pretimeouts:   %u\n",
1352                    ssif_get_stat(ssif_info, watchdog_pretimeouts));
1353         seq_printf(m, "alerts:                 %u\n",
1354                    ssif_get_stat(ssif_info, alerts));
1355         return 0;
1356 }
1357
1358 static int smi_stats_proc_open(struct inode *inode, struct file *file)
1359 {
1360         return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1361 }
1362
1363 static const struct file_operations smi_stats_proc_ops = {
1364         .open           = smi_stats_proc_open,
1365         .read           = seq_read,
1366         .llseek         = seq_lseek,
1367         .release        = single_release,
1368 };
1369
1370 static int strcmp_nospace(char *s1, char *s2)
1371 {
1372         while (*s1 && *s2) {
1373                 while (isspace(*s1))
1374                         s1++;
1375                 while (isspace(*s2))
1376                         s2++;
1377                 if (*s1 > *s2)
1378                         return 1;
1379                 if (*s1 < *s2)
1380                         return -1;
1381                 s1++;
1382                 s2++;
1383         }
1384         return 0;
1385 }
1386
1387 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1388                                              char *adapter_name,
1389                                              bool match_null_name)
1390 {
1391         struct ssif_addr_info *info, *found = NULL;
1392
1393 restart:
1394         list_for_each_entry(info, &ssif_infos, link) {
1395                 if (info->binfo.addr == addr) {
1396                         if (info->adapter_name || adapter_name) {
1397                                 if (!info->adapter_name != !adapter_name) {
1398                                         /* One is NULL and one is not */
1399                                         continue;
1400                                 }
1401                                 if (adapter_name &&
1402                                     strcmp_nospace(info->adapter_name,
1403                                                    adapter_name))
1404                                         /* Names do not match */
1405                                         continue;
1406                         }
1407                         found = info;
1408                         break;
1409                 }
1410         }
1411
1412         if (!found && match_null_name) {
1413                 /* Try to get an exact match first, then try with a NULL name */
1414                 adapter_name = NULL;
1415                 match_null_name = false;
1416                 goto restart;
1417         }
1418
1419         return found;
1420 }
1421
1422 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1423 {
1424 #ifdef CONFIG_ACPI
1425         acpi_handle acpi_handle;
1426
1427         acpi_handle = ACPI_HANDLE(dev);
1428         if (acpi_handle) {
1429                 ssif_info->addr_source = SI_ACPI;
1430                 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1431                 return true;
1432         }
1433 #endif
1434         return false;
1435 }
1436
1437 /*
1438  * Global enables we care about.
1439  */
1440 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1441                              IPMI_BMC_EVT_MSG_INTR)
1442
1443 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1444 {
1445         unsigned char     msg[3];
1446         unsigned char     *resp;
1447         struct ssif_info   *ssif_info;
1448         int               rv = 0;
1449         int               len;
1450         int               i;
1451         u8                slave_addr = 0;
1452         struct ssif_addr_info *addr_info = NULL;
1453
1454
1455         resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1456         if (!resp)
1457                 return -ENOMEM;
1458
1459         ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1460         if (!ssif_info) {
1461                 kfree(resp);
1462                 return -ENOMEM;
1463         }
1464
1465         if (!check_acpi(ssif_info, &client->dev)) {
1466                 addr_info = ssif_info_find(client->addr, client->adapter->name,
1467                                            true);
1468                 if (!addr_info) {
1469                         /* Must have come in through sysfs. */
1470                         ssif_info->addr_source = SI_HOTMOD;
1471                 } else {
1472                         ssif_info->addr_source = addr_info->addr_src;
1473                         ssif_info->ssif_debug = addr_info->debug;
1474                         ssif_info->addr_info = addr_info->addr_info;
1475                         slave_addr = addr_info->slave_addr;
1476                 }
1477         }
1478
1479         pr_info(PFX "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1480                ipmi_addr_src_to_str(ssif_info->addr_source),
1481                client->addr, client->adapter->name, slave_addr);
1482
1483         /*
1484          * Do a Get Device ID command, since it comes back with some
1485          * useful info.
1486          */
1487         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1488         msg[1] = IPMI_GET_DEVICE_ID_CMD;
1489         rv = do_cmd(client, 2, msg, &len, resp);
1490         if (rv)
1491                 goto out;
1492
1493         rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1494         if (rv)
1495                 goto out;
1496
1497         ssif_info->client = client;
1498         i2c_set_clientdata(client, ssif_info);
1499
1500         /* Now check for system interface capabilities */
1501         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1502         msg[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD;
1503         msg[2] = 0; /* SSIF */
1504         rv = do_cmd(client, 3, msg, &len, resp);
1505         if (!rv && (len >= 3) && (resp[2] == 0)) {
1506                 if (len < 7) {
1507                         if (ssif_dbg_probe)
1508                                 pr_info(PFX "SSIF info too short: %d\n", len);
1509                         goto no_support;
1510                 }
1511
1512                 /* Got a good SSIF response, handle it. */
1513                 ssif_info->max_xmit_msg_size = resp[5];
1514                 ssif_info->max_recv_msg_size = resp[6];
1515                 ssif_info->multi_support = (resp[4] >> 6) & 0x3;
1516                 ssif_info->supports_pec = (resp[4] >> 3) & 0x1;
1517
1518                 /* Sanitize the data */
1519                 switch (ssif_info->multi_support) {
1520                 case SSIF_NO_MULTI:
1521                         if (ssif_info->max_xmit_msg_size > 32)
1522                                 ssif_info->max_xmit_msg_size = 32;
1523                         if (ssif_info->max_recv_msg_size > 32)
1524                                 ssif_info->max_recv_msg_size = 32;
1525                         break;
1526
1527                 case SSIF_MULTI_2_PART:
1528                         if (ssif_info->max_xmit_msg_size > 63)
1529                                 ssif_info->max_xmit_msg_size = 63;
1530                         if (ssif_info->max_recv_msg_size > 62)
1531                                 ssif_info->max_recv_msg_size = 62;
1532                         break;
1533
1534                 case SSIF_MULTI_n_PART:
1535                         /*
1536                          * The specification is rather confusing at
1537                          * this point, but I think I understand what
1538                          * is meant.  At least I have a workable
1539                          * solution.  With multi-part messages, you
1540                          * cannot send a message that is a multiple of
1541                          * 32-bytes in length, because the start and
1542                          * middle messages are 32-bytes and the end
1543                          * message must be at least one byte.  You
1544                          * can't fudge on an extra byte, that would
1545                          * screw up things like fru data writes.  So
1546                          * we limit the length to 63 bytes.  That way
1547                          * a 32-byte message gets sent as a single
1548                          * part.  A larger message will be a 32-byte
1549                          * start and the next message is always going
1550                          * to be 1-31 bytes in length.  Not ideal, but
1551                          * it should work.
1552                          */
1553                         if (ssif_info->max_xmit_msg_size > 63)
1554                                 ssif_info->max_xmit_msg_size = 63;
1555                         break;
1556
1557                 default:
1558                         /* Data is not sane, just give up. */
1559                         goto no_support;
1560                 }
1561         } else {
1562  no_support:
1563                 /* Assume no multi-part or PEC support */
1564                 pr_info(PFX "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1565                        rv, len, resp[2]);
1566
1567                 ssif_info->max_xmit_msg_size = 32;
1568                 ssif_info->max_recv_msg_size = 32;
1569                 ssif_info->multi_support = SSIF_NO_MULTI;
1570                 ssif_info->supports_pec = 0;
1571         }
1572
1573         /* Make sure the NMI timeout is cleared. */
1574         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1575         msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
1576         msg[2] = WDT_PRE_TIMEOUT_INT;
1577         rv = do_cmd(client, 3, msg, &len, resp);
1578         if (rv || (len < 3) || (resp[2] != 0))
1579                 pr_warn(PFX "Unable to clear message flags: %d %d %2.2x\n",
1580                         rv, len, resp[2]);
1581
1582         /* Attempt to enable the event buffer. */
1583         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1584         msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
1585         rv = do_cmd(client, 2, msg, &len, resp);
1586         if (rv || (len < 4) || (resp[2] != 0)) {
1587                 pr_warn(PFX "Error getting global enables: %d %d %2.2x\n",
1588                         rv, len, resp[2]);
1589                 rv = 0; /* Not fatal */
1590                 goto found;
1591         }
1592
1593         ssif_info->global_enables = resp[3];
1594
1595         if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
1596                 ssif_info->has_event_buffer = true;
1597                 /* buffer is already enabled, nothing to do. */
1598                 goto found;
1599         }
1600
1601         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1602         msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1603         msg[2] = ssif_info->global_enables | IPMI_BMC_EVT_MSG_BUFF;
1604         rv = do_cmd(client, 3, msg, &len, resp);
1605         if (rv || (len < 2)) {
1606                 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1607                         rv, len, resp[2]);
1608                 rv = 0; /* Not fatal */
1609                 goto found;
1610         }
1611
1612         if (resp[2] == 0) {
1613                 /* A successful return means the event buffer is supported. */
1614                 ssif_info->has_event_buffer = true;
1615                 ssif_info->global_enables |= IPMI_BMC_EVT_MSG_BUFF;
1616         }
1617
1618         /* Some systems don't behave well if you enable alerts. */
1619         if (alerts_broken)
1620                 goto found;
1621
1622         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1623         msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
1624         msg[2] = ssif_info->global_enables | IPMI_BMC_RCV_MSG_INTR;
1625         rv = do_cmd(client, 3, msg, &len, resp);
1626         if (rv || (len < 2)) {
1627                 pr_warn(PFX "Error setting global enables: %d %d %2.2x\n",
1628                         rv, len, resp[2]);
1629                 rv = 0; /* Not fatal */
1630                 goto found;
1631         }
1632
1633         if (resp[2] == 0) {
1634                 /* A successful return means the alert is supported. */
1635                 ssif_info->supports_alert = true;
1636                 ssif_info->global_enables |= IPMI_BMC_RCV_MSG_INTR;
1637         }
1638
1639  found:
1640         ssif_info->intf_num = atomic_inc_return(&next_intf);
1641
1642         if (ssif_dbg_probe) {
1643                 pr_info("ssif_probe: i2c_probe found device at i2c address %x\n",
1644                         client->addr);
1645         }
1646
1647         spin_lock_init(&ssif_info->lock);
1648         ssif_info->ssif_state = SSIF_NORMAL;
1649         init_timer(&ssif_info->retry_timer);
1650         ssif_info->retry_timer.data = (unsigned long) ssif_info;
1651         ssif_info->retry_timer.function = retry_timeout;
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 = ((ssif_info->client->adapter->nr << 8) |
1671                               ssif_info->client->addr);
1672                 init_completion(&ssif_info->wake_thread);
1673                 ssif_info->thread = kthread_run(ipmi_ssif_thread, ssif_info,
1674                                                "kssif%4.4x", thread_num);
1675                 if (IS_ERR(ssif_info->thread)) {
1676                         rv = PTR_ERR(ssif_info->thread);
1677                         dev_notice(&ssif_info->client->dev,
1678                                    "Could not start kernel thread: error %d\n",
1679                                    rv);
1680                         goto out;
1681                 }
1682         }
1683
1684         rv = ipmi_register_smi(&ssif_info->handlers,
1685                                ssif_info,
1686                                &ssif_info->device_id,
1687                                &ssif_info->client->dev,
1688                                slave_addr);
1689          if (rv) {
1690                 pr_err(PFX "Unable to register device: error %d\n", rv);
1691                 goto out;
1692         }
1693
1694         rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1695                                      &smi_type_proc_ops,
1696                                      ssif_info);
1697         if (rv) {
1698                 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1699                 goto out_err_unreg;
1700         }
1701
1702         rv = ipmi_smi_add_proc_entry(ssif_info->intf, "ssif_stats",
1703                                      &smi_stats_proc_ops,
1704                                      ssif_info);
1705         if (rv) {
1706                 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1707                 goto out_err_unreg;
1708         }
1709
1710  out:
1711         if (rv)
1712                 kfree(ssif_info);
1713         kfree(resp);
1714         return rv;
1715
1716  out_err_unreg:
1717         ipmi_unregister_smi(ssif_info->intf);
1718         goto out;
1719 }
1720
1721 static int ssif_adapter_handler(struct device *adev, void *opaque)
1722 {
1723         struct ssif_addr_info *addr_info = opaque;
1724
1725         if (adev->type != &i2c_adapter_type)
1726                 return 0;
1727
1728         i2c_new_device(to_i2c_adapter(adev), &addr_info->binfo);
1729
1730         if (!addr_info->adapter_name)
1731                 return 1; /* Only try the first I2C adapter by default. */
1732         return 0;
1733 }
1734
1735 static int new_ssif_client(int addr, char *adapter_name,
1736                            int debug, int slave_addr,
1737                            enum ipmi_addr_src addr_src)
1738 {
1739         struct ssif_addr_info *addr_info;
1740         int rv = 0;
1741
1742         mutex_lock(&ssif_infos_mutex);
1743         if (ssif_info_find(addr, adapter_name, false)) {
1744                 rv = -EEXIST;
1745                 goto out_unlock;
1746         }
1747
1748         addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1749         if (!addr_info) {
1750                 rv = -ENOMEM;
1751                 goto out_unlock;
1752         }
1753
1754         if (adapter_name) {
1755                 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1756                 if (!addr_info->adapter_name) {
1757                         kfree(addr_info);
1758                         rv = -ENOMEM;
1759                         goto out_unlock;
1760                 }
1761         }
1762
1763         strncpy(addr_info->binfo.type, DEVICE_NAME,
1764                 sizeof(addr_info->binfo.type));
1765         addr_info->binfo.addr = addr;
1766         addr_info->binfo.platform_data = addr_info;
1767         addr_info->debug = debug;
1768         addr_info->slave_addr = slave_addr;
1769         addr_info->addr_src = addr_src;
1770
1771         list_add_tail(&addr_info->link, &ssif_infos);
1772
1773         if (initialized)
1774                 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1775         /* Otherwise address list will get it */
1776
1777 out_unlock:
1778         mutex_unlock(&ssif_infos_mutex);
1779         return rv;
1780 }
1781
1782 static void free_ssif_clients(void)
1783 {
1784         struct ssif_addr_info *info, *tmp;
1785
1786         mutex_lock(&ssif_infos_mutex);
1787         list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1788                 list_del(&info->link);
1789                 kfree(info->adapter_name);
1790                 kfree(info);
1791         }
1792         mutex_unlock(&ssif_infos_mutex);
1793 }
1794
1795 static unsigned short *ssif_address_list(void)
1796 {
1797         struct ssif_addr_info *info;
1798         unsigned int count = 0, i;
1799         unsigned short *address_list;
1800
1801         list_for_each_entry(info, &ssif_infos, link)
1802                 count++;
1803
1804         address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1805         if (!address_list)
1806                 return NULL;
1807
1808         i = 0;
1809         list_for_each_entry(info, &ssif_infos, link) {
1810                 unsigned short addr = info->binfo.addr;
1811                 int j;
1812
1813                 for (j = 0; j < i; j++) {
1814                         if (address_list[j] == addr)
1815                                 goto skip_addr;
1816                 }
1817                 address_list[i] = addr;
1818 skip_addr:
1819                 i++;
1820         }
1821         address_list[i] = I2C_CLIENT_END;
1822
1823         return address_list;
1824 }
1825
1826 #ifdef CONFIG_ACPI
1827 static const struct acpi_device_id ssif_acpi_match[] = {
1828         { "IPI0001", 0 },
1829         { },
1830 };
1831 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1832
1833 /*
1834  * Once we get an ACPI failure, we don't try any more, because we go
1835  * through the tables sequentially.  Once we don't find a table, there
1836  * are no more.
1837  */
1838 static int acpi_failure;
1839
1840 /*
1841  * Defined in the IPMI 2.0 spec.
1842  */
1843 struct SPMITable {
1844         s8      Signature[4];
1845         u32     Length;
1846         u8      Revision;
1847         u8      Checksum;
1848         s8      OEMID[6];
1849         s8      OEMTableID[8];
1850         s8      OEMRevision[4];
1851         s8      CreatorID[4];
1852         s8      CreatorRevision[4];
1853         u8      InterfaceType;
1854         u8      IPMIlegacy;
1855         s16     SpecificationRevision;
1856
1857         /*
1858          * Bit 0 - SCI interrupt supported
1859          * Bit 1 - I/O APIC/SAPIC
1860          */
1861         u8      InterruptType;
1862
1863         /*
1864          * If bit 0 of InterruptType is set, then this is the SCI
1865          * interrupt in the GPEx_STS register.
1866          */
1867         u8      GPE;
1868
1869         s16     Reserved;
1870
1871         /*
1872          * If bit 1 of InterruptType is set, then this is the I/O
1873          * APIC/SAPIC interrupt.
1874          */
1875         u32     GlobalSystemInterrupt;
1876
1877         /* The actual register address. */
1878         struct acpi_generic_address addr;
1879
1880         u8      UID[4];
1881
1882         s8      spmi_id[1]; /* A '\0' terminated array starts here. */
1883 };
1884
1885 static int try_init_spmi(struct SPMITable *spmi)
1886 {
1887         unsigned short myaddr;
1888
1889         if (num_addrs >= MAX_SSIF_BMCS)
1890                 return -1;
1891
1892         if (spmi->IPMIlegacy != 1) {
1893                 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1894                 return -ENODEV;
1895         }
1896
1897         if (spmi->InterfaceType != 4)
1898                 return -ENODEV;
1899
1900         if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1901                 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1902                         spmi->addr.space_id);
1903                 return -EIO;
1904         }
1905
1906         myaddr = spmi->addr.address >> 1;
1907
1908         return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI);
1909 }
1910
1911 static void spmi_find_bmc(void)
1912 {
1913         acpi_status      status;
1914         struct SPMITable *spmi;
1915         int              i;
1916
1917         if (acpi_disabled)
1918                 return;
1919
1920         if (acpi_failure)
1921                 return;
1922
1923         for (i = 0; ; i++) {
1924                 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1925                                         (struct acpi_table_header **)&spmi);
1926                 if (status != AE_OK)
1927                         return;
1928
1929                 try_init_spmi(spmi);
1930         }
1931 }
1932 #else
1933 static void spmi_find_bmc(void) { }
1934 #endif
1935
1936 #ifdef CONFIG_DMI
1937 static int decode_dmi(const struct dmi_device *dmi_dev)
1938 {
1939         struct dmi_header *dm = dmi_dev->device_data;
1940         u8             *data = (u8 *) dm;
1941         u8             len = dm->length;
1942         unsigned short myaddr;
1943         int            slave_addr;
1944
1945         if (num_addrs >= MAX_SSIF_BMCS)
1946                 return -1;
1947
1948         if (len < 9)
1949                 return -1;
1950
1951         if (data[0x04] != 4) /* Not SSIF */
1952                 return -1;
1953
1954         if ((data[8] >> 1) == 0) {
1955                 /*
1956                  * Some broken systems put the I2C address in
1957                  * the slave address field.  We try to
1958                  * accommodate them here.
1959                  */
1960                 myaddr = data[6] >> 1;
1961                 slave_addr = 0;
1962         } else {
1963                 myaddr = data[8] >> 1;
1964                 slave_addr = data[6];
1965         }
1966
1967         return new_ssif_client(myaddr, NULL, 0, 0, SI_SMBIOS);
1968 }
1969
1970 static void dmi_iterator(void)
1971 {
1972         const struct dmi_device *dev = NULL;
1973
1974         while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
1975                 decode_dmi(dev);
1976 }
1977 #else
1978 static void dmi_iterator(void) { }
1979 #endif
1980
1981 static const struct i2c_device_id ssif_id[] = {
1982         { DEVICE_NAME, 0 },
1983         { }
1984 };
1985 MODULE_DEVICE_TABLE(i2c, ssif_id);
1986
1987 static struct i2c_driver ssif_i2c_driver = {
1988         .class          = I2C_CLASS_HWMON,
1989         .driver         = {
1990                 .owner                  = THIS_MODULE,
1991                 .name                   = DEVICE_NAME
1992         },
1993         .probe          = ssif_probe,
1994         .remove         = ssif_remove,
1995         .alert          = ssif_alert,
1996         .id_table       = ssif_id,
1997         .detect         = ssif_detect
1998 };
1999
2000 static int init_ipmi_ssif(void)
2001 {
2002         int i;
2003         int rv;
2004
2005         if (initialized)
2006                 return 0;
2007
2008         pr_info("IPMI SSIF Interface driver\n");
2009
2010         /* build list for i2c from addr list */
2011         for (i = 0; i < num_addrs; i++) {
2012                 rv = new_ssif_client(addr[i], adapter_name[i],
2013                                      dbg[i], slave_addrs[i],
2014                                      SI_HARDCODED);
2015                 if (rv)
2016                         pr_err(PFX
2017                                "Couldn't add hardcoded device at addr 0x%x\n",
2018                                addr[i]);
2019         }
2020
2021         if (ssif_tryacpi)
2022                 ssif_i2c_driver.driver.acpi_match_table =
2023                         ACPI_PTR(ssif_acpi_match);
2024         if (ssif_trydmi)
2025                 dmi_iterator();
2026         if (ssif_tryacpi)
2027                 spmi_find_bmc();
2028
2029         ssif_i2c_driver.address_list = ssif_address_list();
2030
2031         rv = i2c_add_driver(&ssif_i2c_driver);
2032         if (!rv)
2033                 initialized = true;
2034
2035         return rv;
2036 }
2037 module_init(init_ipmi_ssif);
2038
2039 static void cleanup_ipmi_ssif(void)
2040 {
2041         if (!initialized)
2042                 return;
2043
2044         initialized = false;
2045
2046         i2c_del_driver(&ssif_i2c_driver);
2047
2048         free_ssif_clients();
2049 }
2050 module_exit(cleanup_ipmi_ssif);
2051
2052 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2053 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2054 MODULE_LICENSE("GPL");