GNU Linux-libre 4.14.259-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 ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
820                         /* Error getting event, probably done. */
821                         msg->done(msg);
822
823                         /* Take off the event flag. */
824                         ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
825                         handle_flags(ssif_info, flags);
826                 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
827                            || msg->rsp[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD) {
828                         pr_warn(PFX "Invalid response getting events: %x %x\n",
829                                 msg->rsp[0], msg->rsp[1]);
830                         msg->done(msg);
831                         /* Take off the event flag. */
832                         ssif_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
833                         handle_flags(ssif_info, flags);
834                 } else {
835                         handle_flags(ssif_info, flags);
836                         ssif_inc_stat(ssif_info, events);
837                         deliver_recv_msg(ssif_info, msg);
838                 }
839                 break;
840
841         case SSIF_GETTING_MESSAGES:
842                 if ((result < 0) || (len < 3) || (msg->rsp[2] != 0)) {
843                         /* Error getting event, probably done. */
844                         msg->done(msg);
845
846                         /* Take off the msg flag. */
847                         ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
848                         handle_flags(ssif_info, flags);
849                 } else if (msg->rsp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2
850                            || msg->rsp[1] != IPMI_GET_MSG_CMD) {
851                         pr_warn(PFX "Invalid response clearing flags: %x %x\n",
852                                 msg->rsp[0], msg->rsp[1]);
853                         msg->done(msg);
854
855                         /* Take off the msg flag. */
856                         ssif_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
857                         handle_flags(ssif_info, flags);
858                 } else {
859                         ssif_inc_stat(ssif_info, incoming_messages);
860                         handle_flags(ssif_info, flags);
861                         deliver_recv_msg(ssif_info, msg);
862                 }
863                 break;
864         }
865
866         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
867         if (SSIF_IDLE(ssif_info) && !ssif_info->stopping) {
868                 if (ssif_info->req_events)
869                         start_event_fetch(ssif_info, flags);
870                 else if (ssif_info->req_flags)
871                         start_flag_fetch(ssif_info, flags);
872                 else
873                         start_next_msg(ssif_info, flags);
874         } else
875                 ipmi_ssif_unlock_cond(ssif_info, flags);
876
877         if (ssif_info->ssif_debug & SSIF_DEBUG_STATE)
878                 pr_info(PFX "DONE 2: state = %d.\n", ssif_info->ssif_state);
879 }
880
881 static void msg_written_handler(struct ssif_info *ssif_info, int result,
882                                 unsigned char *data, unsigned int len)
883 {
884         int rv;
885
886         /* We are single-threaded here, so no need for a lock. */
887         if (result < 0) {
888                 ssif_info->retries_left--;
889                 if (ssif_info->retries_left > 0) {
890                         if (!start_resend(ssif_info)) {
891                                 ssif_inc_stat(ssif_info, send_retries);
892                                 return;
893                         }
894                         /* request failed, just return the error. */
895                         ssif_inc_stat(ssif_info, send_errors);
896
897                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
898                                 pr_info(PFX
899                                         "Out of retries in msg_written_handler\n");
900                         msg_done_handler(ssif_info, -EIO, NULL, 0);
901                         return;
902                 }
903
904                 ssif_inc_stat(ssif_info, send_errors);
905
906                 /*
907                  * Got an error on transmit, let the done routine
908                  * handle it.
909                  */
910                 if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
911                         pr_info("Error in msg_written_handler: %d\n", result);
912
913                 msg_done_handler(ssif_info, result, NULL, 0);
914                 return;
915         }
916
917         if (ssif_info->multi_data) {
918                 /*
919                  * In the middle of a multi-data write.  See the comment
920                  * in the SSIF_MULTI_n_PART case in the probe function
921                  * for details on the intricacies of this.
922                  */
923                 int left;
924                 unsigned char *data_to_send;
925
926                 ssif_inc_stat(ssif_info, sent_messages_parts);
927
928                 left = ssif_info->multi_len - ssif_info->multi_pos;
929                 if (left > 32)
930                         left = 32;
931                 /* Length byte. */
932                 ssif_info->multi_data[ssif_info->multi_pos] = left;
933                 data_to_send = ssif_info->multi_data + ssif_info->multi_pos;
934                 ssif_info->multi_pos += left;
935                 if (left < 32)
936                         /*
937                          * Write is finished.  Note that we must end
938                          * with a write of less than 32 bytes to
939                          * complete the transaction, even if it is
940                          * zero bytes.
941                          */
942                         ssif_info->multi_data = NULL;
943
944                 rv = ssif_i2c_send(ssif_info, msg_written_handler,
945                                   I2C_SMBUS_WRITE,
946                                   SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE,
947                                   data_to_send,
948                                   I2C_SMBUS_BLOCK_DATA);
949                 if (rv < 0) {
950                         /* request failed, just return the error. */
951                         ssif_inc_stat(ssif_info, send_errors);
952
953                         if (ssif_info->ssif_debug & SSIF_DEBUG_MSG)
954                                 pr_info("Error from i2c_non_blocking_op(3)\n");
955                         msg_done_handler(ssif_info, -EIO, NULL, 0);
956                 }
957         } else {
958                 /* Ready to request the result. */
959                 unsigned long oflags, *flags;
960
961                 ssif_inc_stat(ssif_info, sent_messages);
962                 ssif_inc_stat(ssif_info, sent_messages_parts);
963
964                 flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
965                 if (ssif_info->got_alert) {
966                         /* The result is already ready, just start it. */
967                         ssif_info->got_alert = false;
968                         ipmi_ssif_unlock_cond(ssif_info, flags);
969                         start_get(ssif_info);
970                 } else {
971                         /* Wait a jiffie then request the next message */
972                         ssif_info->waiting_alert = true;
973                         ssif_info->retries_left = SSIF_RECV_RETRIES;
974                         ssif_info->rtc_us_timer = SSIF_MSG_PART_USEC;
975                         if (!ssif_info->stopping)
976                                 mod_timer(&ssif_info->retry_timer,
977                                           jiffies + SSIF_MSG_PART_JIFFIES);
978                         ipmi_ssif_unlock_cond(ssif_info, flags);
979                 }
980         }
981 }
982
983 static int start_resend(struct ssif_info *ssif_info)
984 {
985         int rv;
986         int command;
987
988         ssif_info->got_alert = false;
989
990         if (ssif_info->data_len > 32) {
991                 command = SSIF_IPMI_MULTI_PART_REQUEST_START;
992                 ssif_info->multi_data = ssif_info->data;
993                 ssif_info->multi_len = ssif_info->data_len;
994                 /*
995                  * Subtle thing, this is 32, not 33, because we will
996                  * overwrite the thing at position 32 (which was just
997                  * transmitted) with the new length.
998                  */
999                 ssif_info->multi_pos = 32;
1000                 ssif_info->data[0] = 32;
1001         } else {
1002                 ssif_info->multi_data = NULL;
1003                 command = SSIF_IPMI_REQUEST;
1004                 ssif_info->data[0] = ssif_info->data_len;
1005         }
1006
1007         rv = ssif_i2c_send(ssif_info, msg_written_handler, I2C_SMBUS_WRITE,
1008                           command, ssif_info->data, I2C_SMBUS_BLOCK_DATA);
1009         if (rv && (ssif_info->ssif_debug & SSIF_DEBUG_MSG))
1010                 pr_info("Error from i2c_non_blocking_op(4)\n");
1011         return rv;
1012 }
1013
1014 static int start_send(struct ssif_info *ssif_info,
1015                       unsigned char   *data,
1016                       unsigned int    len)
1017 {
1018         if (len > IPMI_MAX_MSG_LENGTH)
1019                 return -E2BIG;
1020         if (len > ssif_info->max_xmit_msg_size)
1021                 return -E2BIG;
1022
1023         ssif_info->retries_left = SSIF_SEND_RETRIES;
1024         memcpy(ssif_info->data + 1, data, len);
1025         ssif_info->data_len = len;
1026         return start_resend(ssif_info);
1027 }
1028
1029 /* Must be called with the message lock held. */
1030 static void start_next_msg(struct ssif_info *ssif_info, unsigned long *flags)
1031 {
1032         struct ipmi_smi_msg *msg;
1033         unsigned long oflags;
1034
1035  restart:
1036         if (!SSIF_IDLE(ssif_info)) {
1037                 ipmi_ssif_unlock_cond(ssif_info, flags);
1038                 return;
1039         }
1040
1041         if (!ssif_info->waiting_msg) {
1042                 ssif_info->curr_msg = NULL;
1043                 ipmi_ssif_unlock_cond(ssif_info, flags);
1044         } else {
1045                 int rv;
1046
1047                 ssif_info->curr_msg = ssif_info->waiting_msg;
1048                 ssif_info->waiting_msg = NULL;
1049                 ipmi_ssif_unlock_cond(ssif_info, flags);
1050                 rv = start_send(ssif_info,
1051                                 ssif_info->curr_msg->data,
1052                                 ssif_info->curr_msg->data_size);
1053                 if (rv) {
1054                         msg = ssif_info->curr_msg;
1055                         ssif_info->curr_msg = NULL;
1056                         return_hosed_msg(ssif_info, msg);
1057                         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1058                         goto restart;
1059                 }
1060         }
1061 }
1062
1063 static void sender(void                *send_info,
1064                    struct ipmi_smi_msg *msg)
1065 {
1066         struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1067         unsigned long oflags, *flags;
1068
1069         BUG_ON(ssif_info->waiting_msg);
1070         ssif_info->waiting_msg = msg;
1071
1072         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1073         start_next_msg(ssif_info, flags);
1074
1075         if (ssif_info->ssif_debug & SSIF_DEBUG_TIMING) {
1076                 struct timespec64 t;
1077
1078                 ktime_get_real_ts64(&t);
1079                 pr_info("**Enqueue %02x %02x: %lld.%6.6ld\n",
1080                        msg->data[0], msg->data[1],
1081                        (long long) t.tv_sec, (long) t.tv_nsec / NSEC_PER_USEC);
1082         }
1083 }
1084
1085 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1086 {
1087         struct ssif_info *ssif_info = send_info;
1088
1089         data->addr_src = ssif_info->addr_source;
1090         data->dev = &ssif_info->client->dev;
1091         data->addr_info = ssif_info->addr_info;
1092         get_device(data->dev);
1093
1094         return 0;
1095 }
1096
1097 /*
1098  * Instead of having our own timer to periodically check the message
1099  * flags, we let the message handler drive us.
1100  */
1101 static void request_events(void *send_info)
1102 {
1103         struct ssif_info *ssif_info = (struct ssif_info *) send_info;
1104         unsigned long oflags, *flags;
1105
1106         if (!ssif_info->has_event_buffer)
1107                 return;
1108
1109         flags = ipmi_ssif_lock_cond(ssif_info, &oflags);
1110         /*
1111          * Request flags first, not events, because the lower layer
1112          * doesn't have a way to send an attention.  But make sure
1113          * event checking still happens.
1114          */
1115         ssif_info->req_events = true;
1116         if (SSIF_IDLE(ssif_info))
1117                 start_flag_fetch(ssif_info, flags);
1118         else {
1119                 ssif_info->req_flags = true;
1120                 ipmi_ssif_unlock_cond(ssif_info, flags);
1121         }
1122 }
1123
1124 static int inc_usecount(void *send_info)
1125 {
1126         struct ssif_info *ssif_info = send_info;
1127
1128         if (!i2c_get_adapter(i2c_adapter_id(ssif_info->client->adapter)))
1129                 return -ENODEV;
1130
1131         i2c_use_client(ssif_info->client);
1132         return 0;
1133 }
1134
1135 static void dec_usecount(void *send_info)
1136 {
1137         struct ssif_info *ssif_info = send_info;
1138
1139         i2c_release_client(ssif_info->client);
1140         i2c_put_adapter(ssif_info->client->adapter);
1141 }
1142
1143 static int ssif_start_processing(void *send_info,
1144                                  ipmi_smi_t intf)
1145 {
1146         struct ssif_info *ssif_info = send_info;
1147
1148         ssif_info->intf = intf;
1149
1150         return 0;
1151 }
1152
1153 #define MAX_SSIF_BMCS 4
1154
1155 static unsigned short addr[MAX_SSIF_BMCS];
1156 static int num_addrs;
1157 module_param_array(addr, ushort, &num_addrs, 0);
1158 MODULE_PARM_DESC(addr, "The addresses to scan for IPMI BMCs on the SSIFs.");
1159
1160 static char *adapter_name[MAX_SSIF_BMCS];
1161 static int num_adapter_names;
1162 module_param_array(adapter_name, charp, &num_adapter_names, 0);
1163 MODULE_PARM_DESC(adapter_name, "The string name of the I2C device that has the BMC.  By default all devices are scanned.");
1164
1165 static int slave_addrs[MAX_SSIF_BMCS];
1166 static int num_slave_addrs;
1167 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1168 MODULE_PARM_DESC(slave_addrs,
1169                  "The default IPMB slave address for the controller.");
1170
1171 static bool alerts_broken;
1172 module_param(alerts_broken, bool, 0);
1173 MODULE_PARM_DESC(alerts_broken, "Don't enable alerts for the controller.");
1174
1175 /*
1176  * Bit 0 enables message debugging, bit 1 enables state debugging, and
1177  * bit 2 enables timing debugging.  This is an array indexed by
1178  * interface number"
1179  */
1180 static int dbg[MAX_SSIF_BMCS];
1181 static int num_dbg;
1182 module_param_array(dbg, int, &num_dbg, 0);
1183 MODULE_PARM_DESC(dbg, "Turn on debugging.");
1184
1185 static bool ssif_dbg_probe;
1186 module_param_named(dbg_probe, ssif_dbg_probe, bool, 0);
1187 MODULE_PARM_DESC(dbg_probe, "Enable debugging of probing of adapters.");
1188
1189 static bool ssif_tryacpi = true;
1190 module_param_named(tryacpi, ssif_tryacpi, bool, 0);
1191 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1192
1193 static bool ssif_trydmi = true;
1194 module_param_named(trydmi, ssif_trydmi, bool, 0);
1195 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1196
1197 static DEFINE_MUTEX(ssif_infos_mutex);
1198 static LIST_HEAD(ssif_infos);
1199
1200 static int ssif_remove(struct i2c_client *client)
1201 {
1202         struct ssif_info *ssif_info = i2c_get_clientdata(client);
1203         struct ssif_addr_info *addr_info;
1204         int rv;
1205
1206         if (!ssif_info)
1207                 return 0;
1208
1209         /*
1210          * After this point, we won't deliver anything asychronously
1211          * to the message handler.  We can unregister ourself.
1212          */
1213         rv = ipmi_unregister_smi(ssif_info->intf);
1214         if (rv) {
1215                 pr_err(PFX "Unable to unregister device: errno=%d\n", rv);
1216                 return rv;
1217         }
1218         ssif_info->intf = NULL;
1219
1220         /* make sure the driver is not looking for flags any more. */
1221         while (ssif_info->ssif_state != SSIF_NORMAL)
1222                 schedule_timeout(1);
1223
1224         ssif_info->stopping = true;
1225         del_timer_sync(&ssif_info->retry_timer);
1226         if (ssif_info->thread) {
1227                 complete(&ssif_info->wake_thread);
1228                 kthread_stop(ssif_info->thread);
1229         }
1230
1231         list_for_each_entry(addr_info, &ssif_infos, link) {
1232                 if (addr_info->client == client) {
1233                         addr_info->client = NULL;
1234                         break;
1235                 }
1236         }
1237
1238         /*
1239          * No message can be outstanding now, we have removed the
1240          * upper layer and it permitted us to do so.
1241          */
1242         kfree(ssif_info);
1243         return 0;
1244 }
1245
1246 static int do_cmd(struct i2c_client *client, int len, unsigned char *msg,
1247                   int *resp_len, unsigned char *resp)
1248 {
1249         int retry_cnt;
1250         int ret;
1251
1252         retry_cnt = SSIF_SEND_RETRIES;
1253  retry1:
1254         ret = i2c_smbus_write_block_data(client, SSIF_IPMI_REQUEST, len, msg);
1255         if (ret) {
1256                 retry_cnt--;
1257                 if (retry_cnt > 0)
1258                         goto retry1;
1259                 return -ENODEV;
1260         }
1261
1262         ret = -ENODEV;
1263         retry_cnt = SSIF_RECV_RETRIES;
1264         while (retry_cnt > 0) {
1265                 ret = i2c_smbus_read_block_data(client, SSIF_IPMI_RESPONSE,
1266                                                 resp);
1267                 if (ret > 0)
1268                         break;
1269                 msleep(SSIF_MSG_MSEC);
1270                 retry_cnt--;
1271                 if (retry_cnt <= 0)
1272                         break;
1273         }
1274
1275         if (ret > 0) {
1276                 /* Validate that the response is correct. */
1277                 if (ret < 3 ||
1278                     (resp[0] != (msg[0] | (1 << 2))) ||
1279                     (resp[1] != msg[1]))
1280                         ret = -EINVAL;
1281                 else {
1282                         *resp_len = ret;
1283                         ret = 0;
1284                 }
1285         }
1286
1287         return ret;
1288 }
1289
1290 static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1291 {
1292         unsigned char *resp;
1293         unsigned char msg[3];
1294         int           rv;
1295         int           len;
1296
1297         resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1298         if (!resp)
1299                 return -ENOMEM;
1300
1301         /* Do a Get Device ID command, since it is required. */
1302         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1303         msg[1] = IPMI_GET_DEVICE_ID_CMD;
1304         rv = do_cmd(client, 2, msg, &len, resp);
1305         if (rv)
1306                 rv = -ENODEV;
1307         else
1308                 strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
1309         kfree(resp);
1310         return rv;
1311 }
1312
1313 static int smi_type_proc_show(struct seq_file *m, void *v)
1314 {
1315         seq_puts(m, "ssif\n");
1316
1317         return 0;
1318 }
1319
1320 static int smi_type_proc_open(struct inode *inode, struct file *file)
1321 {
1322         return single_open(file, smi_type_proc_show, inode->i_private);
1323 }
1324
1325 static const struct file_operations smi_type_proc_ops = {
1326         .open           = smi_type_proc_open,
1327         .read           = seq_read,
1328         .llseek         = seq_lseek,
1329         .release        = single_release,
1330 };
1331
1332 static int smi_stats_proc_show(struct seq_file *m, void *v)
1333 {
1334         struct ssif_info *ssif_info = m->private;
1335
1336         seq_printf(m, "sent_messages:          %u\n",
1337                    ssif_get_stat(ssif_info, sent_messages));
1338         seq_printf(m, "sent_messages_parts:    %u\n",
1339                    ssif_get_stat(ssif_info, sent_messages_parts));
1340         seq_printf(m, "send_retries:           %u\n",
1341                    ssif_get_stat(ssif_info, send_retries));
1342         seq_printf(m, "send_errors:            %u\n",
1343                    ssif_get_stat(ssif_info, send_errors));
1344         seq_printf(m, "received_messages:      %u\n",
1345                    ssif_get_stat(ssif_info, received_messages));
1346         seq_printf(m, "received_message_parts: %u\n",
1347                    ssif_get_stat(ssif_info, received_message_parts));
1348         seq_printf(m, "receive_retries:        %u\n",
1349                    ssif_get_stat(ssif_info, receive_retries));
1350         seq_printf(m, "receive_errors:         %u\n",
1351                    ssif_get_stat(ssif_info, receive_errors));
1352         seq_printf(m, "flag_fetches:           %u\n",
1353                    ssif_get_stat(ssif_info, flag_fetches));
1354         seq_printf(m, "hosed:                  %u\n",
1355                    ssif_get_stat(ssif_info, hosed));
1356         seq_printf(m, "events:                 %u\n",
1357                    ssif_get_stat(ssif_info, events));
1358         seq_printf(m, "watchdog_pretimeouts:   %u\n",
1359                    ssif_get_stat(ssif_info, watchdog_pretimeouts));
1360         seq_printf(m, "alerts:                 %u\n",
1361                    ssif_get_stat(ssif_info, alerts));
1362         return 0;
1363 }
1364
1365 static int smi_stats_proc_open(struct inode *inode, struct file *file)
1366 {
1367         return single_open(file, smi_stats_proc_show, PDE_DATA(inode));
1368 }
1369
1370 static const struct file_operations smi_stats_proc_ops = {
1371         .open           = smi_stats_proc_open,
1372         .read           = seq_read,
1373         .llseek         = seq_lseek,
1374         .release        = single_release,
1375 };
1376
1377 static int strcmp_nospace(char *s1, char *s2)
1378 {
1379         while (*s1 && *s2) {
1380                 while (isspace(*s1))
1381                         s1++;
1382                 while (isspace(*s2))
1383                         s2++;
1384                 if (*s1 > *s2)
1385                         return 1;
1386                 if (*s1 < *s2)
1387                         return -1;
1388                 s1++;
1389                 s2++;
1390         }
1391         return 0;
1392 }
1393
1394 static struct ssif_addr_info *ssif_info_find(unsigned short addr,
1395                                              char *adapter_name,
1396                                              bool match_null_name)
1397 {
1398         struct ssif_addr_info *info, *found = NULL;
1399
1400 restart:
1401         list_for_each_entry(info, &ssif_infos, link) {
1402                 if (info->binfo.addr == addr) {
1403                         if (info->adapter_name || adapter_name) {
1404                                 if (!info->adapter_name != !adapter_name) {
1405                                         /* One is NULL and one is not */
1406                                         continue;
1407                                 }
1408                                 if (adapter_name &&
1409                                     strcmp_nospace(info->adapter_name,
1410                                                    adapter_name))
1411                                         /* Names do not match */
1412                                         continue;
1413                         }
1414                         found = info;
1415                         break;
1416                 }
1417         }
1418
1419         if (!found && match_null_name) {
1420                 /* Try to get an exact match first, then try with a NULL name */
1421                 adapter_name = NULL;
1422                 match_null_name = false;
1423                 goto restart;
1424         }
1425
1426         return found;
1427 }
1428
1429 static bool check_acpi(struct ssif_info *ssif_info, struct device *dev)
1430 {
1431 #ifdef CONFIG_ACPI
1432         acpi_handle acpi_handle;
1433
1434         acpi_handle = ACPI_HANDLE(dev);
1435         if (acpi_handle) {
1436                 ssif_info->addr_source = SI_ACPI;
1437                 ssif_info->addr_info.acpi_info.acpi_handle = acpi_handle;
1438                 return true;
1439         }
1440 #endif
1441         return false;
1442 }
1443
1444 static int find_slave_address(struct i2c_client *client, int slave_addr)
1445 {
1446 #ifdef CONFIG_IPMI_DMI_DECODE
1447         if (!slave_addr)
1448                 slave_addr = ipmi_dmi_get_slave_addr(
1449                         IPMI_DMI_TYPE_SSIF,
1450                         i2c_adapter_id(client->adapter),
1451                         client->addr);
1452 #endif
1453
1454         return slave_addr;
1455 }
1456
1457 /*
1458  * Global enables we care about.
1459  */
1460 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1461                              IPMI_BMC_EVT_MSG_INTR)
1462
1463 static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1464 {
1465         unsigned char     msg[3];
1466         unsigned char     *resp;
1467         struct ssif_info   *ssif_info;
1468         int               rv = 0;
1469         int               len;
1470         int               i;
1471         u8                slave_addr = 0;
1472         struct ssif_addr_info *addr_info = NULL;
1473
1474         resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
1475         if (!resp)
1476                 return -ENOMEM;
1477
1478         ssif_info = kzalloc(sizeof(*ssif_info), GFP_KERNEL);
1479         if (!ssif_info) {
1480                 kfree(resp);
1481                 return -ENOMEM;
1482         }
1483
1484         if (!check_acpi(ssif_info, &client->dev)) {
1485                 addr_info = ssif_info_find(client->addr, client->adapter->name,
1486                                            true);
1487                 if (!addr_info) {
1488                         /* Must have come in through sysfs. */
1489                         ssif_info->addr_source = SI_HOTMOD;
1490                 } else {
1491                         ssif_info->addr_source = addr_info->addr_src;
1492                         ssif_info->ssif_debug = addr_info->debug;
1493                         ssif_info->addr_info = addr_info->addr_info;
1494                         addr_info->client = client;
1495                         slave_addr = addr_info->slave_addr;
1496                 }
1497         }
1498
1499         slave_addr = find_slave_address(client, slave_addr);
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         setup_timer(&ssif_info->retry_timer, retry_timeout,
1672                     (unsigned long)ssif_info);
1673
1674         for (i = 0; i < SSIF_NUM_STATS; i++)
1675                 atomic_set(&ssif_info->stats[i], 0);
1676
1677         if (ssif_info->supports_pec)
1678                 ssif_info->client->flags |= I2C_CLIENT_PEC;
1679
1680         ssif_info->handlers.owner = THIS_MODULE;
1681         ssif_info->handlers.start_processing = ssif_start_processing;
1682         ssif_info->handlers.get_smi_info = get_smi_info;
1683         ssif_info->handlers.sender = sender;
1684         ssif_info->handlers.request_events = request_events;
1685         ssif_info->handlers.inc_usecount = inc_usecount;
1686         ssif_info->handlers.dec_usecount = dec_usecount;
1687
1688         {
1689                 unsigned int thread_num;
1690
1691                 thread_num = ((i2c_adapter_id(ssif_info->client->adapter)
1692                                << 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                 if (addr_info)
1735                         addr_info->client = NULL;
1736
1737                 dev_err(&client->dev, "Unable to start IPMI SSIF: %d\n", rv);
1738                 kfree(ssif_info);
1739         }
1740         kfree(resp);
1741         return rv;
1742
1743  out_err_unreg:
1744         ipmi_unregister_smi(ssif_info->intf);
1745         goto out;
1746 }
1747
1748 static int ssif_adapter_handler(struct device *adev, void *opaque)
1749 {
1750         struct ssif_addr_info *addr_info = opaque;
1751
1752         if (adev->type != &i2c_adapter_type)
1753                 return 0;
1754
1755         addr_info->added_client = i2c_new_device(to_i2c_adapter(adev),
1756                                                  &addr_info->binfo);
1757
1758         if (!addr_info->adapter_name)
1759                 return 1; /* Only try the first I2C adapter by default. */
1760         return 0;
1761 }
1762
1763 static int new_ssif_client(int addr, char *adapter_name,
1764                            int debug, int slave_addr,
1765                            enum ipmi_addr_src addr_src,
1766                            struct device *dev)
1767 {
1768         struct ssif_addr_info *addr_info;
1769         int rv = 0;
1770
1771         mutex_lock(&ssif_infos_mutex);
1772         if (ssif_info_find(addr, adapter_name, false)) {
1773                 rv = -EEXIST;
1774                 goto out_unlock;
1775         }
1776
1777         addr_info = kzalloc(sizeof(*addr_info), GFP_KERNEL);
1778         if (!addr_info) {
1779                 rv = -ENOMEM;
1780                 goto out_unlock;
1781         }
1782
1783         if (adapter_name) {
1784                 addr_info->adapter_name = kstrdup(adapter_name, GFP_KERNEL);
1785                 if (!addr_info->adapter_name) {
1786                         kfree(addr_info);
1787                         rv = -ENOMEM;
1788                         goto out_unlock;
1789                 }
1790         }
1791
1792         strncpy(addr_info->binfo.type, DEVICE_NAME,
1793                 sizeof(addr_info->binfo.type));
1794         addr_info->binfo.addr = addr;
1795         addr_info->binfo.platform_data = addr_info;
1796         addr_info->debug = debug;
1797         addr_info->slave_addr = slave_addr;
1798         addr_info->addr_src = addr_src;
1799         addr_info->dev = dev;
1800
1801         if (dev)
1802                 dev_set_drvdata(dev, addr_info);
1803
1804         list_add_tail(&addr_info->link, &ssif_infos);
1805
1806         if (initialized)
1807                 i2c_for_each_dev(addr_info, ssif_adapter_handler);
1808         /* Otherwise address list will get it */
1809
1810 out_unlock:
1811         mutex_unlock(&ssif_infos_mutex);
1812         return rv;
1813 }
1814
1815 static void free_ssif_clients(void)
1816 {
1817         struct ssif_addr_info *info, *tmp;
1818
1819         mutex_lock(&ssif_infos_mutex);
1820         list_for_each_entry_safe(info, tmp, &ssif_infos, link) {
1821                 list_del(&info->link);
1822                 kfree(info->adapter_name);
1823                 kfree(info);
1824         }
1825         mutex_unlock(&ssif_infos_mutex);
1826 }
1827
1828 static unsigned short *ssif_address_list(void)
1829 {
1830         struct ssif_addr_info *info;
1831         unsigned int count = 0, i;
1832         unsigned short *address_list;
1833
1834         list_for_each_entry(info, &ssif_infos, link)
1835                 count++;
1836
1837         address_list = kzalloc(sizeof(*address_list) * (count + 1), GFP_KERNEL);
1838         if (!address_list)
1839                 return NULL;
1840
1841         i = 0;
1842         list_for_each_entry(info, &ssif_infos, link) {
1843                 unsigned short addr = info->binfo.addr;
1844                 int j;
1845
1846                 for (j = 0; j < i; j++) {
1847                         if (address_list[j] == addr)
1848                                 goto skip_addr;
1849                 }
1850                 address_list[i] = addr;
1851 skip_addr:
1852                 i++;
1853         }
1854         address_list[i] = I2C_CLIENT_END;
1855
1856         return address_list;
1857 }
1858
1859 #ifdef CONFIG_ACPI
1860 static const struct acpi_device_id ssif_acpi_match[] = {
1861         { "IPI0001", 0 },
1862         { },
1863 };
1864 MODULE_DEVICE_TABLE(acpi, ssif_acpi_match);
1865
1866 /*
1867  * Once we get an ACPI failure, we don't try any more, because we go
1868  * through the tables sequentially.  Once we don't find a table, there
1869  * are no more.
1870  */
1871 static int acpi_failure;
1872
1873 /*
1874  * Defined in the IPMI 2.0 spec.
1875  */
1876 struct SPMITable {
1877         s8      Signature[4];
1878         u32     Length;
1879         u8      Revision;
1880         u8      Checksum;
1881         s8      OEMID[6];
1882         s8      OEMTableID[8];
1883         s8      OEMRevision[4];
1884         s8      CreatorID[4];
1885         s8      CreatorRevision[4];
1886         u8      InterfaceType;
1887         u8      IPMIlegacy;
1888         s16     SpecificationRevision;
1889
1890         /*
1891          * Bit 0 - SCI interrupt supported
1892          * Bit 1 - I/O APIC/SAPIC
1893          */
1894         u8      InterruptType;
1895
1896         /*
1897          * If bit 0 of InterruptType is set, then this is the SCI
1898          * interrupt in the GPEx_STS register.
1899          */
1900         u8      GPE;
1901
1902         s16     Reserved;
1903
1904         /*
1905          * If bit 1 of InterruptType is set, then this is the I/O
1906          * APIC/SAPIC interrupt.
1907          */
1908         u32     GlobalSystemInterrupt;
1909
1910         /* The actual register address. */
1911         struct acpi_generic_address addr;
1912
1913         u8      UID[4];
1914
1915         s8      spmi_id[1]; /* A '\0' terminated array starts here. */
1916 };
1917
1918 static int try_init_spmi(struct SPMITable *spmi)
1919 {
1920         unsigned short myaddr;
1921
1922         if (num_addrs >= MAX_SSIF_BMCS)
1923                 return -1;
1924
1925         if (spmi->IPMIlegacy != 1) {
1926                 pr_warn("IPMI: Bad SPMI legacy: %d\n", spmi->IPMIlegacy);
1927                 return -ENODEV;
1928         }
1929
1930         if (spmi->InterfaceType != 4)
1931                 return -ENODEV;
1932
1933         if (spmi->addr.space_id != ACPI_ADR_SPACE_SMBUS) {
1934                 pr_warn(PFX "Invalid ACPI SSIF I/O Address type: %d\n",
1935                         spmi->addr.space_id);
1936                 return -EIO;
1937         }
1938
1939         myaddr = spmi->addr.address & 0x7f;
1940
1941         return new_ssif_client(myaddr, NULL, 0, 0, SI_SPMI, NULL);
1942 }
1943
1944 static void spmi_find_bmc(void)
1945 {
1946         acpi_status      status;
1947         struct SPMITable *spmi;
1948         int              i;
1949
1950         if (acpi_disabled)
1951                 return;
1952
1953         if (acpi_failure)
1954                 return;
1955
1956         for (i = 0; ; i++) {
1957                 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
1958                                         (struct acpi_table_header **)&spmi);
1959                 if (status != AE_OK)
1960                         return;
1961
1962                 try_init_spmi(spmi);
1963         }
1964 }
1965 #else
1966 static void spmi_find_bmc(void) { }
1967 #endif
1968
1969 #ifdef CONFIG_DMI
1970 static int dmi_ipmi_probe(struct platform_device *pdev)
1971 {
1972         u8 type, slave_addr = 0;
1973         u16 i2c_addr;
1974         int rv;
1975
1976         if (!ssif_trydmi)
1977                 return -ENODEV;
1978
1979         rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type);
1980         if (rv)
1981                 return -ENODEV;
1982
1983         if (type != IPMI_DMI_TYPE_SSIF)
1984                 return -ENODEV;
1985
1986         rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
1987         if (rv) {
1988                 dev_warn(&pdev->dev, PFX "No i2c-addr property\n");
1989                 return -ENODEV;
1990         }
1991
1992         rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
1993         if (rv)
1994                 dev_warn(&pdev->dev, "device has no slave-addr property");
1995
1996         return new_ssif_client(i2c_addr, NULL, 0,
1997                                slave_addr, SI_SMBIOS, &pdev->dev);
1998 }
1999 #else
2000 static int dmi_ipmi_probe(struct platform_device *pdev)
2001 {
2002         return -ENODEV;
2003 }
2004 #endif
2005
2006 static const struct i2c_device_id ssif_id[] = {
2007         { DEVICE_NAME, 0 },
2008         { }
2009 };
2010 MODULE_DEVICE_TABLE(i2c, ssif_id);
2011
2012 static struct i2c_driver ssif_i2c_driver = {
2013         .class          = I2C_CLASS_HWMON,
2014         .driver         = {
2015                 .name                   = DEVICE_NAME
2016         },
2017         .probe          = ssif_probe,
2018         .remove         = ssif_remove,
2019         .alert          = ssif_alert,
2020         .id_table       = ssif_id,
2021         .detect         = ssif_detect
2022 };
2023
2024 static int ssif_platform_probe(struct platform_device *dev)
2025 {
2026         return dmi_ipmi_probe(dev);
2027 }
2028
2029 static int ssif_platform_remove(struct platform_device *dev)
2030 {
2031         struct ssif_addr_info *addr_info = dev_get_drvdata(&dev->dev);
2032
2033         if (!addr_info)
2034                 return 0;
2035
2036         mutex_lock(&ssif_infos_mutex);
2037         if (addr_info->added_client)
2038                 i2c_unregister_device(addr_info->added_client);
2039
2040         list_del(&addr_info->link);
2041         kfree(addr_info);
2042         mutex_unlock(&ssif_infos_mutex);
2043         return 0;
2044 }
2045
2046 static struct platform_driver ipmi_driver = {
2047         .driver = {
2048                 .name = DEVICE_NAME,
2049         },
2050         .probe          = ssif_platform_probe,
2051         .remove         = ssif_platform_remove,
2052 };
2053
2054 static int init_ipmi_ssif(void)
2055 {
2056         int i;
2057         int rv;
2058
2059         if (initialized)
2060                 return 0;
2061
2062         pr_info("IPMI SSIF Interface driver\n");
2063
2064         /* build list for i2c from addr list */
2065         for (i = 0; i < num_addrs; i++) {
2066                 rv = new_ssif_client(addr[i], adapter_name[i],
2067                                      dbg[i], slave_addrs[i],
2068                                      SI_HARDCODED, NULL);
2069                 if (rv)
2070                         pr_err(PFX
2071                                "Couldn't add hardcoded device at addr 0x%x\n",
2072                                addr[i]);
2073         }
2074
2075         if (ssif_tryacpi)
2076                 ssif_i2c_driver.driver.acpi_match_table =
2077                         ACPI_PTR(ssif_acpi_match);
2078
2079         if (ssif_tryacpi)
2080                 spmi_find_bmc();
2081
2082         if (ssif_trydmi) {
2083                 rv = platform_driver_register(&ipmi_driver);
2084                 if (rv)
2085                         pr_err(PFX "Unable to register driver: %d\n", rv);
2086         }
2087
2088         ssif_i2c_driver.address_list = ssif_address_list();
2089
2090         rv = i2c_add_driver(&ssif_i2c_driver);
2091         if (!rv)
2092                 initialized = true;
2093
2094         return rv;
2095 }
2096 module_init(init_ipmi_ssif);
2097
2098 static void cleanup_ipmi_ssif(void)
2099 {
2100         if (!initialized)
2101                 return;
2102
2103         initialized = false;
2104
2105         i2c_del_driver(&ssif_i2c_driver);
2106
2107         platform_driver_unregister(&ipmi_driver);
2108
2109         free_ssif_clients();
2110 }
2111 module_exit(cleanup_ipmi_ssif);
2112
2113 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2114 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2115 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2116 MODULE_LICENSE("GPL");