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