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