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