]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/char/ipmi/ipmi_si_intf.c
25a1436a429181f0526879c0d184c9418a28a1fe
[linux-beck.git] / drivers / char / ipmi / ipmi_si_intf.c
1 /*
2  * ipmi_si.c
3  *
4  * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
5  * BT).
6  *
7  * Author: MontaVista Software, Inc.
8  *         Corey Minyard <minyard@mvista.com>
9  *         source@mvista.com
10  *
11  * Copyright 2002 MontaVista Software Inc.
12  * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
13  *
14  *  This program is free software; you can redistribute it and/or modify it
15  *  under the terms of the GNU General Public License as published by the
16  *  Free Software Foundation; either version 2 of the License, or (at your
17  *  option) any later version.
18  *
19  *
20  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
29  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  *  You should have received a copy of the GNU General Public License along
32  *  with this program; if not, write to the Free Software Foundation, Inc.,
33  *  675 Mass Ave, Cambridge, MA 02139, USA.
34  */
35
36 /*
37  * This file holds the "policy" for the interface to the SMI state
38  * machine.  It does the configuration, handles timers and interrupts,
39  * and drives the real SMI state machine.
40  */
41
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/sched.h>
45 #include <linux/seq_file.h>
46 #include <linux/timer.h>
47 #include <linux/errno.h>
48 #include <linux/spinlock.h>
49 #include <linux/slab.h>
50 #include <linux/delay.h>
51 #include <linux/list.h>
52 #include <linux/pci.h>
53 #include <linux/ioport.h>
54 #include <linux/notifier.h>
55 #include <linux/mutex.h>
56 #include <linux/kthread.h>
57 #include <asm/irq.h>
58 #include <linux/interrupt.h>
59 #include <linux/rcupdate.h>
60 #include <linux/ipmi.h>
61 #include <linux/ipmi_smi.h>
62 #include <asm/io.h>
63 #include "ipmi_si_sm.h"
64 #include <linux/dmi.h>
65 #include <linux/string.h>
66 #include <linux/ctype.h>
67 #include <linux/pnp.h>
68 #include <linux/of_device.h>
69 #include <linux/of_platform.h>
70 #include <linux/of_address.h>
71 #include <linux/of_irq.h>
72
73 #ifdef CONFIG_PARISC
74 #include <asm/hardware.h>       /* for register_parisc_driver() stuff */
75 #include <asm/parisc-device.h>
76 #endif
77
78 #define PFX "ipmi_si: "
79
80 /* Measure times between events in the driver. */
81 #undef DEBUG_TIMING
82
83 /* Call every 10 ms. */
84 #define SI_TIMEOUT_TIME_USEC    10000
85 #define SI_USEC_PER_JIFFY       (1000000/HZ)
86 #define SI_TIMEOUT_JIFFIES      (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
87 #define SI_SHORT_TIMEOUT_USEC  250 /* .25ms when the SM request a
88                                       short timeout */
89
90 enum si_intf_state {
91         SI_NORMAL,
92         SI_GETTING_FLAGS,
93         SI_GETTING_EVENTS,
94         SI_CLEARING_FLAGS,
95         SI_CLEARING_FLAGS_THEN_SET_IRQ,
96         SI_GETTING_MESSAGES,
97         SI_ENABLE_INTERRUPTS1,
98         SI_ENABLE_INTERRUPTS2,
99         SI_DISABLE_INTERRUPTS1,
100         SI_DISABLE_INTERRUPTS2
101         /* FIXME - add watchdog stuff. */
102 };
103
104 /* Some BT-specific defines we need here. */
105 #define IPMI_BT_INTMASK_REG             2
106 #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT   2
107 #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT  1
108
109 enum si_type {
110     SI_KCS, SI_SMIC, SI_BT
111 };
112 static char *si_to_str[] = { "kcs", "smic", "bt" };
113
114 static char *ipmi_addr_src_to_str[] = { NULL, "hotmod", "hardcoded", "SPMI",
115                                         "ACPI", "SMBIOS", "PCI",
116                                         "device-tree", "default" };
117
118 #define DEVICE_NAME "ipmi_si"
119
120 static struct platform_driver ipmi_driver;
121
122 /*
123  * Indexes into stats[] in smi_info below.
124  */
125 enum si_stat_indexes {
126         /*
127          * Number of times the driver requested a timer while an operation
128          * was in progress.
129          */
130         SI_STAT_short_timeouts = 0,
131
132         /*
133          * Number of times the driver requested a timer while nothing was in
134          * progress.
135          */
136         SI_STAT_long_timeouts,
137
138         /* Number of times the interface was idle while being polled. */
139         SI_STAT_idles,
140
141         /* Number of interrupts the driver handled. */
142         SI_STAT_interrupts,
143
144         /* Number of time the driver got an ATTN from the hardware. */
145         SI_STAT_attentions,
146
147         /* Number of times the driver requested flags from the hardware. */
148         SI_STAT_flag_fetches,
149
150         /* Number of times the hardware didn't follow the state machine. */
151         SI_STAT_hosed_count,
152
153         /* Number of completed messages. */
154         SI_STAT_complete_transactions,
155
156         /* Number of IPMI events received from the hardware. */
157         SI_STAT_events,
158
159         /* Number of watchdog pretimeouts. */
160         SI_STAT_watchdog_pretimeouts,
161
162         /* Number of asynchronous messages received. */
163         SI_STAT_incoming_messages,
164
165
166         /* This *must* remain last, add new values above this. */
167         SI_NUM_STATS
168 };
169
170 struct smi_info {
171         int                    intf_num;
172         ipmi_smi_t             intf;
173         struct si_sm_data      *si_sm;
174         struct si_sm_handlers  *handlers;
175         enum si_type           si_type;
176         spinlock_t             si_lock;
177         struct list_head       xmit_msgs;
178         struct list_head       hp_xmit_msgs;
179         struct ipmi_smi_msg    *curr_msg;
180         enum si_intf_state     si_state;
181
182         /*
183          * Used to handle the various types of I/O that can occur with
184          * IPMI
185          */
186         struct si_sm_io io;
187         int (*io_setup)(struct smi_info *info);
188         void (*io_cleanup)(struct smi_info *info);
189         int (*irq_setup)(struct smi_info *info);
190         void (*irq_cleanup)(struct smi_info *info);
191         unsigned int io_size;
192         enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
193         void (*addr_source_cleanup)(struct smi_info *info);
194         void *addr_source_data;
195
196         /*
197          * Per-OEM handler, called from handle_flags().  Returns 1
198          * when handle_flags() needs to be re-run or 0 indicating it
199          * set si_state itself.
200          */
201         int (*oem_data_avail_handler)(struct smi_info *smi_info);
202
203         /*
204          * Flags from the last GET_MSG_FLAGS command, used when an ATTN
205          * is set to hold the flags until we are done handling everything
206          * from the flags.
207          */
208 #define RECEIVE_MSG_AVAIL       0x01
209 #define EVENT_MSG_BUFFER_FULL   0x02
210 #define WDT_PRE_TIMEOUT_INT     0x08
211 #define OEM0_DATA_AVAIL     0x20
212 #define OEM1_DATA_AVAIL     0x40
213 #define OEM2_DATA_AVAIL     0x80
214 #define OEM_DATA_AVAIL      (OEM0_DATA_AVAIL | \
215                              OEM1_DATA_AVAIL | \
216                              OEM2_DATA_AVAIL)
217         unsigned char       msg_flags;
218
219         /* Does the BMC have an event buffer? */
220         char                has_event_buffer;
221
222         /*
223          * If set to true, this will request events the next time the
224          * state machine is idle.
225          */
226         atomic_t            req_events;
227
228         /*
229          * If true, run the state machine to completion on every send
230          * call.  Generally used after a panic to make sure stuff goes
231          * out.
232          */
233         int                 run_to_completion;
234
235         /* The I/O port of an SI interface. */
236         int                 port;
237
238         /*
239          * The space between start addresses of the two ports.  For
240          * instance, if the first port is 0xca2 and the spacing is 4, then
241          * the second port is 0xca6.
242          */
243         unsigned int        spacing;
244
245         /* zero if no irq; */
246         int                 irq;
247
248         /* The timer for this si. */
249         struct timer_list   si_timer;
250
251         /* This flag is set, if the timer is running (timer_pending() isn't enough) */
252         bool                timer_running;
253
254         /* The time (in jiffies) the last timeout occurred at. */
255         unsigned long       last_timeout_jiffies;
256
257         /* Used to gracefully stop the timer without race conditions. */
258         atomic_t            stop_operation;
259
260         /*
261          * The driver will disable interrupts when it gets into a
262          * situation where it cannot handle messages due to lack of
263          * memory.  Once that situation clears up, it will re-enable
264          * interrupts.
265          */
266         int interrupt_disabled;
267
268         /* From the get device id response... */
269         struct ipmi_device_id device_id;
270
271         /* Driver model stuff. */
272         struct device *dev;
273         struct platform_device *pdev;
274
275         /*
276          * True if we allocated the device, false if it came from
277          * someplace else (like PCI).
278          */
279         int dev_registered;
280
281         /* Slave address, could be reported from DMI. */
282         unsigned char slave_addr;
283
284         /* Counters and things for the proc filesystem. */
285         atomic_t stats[SI_NUM_STATS];
286
287         struct task_struct *thread;
288
289         struct list_head link;
290         union ipmi_smi_info_union addr_info;
291 };
292
293 #define smi_inc_stat(smi, stat) \
294         atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
295 #define smi_get_stat(smi, stat) \
296         ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
297
298 #define SI_MAX_PARMS 4
299
300 static int force_kipmid[SI_MAX_PARMS];
301 static int num_force_kipmid;
302 #ifdef CONFIG_PCI
303 static int pci_registered;
304 #endif
305 #ifdef CONFIG_ACPI
306 static int pnp_registered;
307 #endif
308 #ifdef CONFIG_PARISC
309 static int parisc_registered;
310 #endif
311
312 static unsigned int kipmid_max_busy_us[SI_MAX_PARMS];
313 static int num_max_busy_us;
314
315 static int unload_when_empty = 1;
316
317 static int add_smi(struct smi_info *smi);
318 static int try_smi_init(struct smi_info *smi);
319 static void cleanup_one_si(struct smi_info *to_clean);
320 static void cleanup_ipmi_si(void);
321
322 static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
323 static int register_xaction_notifier(struct notifier_block *nb)
324 {
325         return atomic_notifier_chain_register(&xaction_notifier_list, nb);
326 }
327
328 static void deliver_recv_msg(struct smi_info *smi_info,
329                              struct ipmi_smi_msg *msg)
330 {
331         /* Deliver the message to the upper layer. */
332         ipmi_smi_msg_received(smi_info->intf, msg);
333 }
334
335 static void return_hosed_msg(struct smi_info *smi_info, int cCode)
336 {
337         struct ipmi_smi_msg *msg = smi_info->curr_msg;
338
339         if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
340                 cCode = IPMI_ERR_UNSPECIFIED;
341         /* else use it as is */
342
343         /* Make it a response */
344         msg->rsp[0] = msg->data[0] | 4;
345         msg->rsp[1] = msg->data[1];
346         msg->rsp[2] = cCode;
347         msg->rsp_size = 3;
348
349         smi_info->curr_msg = NULL;
350         deliver_recv_msg(smi_info, msg);
351 }
352
353 static enum si_sm_result start_next_msg(struct smi_info *smi_info)
354 {
355         int              rv;
356         struct list_head *entry = NULL;
357 #ifdef DEBUG_TIMING
358         struct timeval t;
359 #endif
360
361         /* Pick the high priority queue first. */
362         if (!list_empty(&(smi_info->hp_xmit_msgs))) {
363                 entry = smi_info->hp_xmit_msgs.next;
364         } else if (!list_empty(&(smi_info->xmit_msgs))) {
365                 entry = smi_info->xmit_msgs.next;
366         }
367
368         if (!entry) {
369                 smi_info->curr_msg = NULL;
370                 rv = SI_SM_IDLE;
371         } else {
372                 int err;
373
374                 list_del(entry);
375                 smi_info->curr_msg = list_entry(entry,
376                                                 struct ipmi_smi_msg,
377                                                 link);
378 #ifdef DEBUG_TIMING
379                 do_gettimeofday(&t);
380                 printk(KERN_DEBUG "**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
381 #endif
382                 err = atomic_notifier_call_chain(&xaction_notifier_list,
383                                 0, smi_info);
384                 if (err & NOTIFY_STOP_MASK) {
385                         rv = SI_SM_CALL_WITHOUT_DELAY;
386                         goto out;
387                 }
388                 err = smi_info->handlers->start_transaction(
389                         smi_info->si_sm,
390                         smi_info->curr_msg->data,
391                         smi_info->curr_msg->data_size);
392                 if (err)
393                         return_hosed_msg(smi_info, err);
394
395                 rv = SI_SM_CALL_WITHOUT_DELAY;
396         }
397  out:
398         return rv;
399 }
400
401 static void start_enable_irq(struct smi_info *smi_info)
402 {
403         unsigned char msg[2];
404
405         /*
406          * If we are enabling interrupts, we have to tell the
407          * BMC to use them.
408          */
409         msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
410         msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
411
412         smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
413         smi_info->si_state = SI_ENABLE_INTERRUPTS1;
414 }
415
416 static void start_disable_irq(struct smi_info *smi_info)
417 {
418         unsigned char msg[2];
419
420         msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
421         msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
422
423         smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
424         smi_info->si_state = SI_DISABLE_INTERRUPTS1;
425 }
426
427 static void start_clear_flags(struct smi_info *smi_info)
428 {
429         unsigned char msg[3];
430
431         /* Make sure the watchdog pre-timeout flag is not set at startup. */
432         msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
433         msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
434         msg[2] = WDT_PRE_TIMEOUT_INT;
435
436         smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
437         smi_info->si_state = SI_CLEARING_FLAGS;
438 }
439
440 static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
441 {
442         smi_info->last_timeout_jiffies = jiffies;
443         mod_timer(&smi_info->si_timer, new_val);
444         smi_info->timer_running = true;
445 }
446
447 /*
448  * When we have a situtaion where we run out of memory and cannot
449  * allocate messages, we just leave them in the BMC and run the system
450  * polled until we can allocate some memory.  Once we have some
451  * memory, we will re-enable the interrupt.
452  */
453 static inline void disable_si_irq(struct smi_info *smi_info)
454 {
455         if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
456                 start_disable_irq(smi_info);
457                 smi_info->interrupt_disabled = 1;
458                 if (!atomic_read(&smi_info->stop_operation))
459                         smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
460         }
461 }
462
463 static inline void enable_si_irq(struct smi_info *smi_info)
464 {
465         if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
466                 start_enable_irq(smi_info);
467                 smi_info->interrupt_disabled = 0;
468         }
469 }
470
471 static void handle_flags(struct smi_info *smi_info)
472 {
473  retry:
474         if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
475                 /* Watchdog pre-timeout */
476                 smi_inc_stat(smi_info, watchdog_pretimeouts);
477
478                 start_clear_flags(smi_info);
479                 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
480                 ipmi_smi_watchdog_pretimeout(smi_info->intf);
481         } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
482                 /* Messages available. */
483                 smi_info->curr_msg = ipmi_alloc_smi_msg();
484                 if (!smi_info->curr_msg) {
485                         disable_si_irq(smi_info);
486                         smi_info->si_state = SI_NORMAL;
487                         return;
488                 }
489                 enable_si_irq(smi_info);
490
491                 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
492                 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
493                 smi_info->curr_msg->data_size = 2;
494
495                 smi_info->handlers->start_transaction(
496                         smi_info->si_sm,
497                         smi_info->curr_msg->data,
498                         smi_info->curr_msg->data_size);
499                 smi_info->si_state = SI_GETTING_MESSAGES;
500         } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
501                 /* Events available. */
502                 smi_info->curr_msg = ipmi_alloc_smi_msg();
503                 if (!smi_info->curr_msg) {
504                         disable_si_irq(smi_info);
505                         smi_info->si_state = SI_NORMAL;
506                         return;
507                 }
508                 enable_si_irq(smi_info);
509
510                 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
511                 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
512                 smi_info->curr_msg->data_size = 2;
513
514                 smi_info->handlers->start_transaction(
515                         smi_info->si_sm,
516                         smi_info->curr_msg->data,
517                         smi_info->curr_msg->data_size);
518                 smi_info->si_state = SI_GETTING_EVENTS;
519         } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
520                    smi_info->oem_data_avail_handler) {
521                 if (smi_info->oem_data_avail_handler(smi_info))
522                         goto retry;
523         } else
524                 smi_info->si_state = SI_NORMAL;
525 }
526
527 static void handle_transaction_done(struct smi_info *smi_info)
528 {
529         struct ipmi_smi_msg *msg;
530 #ifdef DEBUG_TIMING
531         struct timeval t;
532
533         do_gettimeofday(&t);
534         printk(KERN_DEBUG "**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
535 #endif
536         switch (smi_info->si_state) {
537         case SI_NORMAL:
538                 if (!smi_info->curr_msg)
539                         break;
540
541                 smi_info->curr_msg->rsp_size
542                         = smi_info->handlers->get_result(
543                                 smi_info->si_sm,
544                                 smi_info->curr_msg->rsp,
545                                 IPMI_MAX_MSG_LENGTH);
546
547                 /*
548                  * Do this here becase deliver_recv_msg() releases the
549                  * lock, and a new message can be put in during the
550                  * time the lock is released.
551                  */
552                 msg = smi_info->curr_msg;
553                 smi_info->curr_msg = NULL;
554                 deliver_recv_msg(smi_info, msg);
555                 break;
556
557         case SI_GETTING_FLAGS:
558         {
559                 unsigned char msg[4];
560                 unsigned int  len;
561
562                 /* We got the flags from the SMI, now handle them. */
563                 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
564                 if (msg[2] != 0) {
565                         /* Error fetching flags, just give up for now. */
566                         smi_info->si_state = SI_NORMAL;
567                 } else if (len < 4) {
568                         /*
569                          * Hmm, no flags.  That's technically illegal, but
570                          * don't use uninitialized data.
571                          */
572                         smi_info->si_state = SI_NORMAL;
573                 } else {
574                         smi_info->msg_flags = msg[3];
575                         handle_flags(smi_info);
576                 }
577                 break;
578         }
579
580         case SI_CLEARING_FLAGS:
581         case SI_CLEARING_FLAGS_THEN_SET_IRQ:
582         {
583                 unsigned char msg[3];
584
585                 /* We cleared the flags. */
586                 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
587                 if (msg[2] != 0) {
588                         /* Error clearing flags */
589                         dev_warn(smi_info->dev,
590                                  "Error clearing flags: %2.2x\n", msg[2]);
591                 }
592                 if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
593                         start_enable_irq(smi_info);
594                 else
595                         smi_info->si_state = SI_NORMAL;
596                 break;
597         }
598
599         case SI_GETTING_EVENTS:
600         {
601                 smi_info->curr_msg->rsp_size
602                         = smi_info->handlers->get_result(
603                                 smi_info->si_sm,
604                                 smi_info->curr_msg->rsp,
605                                 IPMI_MAX_MSG_LENGTH);
606
607                 /*
608                  * Do this here becase deliver_recv_msg() releases the
609                  * lock, and a new message can be put in during the
610                  * time the lock is released.
611                  */
612                 msg = smi_info->curr_msg;
613                 smi_info->curr_msg = NULL;
614                 if (msg->rsp[2] != 0) {
615                         /* Error getting event, probably done. */
616                         msg->done(msg);
617
618                         /* Take off the event flag. */
619                         smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
620                         handle_flags(smi_info);
621                 } else {
622                         smi_inc_stat(smi_info, events);
623
624                         /*
625                          * Do this before we deliver the message
626                          * because delivering the message releases the
627                          * lock and something else can mess with the
628                          * state.
629                          */
630                         handle_flags(smi_info);
631
632                         deliver_recv_msg(smi_info, msg);
633                 }
634                 break;
635         }
636
637         case SI_GETTING_MESSAGES:
638         {
639                 smi_info->curr_msg->rsp_size
640                         = smi_info->handlers->get_result(
641                                 smi_info->si_sm,
642                                 smi_info->curr_msg->rsp,
643                                 IPMI_MAX_MSG_LENGTH);
644
645                 /*
646                  * Do this here becase deliver_recv_msg() releases the
647                  * lock, and a new message can be put in during the
648                  * time the lock is released.
649                  */
650                 msg = smi_info->curr_msg;
651                 smi_info->curr_msg = NULL;
652                 if (msg->rsp[2] != 0) {
653                         /* Error getting event, probably done. */
654                         msg->done(msg);
655
656                         /* Take off the msg flag. */
657                         smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
658                         handle_flags(smi_info);
659                 } else {
660                         smi_inc_stat(smi_info, incoming_messages);
661
662                         /*
663                          * Do this before we deliver the message
664                          * because delivering the message releases the
665                          * lock and something else can mess with the
666                          * state.
667                          */
668                         handle_flags(smi_info);
669
670                         deliver_recv_msg(smi_info, msg);
671                 }
672                 break;
673         }
674
675         case SI_ENABLE_INTERRUPTS1:
676         {
677                 unsigned char msg[4];
678
679                 /* We got the flags from the SMI, now handle them. */
680                 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
681                 if (msg[2] != 0) {
682                         dev_warn(smi_info->dev,
683                                  "Couldn't get irq info: %x.\n", msg[2]);
684                         dev_warn(smi_info->dev,
685                                  "Maybe ok, but ipmi might run very slowly.\n");
686                         smi_info->si_state = SI_NORMAL;
687                 } else {
688                         msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
689                         msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
690                         msg[2] = (msg[3] |
691                                   IPMI_BMC_RCV_MSG_INTR |
692                                   IPMI_BMC_EVT_MSG_INTR);
693                         smi_info->handlers->start_transaction(
694                                 smi_info->si_sm, msg, 3);
695                         smi_info->si_state = SI_ENABLE_INTERRUPTS2;
696                 }
697                 break;
698         }
699
700         case SI_ENABLE_INTERRUPTS2:
701         {
702                 unsigned char msg[4];
703
704                 /* We got the flags from the SMI, now handle them. */
705                 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
706                 if (msg[2] != 0) {
707                         dev_warn(smi_info->dev,
708                                  "Couldn't set irq info: %x.\n", msg[2]);
709                         dev_warn(smi_info->dev,
710                                  "Maybe ok, but ipmi might run very slowly.\n");
711                 } else
712                         smi_info->interrupt_disabled = 0;
713                 smi_info->si_state = SI_NORMAL;
714                 break;
715         }
716
717         case SI_DISABLE_INTERRUPTS1:
718         {
719                 unsigned char msg[4];
720
721                 /* We got the flags from the SMI, now handle them. */
722                 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
723                 if (msg[2] != 0) {
724                         dev_warn(smi_info->dev, "Could not disable interrupts"
725                                  ", failed get.\n");
726                         smi_info->si_state = SI_NORMAL;
727                 } else {
728                         msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
729                         msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
730                         msg[2] = (msg[3] &
731                                   ~(IPMI_BMC_RCV_MSG_INTR |
732                                     IPMI_BMC_EVT_MSG_INTR));
733                         smi_info->handlers->start_transaction(
734                                 smi_info->si_sm, msg, 3);
735                         smi_info->si_state = SI_DISABLE_INTERRUPTS2;
736                 }
737                 break;
738         }
739
740         case SI_DISABLE_INTERRUPTS2:
741         {
742                 unsigned char msg[4];
743
744                 /* We got the flags from the SMI, now handle them. */
745                 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
746                 if (msg[2] != 0) {
747                         dev_warn(smi_info->dev, "Could not disable interrupts"
748                                  ", failed set.\n");
749                 }
750                 smi_info->si_state = SI_NORMAL;
751                 break;
752         }
753         }
754 }
755
756 /*
757  * Called on timeouts and events.  Timeouts should pass the elapsed
758  * time, interrupts should pass in zero.  Must be called with
759  * si_lock held and interrupts disabled.
760  */
761 static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
762                                            int time)
763 {
764         enum si_sm_result si_sm_result;
765
766  restart:
767         /*
768          * There used to be a loop here that waited a little while
769          * (around 25us) before giving up.  That turned out to be
770          * pointless, the minimum delays I was seeing were in the 300us
771          * range, which is far too long to wait in an interrupt.  So
772          * we just run until the state machine tells us something
773          * happened or it needs a delay.
774          */
775         si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
776         time = 0;
777         while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
778                 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
779
780         if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
781                 smi_inc_stat(smi_info, complete_transactions);
782
783                 handle_transaction_done(smi_info);
784                 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
785         } else if (si_sm_result == SI_SM_HOSED) {
786                 smi_inc_stat(smi_info, hosed_count);
787
788                 /*
789                  * Do the before return_hosed_msg, because that
790                  * releases the lock.
791                  */
792                 smi_info->si_state = SI_NORMAL;
793                 if (smi_info->curr_msg != NULL) {
794                         /*
795                          * If we were handling a user message, format
796                          * a response to send to the upper layer to
797                          * tell it about the error.
798                          */
799                         return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
800                 }
801                 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
802         }
803
804         /*
805          * We prefer handling attn over new messages.  But don't do
806          * this if there is not yet an upper layer to handle anything.
807          */
808         if (likely(smi_info->intf) && si_sm_result == SI_SM_ATTN) {
809                 unsigned char msg[2];
810
811                 smi_inc_stat(smi_info, attentions);
812
813                 /*
814                  * Got a attn, send down a get message flags to see
815                  * what's causing it.  It would be better to handle
816                  * this in the upper layer, but due to the way
817                  * interrupts work with the SMI, that's not really
818                  * possible.
819                  */
820                 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
821                 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
822
823                 smi_info->handlers->start_transaction(
824                         smi_info->si_sm, msg, 2);
825                 smi_info->si_state = SI_GETTING_FLAGS;
826                 goto restart;
827         }
828
829         /* If we are currently idle, try to start the next message. */
830         if (si_sm_result == SI_SM_IDLE) {
831                 smi_inc_stat(smi_info, idles);
832
833                 si_sm_result = start_next_msg(smi_info);
834                 if (si_sm_result != SI_SM_IDLE)
835                         goto restart;
836         }
837
838         if ((si_sm_result == SI_SM_IDLE)
839             && (atomic_read(&smi_info->req_events))) {
840                 /*
841                  * We are idle and the upper layer requested that I fetch
842                  * events, so do so.
843                  */
844                 atomic_set(&smi_info->req_events, 0);
845
846                 smi_info->curr_msg = ipmi_alloc_smi_msg();
847                 if (!smi_info->curr_msg)
848                         goto out;
849
850                 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
851                 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
852                 smi_info->curr_msg->data_size = 2;
853
854                 smi_info->handlers->start_transaction(
855                         smi_info->si_sm,
856                         smi_info->curr_msg->data,
857                         smi_info->curr_msg->data_size);
858                 smi_info->si_state = SI_GETTING_EVENTS;
859                 goto restart;
860         }
861  out:
862         return si_sm_result;
863 }
864
865 static void sender(void                *send_info,
866                    struct ipmi_smi_msg *msg,
867                    int                 priority)
868 {
869         struct smi_info   *smi_info = send_info;
870         enum si_sm_result result;
871         unsigned long     flags;
872 #ifdef DEBUG_TIMING
873         struct timeval    t;
874 #endif
875
876         if (atomic_read(&smi_info->stop_operation)) {
877                 msg->rsp[0] = msg->data[0] | 4;
878                 msg->rsp[1] = msg->data[1];
879                 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
880                 msg->rsp_size = 3;
881                 deliver_recv_msg(smi_info, msg);
882                 return;
883         }
884
885 #ifdef DEBUG_TIMING
886         do_gettimeofday(&t);
887         printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
888 #endif
889
890         if (smi_info->run_to_completion) {
891                 /*
892                  * If we are running to completion, then throw it in
893                  * the list and run transactions until everything is
894                  * clear.  Priority doesn't matter here.
895                  */
896
897                 /*
898                  * Run to completion means we are single-threaded, no
899                  * need for locks.
900                  */
901                 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
902
903                 result = smi_event_handler(smi_info, 0);
904                 while (result != SI_SM_IDLE) {
905                         udelay(SI_SHORT_TIMEOUT_USEC);
906                         result = smi_event_handler(smi_info,
907                                                    SI_SHORT_TIMEOUT_USEC);
908                 }
909                 return;
910         }
911
912         spin_lock_irqsave(&smi_info->si_lock, flags);
913         if (priority > 0)
914                 list_add_tail(&msg->link, &smi_info->hp_xmit_msgs);
915         else
916                 list_add_tail(&msg->link, &smi_info->xmit_msgs);
917
918         if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
919                 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
920
921                 if (smi_info->thread)
922                         wake_up_process(smi_info->thread);
923
924                 start_next_msg(smi_info);
925                 smi_event_handler(smi_info, 0);
926         }
927         spin_unlock_irqrestore(&smi_info->si_lock, flags);
928 }
929
930 static void set_run_to_completion(void *send_info, int i_run_to_completion)
931 {
932         struct smi_info   *smi_info = send_info;
933         enum si_sm_result result;
934
935         smi_info->run_to_completion = i_run_to_completion;
936         if (i_run_to_completion) {
937                 result = smi_event_handler(smi_info, 0);
938                 while (result != SI_SM_IDLE) {
939                         udelay(SI_SHORT_TIMEOUT_USEC);
940                         result = smi_event_handler(smi_info,
941                                                    SI_SHORT_TIMEOUT_USEC);
942                 }
943         }
944 }
945
946 /*
947  * Use -1 in the nsec value of the busy waiting timespec to tell that
948  * we are spinning in kipmid looking for something and not delaying
949  * between checks
950  */
951 static inline void ipmi_si_set_not_busy(struct timespec *ts)
952 {
953         ts->tv_nsec = -1;
954 }
955 static inline int ipmi_si_is_busy(struct timespec *ts)
956 {
957         return ts->tv_nsec != -1;
958 }
959
960 static int ipmi_thread_busy_wait(enum si_sm_result smi_result,
961                                  const struct smi_info *smi_info,
962                                  struct timespec *busy_until)
963 {
964         unsigned int max_busy_us = 0;
965
966         if (smi_info->intf_num < num_max_busy_us)
967                 max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
968         if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
969                 ipmi_si_set_not_busy(busy_until);
970         else if (!ipmi_si_is_busy(busy_until)) {
971                 getnstimeofday(busy_until);
972                 timespec_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
973         } else {
974                 struct timespec now;
975                 getnstimeofday(&now);
976                 if (unlikely(timespec_compare(&now, busy_until) > 0)) {
977                         ipmi_si_set_not_busy(busy_until);
978                         return 0;
979                 }
980         }
981         return 1;
982 }
983
984
985 /*
986  * A busy-waiting loop for speeding up IPMI operation.
987  *
988  * Lousy hardware makes this hard.  This is only enabled for systems
989  * that are not BT and do not have interrupts.  It starts spinning
990  * when an operation is complete or until max_busy tells it to stop
991  * (if that is enabled).  See the paragraph on kimid_max_busy_us in
992  * Documentation/IPMI.txt for details.
993  */
994 static int ipmi_thread(void *data)
995 {
996         struct smi_info *smi_info = data;
997         unsigned long flags;
998         enum si_sm_result smi_result;
999         struct timespec busy_until;
1000
1001         ipmi_si_set_not_busy(&busy_until);
1002         set_user_nice(current, 19);
1003         while (!kthread_should_stop()) {
1004                 int busy_wait;
1005
1006                 spin_lock_irqsave(&(smi_info->si_lock), flags);
1007                 smi_result = smi_event_handler(smi_info, 0);
1008
1009                 /*
1010                  * If the driver is doing something, there is a possible
1011                  * race with the timer.  If the timer handler see idle,
1012                  * and the thread here sees something else, the timer
1013                  * handler won't restart the timer even though it is
1014                  * required.  So start it here if necessary.
1015                  */
1016                 if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
1017                         smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
1018
1019                 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1020                 busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1021                                                   &busy_until);
1022                 if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1023                         ; /* do nothing */
1024                 else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
1025                         schedule();
1026                 else if (smi_result == SI_SM_IDLE)
1027                         schedule_timeout_interruptible(100);
1028                 else
1029                         schedule_timeout_interruptible(1);
1030         }
1031         return 0;
1032 }
1033
1034
1035 static void poll(void *send_info)
1036 {
1037         struct smi_info *smi_info = send_info;
1038         unsigned long flags = 0;
1039         int run_to_completion = smi_info->run_to_completion;
1040
1041         /*
1042          * Make sure there is some delay in the poll loop so we can
1043          * drive time forward and timeout things.
1044          */
1045         udelay(10);
1046         if (!run_to_completion)
1047                 spin_lock_irqsave(&smi_info->si_lock, flags);
1048         smi_event_handler(smi_info, 10);
1049         if (!run_to_completion)
1050                 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1051 }
1052
1053 static void request_events(void *send_info)
1054 {
1055         struct smi_info *smi_info = send_info;
1056
1057         if (atomic_read(&smi_info->stop_operation) ||
1058                                 !smi_info->has_event_buffer)
1059                 return;
1060
1061         atomic_set(&smi_info->req_events, 1);
1062 }
1063
1064 static int initialized;
1065
1066 static void smi_timeout(unsigned long data)
1067 {
1068         struct smi_info   *smi_info = (struct smi_info *) data;
1069         enum si_sm_result smi_result;
1070         unsigned long     flags;
1071         unsigned long     jiffies_now;
1072         long              time_diff;
1073         long              timeout;
1074 #ifdef DEBUG_TIMING
1075         struct timeval    t;
1076 #endif
1077
1078         spin_lock_irqsave(&(smi_info->si_lock), flags);
1079 #ifdef DEBUG_TIMING
1080         do_gettimeofday(&t);
1081         printk(KERN_DEBUG "**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1082 #endif
1083         jiffies_now = jiffies;
1084         time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
1085                      * SI_USEC_PER_JIFFY);
1086         smi_result = smi_event_handler(smi_info, time_diff);
1087
1088         if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
1089                 /* Running with interrupts, only do long timeouts. */
1090                 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1091                 smi_inc_stat(smi_info, long_timeouts);
1092                 goto do_mod_timer;
1093         }
1094
1095         /*
1096          * If the state machine asks for a short delay, then shorten
1097          * the timer timeout.
1098          */
1099         if (smi_result == SI_SM_CALL_WITH_DELAY) {
1100                 smi_inc_stat(smi_info, short_timeouts);
1101                 timeout = jiffies + 1;
1102         } else {
1103                 smi_inc_stat(smi_info, long_timeouts);
1104                 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1105         }
1106
1107  do_mod_timer:
1108         if (smi_result != SI_SM_IDLE)
1109                 smi_mod_timer(smi_info, timeout);
1110         else
1111                 smi_info->timer_running = false;
1112         spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1113 }
1114
1115 static irqreturn_t si_irq_handler(int irq, void *data)
1116 {
1117         struct smi_info *smi_info = data;
1118         unsigned long   flags;
1119 #ifdef DEBUG_TIMING
1120         struct timeval  t;
1121 #endif
1122
1123         spin_lock_irqsave(&(smi_info->si_lock), flags);
1124
1125         smi_inc_stat(smi_info, interrupts);
1126
1127 #ifdef DEBUG_TIMING
1128         do_gettimeofday(&t);
1129         printk(KERN_DEBUG "**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1130 #endif
1131         smi_event_handler(smi_info, 0);
1132         spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1133         return IRQ_HANDLED;
1134 }
1135
1136 static irqreturn_t si_bt_irq_handler(int irq, void *data)
1137 {
1138         struct smi_info *smi_info = data;
1139         /* We need to clear the IRQ flag for the BT interface. */
1140         smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1141                              IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1142                              | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1143         return si_irq_handler(irq, data);
1144 }
1145
1146 static int smi_start_processing(void       *send_info,
1147                                 ipmi_smi_t intf)
1148 {
1149         struct smi_info *new_smi = send_info;
1150         int             enable = 0;
1151
1152         new_smi->intf = intf;
1153
1154         /* Try to claim any interrupts. */
1155         if (new_smi->irq_setup)
1156                 new_smi->irq_setup(new_smi);
1157
1158         /* Set up the timer that drives the interface. */
1159         setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
1160         smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1161
1162         /*
1163          * Check if the user forcefully enabled the daemon.
1164          */
1165         if (new_smi->intf_num < num_force_kipmid)
1166                 enable = force_kipmid[new_smi->intf_num];
1167         /*
1168          * The BT interface is efficient enough to not need a thread,
1169          * and there is no need for a thread if we have interrupts.
1170          */
1171         else if ((new_smi->si_type != SI_BT) && (!new_smi->irq))
1172                 enable = 1;
1173
1174         if (enable) {
1175                 new_smi->thread = kthread_run(ipmi_thread, new_smi,
1176                                               "kipmi%d", new_smi->intf_num);
1177                 if (IS_ERR(new_smi->thread)) {
1178                         dev_notice(new_smi->dev, "Could not start"
1179                                    " kernel thread due to error %ld, only using"
1180                                    " timers to drive the interface\n",
1181                                    PTR_ERR(new_smi->thread));
1182                         new_smi->thread = NULL;
1183                 }
1184         }
1185
1186         return 0;
1187 }
1188
1189 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1190 {
1191         struct smi_info *smi = send_info;
1192
1193         data->addr_src = smi->addr_source;
1194         data->dev = smi->dev;
1195         data->addr_info = smi->addr_info;
1196         get_device(smi->dev);
1197
1198         return 0;
1199 }
1200
1201 static void set_maintenance_mode(void *send_info, int enable)
1202 {
1203         struct smi_info   *smi_info = send_info;
1204
1205         if (!enable)
1206                 atomic_set(&smi_info->req_events, 0);
1207 }
1208
1209 static struct ipmi_smi_handlers handlers = {
1210         .owner                  = THIS_MODULE,
1211         .start_processing       = smi_start_processing,
1212         .get_smi_info           = get_smi_info,
1213         .sender                 = sender,
1214         .request_events         = request_events,
1215         .set_maintenance_mode   = set_maintenance_mode,
1216         .set_run_to_completion  = set_run_to_completion,
1217         .poll                   = poll,
1218 };
1219
1220 /*
1221  * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
1222  * a default IO port, and 1 ACPI/SPMI address.  That sets SI_MAX_DRIVERS.
1223  */
1224
1225 static LIST_HEAD(smi_infos);
1226 static DEFINE_MUTEX(smi_infos_lock);
1227 static int smi_num; /* Used to sequence the SMIs */
1228
1229 #define DEFAULT_REGSPACING      1
1230 #define DEFAULT_REGSIZE         1
1231
1232 #ifdef CONFIG_ACPI
1233 static bool          si_tryacpi = 1;
1234 #endif
1235 #ifdef CONFIG_DMI
1236 static bool          si_trydmi = 1;
1237 #endif
1238 static bool          si_tryplatform = 1;
1239 #ifdef CONFIG_PCI
1240 static bool          si_trypci = 1;
1241 #endif
1242 static bool          si_trydefaults = IS_ENABLED(CONFIG_IPMI_SI_PROBE_DEFAULTS);
1243 static char          *si_type[SI_MAX_PARMS];
1244 #define MAX_SI_TYPE_STR 30
1245 static char          si_type_str[MAX_SI_TYPE_STR];
1246 static unsigned long addrs[SI_MAX_PARMS];
1247 static unsigned int num_addrs;
1248 static unsigned int  ports[SI_MAX_PARMS];
1249 static unsigned int num_ports;
1250 static int           irqs[SI_MAX_PARMS];
1251 static unsigned int num_irqs;
1252 static int           regspacings[SI_MAX_PARMS];
1253 static unsigned int num_regspacings;
1254 static int           regsizes[SI_MAX_PARMS];
1255 static unsigned int num_regsizes;
1256 static int           regshifts[SI_MAX_PARMS];
1257 static unsigned int num_regshifts;
1258 static int slave_addrs[SI_MAX_PARMS]; /* Leaving 0 chooses the default value */
1259 static unsigned int num_slave_addrs;
1260
1261 #define IPMI_IO_ADDR_SPACE  0
1262 #define IPMI_MEM_ADDR_SPACE 1
1263 static char *addr_space_to_str[] = { "i/o", "mem" };
1264
1265 static int hotmod_handler(const char *val, struct kernel_param *kp);
1266
1267 module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
1268 MODULE_PARM_DESC(hotmod, "Add and remove interfaces.  See"
1269                  " Documentation/IPMI.txt in the kernel sources for the"
1270                  " gory details.");
1271
1272 #ifdef CONFIG_ACPI
1273 module_param_named(tryacpi, si_tryacpi, bool, 0);
1274 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1275                  " default scan of the interfaces identified via ACPI");
1276 #endif
1277 #ifdef CONFIG_DMI
1278 module_param_named(trydmi, si_trydmi, bool, 0);
1279 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the"
1280                  " default scan of the interfaces identified via DMI");
1281 #endif
1282 module_param_named(tryplatform, si_tryplatform, bool, 0);
1283 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1284                  " default scan of the interfaces identified via platform"
1285                  " interfaces like openfirmware");
1286 #ifdef CONFIG_PCI
1287 module_param_named(trypci, si_trypci, bool, 0);
1288 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1289                  " default scan of the interfaces identified via pci");
1290 #endif
1291 module_param_named(trydefaults, si_trydefaults, bool, 0);
1292 MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
1293                  " default scan of the KCS and SMIC interface at the standard"
1294                  " address");
1295 module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
1296 MODULE_PARM_DESC(type, "Defines the type of each interface, each"
1297                  " interface separated by commas.  The types are 'kcs',"
1298                  " 'smic', and 'bt'.  For example si_type=kcs,bt will set"
1299                  " the first interface to kcs and the second to bt");
1300 module_param_array(addrs, ulong, &num_addrs, 0);
1301 MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
1302                  " addresses separated by commas.  Only use if an interface"
1303                  " is in memory.  Otherwise, set it to zero or leave"
1304                  " it blank.");
1305 module_param_array(ports, uint, &num_ports, 0);
1306 MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
1307                  " addresses separated by commas.  Only use if an interface"
1308                  " is a port.  Otherwise, set it to zero or leave"
1309                  " it blank.");
1310 module_param_array(irqs, int, &num_irqs, 0);
1311 MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
1312                  " addresses separated by commas.  Only use if an interface"
1313                  " has an interrupt.  Otherwise, set it to zero or leave"
1314                  " it blank.");
1315 module_param_array(regspacings, int, &num_regspacings, 0);
1316 MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
1317                  " and each successive register used by the interface.  For"
1318                  " instance, if the start address is 0xca2 and the spacing"
1319                  " is 2, then the second address is at 0xca4.  Defaults"
1320                  " to 1.");
1321 module_param_array(regsizes, int, &num_regsizes, 0);
1322 MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
1323                  " This should generally be 1, 2, 4, or 8 for an 8-bit,"
1324                  " 16-bit, 32-bit, or 64-bit register.  Use this if you"
1325                  " the 8-bit IPMI register has to be read from a larger"
1326                  " register.");
1327 module_param_array(regshifts, int, &num_regshifts, 0);
1328 MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
1329                  " IPMI register, in bits.  For instance, if the data"
1330                  " is read from a 32-bit word and the IPMI data is in"
1331                  " bit 8-15, then the shift would be 8");
1332 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1333 MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
1334                  " the controller.  Normally this is 0x20, but can be"
1335                  " overridden by this parm.  This is an array indexed"
1336                  " by interface number.");
1337 module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1338 MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1339                  " disabled(0).  Normally the IPMI driver auto-detects"
1340                  " this, but the value may be overridden by this parm.");
1341 module_param(unload_when_empty, int, 0);
1342 MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1343                  " specified or found, default is 1.  Setting to 0"
1344                  " is useful for hot add of devices using hotmod.");
1345 module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1346 MODULE_PARM_DESC(kipmid_max_busy_us,
1347                  "Max time (in microseconds) to busy-wait for IPMI data before"
1348                  " sleeping. 0 (default) means to wait forever. Set to 100-500"
1349                  " if kipmid is using up a lot of CPU time.");
1350
1351
1352 static void std_irq_cleanup(struct smi_info *info)
1353 {
1354         if (info->si_type == SI_BT)
1355                 /* Disable the interrupt in the BT interface. */
1356                 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
1357         free_irq(info->irq, info);
1358 }
1359
1360 static int std_irq_setup(struct smi_info *info)
1361 {
1362         int rv;
1363
1364         if (!info->irq)
1365                 return 0;
1366
1367         if (info->si_type == SI_BT) {
1368                 rv = request_irq(info->irq,
1369                                  si_bt_irq_handler,
1370                                  IRQF_SHARED,
1371                                  DEVICE_NAME,
1372                                  info);
1373                 if (!rv)
1374                         /* Enable the interrupt in the BT interface. */
1375                         info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1376                                          IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1377         } else
1378                 rv = request_irq(info->irq,
1379                                  si_irq_handler,
1380                                  IRQF_SHARED,
1381                                  DEVICE_NAME,
1382                                  info);
1383         if (rv) {
1384                 dev_warn(info->dev, "%s unable to claim interrupt %d,"
1385                          " running polled\n",
1386                          DEVICE_NAME, info->irq);
1387                 info->irq = 0;
1388         } else {
1389                 info->irq_cleanup = std_irq_cleanup;
1390                 dev_info(info->dev, "Using irq %d\n", info->irq);
1391         }
1392
1393         return rv;
1394 }
1395
1396 static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1397 {
1398         unsigned int addr = io->addr_data;
1399
1400         return inb(addr + (offset * io->regspacing));
1401 }
1402
1403 static void port_outb(struct si_sm_io *io, unsigned int offset,
1404                       unsigned char b)
1405 {
1406         unsigned int addr = io->addr_data;
1407
1408         outb(b, addr + (offset * io->regspacing));
1409 }
1410
1411 static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1412 {
1413         unsigned int addr = io->addr_data;
1414
1415         return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1416 }
1417
1418 static void port_outw(struct si_sm_io *io, unsigned int offset,
1419                       unsigned char b)
1420 {
1421         unsigned int addr = io->addr_data;
1422
1423         outw(b << io->regshift, addr + (offset * io->regspacing));
1424 }
1425
1426 static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1427 {
1428         unsigned int addr = io->addr_data;
1429
1430         return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1431 }
1432
1433 static void port_outl(struct si_sm_io *io, unsigned int offset,
1434                       unsigned char b)
1435 {
1436         unsigned int addr = io->addr_data;
1437
1438         outl(b << io->regshift, addr+(offset * io->regspacing));
1439 }
1440
1441 static void port_cleanup(struct smi_info *info)
1442 {
1443         unsigned int addr = info->io.addr_data;
1444         int          idx;
1445
1446         if (addr) {
1447                 for (idx = 0; idx < info->io_size; idx++)
1448                         release_region(addr + idx * info->io.regspacing,
1449                                        info->io.regsize);
1450         }
1451 }
1452
1453 static int port_setup(struct smi_info *info)
1454 {
1455         unsigned int addr = info->io.addr_data;
1456         int          idx;
1457
1458         if (!addr)
1459                 return -ENODEV;
1460
1461         info->io_cleanup = port_cleanup;
1462
1463         /*
1464          * Figure out the actual inb/inw/inl/etc routine to use based
1465          * upon the register size.
1466          */
1467         switch (info->io.regsize) {
1468         case 1:
1469                 info->io.inputb = port_inb;
1470                 info->io.outputb = port_outb;
1471                 break;
1472         case 2:
1473                 info->io.inputb = port_inw;
1474                 info->io.outputb = port_outw;
1475                 break;
1476         case 4:
1477                 info->io.inputb = port_inl;
1478                 info->io.outputb = port_outl;
1479                 break;
1480         default:
1481                 dev_warn(info->dev, "Invalid register size: %d\n",
1482                          info->io.regsize);
1483                 return -EINVAL;
1484         }
1485
1486         /*
1487          * Some BIOSes reserve disjoint I/O regions in their ACPI
1488          * tables.  This causes problems when trying to register the
1489          * entire I/O region.  Therefore we must register each I/O
1490          * port separately.
1491          */
1492         for (idx = 0; idx < info->io_size; idx++) {
1493                 if (request_region(addr + idx * info->io.regspacing,
1494                                    info->io.regsize, DEVICE_NAME) == NULL) {
1495                         /* Undo allocations */
1496                         while (idx--) {
1497                                 release_region(addr + idx * info->io.regspacing,
1498                                                info->io.regsize);
1499                         }
1500                         return -EIO;
1501                 }
1502         }
1503         return 0;
1504 }
1505
1506 static unsigned char intf_mem_inb(struct si_sm_io *io, unsigned int offset)
1507 {
1508         return readb((io->addr)+(offset * io->regspacing));
1509 }
1510
1511 static void intf_mem_outb(struct si_sm_io *io, unsigned int offset,
1512                      unsigned char b)
1513 {
1514         writeb(b, (io->addr)+(offset * io->regspacing));
1515 }
1516
1517 static unsigned char intf_mem_inw(struct si_sm_io *io, unsigned int offset)
1518 {
1519         return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
1520                 & 0xff;
1521 }
1522
1523 static void intf_mem_outw(struct si_sm_io *io, unsigned int offset,
1524                      unsigned char b)
1525 {
1526         writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1527 }
1528
1529 static unsigned char intf_mem_inl(struct si_sm_io *io, unsigned int offset)
1530 {
1531         return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
1532                 & 0xff;
1533 }
1534
1535 static void intf_mem_outl(struct si_sm_io *io, unsigned int offset,
1536                      unsigned char b)
1537 {
1538         writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1539 }
1540
1541 #ifdef readq
1542 static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1543 {
1544         return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
1545                 & 0xff;
1546 }
1547
1548 static void mem_outq(struct si_sm_io *io, unsigned int offset,
1549                      unsigned char b)
1550 {
1551         writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1552 }
1553 #endif
1554
1555 static void mem_cleanup(struct smi_info *info)
1556 {
1557         unsigned long addr = info->io.addr_data;
1558         int           mapsize;
1559
1560         if (info->io.addr) {
1561                 iounmap(info->io.addr);
1562
1563                 mapsize = ((info->io_size * info->io.regspacing)
1564                            - (info->io.regspacing - info->io.regsize));
1565
1566                 release_mem_region(addr, mapsize);
1567         }
1568 }
1569
1570 static int mem_setup(struct smi_info *info)
1571 {
1572         unsigned long addr = info->io.addr_data;
1573         int           mapsize;
1574
1575         if (!addr)
1576                 return -ENODEV;
1577
1578         info->io_cleanup = mem_cleanup;
1579
1580         /*
1581          * Figure out the actual readb/readw/readl/etc routine to use based
1582          * upon the register size.
1583          */
1584         switch (info->io.regsize) {
1585         case 1:
1586                 info->io.inputb = intf_mem_inb;
1587                 info->io.outputb = intf_mem_outb;
1588                 break;
1589         case 2:
1590                 info->io.inputb = intf_mem_inw;
1591                 info->io.outputb = intf_mem_outw;
1592                 break;
1593         case 4:
1594                 info->io.inputb = intf_mem_inl;
1595                 info->io.outputb = intf_mem_outl;
1596                 break;
1597 #ifdef readq
1598         case 8:
1599                 info->io.inputb = mem_inq;
1600                 info->io.outputb = mem_outq;
1601                 break;
1602 #endif
1603         default:
1604                 dev_warn(info->dev, "Invalid register size: %d\n",
1605                          info->io.regsize);
1606                 return -EINVAL;
1607         }
1608
1609         /*
1610          * Calculate the total amount of memory to claim.  This is an
1611          * unusual looking calculation, but it avoids claiming any
1612          * more memory than it has to.  It will claim everything
1613          * between the first address to the end of the last full
1614          * register.
1615          */
1616         mapsize = ((info->io_size * info->io.regspacing)
1617                    - (info->io.regspacing - info->io.regsize));
1618
1619         if (request_mem_region(addr, mapsize, DEVICE_NAME) == NULL)
1620                 return -EIO;
1621
1622         info->io.addr = ioremap(addr, mapsize);
1623         if (info->io.addr == NULL) {
1624                 release_mem_region(addr, mapsize);
1625                 return -EIO;
1626         }
1627         return 0;
1628 }
1629
1630 /*
1631  * Parms come in as <op1>[:op2[:op3...]].  ops are:
1632  *   add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
1633  * Options are:
1634  *   rsp=<regspacing>
1635  *   rsi=<regsize>
1636  *   rsh=<regshift>
1637  *   irq=<irq>
1638  *   ipmb=<ipmb addr>
1639  */
1640 enum hotmod_op { HM_ADD, HM_REMOVE };
1641 struct hotmod_vals {
1642         char *name;
1643         int  val;
1644 };
1645 static struct hotmod_vals hotmod_ops[] = {
1646         { "add",        HM_ADD },
1647         { "remove",     HM_REMOVE },
1648         { NULL }
1649 };
1650 static struct hotmod_vals hotmod_si[] = {
1651         { "kcs",        SI_KCS },
1652         { "smic",       SI_SMIC },
1653         { "bt",         SI_BT },
1654         { NULL }
1655 };
1656 static struct hotmod_vals hotmod_as[] = {
1657         { "mem",        IPMI_MEM_ADDR_SPACE },
1658         { "i/o",        IPMI_IO_ADDR_SPACE },
1659         { NULL }
1660 };
1661
1662 static int parse_str(struct hotmod_vals *v, int *val, char *name, char **curr)
1663 {
1664         char *s;
1665         int  i;
1666
1667         s = strchr(*curr, ',');
1668         if (!s) {
1669                 printk(KERN_WARNING PFX "No hotmod %s given.\n", name);
1670                 return -EINVAL;
1671         }
1672         *s = '\0';
1673         s++;
1674         for (i = 0; hotmod_ops[i].name; i++) {
1675                 if (strcmp(*curr, v[i].name) == 0) {
1676                         *val = v[i].val;
1677                         *curr = s;
1678                         return 0;
1679                 }
1680         }
1681
1682         printk(KERN_WARNING PFX "Invalid hotmod %s '%s'\n", name, *curr);
1683         return -EINVAL;
1684 }
1685
1686 static int check_hotmod_int_op(const char *curr, const char *option,
1687                                const char *name, int *val)
1688 {
1689         char *n;
1690
1691         if (strcmp(curr, name) == 0) {
1692                 if (!option) {
1693                         printk(KERN_WARNING PFX
1694                                "No option given for '%s'\n",
1695                                curr);
1696                         return -EINVAL;
1697                 }
1698                 *val = simple_strtoul(option, &n, 0);
1699                 if ((*n != '\0') || (*option == '\0')) {
1700                         printk(KERN_WARNING PFX
1701                                "Bad option given for '%s'\n",
1702                                curr);
1703                         return -EINVAL;
1704                 }
1705                 return 1;
1706         }
1707         return 0;
1708 }
1709
1710 static struct smi_info *smi_info_alloc(void)
1711 {
1712         struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
1713
1714         if (info)
1715                 spin_lock_init(&info->si_lock);
1716         return info;
1717 }
1718
1719 static int hotmod_handler(const char *val, struct kernel_param *kp)
1720 {
1721         char *str = kstrdup(val, GFP_KERNEL);
1722         int  rv;
1723         char *next, *curr, *s, *n, *o;
1724         enum hotmod_op op;
1725         enum si_type si_type;
1726         int  addr_space;
1727         unsigned long addr;
1728         int regspacing;
1729         int regsize;
1730         int regshift;
1731         int irq;
1732         int ipmb;
1733         int ival;
1734         int len;
1735         struct smi_info *info;
1736
1737         if (!str)
1738                 return -ENOMEM;
1739
1740         /* Kill any trailing spaces, as we can get a "\n" from echo. */
1741         len = strlen(str);
1742         ival = len - 1;
1743         while ((ival >= 0) && isspace(str[ival])) {
1744                 str[ival] = '\0';
1745                 ival--;
1746         }
1747
1748         for (curr = str; curr; curr = next) {
1749                 regspacing = 1;
1750                 regsize = 1;
1751                 regshift = 0;
1752                 irq = 0;
1753                 ipmb = 0; /* Choose the default if not specified */
1754
1755                 next = strchr(curr, ':');
1756                 if (next) {
1757                         *next = '\0';
1758                         next++;
1759                 }
1760
1761                 rv = parse_str(hotmod_ops, &ival, "operation", &curr);
1762                 if (rv)
1763                         break;
1764                 op = ival;
1765
1766                 rv = parse_str(hotmod_si, &ival, "interface type", &curr);
1767                 if (rv)
1768                         break;
1769                 si_type = ival;
1770
1771                 rv = parse_str(hotmod_as, &addr_space, "address space", &curr);
1772                 if (rv)
1773                         break;
1774
1775                 s = strchr(curr, ',');
1776                 if (s) {
1777                         *s = '\0';
1778                         s++;
1779                 }
1780                 addr = simple_strtoul(curr, &n, 0);
1781                 if ((*n != '\0') || (*curr == '\0')) {
1782                         printk(KERN_WARNING PFX "Invalid hotmod address"
1783                                " '%s'\n", curr);
1784                         break;
1785                 }
1786
1787                 while (s) {
1788                         curr = s;
1789                         s = strchr(curr, ',');
1790                         if (s) {
1791                                 *s = '\0';
1792                                 s++;
1793                         }
1794                         o = strchr(curr, '=');
1795                         if (o) {
1796                                 *o = '\0';
1797                                 o++;
1798                         }
1799                         rv = check_hotmod_int_op(curr, o, "rsp", &regspacing);
1800                         if (rv < 0)
1801                                 goto out;
1802                         else if (rv)
1803                                 continue;
1804                         rv = check_hotmod_int_op(curr, o, "rsi", &regsize);
1805                         if (rv < 0)
1806                                 goto out;
1807                         else if (rv)
1808                                 continue;
1809                         rv = check_hotmod_int_op(curr, o, "rsh", &regshift);
1810                         if (rv < 0)
1811                                 goto out;
1812                         else if (rv)
1813                                 continue;
1814                         rv = check_hotmod_int_op(curr, o, "irq", &irq);
1815                         if (rv < 0)
1816                                 goto out;
1817                         else if (rv)
1818                                 continue;
1819                         rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);
1820                         if (rv < 0)
1821                                 goto out;
1822                         else if (rv)
1823                                 continue;
1824
1825                         rv = -EINVAL;
1826                         printk(KERN_WARNING PFX
1827                                "Invalid hotmod option '%s'\n",
1828                                curr);
1829                         goto out;
1830                 }
1831
1832                 if (op == HM_ADD) {
1833                         info = smi_info_alloc();
1834                         if (!info) {
1835                                 rv = -ENOMEM;
1836                                 goto out;
1837                         }
1838
1839                         info->addr_source = SI_HOTMOD;
1840                         info->si_type = si_type;
1841                         info->io.addr_data = addr;
1842                         info->io.addr_type = addr_space;
1843                         if (addr_space == IPMI_MEM_ADDR_SPACE)
1844                                 info->io_setup = mem_setup;
1845                         else
1846                                 info->io_setup = port_setup;
1847
1848                         info->io.addr = NULL;
1849                         info->io.regspacing = regspacing;
1850                         if (!info->io.regspacing)
1851                                 info->io.regspacing = DEFAULT_REGSPACING;
1852                         info->io.regsize = regsize;
1853                         if (!info->io.regsize)
1854                                 info->io.regsize = DEFAULT_REGSPACING;
1855                         info->io.regshift = regshift;
1856                         info->irq = irq;
1857                         if (info->irq)
1858                                 info->irq_setup = std_irq_setup;
1859                         info->slave_addr = ipmb;
1860
1861                         rv = add_smi(info);
1862                         if (rv) {
1863                                 kfree(info);
1864                                 goto out;
1865                         }
1866                         rv = try_smi_init(info);
1867                         if (rv) {
1868                                 cleanup_one_si(info);
1869                                 goto out;
1870                         }
1871                 } else {
1872                         /* remove */
1873                         struct smi_info *e, *tmp_e;
1874
1875                         mutex_lock(&smi_infos_lock);
1876                         list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
1877                                 if (e->io.addr_type != addr_space)
1878                                         continue;
1879                                 if (e->si_type != si_type)
1880                                         continue;
1881                                 if (e->io.addr_data == addr)
1882                                         cleanup_one_si(e);
1883                         }
1884                         mutex_unlock(&smi_infos_lock);
1885                 }
1886         }
1887         rv = len;
1888  out:
1889         kfree(str);
1890         return rv;
1891 }
1892
1893 static int hardcode_find_bmc(void)
1894 {
1895         int ret = -ENODEV;
1896         int             i;
1897         struct smi_info *info;
1898
1899         for (i = 0; i < SI_MAX_PARMS; i++) {
1900                 if (!ports[i] && !addrs[i])
1901                         continue;
1902
1903                 info = smi_info_alloc();
1904                 if (!info)
1905                         return -ENOMEM;
1906
1907                 info->addr_source = SI_HARDCODED;
1908                 printk(KERN_INFO PFX "probing via hardcoded address\n");
1909
1910                 if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
1911                         info->si_type = SI_KCS;
1912                 } else if (strcmp(si_type[i], "smic") == 0) {
1913                         info->si_type = SI_SMIC;
1914                 } else if (strcmp(si_type[i], "bt") == 0) {
1915                         info->si_type = SI_BT;
1916                 } else {
1917                         printk(KERN_WARNING PFX "Interface type specified "
1918                                "for interface %d, was invalid: %s\n",
1919                                i, si_type[i]);
1920                         kfree(info);
1921                         continue;
1922                 }
1923
1924                 if (ports[i]) {
1925                         /* An I/O port */
1926                         info->io_setup = port_setup;
1927                         info->io.addr_data = ports[i];
1928                         info->io.addr_type = IPMI_IO_ADDR_SPACE;
1929                 } else if (addrs[i]) {
1930                         /* A memory port */
1931                         info->io_setup = mem_setup;
1932                         info->io.addr_data = addrs[i];
1933                         info->io.addr_type = IPMI_MEM_ADDR_SPACE;
1934                 } else {
1935                         printk(KERN_WARNING PFX "Interface type specified "
1936                                "for interface %d, but port and address were "
1937                                "not set or set to zero.\n", i);
1938                         kfree(info);
1939                         continue;
1940                 }
1941
1942                 info->io.addr = NULL;
1943                 info->io.regspacing = regspacings[i];
1944                 if (!info->io.regspacing)
1945                         info->io.regspacing = DEFAULT_REGSPACING;
1946                 info->io.regsize = regsizes[i];
1947                 if (!info->io.regsize)
1948                         info->io.regsize = DEFAULT_REGSPACING;
1949                 info->io.regshift = regshifts[i];
1950                 info->irq = irqs[i];
1951                 if (info->irq)
1952                         info->irq_setup = std_irq_setup;
1953                 info->slave_addr = slave_addrs[i];
1954
1955                 if (!add_smi(info)) {
1956                         if (try_smi_init(info))
1957                                 cleanup_one_si(info);
1958                         ret = 0;
1959                 } else {
1960                         kfree(info);
1961                 }
1962         }
1963         return ret;
1964 }
1965
1966 #ifdef CONFIG_ACPI
1967
1968 #include <linux/acpi.h>
1969
1970 /*
1971  * Once we get an ACPI failure, we don't try any more, because we go
1972  * through the tables sequentially.  Once we don't find a table, there
1973  * are no more.
1974  */
1975 static int acpi_failure;
1976
1977 /* For GPE-type interrupts. */
1978 static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
1979         u32 gpe_number, void *context)
1980 {
1981         struct smi_info *smi_info = context;
1982         unsigned long   flags;
1983 #ifdef DEBUG_TIMING
1984         struct timeval t;
1985 #endif
1986
1987         spin_lock_irqsave(&(smi_info->si_lock), flags);
1988
1989         smi_inc_stat(smi_info, interrupts);
1990
1991 #ifdef DEBUG_TIMING
1992         do_gettimeofday(&t);
1993         printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1994 #endif
1995         smi_event_handler(smi_info, 0);
1996         spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1997
1998         return ACPI_INTERRUPT_HANDLED;
1999 }
2000
2001 static void acpi_gpe_irq_cleanup(struct smi_info *info)
2002 {
2003         if (!info->irq)
2004                 return;
2005
2006         acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
2007 }
2008
2009 static int acpi_gpe_irq_setup(struct smi_info *info)
2010 {
2011         acpi_status status;
2012
2013         if (!info->irq)
2014                 return 0;
2015
2016         /* FIXME - is level triggered right? */
2017         status = acpi_install_gpe_handler(NULL,
2018                                           info->irq,
2019                                           ACPI_GPE_LEVEL_TRIGGERED,
2020                                           &ipmi_acpi_gpe,
2021                                           info);
2022         if (status != AE_OK) {
2023                 dev_warn(info->dev, "%s unable to claim ACPI GPE %d,"
2024                          " running polled\n", DEVICE_NAME, info->irq);
2025                 info->irq = 0;
2026                 return -EINVAL;
2027         } else {
2028                 info->irq_cleanup = acpi_gpe_irq_cleanup;
2029                 dev_info(info->dev, "Using ACPI GPE %d\n", info->irq);
2030                 return 0;
2031         }
2032 }
2033
2034 /*
2035  * Defined at
2036  * http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf
2037  */
2038 struct SPMITable {
2039         s8      Signature[4];
2040         u32     Length;
2041         u8      Revision;
2042         u8      Checksum;
2043         s8      OEMID[6];
2044         s8      OEMTableID[8];
2045         s8      OEMRevision[4];
2046         s8      CreatorID[4];
2047         s8      CreatorRevision[4];
2048         u8      InterfaceType;
2049         u8      IPMIlegacy;
2050         s16     SpecificationRevision;
2051
2052         /*
2053          * Bit 0 - SCI interrupt supported
2054          * Bit 1 - I/O APIC/SAPIC
2055          */
2056         u8      InterruptType;
2057
2058         /*
2059          * If bit 0 of InterruptType is set, then this is the SCI
2060          * interrupt in the GPEx_STS register.
2061          */
2062         u8      GPE;
2063
2064         s16     Reserved;
2065
2066         /*
2067          * If bit 1 of InterruptType is set, then this is the I/O
2068          * APIC/SAPIC interrupt.
2069          */
2070         u32     GlobalSystemInterrupt;
2071
2072         /* The actual register address. */
2073         struct acpi_generic_address addr;
2074
2075         u8      UID[4];
2076
2077         s8      spmi_id[1]; /* A '\0' terminated array starts here. */
2078 };
2079
2080 static int try_init_spmi(struct SPMITable *spmi)
2081 {
2082         struct smi_info  *info;
2083         int rv;
2084
2085         if (spmi->IPMIlegacy != 1) {
2086                 printk(KERN_INFO PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);
2087                 return -ENODEV;
2088         }
2089
2090         info = smi_info_alloc();
2091         if (!info) {
2092                 printk(KERN_ERR PFX "Could not allocate SI data (3)\n");
2093                 return -ENOMEM;
2094         }
2095
2096         info->addr_source = SI_SPMI;
2097         printk(KERN_INFO PFX "probing via SPMI\n");
2098
2099         /* Figure out the interface type. */
2100         switch (spmi->InterfaceType) {
2101         case 1: /* KCS */
2102                 info->si_type = SI_KCS;
2103                 break;
2104         case 2: /* SMIC */
2105                 info->si_type = SI_SMIC;
2106                 break;
2107         case 3: /* BT */
2108                 info->si_type = SI_BT;
2109                 break;
2110         default:
2111                 printk(KERN_INFO PFX "Unknown ACPI/SPMI SI type %d\n",
2112                        spmi->InterfaceType);
2113                 kfree(info);
2114                 return -EIO;
2115         }
2116
2117         if (spmi->InterruptType & 1) {
2118                 /* We've got a GPE interrupt. */
2119                 info->irq = spmi->GPE;
2120                 info->irq_setup = acpi_gpe_irq_setup;
2121         } else if (spmi->InterruptType & 2) {
2122                 /* We've got an APIC/SAPIC interrupt. */
2123                 info->irq = spmi->GlobalSystemInterrupt;
2124                 info->irq_setup = std_irq_setup;
2125         } else {
2126                 /* Use the default interrupt setting. */
2127                 info->irq = 0;
2128                 info->irq_setup = NULL;
2129         }
2130
2131         if (spmi->addr.bit_width) {
2132                 /* A (hopefully) properly formed register bit width. */
2133                 info->io.regspacing = spmi->addr.bit_width / 8;
2134         } else {
2135                 info->io.regspacing = DEFAULT_REGSPACING;
2136         }
2137         info->io.regsize = info->io.regspacing;
2138         info->io.regshift = spmi->addr.bit_offset;
2139
2140         if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
2141                 info->io_setup = mem_setup;
2142                 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2143         } else if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
2144                 info->io_setup = port_setup;
2145                 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2146         } else {
2147                 kfree(info);
2148                 printk(KERN_WARNING PFX "Unknown ACPI I/O Address type\n");
2149                 return -EIO;
2150         }
2151         info->io.addr_data = spmi->addr.address;
2152
2153         pr_info("ipmi_si: SPMI: %s %#lx regsize %d spacing %d irq %d\n",
2154                  (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2155                  info->io.addr_data, info->io.regsize, info->io.regspacing,
2156                  info->irq);
2157
2158         rv = add_smi(info);
2159         if (rv)
2160                 kfree(info);
2161
2162         return rv;
2163 }
2164
2165 static void spmi_find_bmc(void)
2166 {
2167         acpi_status      status;
2168         struct SPMITable *spmi;
2169         int              i;
2170
2171         if (acpi_disabled)
2172                 return;
2173
2174         if (acpi_failure)
2175                 return;
2176
2177         for (i = 0; ; i++) {
2178                 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
2179                                         (struct acpi_table_header **)&spmi);
2180                 if (status != AE_OK)
2181                         return;
2182
2183                 try_init_spmi(spmi);
2184         }
2185 }
2186
2187 static int ipmi_pnp_probe(struct pnp_dev *dev,
2188                                     const struct pnp_device_id *dev_id)
2189 {
2190         struct acpi_device *acpi_dev;
2191         struct smi_info *info;
2192         struct resource *res, *res_second;
2193         acpi_handle handle;
2194         acpi_status status;
2195         unsigned long long tmp;
2196         int rv;
2197
2198         acpi_dev = pnp_acpi_device(dev);
2199         if (!acpi_dev)
2200                 return -ENODEV;
2201
2202         info = smi_info_alloc();
2203         if (!info)
2204                 return -ENOMEM;
2205
2206         info->addr_source = SI_ACPI;
2207         printk(KERN_INFO PFX "probing via ACPI\n");
2208
2209         handle = acpi_dev->handle;
2210         info->addr_info.acpi_info.acpi_handle = handle;
2211
2212         /* _IFT tells us the interface type: KCS, BT, etc */
2213         status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
2214         if (ACPI_FAILURE(status))
2215                 goto err_free;
2216
2217         switch (tmp) {
2218         case 1:
2219                 info->si_type = SI_KCS;
2220                 break;
2221         case 2:
2222                 info->si_type = SI_SMIC;
2223                 break;
2224         case 3:
2225                 info->si_type = SI_BT;
2226                 break;
2227         default:
2228                 dev_info(&dev->dev, "unknown IPMI type %lld\n", tmp);
2229                 goto err_free;
2230         }
2231
2232         res = pnp_get_resource(dev, IORESOURCE_IO, 0);
2233         if (res) {
2234                 info->io_setup = port_setup;
2235                 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2236         } else {
2237                 res = pnp_get_resource(dev, IORESOURCE_MEM, 0);
2238                 if (res) {
2239                         info->io_setup = mem_setup;
2240                         info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2241                 }
2242         }
2243         if (!res) {
2244                 dev_err(&dev->dev, "no I/O or memory address\n");
2245                 goto err_free;
2246         }
2247         info->io.addr_data = res->start;
2248
2249         info->io.regspacing = DEFAULT_REGSPACING;
2250         res_second = pnp_get_resource(dev,
2251                                (info->io.addr_type == IPMI_IO_ADDR_SPACE) ?
2252                                         IORESOURCE_IO : IORESOURCE_MEM,
2253                                1);
2254         if (res_second) {
2255                 if (res_second->start > info->io.addr_data)
2256                         info->io.regspacing = res_second->start - info->io.addr_data;
2257         }
2258         info->io.regsize = DEFAULT_REGSPACING;
2259         info->io.regshift = 0;
2260
2261         /* If _GPE exists, use it; otherwise use standard interrupts */
2262         status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
2263         if (ACPI_SUCCESS(status)) {
2264                 info->irq = tmp;
2265                 info->irq_setup = acpi_gpe_irq_setup;
2266         } else if (pnp_irq_valid(dev, 0)) {
2267                 info->irq = pnp_irq(dev, 0);
2268                 info->irq_setup = std_irq_setup;
2269         }
2270
2271         info->dev = &dev->dev;
2272         pnp_set_drvdata(dev, info);
2273
2274         dev_info(info->dev, "%pR regsize %d spacing %d irq %d\n",
2275                  res, info->io.regsize, info->io.regspacing,
2276                  info->irq);
2277
2278         rv = add_smi(info);
2279         if (rv)
2280                 kfree(info);
2281
2282         return rv;
2283
2284 err_free:
2285         kfree(info);
2286         return -EINVAL;
2287 }
2288
2289 static void ipmi_pnp_remove(struct pnp_dev *dev)
2290 {
2291         struct smi_info *info = pnp_get_drvdata(dev);
2292
2293         cleanup_one_si(info);
2294 }
2295
2296 static const struct pnp_device_id pnp_dev_table[] = {
2297         {"IPI0001", 0},
2298         {"", 0},
2299 };
2300
2301 static struct pnp_driver ipmi_pnp_driver = {
2302         .name           = DEVICE_NAME,
2303         .probe          = ipmi_pnp_probe,
2304         .remove         = ipmi_pnp_remove,
2305         .id_table       = pnp_dev_table,
2306 };
2307
2308 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
2309 #endif
2310
2311 #ifdef CONFIG_DMI
2312 struct dmi_ipmi_data {
2313         u8              type;
2314         u8              addr_space;
2315         unsigned long   base_addr;
2316         u8              irq;
2317         u8              offset;
2318         u8              slave_addr;
2319 };
2320
2321 static int decode_dmi(const struct dmi_header *dm,
2322                                 struct dmi_ipmi_data *dmi)
2323 {
2324         const u8        *data = (const u8 *)dm;
2325         unsigned long   base_addr;
2326         u8              reg_spacing;
2327         u8              len = dm->length;
2328
2329         dmi->type = data[4];
2330
2331         memcpy(&base_addr, data+8, sizeof(unsigned long));
2332         if (len >= 0x11) {
2333                 if (base_addr & 1) {
2334                         /* I/O */
2335                         base_addr &= 0xFFFE;
2336                         dmi->addr_space = IPMI_IO_ADDR_SPACE;
2337                 } else
2338                         /* Memory */
2339                         dmi->addr_space = IPMI_MEM_ADDR_SPACE;
2340
2341                 /* If bit 4 of byte 0x10 is set, then the lsb for the address
2342                    is odd. */
2343                 dmi->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
2344
2345                 dmi->irq = data[0x11];
2346
2347                 /* The top two bits of byte 0x10 hold the register spacing. */
2348                 reg_spacing = (data[0x10] & 0xC0) >> 6;
2349                 switch (reg_spacing) {
2350                 case 0x00: /* Byte boundaries */
2351                     dmi->offset = 1;
2352                     break;
2353                 case 0x01: /* 32-bit boundaries */
2354                     dmi->offset = 4;
2355                     break;
2356                 case 0x02: /* 16-byte boundaries */
2357                     dmi->offset = 16;
2358                     break;
2359                 default:
2360                     /* Some other interface, just ignore it. */
2361                     return -EIO;
2362                 }
2363         } else {
2364                 /* Old DMI spec. */
2365                 /*
2366                  * Note that technically, the lower bit of the base
2367                  * address should be 1 if the address is I/O and 0 if
2368                  * the address is in memory.  So many systems get that
2369                  * wrong (and all that I have seen are I/O) so we just
2370                  * ignore that bit and assume I/O.  Systems that use
2371                  * memory should use the newer spec, anyway.
2372                  */
2373                 dmi->base_addr = base_addr & 0xfffe;
2374                 dmi->addr_space = IPMI_IO_ADDR_SPACE;
2375                 dmi->offset = 1;
2376         }
2377
2378         dmi->slave_addr = data[6];
2379
2380         return 0;
2381 }
2382
2383 static void try_init_dmi(struct dmi_ipmi_data *ipmi_data)
2384 {
2385         struct smi_info *info;
2386
2387         info = smi_info_alloc();
2388         if (!info) {
2389                 printk(KERN_ERR PFX "Could not allocate SI data\n");
2390                 return;
2391         }
2392
2393         info->addr_source = SI_SMBIOS;
2394         printk(KERN_INFO PFX "probing via SMBIOS\n");
2395
2396         switch (ipmi_data->type) {
2397         case 0x01: /* KCS */
2398                 info->si_type = SI_KCS;
2399                 break;
2400         case 0x02: /* SMIC */
2401                 info->si_type = SI_SMIC;
2402                 break;
2403         case 0x03: /* BT */
2404                 info->si_type = SI_BT;
2405                 break;
2406         default:
2407                 kfree(info);
2408                 return;
2409         }
2410
2411         switch (ipmi_data->addr_space) {
2412         case IPMI_MEM_ADDR_SPACE:
2413                 info->io_setup = mem_setup;
2414                 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2415                 break;
2416
2417         case IPMI_IO_ADDR_SPACE:
2418                 info->io_setup = port_setup;
2419                 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2420                 break;
2421
2422         default:
2423                 kfree(info);
2424                 printk(KERN_WARNING PFX "Unknown SMBIOS I/O Address type: %d\n",
2425                        ipmi_data->addr_space);
2426                 return;
2427         }
2428         info->io.addr_data = ipmi_data->base_addr;
2429
2430         info->io.regspacing = ipmi_data->offset;
2431         if (!info->io.regspacing)
2432                 info->io.regspacing = DEFAULT_REGSPACING;
2433         info->io.regsize = DEFAULT_REGSPACING;
2434         info->io.regshift = 0;
2435
2436         info->slave_addr = ipmi_data->slave_addr;
2437
2438         info->irq = ipmi_data->irq;
2439         if (info->irq)
2440                 info->irq_setup = std_irq_setup;
2441
2442         pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
2443                  (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2444                  info->io.addr_data, info->io.regsize, info->io.regspacing,
2445                  info->irq);
2446
2447         if (add_smi(info))
2448                 kfree(info);
2449 }
2450
2451 static void dmi_find_bmc(void)
2452 {
2453         const struct dmi_device *dev = NULL;
2454         struct dmi_ipmi_data data;
2455         int                  rv;
2456
2457         while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
2458                 memset(&data, 0, sizeof(data));
2459                 rv = decode_dmi((const struct dmi_header *) dev->device_data,
2460                                 &data);
2461                 if (!rv)
2462                         try_init_dmi(&data);
2463         }
2464 }
2465 #endif /* CONFIG_DMI */
2466
2467 #ifdef CONFIG_PCI
2468
2469 #define PCI_ERMC_CLASSCODE              0x0C0700
2470 #define PCI_ERMC_CLASSCODE_MASK         0xffffff00
2471 #define PCI_ERMC_CLASSCODE_TYPE_MASK    0xff
2472 #define PCI_ERMC_CLASSCODE_TYPE_SMIC    0x00
2473 #define PCI_ERMC_CLASSCODE_TYPE_KCS     0x01
2474 #define PCI_ERMC_CLASSCODE_TYPE_BT      0x02
2475
2476 #define PCI_HP_VENDOR_ID    0x103C
2477 #define PCI_MMC_DEVICE_ID   0x121A
2478 #define PCI_MMC_ADDR_CW     0x10
2479
2480 static void ipmi_pci_cleanup(struct smi_info *info)
2481 {
2482         struct pci_dev *pdev = info->addr_source_data;
2483
2484         pci_disable_device(pdev);
2485 }
2486
2487 static int ipmi_pci_probe_regspacing(struct smi_info *info)
2488 {
2489         if (info->si_type == SI_KCS) {
2490                 unsigned char   status;
2491                 int             regspacing;
2492
2493                 info->io.regsize = DEFAULT_REGSIZE;
2494                 info->io.regshift = 0;
2495                 info->io_size = 2;
2496                 info->handlers = &kcs_smi_handlers;
2497
2498                 /* detect 1, 4, 16byte spacing */
2499                 for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
2500                         info->io.regspacing = regspacing;
2501                         if (info->io_setup(info)) {
2502                                 dev_err(info->dev,
2503                                         "Could not setup I/O space\n");
2504                                 return DEFAULT_REGSPACING;
2505                         }
2506                         /* write invalid cmd */
2507                         info->io.outputb(&info->io, 1, 0x10);
2508                         /* read status back */
2509                         status = info->io.inputb(&info->io, 1);
2510                         info->io_cleanup(info);
2511                         if (status)
2512                                 return regspacing;
2513                         regspacing *= 4;
2514                 }
2515         }
2516         return DEFAULT_REGSPACING;
2517 }
2518
2519 static int ipmi_pci_probe(struct pci_dev *pdev,
2520                                     const struct pci_device_id *ent)
2521 {
2522         int rv;
2523         int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
2524         struct smi_info *info;
2525
2526         info = smi_info_alloc();
2527         if (!info)
2528                 return -ENOMEM;
2529
2530         info->addr_source = SI_PCI;
2531         dev_info(&pdev->dev, "probing via PCI");
2532
2533         switch (class_type) {
2534         case PCI_ERMC_CLASSCODE_TYPE_SMIC:
2535                 info->si_type = SI_SMIC;
2536                 break;
2537
2538         case PCI_ERMC_CLASSCODE_TYPE_KCS:
2539                 info->si_type = SI_KCS;
2540                 break;
2541
2542         case PCI_ERMC_CLASSCODE_TYPE_BT:
2543                 info->si_type = SI_BT;
2544                 break;
2545
2546         default:
2547                 kfree(info);
2548                 dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);
2549                 return -ENOMEM;
2550         }
2551
2552         rv = pci_enable_device(pdev);
2553         if (rv) {
2554                 dev_err(&pdev->dev, "couldn't enable PCI device\n");
2555                 kfree(info);
2556                 return rv;
2557         }
2558
2559         info->addr_source_cleanup = ipmi_pci_cleanup;
2560         info->addr_source_data = pdev;
2561
2562         if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
2563                 info->io_setup = port_setup;
2564                 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2565         } else {
2566                 info->io_setup = mem_setup;
2567                 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2568         }
2569         info->io.addr_data = pci_resource_start(pdev, 0);
2570
2571         info->io.regspacing = ipmi_pci_probe_regspacing(info);
2572         info->io.regsize = DEFAULT_REGSIZE;
2573         info->io.regshift = 0;
2574
2575         info->irq = pdev->irq;
2576         if (info->irq)
2577                 info->irq_setup = std_irq_setup;
2578
2579         info->dev = &pdev->dev;
2580         pci_set_drvdata(pdev, info);
2581
2582         dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
2583                 &pdev->resource[0], info->io.regsize, info->io.regspacing,
2584                 info->irq);
2585
2586         rv = add_smi(info);
2587         if (rv) {
2588                 kfree(info);
2589                 pci_disable_device(pdev);
2590         }
2591
2592         return rv;
2593 }
2594
2595 static void ipmi_pci_remove(struct pci_dev *pdev)
2596 {
2597         struct smi_info *info = pci_get_drvdata(pdev);
2598         cleanup_one_si(info);
2599         pci_disable_device(pdev);
2600 }
2601
2602 static struct pci_device_id ipmi_pci_devices[] = {
2603         { PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },
2604         { PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE_MASK) },
2605         { 0, }
2606 };
2607 MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
2608
2609 static struct pci_driver ipmi_pci_driver = {
2610         .name =         DEVICE_NAME,
2611         .id_table =     ipmi_pci_devices,
2612         .probe =        ipmi_pci_probe,
2613         .remove =       ipmi_pci_remove,
2614 };
2615 #endif /* CONFIG_PCI */
2616
2617 static struct of_device_id ipmi_match[];
2618 static int ipmi_probe(struct platform_device *dev)
2619 {
2620 #ifdef CONFIG_OF
2621         const struct of_device_id *match;
2622         struct smi_info *info;
2623         struct resource resource;
2624         const __be32 *regsize, *regspacing, *regshift;
2625         struct device_node *np = dev->dev.of_node;
2626         int ret;
2627         int proplen;
2628
2629         dev_info(&dev->dev, "probing via device tree\n");
2630
2631         match = of_match_device(ipmi_match, &dev->dev);
2632         if (!match)
2633                 return -EINVAL;
2634
2635         ret = of_address_to_resource(np, 0, &resource);
2636         if (ret) {
2637                 dev_warn(&dev->dev, PFX "invalid address from OF\n");
2638                 return ret;
2639         }
2640
2641         regsize = of_get_property(np, "reg-size", &proplen);
2642         if (regsize && proplen != 4) {
2643                 dev_warn(&dev->dev, PFX "invalid regsize from OF\n");
2644                 return -EINVAL;
2645         }
2646
2647         regspacing = of_get_property(np, "reg-spacing", &proplen);
2648         if (regspacing && proplen != 4) {
2649                 dev_warn(&dev->dev, PFX "invalid regspacing from OF\n");
2650                 return -EINVAL;
2651         }
2652
2653         regshift = of_get_property(np, "reg-shift", &proplen);
2654         if (regshift && proplen != 4) {
2655                 dev_warn(&dev->dev, PFX "invalid regshift from OF\n");
2656                 return -EINVAL;
2657         }
2658
2659         info = smi_info_alloc();
2660
2661         if (!info) {
2662                 dev_err(&dev->dev,
2663                         "could not allocate memory for OF probe\n");
2664                 return -ENOMEM;
2665         }
2666
2667         info->si_type           = (enum si_type) match->data;
2668         info->addr_source       = SI_DEVICETREE;
2669         info->irq_setup         = std_irq_setup;
2670
2671         if (resource.flags & IORESOURCE_IO) {
2672                 info->io_setup          = port_setup;
2673                 info->io.addr_type      = IPMI_IO_ADDR_SPACE;
2674         } else {
2675                 info->io_setup          = mem_setup;
2676                 info->io.addr_type      = IPMI_MEM_ADDR_SPACE;
2677         }
2678
2679         info->io.addr_data      = resource.start;
2680
2681         info->io.regsize        = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
2682         info->io.regspacing     = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
2683         info->io.regshift       = regshift ? be32_to_cpup(regshift) : 0;
2684
2685         info->irq               = irq_of_parse_and_map(dev->dev.of_node, 0);
2686         info->dev               = &dev->dev;
2687
2688         dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
2689                 info->io.addr_data, info->io.regsize, info->io.regspacing,
2690                 info->irq);
2691
2692         dev_set_drvdata(&dev->dev, info);
2693
2694         ret = add_smi(info);
2695         if (ret) {
2696                 kfree(info);
2697                 return ret;
2698         }
2699 #endif
2700         return 0;
2701 }
2702
2703 static int ipmi_remove(struct platform_device *dev)
2704 {
2705 #ifdef CONFIG_OF
2706         cleanup_one_si(dev_get_drvdata(&dev->dev));
2707 #endif
2708         return 0;
2709 }
2710
2711 static struct of_device_id ipmi_match[] =
2712 {
2713         { .type = "ipmi", .compatible = "ipmi-kcs",
2714           .data = (void *)(unsigned long) SI_KCS },
2715         { .type = "ipmi", .compatible = "ipmi-smic",
2716           .data = (void *)(unsigned long) SI_SMIC },
2717         { .type = "ipmi", .compatible = "ipmi-bt",
2718           .data = (void *)(unsigned long) SI_BT },
2719         {},
2720 };
2721
2722 static struct platform_driver ipmi_driver = {
2723         .driver = {
2724                 .name = DEVICE_NAME,
2725                 .owner = THIS_MODULE,
2726                 .of_match_table = ipmi_match,
2727         },
2728         .probe          = ipmi_probe,
2729         .remove         = ipmi_remove,
2730 };
2731
2732 #ifdef CONFIG_PARISC
2733 static int ipmi_parisc_probe(struct parisc_device *dev)
2734 {
2735         struct smi_info *info;
2736         int rv;
2737
2738         info = smi_info_alloc();
2739
2740         if (!info) {
2741                 dev_err(&dev->dev,
2742                         "could not allocate memory for PARISC probe\n");
2743                 return -ENOMEM;
2744         }
2745
2746         info->si_type           = SI_KCS;
2747         info->addr_source       = SI_DEVICETREE;
2748         info->io_setup          = mem_setup;
2749         info->io.addr_type      = IPMI_MEM_ADDR_SPACE;
2750         info->io.addr_data      = dev->hpa.start;
2751         info->io.regsize        = 1;
2752         info->io.regspacing     = 1;
2753         info->io.regshift       = 0;
2754         info->irq               = 0; /* no interrupt */
2755         info->irq_setup         = NULL;
2756         info->dev               = &dev->dev;
2757
2758         dev_dbg(&dev->dev, "addr 0x%lx\n", info->io.addr_data);
2759
2760         dev_set_drvdata(&dev->dev, info);
2761
2762         rv = add_smi(info);
2763         if (rv) {
2764                 kfree(info);
2765                 return rv;
2766         }
2767
2768         return 0;
2769 }
2770
2771 static int ipmi_parisc_remove(struct parisc_device *dev)
2772 {
2773         cleanup_one_si(dev_get_drvdata(&dev->dev));
2774         return 0;
2775 }
2776
2777 static struct parisc_device_id ipmi_parisc_tbl[] = {
2778         { HPHW_MC, HVERSION_REV_ANY_ID, 0x004, 0xC0 },
2779         { 0, }
2780 };
2781
2782 static struct parisc_driver ipmi_parisc_driver = {
2783         .name =         "ipmi",
2784         .id_table =     ipmi_parisc_tbl,
2785         .probe =        ipmi_parisc_probe,
2786         .remove =       ipmi_parisc_remove,
2787 };
2788 #endif /* CONFIG_PARISC */
2789
2790 static int wait_for_msg_done(struct smi_info *smi_info)
2791 {
2792         enum si_sm_result     smi_result;
2793
2794         smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
2795         for (;;) {
2796                 if (smi_result == SI_SM_CALL_WITH_DELAY ||
2797                     smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
2798                         schedule_timeout_uninterruptible(1);
2799                         smi_result = smi_info->handlers->event(
2800                                 smi_info->si_sm, jiffies_to_usecs(1));
2801                 } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
2802                         smi_result = smi_info->handlers->event(
2803                                 smi_info->si_sm, 0);
2804                 } else
2805                         break;
2806         }
2807         if (smi_result == SI_SM_HOSED)
2808                 /*
2809                  * We couldn't get the state machine to run, so whatever's at
2810                  * the port is probably not an IPMI SMI interface.
2811                  */
2812                 return -ENODEV;
2813
2814         return 0;
2815 }
2816
2817 static int try_get_dev_id(struct smi_info *smi_info)
2818 {
2819         unsigned char         msg[2];
2820         unsigned char         *resp;
2821         unsigned long         resp_len;
2822         int                   rv = 0;
2823
2824         resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
2825         if (!resp)
2826                 return -ENOMEM;
2827
2828         /*
2829          * Do a Get Device ID command, since it comes back with some
2830          * useful info.
2831          */
2832         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2833         msg[1] = IPMI_GET_DEVICE_ID_CMD;
2834         smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
2835
2836         rv = wait_for_msg_done(smi_info);
2837         if (rv)
2838                 goto out;
2839
2840         resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2841                                                   resp, IPMI_MAX_MSG_LENGTH);
2842
2843         /* Check and record info from the get device id, in case we need it. */
2844         rv = ipmi_demangle_device_id(resp, resp_len, &smi_info->device_id);
2845
2846  out:
2847         kfree(resp);
2848         return rv;
2849 }
2850
2851 static int try_enable_event_buffer(struct smi_info *smi_info)
2852 {
2853         unsigned char         msg[3];
2854         unsigned char         *resp;
2855         unsigned long         resp_len;
2856         int                   rv = 0;
2857
2858         resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
2859         if (!resp)
2860                 return -ENOMEM;
2861
2862         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2863         msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
2864         smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
2865
2866         rv = wait_for_msg_done(smi_info);
2867         if (rv) {
2868                 printk(KERN_WARNING PFX "Error getting response from get"
2869                        " global enables command, the event buffer is not"
2870                        " enabled.\n");
2871                 goto out;
2872         }
2873
2874         resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2875                                                   resp, IPMI_MAX_MSG_LENGTH);
2876
2877         if (resp_len < 4 ||
2878                         resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2879                         resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
2880                         resp[2] != 0) {
2881                 printk(KERN_WARNING PFX "Invalid return from get global"
2882                        " enables command, cannot enable the event buffer.\n");
2883                 rv = -EINVAL;
2884                 goto out;
2885         }
2886
2887         if (resp[3] & IPMI_BMC_EVT_MSG_BUFF)
2888                 /* buffer is already enabled, nothing to do. */
2889                 goto out;
2890
2891         msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2892         msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
2893         msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
2894         smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
2895
2896         rv = wait_for_msg_done(smi_info);
2897         if (rv) {
2898                 printk(KERN_WARNING PFX "Error getting response from set"
2899                        " global, enables command, the event buffer is not"
2900                        " enabled.\n");
2901                 goto out;
2902         }
2903
2904         resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2905                                                   resp, IPMI_MAX_MSG_LENGTH);
2906
2907         if (resp_len < 3 ||
2908                         resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2909                         resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
2910                 printk(KERN_WARNING PFX "Invalid return from get global,"
2911                        "enables command, not enable the event buffer.\n");
2912                 rv = -EINVAL;
2913                 goto out;
2914         }
2915
2916         if (resp[2] != 0)
2917                 /*
2918                  * An error when setting the event buffer bit means
2919                  * that the event buffer is not supported.
2920                  */
2921                 rv = -ENOENT;
2922  out:
2923         kfree(resp);
2924         return rv;
2925 }
2926
2927 static int smi_type_proc_show(struct seq_file *m, void *v)
2928 {
2929         struct smi_info *smi = m->private;
2930
2931         return seq_printf(m, "%s\n", si_to_str[smi->si_type]);
2932 }
2933
2934 static int smi_type_proc_open(struct inode *inode, struct file *file)
2935 {
2936         return single_open(file, smi_type_proc_show, PDE_DATA(inode));
2937 }
2938
2939 static const struct file_operations smi_type_proc_ops = {
2940         .open           = smi_type_proc_open,
2941         .read           = seq_read,
2942         .llseek         = seq_lseek,
2943         .release        = single_release,
2944 };
2945
2946 static int smi_si_stats_proc_show(struct seq_file *m, void *v)
2947 {
2948         struct smi_info *smi = m->private;
2949
2950         seq_printf(m, "interrupts_enabled:    %d\n",
2951                        smi->irq && !smi->interrupt_disabled);
2952         seq_printf(m, "short_timeouts:        %u\n",
2953                        smi_get_stat(smi, short_timeouts));
2954         seq_printf(m, "long_timeouts:         %u\n",
2955                        smi_get_stat(smi, long_timeouts));
2956         seq_printf(m, "idles:                 %u\n",
2957                        smi_get_stat(smi, idles));
2958         seq_printf(m, "interrupts:            %u\n",
2959                        smi_get_stat(smi, interrupts));
2960         seq_printf(m, "attentions:            %u\n",
2961                        smi_get_stat(smi, attentions));
2962         seq_printf(m, "flag_fetches:          %u\n",
2963                        smi_get_stat(smi, flag_fetches));
2964         seq_printf(m, "hosed_count:           %u\n",
2965                        smi_get_stat(smi, hosed_count));
2966         seq_printf(m, "complete_transactions: %u\n",
2967                        smi_get_stat(smi, complete_transactions));
2968         seq_printf(m, "events:                %u\n",
2969                        smi_get_stat(smi, events));
2970         seq_printf(m, "watchdog_pretimeouts:  %u\n",
2971                        smi_get_stat(smi, watchdog_pretimeouts));
2972         seq_printf(m, "incoming_messages:     %u\n",
2973                        smi_get_stat(smi, incoming_messages));
2974         return 0;
2975 }
2976
2977 static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
2978 {
2979         return single_open(file, smi_si_stats_proc_show, PDE_DATA(inode));
2980 }
2981
2982 static const struct file_operations smi_si_stats_proc_ops = {
2983         .open           = smi_si_stats_proc_open,
2984         .read           = seq_read,
2985         .llseek         = seq_lseek,
2986         .release        = single_release,
2987 };
2988
2989 static int smi_params_proc_show(struct seq_file *m, void *v)
2990 {
2991         struct smi_info *smi = m->private;
2992
2993         return seq_printf(m,
2994                        "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
2995                        si_to_str[smi->si_type],
2996                        addr_space_to_str[smi->io.addr_type],
2997                        smi->io.addr_data,
2998                        smi->io.regspacing,
2999                        smi->io.regsize,
3000                        smi->io.regshift,
3001                        smi->irq,
3002                        smi->slave_addr);
3003 }
3004
3005 static int smi_params_proc_open(struct inode *inode, struct file *file)
3006 {
3007         return single_open(file, smi_params_proc_show, PDE_DATA(inode));
3008 }
3009
3010 static const struct file_operations smi_params_proc_ops = {
3011         .open           = smi_params_proc_open,
3012         .read           = seq_read,
3013         .llseek         = seq_lseek,
3014         .release        = single_release,
3015 };
3016
3017 /*
3018  * oem_data_avail_to_receive_msg_avail
3019  * @info - smi_info structure with msg_flags set
3020  *
3021  * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
3022  * Returns 1 indicating need to re-run handle_flags().
3023  */
3024 static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
3025 {
3026         smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
3027                                RECEIVE_MSG_AVAIL);
3028         return 1;
3029 }
3030
3031 /*
3032  * setup_dell_poweredge_oem_data_handler
3033  * @info - smi_info.device_id must be populated
3034  *
3035  * Systems that match, but have firmware version < 1.40 may assert
3036  * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
3037  * it's safe to do so.  Such systems will de-assert OEM1_DATA_AVAIL
3038  * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
3039  * as RECEIVE_MSG_AVAIL instead.
3040  *
3041  * As Dell has no plans to release IPMI 1.5 firmware that *ever*
3042  * assert the OEM[012] bits, and if it did, the driver would have to
3043  * change to handle that properly, we don't actually check for the
3044  * firmware version.
3045  * Device ID = 0x20                BMC on PowerEdge 8G servers
3046  * Device Revision = 0x80
3047  * Firmware Revision1 = 0x01       BMC version 1.40
3048  * Firmware Revision2 = 0x40       BCD encoded
3049  * IPMI Version = 0x51             IPMI 1.5
3050  * Manufacturer ID = A2 02 00      Dell IANA
3051  *
3052  * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
3053  * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
3054  *
3055  */
3056 #define DELL_POWEREDGE_8G_BMC_DEVICE_ID  0x20
3057 #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
3058 #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
3059 #define DELL_IANA_MFR_ID 0x0002a2
3060 static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
3061 {
3062         struct ipmi_device_id *id = &smi_info->device_id;
3063         if (id->manufacturer_id == DELL_IANA_MFR_ID) {
3064                 if (id->device_id       == DELL_POWEREDGE_8G_BMC_DEVICE_ID  &&
3065                     id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
3066                     id->ipmi_version   == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
3067                         smi_info->oem_data_avail_handler =
3068                                 oem_data_avail_to_receive_msg_avail;
3069                 } else if (ipmi_version_major(id) < 1 ||
3070                            (ipmi_version_major(id) == 1 &&
3071                             ipmi_version_minor(id) < 5)) {
3072                         smi_info->oem_data_avail_handler =
3073                                 oem_data_avail_to_receive_msg_avail;
3074                 }
3075         }
3076 }
3077
3078 #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
3079 static void return_hosed_msg_badsize(struct smi_info *smi_info)
3080 {
3081         struct ipmi_smi_msg *msg = smi_info->curr_msg;
3082
3083         /* Make it a response */
3084         msg->rsp[0] = msg->data[0] | 4;
3085         msg->rsp[1] = msg->data[1];
3086         msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
3087         msg->rsp_size = 3;
3088         smi_info->curr_msg = NULL;
3089         deliver_recv_msg(smi_info, msg);
3090 }
3091
3092 /*
3093  * dell_poweredge_bt_xaction_handler
3094  * @info - smi_info.device_id must be populated
3095  *
3096  * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
3097  * not respond to a Get SDR command if the length of the data
3098  * requested is exactly 0x3A, which leads to command timeouts and no
3099  * data returned.  This intercepts such commands, and causes userspace
3100  * callers to try again with a different-sized buffer, which succeeds.
3101  */
3102
3103 #define STORAGE_NETFN 0x0A
3104 #define STORAGE_CMD_GET_SDR 0x23
3105 static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
3106                                              unsigned long unused,
3107                                              void *in)
3108 {
3109         struct smi_info *smi_info = in;
3110         unsigned char *data = smi_info->curr_msg->data;
3111         unsigned int size   = smi_info->curr_msg->data_size;
3112         if (size >= 8 &&
3113             (data[0]>>2) == STORAGE_NETFN &&
3114             data[1] == STORAGE_CMD_GET_SDR &&
3115             data[7] == 0x3A) {
3116                 return_hosed_msg_badsize(smi_info);
3117                 return NOTIFY_STOP;
3118         }
3119         return NOTIFY_DONE;
3120 }
3121
3122 static struct notifier_block dell_poweredge_bt_xaction_notifier = {
3123         .notifier_call  = dell_poweredge_bt_xaction_handler,
3124 };
3125
3126 /*
3127  * setup_dell_poweredge_bt_xaction_handler
3128  * @info - smi_info.device_id must be filled in already
3129  *
3130  * Fills in smi_info.device_id.start_transaction_pre_hook
3131  * when we know what function to use there.
3132  */
3133 static void
3134 setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
3135 {
3136         struct ipmi_device_id *id = &smi_info->device_id;
3137         if (id->manufacturer_id == DELL_IANA_MFR_ID &&
3138             smi_info->si_type == SI_BT)
3139                 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
3140 }
3141
3142 /*
3143  * setup_oem_data_handler
3144  * @info - smi_info.device_id must be filled in already
3145  *
3146  * Fills in smi_info.device_id.oem_data_available_handler
3147  * when we know what function to use there.
3148  */
3149
3150 static void setup_oem_data_handler(struct smi_info *smi_info)
3151 {
3152         setup_dell_poweredge_oem_data_handler(smi_info);
3153 }
3154
3155 static void setup_xaction_handlers(struct smi_info *smi_info)
3156 {
3157         setup_dell_poweredge_bt_xaction_handler(smi_info);
3158 }
3159
3160 static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
3161 {
3162         if (smi_info->intf) {
3163                 /*
3164                  * The timer and thread are only running if the
3165                  * interface has been started up and registered.
3166                  */
3167                 if (smi_info->thread != NULL)
3168                         kthread_stop(smi_info->thread);
3169                 del_timer_sync(&smi_info->si_timer);
3170         }
3171 }
3172
3173 static struct ipmi_default_vals
3174 {
3175         int type;
3176         int port;
3177 } ipmi_defaults[] =
3178 {
3179         { .type = SI_KCS, .port = 0xca2 },
3180         { .type = SI_SMIC, .port = 0xca9 },
3181         { .type = SI_BT, .port = 0xe4 },
3182         { .port = 0 }
3183 };
3184
3185 static void default_find_bmc(void)
3186 {
3187         struct smi_info *info;
3188         int             i;
3189
3190         for (i = 0; ; i++) {
3191                 if (!ipmi_defaults[i].port)
3192                         break;
3193 #ifdef CONFIG_PPC
3194                 if (check_legacy_ioport(ipmi_defaults[i].port))
3195                         continue;
3196 #endif
3197                 info = smi_info_alloc();
3198                 if (!info)
3199                         return;
3200
3201                 info->addr_source = SI_DEFAULT;
3202
3203                 info->si_type = ipmi_defaults[i].type;
3204                 info->io_setup = port_setup;
3205                 info->io.addr_data = ipmi_defaults[i].port;
3206                 info->io.addr_type = IPMI_IO_ADDR_SPACE;
3207
3208                 info->io.addr = NULL;
3209                 info->io.regspacing = DEFAULT_REGSPACING;
3210                 info->io.regsize = DEFAULT_REGSPACING;
3211                 info->io.regshift = 0;
3212
3213                 if (add_smi(info) == 0) {
3214                         if ((try_smi_init(info)) == 0) {
3215                                 /* Found one... */
3216                                 printk(KERN_INFO PFX "Found default %s"
3217                                 " state machine at %s address 0x%lx\n",
3218                                 si_to_str[info->si_type],
3219                                 addr_space_to_str[info->io.addr_type],
3220                                 info->io.addr_data);
3221                         } else
3222                                 cleanup_one_si(info);
3223                 } else {
3224                         kfree(info);
3225                 }
3226         }
3227 }
3228
3229 static int is_new_interface(struct smi_info *info)
3230 {
3231         struct smi_info *e;
3232
3233         list_for_each_entry(e, &smi_infos, link) {
3234                 if (e->io.addr_type != info->io.addr_type)
3235                         continue;
3236                 if (e->io.addr_data == info->io.addr_data)
3237                         return 0;
3238         }
3239
3240         return 1;
3241 }
3242
3243 static int add_smi(struct smi_info *new_smi)
3244 {
3245         int rv = 0;
3246
3247         printk(KERN_INFO PFX "Adding %s-specified %s state machine",
3248                         ipmi_addr_src_to_str[new_smi->addr_source],
3249                         si_to_str[new_smi->si_type]);
3250         mutex_lock(&smi_infos_lock);
3251         if (!is_new_interface(new_smi)) {
3252                 printk(KERN_CONT " duplicate interface\n");
3253                 rv = -EBUSY;
3254                 goto out_err;
3255         }
3256
3257         printk(KERN_CONT "\n");
3258
3259         /* So we know not to free it unless we have allocated one. */
3260         new_smi->intf = NULL;
3261         new_smi->si_sm = NULL;
3262         new_smi->handlers = NULL;
3263
3264         list_add_tail(&new_smi->link, &smi_infos);
3265
3266 out_err:
3267         mutex_unlock(&smi_infos_lock);
3268         return rv;
3269 }
3270
3271 static int try_smi_init(struct smi_info *new_smi)
3272 {
3273         int rv = 0;
3274         int i;
3275
3276         printk(KERN_INFO PFX "Trying %s-specified %s state"
3277                " machine at %s address 0x%lx, slave address 0x%x,"
3278                " irq %d\n",
3279                ipmi_addr_src_to_str[new_smi->addr_source],
3280                si_to_str[new_smi->si_type],
3281                addr_space_to_str[new_smi->io.addr_type],
3282                new_smi->io.addr_data,
3283                new_smi->slave_addr, new_smi->irq);
3284
3285         switch (new_smi->si_type) {
3286         case SI_KCS:
3287                 new_smi->handlers = &kcs_smi_handlers;
3288                 break;
3289
3290         case SI_SMIC:
3291                 new_smi->handlers = &smic_smi_handlers;
3292                 break;
3293
3294         case SI_BT:
3295                 new_smi->handlers = &bt_smi_handlers;
3296                 break;
3297
3298         default:
3299                 /* No support for anything else yet. */
3300                 rv = -EIO;
3301                 goto out_err;
3302         }
3303
3304         /* Allocate the state machine's data and initialize it. */
3305         new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
3306         if (!new_smi->si_sm) {
3307                 printk(KERN_ERR PFX
3308                        "Could not allocate state machine memory\n");
3309                 rv = -ENOMEM;
3310                 goto out_err;
3311         }
3312         new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
3313                                                         &new_smi->io);
3314
3315         /* Now that we know the I/O size, we can set up the I/O. */
3316         rv = new_smi->io_setup(new_smi);
3317         if (rv) {
3318                 printk(KERN_ERR PFX "Could not set up I/O space\n");
3319                 goto out_err;
3320         }
3321
3322         /* Do low-level detection first. */
3323         if (new_smi->handlers->detect(new_smi->si_sm)) {
3324                 if (new_smi->addr_source)
3325                         printk(KERN_INFO PFX "Interface detection failed\n");
3326                 rv = -ENODEV;
3327                 goto out_err;
3328         }
3329
3330         /*
3331          * Attempt a get device id command.  If it fails, we probably
3332          * don't have a BMC here.
3333          */
3334         rv = try_get_dev_id(new_smi);
3335         if (rv) {
3336                 if (new_smi->addr_source)
3337                         printk(KERN_INFO PFX "There appears to be no BMC"
3338                                " at this location\n");
3339                 goto out_err;
3340         }
3341
3342         setup_oem_data_handler(new_smi);
3343         setup_xaction_handlers(new_smi);
3344
3345         INIT_LIST_HEAD(&(new_smi->xmit_msgs));
3346         INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
3347         new_smi->curr_msg = NULL;
3348         atomic_set(&new_smi->req_events, 0);
3349         new_smi->run_to_completion = 0;
3350         for (i = 0; i < SI_NUM_STATS; i++)
3351                 atomic_set(&new_smi->stats[i], 0);
3352
3353         new_smi->interrupt_disabled = 1;
3354         atomic_set(&new_smi->stop_operation, 0);
3355         new_smi->intf_num = smi_num;
3356         smi_num++;
3357
3358         rv = try_enable_event_buffer(new_smi);
3359         if (rv == 0)
3360                 new_smi->has_event_buffer = 1;
3361
3362         /*
3363          * Start clearing the flags before we enable interrupts or the
3364          * timer to avoid racing with the timer.
3365          */
3366         start_clear_flags(new_smi);
3367         /* IRQ is defined to be set when non-zero. */
3368         if (new_smi->irq)
3369                 new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
3370
3371         if (!new_smi->dev) {
3372                 /*
3373                  * If we don't already have a device from something
3374                  * else (like PCI), then register a new one.
3375                  */
3376                 new_smi->pdev = platform_device_alloc("ipmi_si",
3377                                                       new_smi->intf_num);
3378                 if (!new_smi->pdev) {
3379                         printk(KERN_ERR PFX
3380                                "Unable to allocate platform device\n");
3381                         goto out_err;
3382                 }
3383                 new_smi->dev = &new_smi->pdev->dev;
3384                 new_smi->dev->driver = &ipmi_driver.driver;
3385
3386                 rv = platform_device_add(new_smi->pdev);
3387                 if (rv) {
3388                         printk(KERN_ERR PFX
3389                                "Unable to register system interface device:"
3390                                " %d\n",
3391                                rv);
3392                         goto out_err;
3393                 }
3394                 new_smi->dev_registered = 1;
3395         }
3396
3397         rv = ipmi_register_smi(&handlers,
3398                                new_smi,
3399                                &new_smi->device_id,
3400                                new_smi->dev,
3401                                "bmc",
3402                                new_smi->slave_addr);
3403         if (rv) {
3404                 dev_err(new_smi->dev, "Unable to register device: error %d\n",
3405                         rv);
3406                 goto out_err_stop_timer;
3407         }
3408
3409         rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
3410                                      &smi_type_proc_ops,
3411                                      new_smi);
3412         if (rv) {
3413                 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
3414                 goto out_err_stop_timer;
3415         }
3416
3417         rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
3418                                      &smi_si_stats_proc_ops,
3419                                      new_smi);
3420         if (rv) {
3421                 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
3422                 goto out_err_stop_timer;
3423         }
3424
3425         rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
3426                                      &smi_params_proc_ops,
3427                                      new_smi);
3428         if (rv) {
3429                 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
3430                 goto out_err_stop_timer;
3431         }
3432
3433         dev_info(new_smi->dev, "IPMI %s interface initialized\n",
3434                  si_to_str[new_smi->si_type]);
3435
3436         return 0;
3437
3438  out_err_stop_timer:
3439         atomic_inc(&new_smi->stop_operation);
3440         wait_for_timer_and_thread(new_smi);
3441
3442  out_err:
3443         new_smi->interrupt_disabled = 1;
3444
3445         if (new_smi->intf) {
3446                 ipmi_unregister_smi(new_smi->intf);
3447                 new_smi->intf = NULL;
3448         }
3449
3450         if (new_smi->irq_cleanup) {
3451                 new_smi->irq_cleanup(new_smi);
3452                 new_smi->irq_cleanup = NULL;
3453         }
3454
3455         /*
3456          * Wait until we know that we are out of any interrupt
3457          * handlers might have been running before we freed the
3458          * interrupt.
3459          */
3460         synchronize_sched();
3461
3462         if (new_smi->si_sm) {
3463                 if (new_smi->handlers)
3464                         new_smi->handlers->cleanup(new_smi->si_sm);
3465                 kfree(new_smi->si_sm);
3466                 new_smi->si_sm = NULL;
3467         }
3468         if (new_smi->addr_source_cleanup) {
3469                 new_smi->addr_source_cleanup(new_smi);
3470                 new_smi->addr_source_cleanup = NULL;
3471         }
3472         if (new_smi->io_cleanup) {
3473                 new_smi->io_cleanup(new_smi);
3474                 new_smi->io_cleanup = NULL;
3475         }
3476
3477         if (new_smi->dev_registered) {
3478                 platform_device_unregister(new_smi->pdev);
3479                 new_smi->dev_registered = 0;
3480         }
3481
3482         return rv;
3483 }
3484
3485 static int init_ipmi_si(void)
3486 {
3487         int  i;
3488         char *str;
3489         int  rv;
3490         struct smi_info *e;
3491         enum ipmi_addr_src type = SI_INVALID;
3492
3493         if (initialized)
3494                 return 0;
3495         initialized = 1;
3496
3497         if (si_tryplatform) {
3498                 rv = platform_driver_register(&ipmi_driver);
3499                 if (rv) {
3500                         printk(KERN_ERR PFX "Unable to register "
3501                                "driver: %d\n", rv);
3502                         return rv;
3503                 }
3504         }
3505
3506         /* Parse out the si_type string into its components. */
3507         str = si_type_str;
3508         if (*str != '\0') {
3509                 for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
3510                         si_type[i] = str;
3511                         str = strchr(str, ',');
3512                         if (str) {
3513                                 *str = '\0';
3514                                 str++;
3515                         } else {
3516                                 break;
3517                         }
3518                 }
3519         }
3520
3521         printk(KERN_INFO "IPMI System Interface driver.\n");
3522
3523         /* If the user gave us a device, they presumably want us to use it */
3524         if (!hardcode_find_bmc())
3525                 return 0;
3526
3527 #ifdef CONFIG_PCI
3528         if (si_trypci) {
3529                 rv = pci_register_driver(&ipmi_pci_driver);
3530                 if (rv)
3531                         printk(KERN_ERR PFX "Unable to register "
3532                                "PCI driver: %d\n", rv);
3533                 else
3534                         pci_registered = 1;
3535         }
3536 #endif
3537
3538 #ifdef CONFIG_ACPI
3539         if (si_tryacpi) {
3540                 pnp_register_driver(&ipmi_pnp_driver);
3541                 pnp_registered = 1;
3542         }
3543 #endif
3544
3545 #ifdef CONFIG_DMI
3546         if (si_trydmi)
3547                 dmi_find_bmc();
3548 #endif
3549
3550 #ifdef CONFIG_ACPI
3551         if (si_tryacpi)
3552                 spmi_find_bmc();
3553 #endif
3554
3555 #ifdef CONFIG_PARISC
3556         register_parisc_driver(&ipmi_parisc_driver);
3557         parisc_registered = 1;
3558         /* poking PC IO addresses will crash machine, don't do it */
3559         si_trydefaults = 0;
3560 #endif
3561
3562         /* We prefer devices with interrupts, but in the case of a machine
3563            with multiple BMCs we assume that there will be several instances
3564            of a given type so if we succeed in registering a type then also
3565            try to register everything else of the same type */
3566
3567         mutex_lock(&smi_infos_lock);
3568         list_for_each_entry(e, &smi_infos, link) {
3569                 /* Try to register a device if it has an IRQ and we either
3570                    haven't successfully registered a device yet or this
3571                    device has the same type as one we successfully registered */
3572                 if (e->irq && (!type || e->addr_source == type)) {
3573                         if (!try_smi_init(e)) {
3574                                 type = e->addr_source;
3575                         }
3576                 }
3577         }
3578
3579         /* type will only have been set if we successfully registered an si */
3580         if (type) {
3581                 mutex_unlock(&smi_infos_lock);
3582                 return 0;
3583         }
3584
3585         /* Fall back to the preferred device */
3586
3587         list_for_each_entry(e, &smi_infos, link) {
3588                 if (!e->irq && (!type || e->addr_source == type)) {
3589                         if (!try_smi_init(e)) {
3590                                 type = e->addr_source;
3591                         }
3592                 }
3593         }
3594         mutex_unlock(&smi_infos_lock);
3595
3596         if (type)
3597                 return 0;
3598
3599         if (si_trydefaults) {
3600                 mutex_lock(&smi_infos_lock);
3601                 if (list_empty(&smi_infos)) {
3602                         /* No BMC was found, try defaults. */
3603                         mutex_unlock(&smi_infos_lock);
3604                         default_find_bmc();
3605                 } else
3606                         mutex_unlock(&smi_infos_lock);
3607         }
3608
3609         mutex_lock(&smi_infos_lock);
3610         if (unload_when_empty && list_empty(&smi_infos)) {
3611                 mutex_unlock(&smi_infos_lock);
3612                 cleanup_ipmi_si();
3613                 printk(KERN_WARNING PFX
3614                        "Unable to find any System Interface(s)\n");
3615                 return -ENODEV;
3616         } else {
3617                 mutex_unlock(&smi_infos_lock);
3618                 return 0;
3619         }
3620 }
3621 module_init(init_ipmi_si);
3622
3623 static void cleanup_one_si(struct smi_info *to_clean)
3624 {
3625         int           rv = 0;
3626         unsigned long flags;
3627
3628         if (!to_clean)
3629                 return;
3630
3631         list_del(&to_clean->link);
3632
3633         /* Tell the driver that we are shutting down. */
3634         atomic_inc(&to_clean->stop_operation);
3635
3636         /*
3637          * Make sure the timer and thread are stopped and will not run
3638          * again.
3639          */
3640         wait_for_timer_and_thread(to_clean);
3641
3642         /*
3643          * Timeouts are stopped, now make sure the interrupts are off
3644          * for the device.  A little tricky with locks to make sure
3645          * there are no races.
3646          */
3647         spin_lock_irqsave(&to_clean->si_lock, flags);
3648         while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3649                 spin_unlock_irqrestore(&to_clean->si_lock, flags);
3650                 poll(to_clean);
3651                 schedule_timeout_uninterruptible(1);
3652                 spin_lock_irqsave(&to_clean->si_lock, flags);
3653         }
3654         disable_si_irq(to_clean);
3655         spin_unlock_irqrestore(&to_clean->si_lock, flags);
3656         while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3657                 poll(to_clean);
3658                 schedule_timeout_uninterruptible(1);
3659         }
3660
3661         /* Clean up interrupts and make sure that everything is done. */
3662         if (to_clean->irq_cleanup)
3663                 to_clean->irq_cleanup(to_clean);
3664         while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3665                 poll(to_clean);
3666                 schedule_timeout_uninterruptible(1);
3667         }
3668
3669         if (to_clean->intf)
3670                 rv = ipmi_unregister_smi(to_clean->intf);
3671
3672         if (rv) {
3673                 printk(KERN_ERR PFX "Unable to unregister device: errno=%d\n",
3674                        rv);
3675         }
3676
3677         if (to_clean->handlers)
3678                 to_clean->handlers->cleanup(to_clean->si_sm);
3679
3680         kfree(to_clean->si_sm);
3681
3682         if (to_clean->addr_source_cleanup)
3683                 to_clean->addr_source_cleanup(to_clean);
3684         if (to_clean->io_cleanup)
3685                 to_clean->io_cleanup(to_clean);
3686
3687         if (to_clean->dev_registered)
3688                 platform_device_unregister(to_clean->pdev);
3689
3690         kfree(to_clean);
3691 }
3692
3693 static void cleanup_ipmi_si(void)
3694 {
3695         struct smi_info *e, *tmp_e;
3696
3697         if (!initialized)
3698                 return;
3699
3700 #ifdef CONFIG_PCI
3701         if (pci_registered)
3702                 pci_unregister_driver(&ipmi_pci_driver);
3703 #endif
3704 #ifdef CONFIG_ACPI
3705         if (pnp_registered)
3706                 pnp_unregister_driver(&ipmi_pnp_driver);
3707 #endif
3708 #ifdef CONFIG_PARISC
3709         if (parisc_registered)
3710                 unregister_parisc_driver(&ipmi_parisc_driver);
3711 #endif
3712
3713         platform_driver_unregister(&ipmi_driver);
3714
3715         mutex_lock(&smi_infos_lock);
3716         list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
3717                 cleanup_one_si(e);
3718         mutex_unlock(&smi_infos_lock);
3719 }
3720 module_exit(cleanup_ipmi_si);
3721
3722 MODULE_LICENSE("GPL");
3723 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
3724 MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
3725                    " system interfaces.");