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