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