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