4 * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
11 * Copyright 2002 MontaVista Software Inc.
12 * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
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.
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.
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.
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.
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/sched.h>
45 #include <linux/seq_file.h>
46 #include <linux/timer.h>
47 #include <linux/errno.h>
48 #include <linux/spinlock.h>
49 #include <linux/slab.h>
50 #include <linux/delay.h>
51 #include <linux/list.h>
52 #include <linux/pci.h>
53 #include <linux/ioport.h>
54 #include <linux/notifier.h>
55 #include <linux/mutex.h>
56 #include <linux/kthread.h>
58 #include <linux/interrupt.h>
59 #include <linux/rcupdate.h>
60 #include <linux/ipmi.h>
61 #include <linux/ipmi_smi.h>
63 #include "ipmi_si_sm.h"
64 #include <linux/dmi.h>
65 #include <linux/string.h>
66 #include <linux/ctype.h>
67 #include <linux/pnp.h>
68 #include <linux/of_device.h>
69 #include <linux/of_platform.h>
70 #include <linux/of_address.h>
71 #include <linux/of_irq.h>
74 #include <asm/hardware.h> /* for register_parisc_driver() stuff */
75 #include <asm/parisc-device.h>
78 #define PFX "ipmi_si: "
80 /* Measure times between events in the driver. */
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
95 SI_CLEARING_FLAGS_THEN_SET_IRQ,
97 SI_ENABLE_INTERRUPTS1,
98 SI_ENABLE_INTERRUPTS2,
99 SI_DISABLE_INTERRUPTS1,
100 SI_DISABLE_INTERRUPTS2
101 /* FIXME - add watchdog stuff. */
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
110 SI_KCS, SI_SMIC, SI_BT
112 static char *si_to_str[] = { "kcs", "smic", "bt" };
114 static char *ipmi_addr_src_to_str[] = { NULL, "hotmod", "hardcoded", "SPMI",
115 "ACPI", "SMBIOS", "PCI",
116 "device-tree", "default" };
118 #define DEVICE_NAME "ipmi_si"
120 static struct platform_driver ipmi_driver;
123 * Indexes into stats[] in smi_info below.
125 enum si_stat_indexes {
127 * Number of times the driver requested a timer while an operation
130 SI_STAT_short_timeouts = 0,
133 * Number of times the driver requested a timer while nothing was in
136 SI_STAT_long_timeouts,
138 /* Number of times the interface was idle while being polled. */
141 /* Number of interrupts the driver handled. */
144 /* Number of time the driver got an ATTN from the hardware. */
147 /* Number of times the driver requested flags from the hardware. */
148 SI_STAT_flag_fetches,
150 /* Number of times the hardware didn't follow the state machine. */
153 /* Number of completed messages. */
154 SI_STAT_complete_transactions,
156 /* Number of IPMI events received from the hardware. */
159 /* Number of watchdog pretimeouts. */
160 SI_STAT_watchdog_pretimeouts,
162 /* Number of asynchronous messages received. */
163 SI_STAT_incoming_messages,
166 /* This *must* remain last, add new values above this. */
173 struct si_sm_data *si_sm;
174 struct si_sm_handlers *handlers;
175 enum si_type si_type;
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;
183 * Used to handle the various types of I/O that can occur with
187 int (*io_setup)(struct smi_info *info);
188 void (*io_cleanup)(struct smi_info *info);
189 int (*irq_setup)(struct smi_info *info);
190 void (*irq_cleanup)(struct smi_info *info);
191 unsigned int io_size;
192 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
193 void (*addr_source_cleanup)(struct smi_info *info);
194 void *addr_source_data;
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.
201 int (*oem_data_avail_handler)(struct smi_info *smi_info);
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
208 #define RECEIVE_MSG_AVAIL 0x01
209 #define EVENT_MSG_BUFFER_FULL 0x02
210 #define WDT_PRE_TIMEOUT_INT 0x08
211 #define OEM0_DATA_AVAIL 0x20
212 #define OEM1_DATA_AVAIL 0x40
213 #define OEM2_DATA_AVAIL 0x80
214 #define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
217 unsigned char msg_flags;
219 /* Does the BMC have an event buffer? */
220 char has_event_buffer;
223 * If set to true, this will request events the next time the
224 * state machine is idle.
229 * If true, run the state machine to completion on every send
230 * call. Generally used after a panic to make sure stuff goes
233 int run_to_completion;
235 /* The I/O port of an SI interface. */
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.
243 unsigned int spacing;
245 /* zero if no irq; */
248 /* The timer for this si. */
249 struct timer_list si_timer;
251 /* This flag is set, if the timer is running (timer_pending() isn't enough) */
254 /* The time (in jiffies) the last timeout occurred at. */
255 unsigned long last_timeout_jiffies;
257 /* Used to gracefully stop the timer without race conditions. */
258 atomic_t stop_operation;
261 * The driver will disable interrupts when it gets into a
262 * situation where it cannot handle messages due to lack of
263 * memory. Once that situation clears up, it will re-enable
266 int interrupt_disabled;
268 /* From the get device id response... */
269 struct ipmi_device_id device_id;
271 /* Driver model stuff. */
273 struct platform_device *pdev;
276 * True if we allocated the device, false if it came from
277 * someplace else (like PCI).
281 /* Slave address, could be reported from DMI. */
282 unsigned char slave_addr;
284 /* Counters and things for the proc filesystem. */
285 atomic_t stats[SI_NUM_STATS];
287 struct task_struct *thread;
289 struct list_head link;
290 union ipmi_smi_info_union addr_info;
293 #define smi_inc_stat(smi, stat) \
294 atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
295 #define smi_get_stat(smi, stat) \
296 ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
298 #define SI_MAX_PARMS 4
300 static int force_kipmid[SI_MAX_PARMS];
301 static int num_force_kipmid;
303 static int pci_registered;
306 static int pnp_registered;
309 static int parisc_registered;
312 static unsigned int kipmid_max_busy_us[SI_MAX_PARMS];
313 static int num_max_busy_us;
315 static int unload_when_empty = 1;
317 static int add_smi(struct smi_info *smi);
318 static int try_smi_init(struct smi_info *smi);
319 static void cleanup_one_si(struct smi_info *to_clean);
320 static void cleanup_ipmi_si(void);
322 static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
323 static int register_xaction_notifier(struct notifier_block *nb)
325 return atomic_notifier_chain_register(&xaction_notifier_list, nb);
328 static void deliver_recv_msg(struct smi_info *smi_info,
329 struct ipmi_smi_msg *msg)
331 /* Deliver the message to the upper layer. */
332 ipmi_smi_msg_received(smi_info->intf, msg);
335 static void return_hosed_msg(struct smi_info *smi_info, int cCode)
337 struct ipmi_smi_msg *msg = smi_info->curr_msg;
339 if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
340 cCode = IPMI_ERR_UNSPECIFIED;
341 /* else use it as is */
343 /* Make it a response */
344 msg->rsp[0] = msg->data[0] | 4;
345 msg->rsp[1] = msg->data[1];
349 smi_info->curr_msg = NULL;
350 deliver_recv_msg(smi_info, msg);
353 static enum si_sm_result start_next_msg(struct smi_info *smi_info)
356 struct list_head *entry = NULL;
361 /* Pick the high priority queue first. */
362 if (!list_empty(&(smi_info->hp_xmit_msgs))) {
363 entry = smi_info->hp_xmit_msgs.next;
364 } else if (!list_empty(&(smi_info->xmit_msgs))) {
365 entry = smi_info->xmit_msgs.next;
369 smi_info->curr_msg = NULL;
375 smi_info->curr_msg = list_entry(entry,
380 printk(KERN_DEBUG "**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
382 err = atomic_notifier_call_chain(&xaction_notifier_list,
384 if (err & NOTIFY_STOP_MASK) {
385 rv = SI_SM_CALL_WITHOUT_DELAY;
388 err = smi_info->handlers->start_transaction(
390 smi_info->curr_msg->data,
391 smi_info->curr_msg->data_size);
393 return_hosed_msg(smi_info, err);
395 rv = SI_SM_CALL_WITHOUT_DELAY;
401 static void start_enable_irq(struct smi_info *smi_info)
403 unsigned char msg[2];
406 * If we are enabling interrupts, we have to tell the
409 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
410 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
412 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
413 smi_info->si_state = SI_ENABLE_INTERRUPTS1;
416 static void start_disable_irq(struct smi_info *smi_info)
418 unsigned char msg[2];
420 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
421 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
423 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
424 smi_info->si_state = SI_DISABLE_INTERRUPTS1;
427 static void start_clear_flags(struct smi_info *smi_info)
429 unsigned char msg[3];
431 /* Make sure the watchdog pre-timeout flag is not set at startup. */
432 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
433 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
434 msg[2] = WDT_PRE_TIMEOUT_INT;
436 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
437 smi_info->si_state = SI_CLEARING_FLAGS;
440 static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
442 smi_info->last_timeout_jiffies = jiffies;
443 mod_timer(&smi_info->si_timer, new_val);
444 smi_info->timer_running = true;
448 * When we have a situtaion where we run out of memory and cannot
449 * allocate messages, we just leave them in the BMC and run the system
450 * polled until we can allocate some memory. Once we have some
451 * memory, we will re-enable the interrupt.
453 static inline void disable_si_irq(struct smi_info *smi_info)
455 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
456 start_disable_irq(smi_info);
457 smi_info->interrupt_disabled = 1;
458 if (!atomic_read(&smi_info->stop_operation))
459 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
463 static inline void enable_si_irq(struct smi_info *smi_info)
465 if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
466 start_enable_irq(smi_info);
467 smi_info->interrupt_disabled = 0;
471 static void handle_flags(struct smi_info *smi_info)
474 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
475 /* Watchdog pre-timeout */
476 smi_inc_stat(smi_info, watchdog_pretimeouts);
478 start_clear_flags(smi_info);
479 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
480 ipmi_smi_watchdog_pretimeout(smi_info->intf);
481 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
482 /* Messages available. */
483 smi_info->curr_msg = ipmi_alloc_smi_msg();
484 if (!smi_info->curr_msg) {
485 disable_si_irq(smi_info);
486 smi_info->si_state = SI_NORMAL;
489 enable_si_irq(smi_info);
491 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
492 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
493 smi_info->curr_msg->data_size = 2;
495 smi_info->handlers->start_transaction(
497 smi_info->curr_msg->data,
498 smi_info->curr_msg->data_size);
499 smi_info->si_state = SI_GETTING_MESSAGES;
500 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
501 /* Events available. */
502 smi_info->curr_msg = ipmi_alloc_smi_msg();
503 if (!smi_info->curr_msg) {
504 disable_si_irq(smi_info);
505 smi_info->si_state = SI_NORMAL;
508 enable_si_irq(smi_info);
510 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
511 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
512 smi_info->curr_msg->data_size = 2;
514 smi_info->handlers->start_transaction(
516 smi_info->curr_msg->data,
517 smi_info->curr_msg->data_size);
518 smi_info->si_state = SI_GETTING_EVENTS;
519 } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
520 smi_info->oem_data_avail_handler) {
521 if (smi_info->oem_data_avail_handler(smi_info))
524 smi_info->si_state = SI_NORMAL;
527 static void handle_transaction_done(struct smi_info *smi_info)
529 struct ipmi_smi_msg *msg;
534 printk(KERN_DEBUG "**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
536 switch (smi_info->si_state) {
538 if (!smi_info->curr_msg)
541 smi_info->curr_msg->rsp_size
542 = smi_info->handlers->get_result(
544 smi_info->curr_msg->rsp,
545 IPMI_MAX_MSG_LENGTH);
548 * Do this here becase deliver_recv_msg() releases the
549 * lock, and a new message can be put in during the
550 * time the lock is released.
552 msg = smi_info->curr_msg;
553 smi_info->curr_msg = NULL;
554 deliver_recv_msg(smi_info, msg);
557 case SI_GETTING_FLAGS:
559 unsigned char msg[4];
562 /* We got the flags from the SMI, now handle them. */
563 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
565 /* Error fetching flags, just give up for now. */
566 smi_info->si_state = SI_NORMAL;
567 } else if (len < 4) {
569 * Hmm, no flags. That's technically illegal, but
570 * don't use uninitialized data.
572 smi_info->si_state = SI_NORMAL;
574 smi_info->msg_flags = msg[3];
575 handle_flags(smi_info);
580 case SI_CLEARING_FLAGS:
581 case SI_CLEARING_FLAGS_THEN_SET_IRQ:
583 unsigned char msg[3];
585 /* We cleared the flags. */
586 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
588 /* Error clearing flags */
589 dev_warn(smi_info->dev,
590 "Error clearing flags: %2.2x\n", msg[2]);
592 if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
593 start_enable_irq(smi_info);
595 smi_info->si_state = SI_NORMAL;
599 case SI_GETTING_EVENTS:
601 smi_info->curr_msg->rsp_size
602 = smi_info->handlers->get_result(
604 smi_info->curr_msg->rsp,
605 IPMI_MAX_MSG_LENGTH);
608 * Do this here becase deliver_recv_msg() releases the
609 * lock, and a new message can be put in during the
610 * time the lock is released.
612 msg = smi_info->curr_msg;
613 smi_info->curr_msg = NULL;
614 if (msg->rsp[2] != 0) {
615 /* Error getting event, probably done. */
618 /* Take off the event flag. */
619 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
620 handle_flags(smi_info);
622 smi_inc_stat(smi_info, events);
625 * Do this before we deliver the message
626 * because delivering the message releases the
627 * lock and something else can mess with the
630 handle_flags(smi_info);
632 deliver_recv_msg(smi_info, msg);
637 case SI_GETTING_MESSAGES:
639 smi_info->curr_msg->rsp_size
640 = smi_info->handlers->get_result(
642 smi_info->curr_msg->rsp,
643 IPMI_MAX_MSG_LENGTH);
646 * Do this here becase deliver_recv_msg() releases the
647 * lock, and a new message can be put in during the
648 * time the lock is released.
650 msg = smi_info->curr_msg;
651 smi_info->curr_msg = NULL;
652 if (msg->rsp[2] != 0) {
653 /* Error getting event, probably done. */
656 /* Take off the msg flag. */
657 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
658 handle_flags(smi_info);
660 smi_inc_stat(smi_info, incoming_messages);
663 * Do this before we deliver the message
664 * because delivering the message releases the
665 * lock and something else can mess with the
668 handle_flags(smi_info);
670 deliver_recv_msg(smi_info, msg);
675 case SI_ENABLE_INTERRUPTS1:
677 unsigned char msg[4];
679 /* We got the flags from the SMI, now handle them. */
680 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
682 dev_warn(smi_info->dev,
683 "Couldn't get irq info: %x.\n", msg[2]);
684 dev_warn(smi_info->dev,
685 "Maybe ok, but ipmi might run very slowly.\n");
686 smi_info->si_state = SI_NORMAL;
688 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
689 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
691 IPMI_BMC_RCV_MSG_INTR |
692 IPMI_BMC_EVT_MSG_INTR);
693 smi_info->handlers->start_transaction(
694 smi_info->si_sm, msg, 3);
695 smi_info->si_state = SI_ENABLE_INTERRUPTS2;
700 case SI_ENABLE_INTERRUPTS2:
702 unsigned char msg[4];
704 /* We got the flags from the SMI, now handle them. */
705 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
707 dev_warn(smi_info->dev,
708 "Couldn't set irq info: %x.\n", msg[2]);
709 dev_warn(smi_info->dev,
710 "Maybe ok, but ipmi might run very slowly.\n");
712 smi_info->interrupt_disabled = 0;
713 smi_info->si_state = SI_NORMAL;
717 case SI_DISABLE_INTERRUPTS1:
719 unsigned char msg[4];
721 /* We got the flags from the SMI, now handle them. */
722 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
724 dev_warn(smi_info->dev, "Could not disable interrupts"
726 smi_info->si_state = SI_NORMAL;
728 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
729 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
731 ~(IPMI_BMC_RCV_MSG_INTR |
732 IPMI_BMC_EVT_MSG_INTR));
733 smi_info->handlers->start_transaction(
734 smi_info->si_sm, msg, 3);
735 smi_info->si_state = SI_DISABLE_INTERRUPTS2;
740 case SI_DISABLE_INTERRUPTS2:
742 unsigned char msg[4];
744 /* We got the flags from the SMI, now handle them. */
745 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
747 dev_warn(smi_info->dev, "Could not disable interrupts"
750 smi_info->si_state = SI_NORMAL;
757 * Called on timeouts and events. Timeouts should pass the elapsed
758 * time, interrupts should pass in zero. Must be called with
759 * si_lock held and interrupts disabled.
761 static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
764 enum si_sm_result si_sm_result;
768 * There used to be a loop here that waited a little while
769 * (around 25us) before giving up. That turned out to be
770 * pointless, the minimum delays I was seeing were in the 300us
771 * range, which is far too long to wait in an interrupt. So
772 * we just run until the state machine tells us something
773 * happened or it needs a delay.
775 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
777 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
778 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
780 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
781 smi_inc_stat(smi_info, complete_transactions);
783 handle_transaction_done(smi_info);
784 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
785 } else if (si_sm_result == SI_SM_HOSED) {
786 smi_inc_stat(smi_info, hosed_count);
789 * Do the before return_hosed_msg, because that
792 smi_info->si_state = SI_NORMAL;
793 if (smi_info->curr_msg != NULL) {
795 * If we were handling a user message, format
796 * a response to send to the upper layer to
797 * tell it about the error.
799 return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
801 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
805 * We prefer handling attn over new messages. But don't do
806 * this if there is not yet an upper layer to handle anything.
808 if (likely(smi_info->intf) && si_sm_result == SI_SM_ATTN) {
809 unsigned char msg[2];
811 smi_inc_stat(smi_info, attentions);
814 * Got a attn, send down a get message flags to see
815 * what's causing it. It would be better to handle
816 * this in the upper layer, but due to the way
817 * interrupts work with the SMI, that's not really
820 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
821 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
823 smi_info->handlers->start_transaction(
824 smi_info->si_sm, msg, 2);
825 smi_info->si_state = SI_GETTING_FLAGS;
829 /* If we are currently idle, try to start the next message. */
830 if (si_sm_result == SI_SM_IDLE) {
831 smi_inc_stat(smi_info, idles);
833 si_sm_result = start_next_msg(smi_info);
834 if (si_sm_result != SI_SM_IDLE)
838 if ((si_sm_result == SI_SM_IDLE)
839 && (atomic_read(&smi_info->req_events))) {
841 * We are idle and the upper layer requested that I fetch
844 atomic_set(&smi_info->req_events, 0);
846 smi_info->curr_msg = ipmi_alloc_smi_msg();
847 if (!smi_info->curr_msg)
850 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
851 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
852 smi_info->curr_msg->data_size = 2;
854 smi_info->handlers->start_transaction(
856 smi_info->curr_msg->data,
857 smi_info->curr_msg->data_size);
858 smi_info->si_state = SI_GETTING_EVENTS;
865 static void sender(void *send_info,
866 struct ipmi_smi_msg *msg,
869 struct smi_info *smi_info = send_info;
870 enum si_sm_result result;
876 if (atomic_read(&smi_info->stop_operation)) {
877 msg->rsp[0] = msg->data[0] | 4;
878 msg->rsp[1] = msg->data[1];
879 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
881 deliver_recv_msg(smi_info, msg);
887 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
890 if (smi_info->run_to_completion) {
892 * If we are running to completion, then throw it in
893 * the list and run transactions until everything is
894 * clear. Priority doesn't matter here.
898 * Run to completion means we are single-threaded, no
901 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
903 result = smi_event_handler(smi_info, 0);
904 while (result != SI_SM_IDLE) {
905 udelay(SI_SHORT_TIMEOUT_USEC);
906 result = smi_event_handler(smi_info,
907 SI_SHORT_TIMEOUT_USEC);
912 spin_lock_irqsave(&smi_info->si_lock, flags);
914 list_add_tail(&msg->link, &smi_info->hp_xmit_msgs);
916 list_add_tail(&msg->link, &smi_info->xmit_msgs);
918 if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
919 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
921 if (smi_info->thread)
922 wake_up_process(smi_info->thread);
924 start_next_msg(smi_info);
925 smi_event_handler(smi_info, 0);
927 spin_unlock_irqrestore(&smi_info->si_lock, flags);
930 static void set_run_to_completion(void *send_info, int i_run_to_completion)
932 struct smi_info *smi_info = send_info;
933 enum si_sm_result result;
935 smi_info->run_to_completion = i_run_to_completion;
936 if (i_run_to_completion) {
937 result = smi_event_handler(smi_info, 0);
938 while (result != SI_SM_IDLE) {
939 udelay(SI_SHORT_TIMEOUT_USEC);
940 result = smi_event_handler(smi_info,
941 SI_SHORT_TIMEOUT_USEC);
947 * Use -1 in the nsec value of the busy waiting timespec to tell that
948 * we are spinning in kipmid looking for something and not delaying
951 static inline void ipmi_si_set_not_busy(struct timespec *ts)
955 static inline int ipmi_si_is_busy(struct timespec *ts)
957 return ts->tv_nsec != -1;
960 static int ipmi_thread_busy_wait(enum si_sm_result smi_result,
961 const struct smi_info *smi_info,
962 struct timespec *busy_until)
964 unsigned int max_busy_us = 0;
966 if (smi_info->intf_num < num_max_busy_us)
967 max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
968 if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
969 ipmi_si_set_not_busy(busy_until);
970 else if (!ipmi_si_is_busy(busy_until)) {
971 getnstimeofday(busy_until);
972 timespec_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
975 getnstimeofday(&now);
976 if (unlikely(timespec_compare(&now, busy_until) > 0)) {
977 ipmi_si_set_not_busy(busy_until);
986 * A busy-waiting loop for speeding up IPMI operation.
988 * Lousy hardware makes this hard. This is only enabled for systems
989 * that are not BT and do not have interrupts. It starts spinning
990 * when an operation is complete or until max_busy tells it to stop
991 * (if that is enabled). See the paragraph on kimid_max_busy_us in
992 * Documentation/IPMI.txt for details.
994 static int ipmi_thread(void *data)
996 struct smi_info *smi_info = data;
998 enum si_sm_result smi_result;
999 struct timespec busy_until;
1001 ipmi_si_set_not_busy(&busy_until);
1002 set_user_nice(current, 19);
1003 while (!kthread_should_stop()) {
1006 spin_lock_irqsave(&(smi_info->si_lock), flags);
1007 smi_result = smi_event_handler(smi_info, 0);
1010 * If the driver is doing something, there is a possible
1011 * race with the timer. If the timer handler see idle,
1012 * and the thread here sees something else, the timer
1013 * handler won't restart the timer even though it is
1014 * required. So start it here if necessary.
1016 if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
1017 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
1019 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1020 busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1022 if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1024 else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
1026 else if (smi_result == SI_SM_IDLE)
1027 schedule_timeout_interruptible(100);
1029 schedule_timeout_interruptible(1);
1035 static void poll(void *send_info)
1037 struct smi_info *smi_info = send_info;
1038 unsigned long flags = 0;
1039 int run_to_completion = smi_info->run_to_completion;
1042 * Make sure there is some delay in the poll loop so we can
1043 * drive time forward and timeout things.
1046 if (!run_to_completion)
1047 spin_lock_irqsave(&smi_info->si_lock, flags);
1048 smi_event_handler(smi_info, 10);
1049 if (!run_to_completion)
1050 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1053 static void request_events(void *send_info)
1055 struct smi_info *smi_info = send_info;
1057 if (atomic_read(&smi_info->stop_operation) ||
1058 !smi_info->has_event_buffer)
1061 atomic_set(&smi_info->req_events, 1);
1064 static int initialized;
1066 static void smi_timeout(unsigned long data)
1068 struct smi_info *smi_info = (struct smi_info *) data;
1069 enum si_sm_result smi_result;
1070 unsigned long flags;
1071 unsigned long jiffies_now;
1078 spin_lock_irqsave(&(smi_info->si_lock), flags);
1080 do_gettimeofday(&t);
1081 printk(KERN_DEBUG "**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1083 jiffies_now = jiffies;
1084 time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
1085 * SI_USEC_PER_JIFFY);
1086 smi_result = smi_event_handler(smi_info, time_diff);
1088 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
1089 /* Running with interrupts, only do long timeouts. */
1090 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1091 smi_inc_stat(smi_info, long_timeouts);
1096 * If the state machine asks for a short delay, then shorten
1097 * the timer timeout.
1099 if (smi_result == SI_SM_CALL_WITH_DELAY) {
1100 smi_inc_stat(smi_info, short_timeouts);
1101 timeout = jiffies + 1;
1103 smi_inc_stat(smi_info, long_timeouts);
1104 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1108 if (smi_result != SI_SM_IDLE)
1109 smi_mod_timer(smi_info, timeout);
1111 smi_info->timer_running = false;
1112 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1115 static irqreturn_t si_irq_handler(int irq, void *data)
1117 struct smi_info *smi_info = data;
1118 unsigned long flags;
1123 spin_lock_irqsave(&(smi_info->si_lock), flags);
1125 smi_inc_stat(smi_info, interrupts);
1128 do_gettimeofday(&t);
1129 printk(KERN_DEBUG "**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1131 smi_event_handler(smi_info, 0);
1132 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1136 static irqreturn_t si_bt_irq_handler(int irq, void *data)
1138 struct smi_info *smi_info = data;
1139 /* We need to clear the IRQ flag for the BT interface. */
1140 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1141 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1142 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1143 return si_irq_handler(irq, data);
1146 static int smi_start_processing(void *send_info,
1149 struct smi_info *new_smi = send_info;
1152 new_smi->intf = intf;
1154 /* Try to claim any interrupts. */
1155 if (new_smi->irq_setup)
1156 new_smi->irq_setup(new_smi);
1158 /* Set up the timer that drives the interface. */
1159 setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
1160 smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1163 * Check if the user forcefully enabled the daemon.
1165 if (new_smi->intf_num < num_force_kipmid)
1166 enable = force_kipmid[new_smi->intf_num];
1168 * The BT interface is efficient enough to not need a thread,
1169 * and there is no need for a thread if we have interrupts.
1171 else if ((new_smi->si_type != SI_BT) && (!new_smi->irq))
1175 new_smi->thread = kthread_run(ipmi_thread, new_smi,
1176 "kipmi%d", new_smi->intf_num);
1177 if (IS_ERR(new_smi->thread)) {
1178 dev_notice(new_smi->dev, "Could not start"
1179 " kernel thread due to error %ld, only using"
1180 " timers to drive the interface\n",
1181 PTR_ERR(new_smi->thread));
1182 new_smi->thread = NULL;
1189 static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1191 struct smi_info *smi = send_info;
1193 data->addr_src = smi->addr_source;
1194 data->dev = smi->dev;
1195 data->addr_info = smi->addr_info;
1196 get_device(smi->dev);
1201 static void set_maintenance_mode(void *send_info, int enable)
1203 struct smi_info *smi_info = send_info;
1206 atomic_set(&smi_info->req_events, 0);
1209 static struct ipmi_smi_handlers handlers = {
1210 .owner = THIS_MODULE,
1211 .start_processing = smi_start_processing,
1212 .get_smi_info = get_smi_info,
1214 .request_events = request_events,
1215 .set_maintenance_mode = set_maintenance_mode,
1216 .set_run_to_completion = set_run_to_completion,
1221 * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
1222 * a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS.
1225 static LIST_HEAD(smi_infos);
1226 static DEFINE_MUTEX(smi_infos_lock);
1227 static int smi_num; /* Used to sequence the SMIs */
1229 #define DEFAULT_REGSPACING 1
1230 #define DEFAULT_REGSIZE 1
1233 static bool si_tryacpi = 1;
1236 static bool si_trydmi = 1;
1238 static bool si_tryplatform = 1;
1240 static bool si_trypci = 1;
1242 static bool si_trydefaults = IS_ENABLED(CONFIG_IPMI_SI_PROBE_DEFAULTS);
1243 static char *si_type[SI_MAX_PARMS];
1244 #define MAX_SI_TYPE_STR 30
1245 static char si_type_str[MAX_SI_TYPE_STR];
1246 static unsigned long addrs[SI_MAX_PARMS];
1247 static unsigned int num_addrs;
1248 static unsigned int ports[SI_MAX_PARMS];
1249 static unsigned int num_ports;
1250 static int irqs[SI_MAX_PARMS];
1251 static unsigned int num_irqs;
1252 static int regspacings[SI_MAX_PARMS];
1253 static unsigned int num_regspacings;
1254 static int regsizes[SI_MAX_PARMS];
1255 static unsigned int num_regsizes;
1256 static int regshifts[SI_MAX_PARMS];
1257 static unsigned int num_regshifts;
1258 static int slave_addrs[SI_MAX_PARMS]; /* Leaving 0 chooses the default value */
1259 static unsigned int num_slave_addrs;
1261 #define IPMI_IO_ADDR_SPACE 0
1262 #define IPMI_MEM_ADDR_SPACE 1
1263 static char *addr_space_to_str[] = { "i/o", "mem" };
1265 static int hotmod_handler(const char *val, struct kernel_param *kp);
1267 module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
1268 MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"
1269 " Documentation/IPMI.txt in the kernel sources for the"
1273 module_param_named(tryacpi, si_tryacpi, bool, 0);
1274 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1275 " default scan of the interfaces identified via ACPI");
1278 module_param_named(trydmi, si_trydmi, bool, 0);
1279 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the"
1280 " default scan of the interfaces identified via DMI");
1282 module_param_named(tryplatform, si_tryplatform, bool, 0);
1283 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1284 " default scan of the interfaces identified via platform"
1285 " interfaces like openfirmware");
1287 module_param_named(trypci, si_trypci, bool, 0);
1288 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1289 " default scan of the interfaces identified via pci");
1291 module_param_named(trydefaults, si_trydefaults, bool, 0);
1292 MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
1293 " default scan of the KCS and SMIC interface at the standard"
1295 module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
1296 MODULE_PARM_DESC(type, "Defines the type of each interface, each"
1297 " interface separated by commas. The types are 'kcs',"
1298 " 'smic', and 'bt'. For example si_type=kcs,bt will set"
1299 " the first interface to kcs and the second to bt");
1300 module_param_array(addrs, ulong, &num_addrs, 0);
1301 MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
1302 " addresses separated by commas. Only use if an interface"
1303 " is in memory. Otherwise, set it to zero or leave"
1305 module_param_array(ports, uint, &num_ports, 0);
1306 MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
1307 " addresses separated by commas. Only use if an interface"
1308 " is a port. Otherwise, set it to zero or leave"
1310 module_param_array(irqs, int, &num_irqs, 0);
1311 MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
1312 " addresses separated by commas. Only use if an interface"
1313 " has an interrupt. Otherwise, set it to zero or leave"
1315 module_param_array(regspacings, int, &num_regspacings, 0);
1316 MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
1317 " and each successive register used by the interface. For"
1318 " instance, if the start address is 0xca2 and the spacing"
1319 " is 2, then the second address is at 0xca4. Defaults"
1321 module_param_array(regsizes, int, &num_regsizes, 0);
1322 MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
1323 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
1324 " 16-bit, 32-bit, or 64-bit register. Use this if you"
1325 " the 8-bit IPMI register has to be read from a larger"
1327 module_param_array(regshifts, int, &num_regshifts, 0);
1328 MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
1329 " IPMI register, in bits. For instance, if the data"
1330 " is read from a 32-bit word and the IPMI data is in"
1331 " bit 8-15, then the shift would be 8");
1332 module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1333 MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
1334 " the controller. Normally this is 0x20, but can be"
1335 " overridden by this parm. This is an array indexed"
1336 " by interface number.");
1337 module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1338 MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1339 " disabled(0). Normally the IPMI driver auto-detects"
1340 " this, but the value may be overridden by this parm.");
1341 module_param(unload_when_empty, int, 0);
1342 MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1343 " specified or found, default is 1. Setting to 0"
1344 " is useful for hot add of devices using hotmod.");
1345 module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1346 MODULE_PARM_DESC(kipmid_max_busy_us,
1347 "Max time (in microseconds) to busy-wait for IPMI data before"
1348 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1349 " if kipmid is using up a lot of CPU time.");
1352 static void std_irq_cleanup(struct smi_info *info)
1354 if (info->si_type == SI_BT)
1355 /* Disable the interrupt in the BT interface. */
1356 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
1357 free_irq(info->irq, info);
1360 static int std_irq_setup(struct smi_info *info)
1367 if (info->si_type == SI_BT) {
1368 rv = request_irq(info->irq,
1374 /* Enable the interrupt in the BT interface. */
1375 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1376 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1378 rv = request_irq(info->irq,
1384 dev_warn(info->dev, "%s unable to claim interrupt %d,"
1385 " running polled\n",
1386 DEVICE_NAME, info->irq);
1389 info->irq_cleanup = std_irq_cleanup;
1390 dev_info(info->dev, "Using irq %d\n", info->irq);
1396 static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1398 unsigned int addr = io->addr_data;
1400 return inb(addr + (offset * io->regspacing));
1403 static void port_outb(struct si_sm_io *io, unsigned int offset,
1406 unsigned int addr = io->addr_data;
1408 outb(b, addr + (offset * io->regspacing));
1411 static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1413 unsigned int addr = io->addr_data;
1415 return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1418 static void port_outw(struct si_sm_io *io, unsigned int offset,
1421 unsigned int addr = io->addr_data;
1423 outw(b << io->regshift, addr + (offset * io->regspacing));
1426 static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1428 unsigned int addr = io->addr_data;
1430 return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1433 static void port_outl(struct si_sm_io *io, unsigned int offset,
1436 unsigned int addr = io->addr_data;
1438 outl(b << io->regshift, addr+(offset * io->regspacing));
1441 static void port_cleanup(struct smi_info *info)
1443 unsigned int addr = info->io.addr_data;
1447 for (idx = 0; idx < info->io_size; idx++)
1448 release_region(addr + idx * info->io.regspacing,
1453 static int port_setup(struct smi_info *info)
1455 unsigned int addr = info->io.addr_data;
1461 info->io_cleanup = port_cleanup;
1464 * Figure out the actual inb/inw/inl/etc routine to use based
1465 * upon the register size.
1467 switch (info->io.regsize) {
1469 info->io.inputb = port_inb;
1470 info->io.outputb = port_outb;
1473 info->io.inputb = port_inw;
1474 info->io.outputb = port_outw;
1477 info->io.inputb = port_inl;
1478 info->io.outputb = port_outl;
1481 dev_warn(info->dev, "Invalid register size: %d\n",
1487 * Some BIOSes reserve disjoint I/O regions in their ACPI
1488 * tables. This causes problems when trying to register the
1489 * entire I/O region. Therefore we must register each I/O
1492 for (idx = 0; idx < info->io_size; idx++) {
1493 if (request_region(addr + idx * info->io.regspacing,
1494 info->io.regsize, DEVICE_NAME) == NULL) {
1495 /* Undo allocations */
1497 release_region(addr + idx * info->io.regspacing,
1506 static unsigned char intf_mem_inb(struct si_sm_io *io, unsigned int offset)
1508 return readb((io->addr)+(offset * io->regspacing));
1511 static void intf_mem_outb(struct si_sm_io *io, unsigned int offset,
1514 writeb(b, (io->addr)+(offset * io->regspacing));
1517 static unsigned char intf_mem_inw(struct si_sm_io *io, unsigned int offset)
1519 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
1523 static void intf_mem_outw(struct si_sm_io *io, unsigned int offset,
1526 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1529 static unsigned char intf_mem_inl(struct si_sm_io *io, unsigned int offset)
1531 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
1535 static void intf_mem_outl(struct si_sm_io *io, unsigned int offset,
1538 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1542 static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1544 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
1548 static void mem_outq(struct si_sm_io *io, unsigned int offset,
1551 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1555 static void mem_cleanup(struct smi_info *info)
1557 unsigned long addr = info->io.addr_data;
1560 if (info->io.addr) {
1561 iounmap(info->io.addr);
1563 mapsize = ((info->io_size * info->io.regspacing)
1564 - (info->io.regspacing - info->io.regsize));
1566 release_mem_region(addr, mapsize);
1570 static int mem_setup(struct smi_info *info)
1572 unsigned long addr = info->io.addr_data;
1578 info->io_cleanup = mem_cleanup;
1581 * Figure out the actual readb/readw/readl/etc routine to use based
1582 * upon the register size.
1584 switch (info->io.regsize) {
1586 info->io.inputb = intf_mem_inb;
1587 info->io.outputb = intf_mem_outb;
1590 info->io.inputb = intf_mem_inw;
1591 info->io.outputb = intf_mem_outw;
1594 info->io.inputb = intf_mem_inl;
1595 info->io.outputb = intf_mem_outl;
1599 info->io.inputb = mem_inq;
1600 info->io.outputb = mem_outq;
1604 dev_warn(info->dev, "Invalid register size: %d\n",
1610 * Calculate the total amount of memory to claim. This is an
1611 * unusual looking calculation, but it avoids claiming any
1612 * more memory than it has to. It will claim everything
1613 * between the first address to the end of the last full
1616 mapsize = ((info->io_size * info->io.regspacing)
1617 - (info->io.regspacing - info->io.regsize));
1619 if (request_mem_region(addr, mapsize, DEVICE_NAME) == NULL)
1622 info->io.addr = ioremap(addr, mapsize);
1623 if (info->io.addr == NULL) {
1624 release_mem_region(addr, mapsize);
1631 * Parms come in as <op1>[:op2[:op3...]]. ops are:
1632 * add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
1640 enum hotmod_op { HM_ADD, HM_REMOVE };
1641 struct hotmod_vals {
1645 static struct hotmod_vals hotmod_ops[] = {
1647 { "remove", HM_REMOVE },
1650 static struct hotmod_vals hotmod_si[] = {
1652 { "smic", SI_SMIC },
1656 static struct hotmod_vals hotmod_as[] = {
1657 { "mem", IPMI_MEM_ADDR_SPACE },
1658 { "i/o", IPMI_IO_ADDR_SPACE },
1662 static int parse_str(struct hotmod_vals *v, int *val, char *name, char **curr)
1667 s = strchr(*curr, ',');
1669 printk(KERN_WARNING PFX "No hotmod %s given.\n", name);
1674 for (i = 0; hotmod_ops[i].name; i++) {
1675 if (strcmp(*curr, v[i].name) == 0) {
1682 printk(KERN_WARNING PFX "Invalid hotmod %s '%s'\n", name, *curr);
1686 static int check_hotmod_int_op(const char *curr, const char *option,
1687 const char *name, int *val)
1691 if (strcmp(curr, name) == 0) {
1693 printk(KERN_WARNING PFX
1694 "No option given for '%s'\n",
1698 *val = simple_strtoul(option, &n, 0);
1699 if ((*n != '\0') || (*option == '\0')) {
1700 printk(KERN_WARNING PFX
1701 "Bad option given for '%s'\n",
1710 static struct smi_info *smi_info_alloc(void)
1712 struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
1715 spin_lock_init(&info->si_lock);
1719 static int hotmod_handler(const char *val, struct kernel_param *kp)
1721 char *str = kstrdup(val, GFP_KERNEL);
1723 char *next, *curr, *s, *n, *o;
1725 enum si_type si_type;
1735 struct smi_info *info;
1740 /* Kill any trailing spaces, as we can get a "\n" from echo. */
1743 while ((ival >= 0) && isspace(str[ival])) {
1748 for (curr = str; curr; curr = next) {
1753 ipmb = 0; /* Choose the default if not specified */
1755 next = strchr(curr, ':');
1761 rv = parse_str(hotmod_ops, &ival, "operation", &curr);
1766 rv = parse_str(hotmod_si, &ival, "interface type", &curr);
1771 rv = parse_str(hotmod_as, &addr_space, "address space", &curr);
1775 s = strchr(curr, ',');
1780 addr = simple_strtoul(curr, &n, 0);
1781 if ((*n != '\0') || (*curr == '\0')) {
1782 printk(KERN_WARNING PFX "Invalid hotmod address"
1789 s = strchr(curr, ',');
1794 o = strchr(curr, '=');
1799 rv = check_hotmod_int_op(curr, o, "rsp", ®spacing);
1804 rv = check_hotmod_int_op(curr, o, "rsi", ®size);
1809 rv = check_hotmod_int_op(curr, o, "rsh", ®shift);
1814 rv = check_hotmod_int_op(curr, o, "irq", &irq);
1819 rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);
1826 printk(KERN_WARNING PFX
1827 "Invalid hotmod option '%s'\n",
1833 info = smi_info_alloc();
1839 info->addr_source = SI_HOTMOD;
1840 info->si_type = si_type;
1841 info->io.addr_data = addr;
1842 info->io.addr_type = addr_space;
1843 if (addr_space == IPMI_MEM_ADDR_SPACE)
1844 info->io_setup = mem_setup;
1846 info->io_setup = port_setup;
1848 info->io.addr = NULL;
1849 info->io.regspacing = regspacing;
1850 if (!info->io.regspacing)
1851 info->io.regspacing = DEFAULT_REGSPACING;
1852 info->io.regsize = regsize;
1853 if (!info->io.regsize)
1854 info->io.regsize = DEFAULT_REGSPACING;
1855 info->io.regshift = regshift;
1858 info->irq_setup = std_irq_setup;
1859 info->slave_addr = ipmb;
1866 rv = try_smi_init(info);
1868 cleanup_one_si(info);
1873 struct smi_info *e, *tmp_e;
1875 mutex_lock(&smi_infos_lock);
1876 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
1877 if (e->io.addr_type != addr_space)
1879 if (e->si_type != si_type)
1881 if (e->io.addr_data == addr)
1884 mutex_unlock(&smi_infos_lock);
1893 static int hardcode_find_bmc(void)
1897 struct smi_info *info;
1899 for (i = 0; i < SI_MAX_PARMS; i++) {
1900 if (!ports[i] && !addrs[i])
1903 info = smi_info_alloc();
1907 info->addr_source = SI_HARDCODED;
1908 printk(KERN_INFO PFX "probing via hardcoded address\n");
1910 if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
1911 info->si_type = SI_KCS;
1912 } else if (strcmp(si_type[i], "smic") == 0) {
1913 info->si_type = SI_SMIC;
1914 } else if (strcmp(si_type[i], "bt") == 0) {
1915 info->si_type = SI_BT;
1917 printk(KERN_WARNING PFX "Interface type specified "
1918 "for interface %d, was invalid: %s\n",
1926 info->io_setup = port_setup;
1927 info->io.addr_data = ports[i];
1928 info->io.addr_type = IPMI_IO_ADDR_SPACE;
1929 } else if (addrs[i]) {
1931 info->io_setup = mem_setup;
1932 info->io.addr_data = addrs[i];
1933 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
1935 printk(KERN_WARNING PFX "Interface type specified "
1936 "for interface %d, but port and address were "
1937 "not set or set to zero.\n", i);
1942 info->io.addr = NULL;
1943 info->io.regspacing = regspacings[i];
1944 if (!info->io.regspacing)
1945 info->io.regspacing = DEFAULT_REGSPACING;
1946 info->io.regsize = regsizes[i];
1947 if (!info->io.regsize)
1948 info->io.regsize = DEFAULT_REGSPACING;
1949 info->io.regshift = regshifts[i];
1950 info->irq = irqs[i];
1952 info->irq_setup = std_irq_setup;
1953 info->slave_addr = slave_addrs[i];
1955 if (!add_smi(info)) {
1956 if (try_smi_init(info))
1957 cleanup_one_si(info);
1968 #include <linux/acpi.h>
1971 * Once we get an ACPI failure, we don't try any more, because we go
1972 * through the tables sequentially. Once we don't find a table, there
1975 static int acpi_failure;
1977 /* For GPE-type interrupts. */
1978 static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
1979 u32 gpe_number, void *context)
1981 struct smi_info *smi_info = context;
1982 unsigned long flags;
1987 spin_lock_irqsave(&(smi_info->si_lock), flags);
1989 smi_inc_stat(smi_info, interrupts);
1992 do_gettimeofday(&t);
1993 printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1995 smi_event_handler(smi_info, 0);
1996 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1998 return ACPI_INTERRUPT_HANDLED;
2001 static void acpi_gpe_irq_cleanup(struct smi_info *info)
2006 acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
2009 static int acpi_gpe_irq_setup(struct smi_info *info)
2016 /* FIXME - is level triggered right? */
2017 status = acpi_install_gpe_handler(NULL,
2019 ACPI_GPE_LEVEL_TRIGGERED,
2022 if (status != AE_OK) {
2023 dev_warn(info->dev, "%s unable to claim ACPI GPE %d,"
2024 " running polled\n", DEVICE_NAME, info->irq);
2028 info->irq_cleanup = acpi_gpe_irq_cleanup;
2029 dev_info(info->dev, "Using ACPI GPE %d\n", info->irq);
2036 * http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf
2047 s8 CreatorRevision[4];
2050 s16 SpecificationRevision;
2053 * Bit 0 - SCI interrupt supported
2054 * Bit 1 - I/O APIC/SAPIC
2059 * If bit 0 of InterruptType is set, then this is the SCI
2060 * interrupt in the GPEx_STS register.
2067 * If bit 1 of InterruptType is set, then this is the I/O
2068 * APIC/SAPIC interrupt.
2070 u32 GlobalSystemInterrupt;
2072 /* The actual register address. */
2073 struct acpi_generic_address addr;
2077 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
2080 static int try_init_spmi(struct SPMITable *spmi)
2082 struct smi_info *info;
2085 if (spmi->IPMIlegacy != 1) {
2086 printk(KERN_INFO PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);
2090 info = smi_info_alloc();
2092 printk(KERN_ERR PFX "Could not allocate SI data (3)\n");
2096 info->addr_source = SI_SPMI;
2097 printk(KERN_INFO PFX "probing via SPMI\n");
2099 /* Figure out the interface type. */
2100 switch (spmi->InterfaceType) {
2102 info->si_type = SI_KCS;
2105 info->si_type = SI_SMIC;
2108 info->si_type = SI_BT;
2111 printk(KERN_INFO PFX "Unknown ACPI/SPMI SI type %d\n",
2112 spmi->InterfaceType);
2117 if (spmi->InterruptType & 1) {
2118 /* We've got a GPE interrupt. */
2119 info->irq = spmi->GPE;
2120 info->irq_setup = acpi_gpe_irq_setup;
2121 } else if (spmi->InterruptType & 2) {
2122 /* We've got an APIC/SAPIC interrupt. */
2123 info->irq = spmi->GlobalSystemInterrupt;
2124 info->irq_setup = std_irq_setup;
2126 /* Use the default interrupt setting. */
2128 info->irq_setup = NULL;
2131 if (spmi->addr.bit_width) {
2132 /* A (hopefully) properly formed register bit width. */
2133 info->io.regspacing = spmi->addr.bit_width / 8;
2135 info->io.regspacing = DEFAULT_REGSPACING;
2137 info->io.regsize = info->io.regspacing;
2138 info->io.regshift = spmi->addr.bit_offset;
2140 if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
2141 info->io_setup = mem_setup;
2142 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2143 } else if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
2144 info->io_setup = port_setup;
2145 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2148 printk(KERN_WARNING PFX "Unknown ACPI I/O Address type\n");
2151 info->io.addr_data = spmi->addr.address;
2153 pr_info("ipmi_si: SPMI: %s %#lx regsize %d spacing %d irq %d\n",
2154 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2155 info->io.addr_data, info->io.regsize, info->io.regspacing,
2165 static void spmi_find_bmc(void)
2168 struct SPMITable *spmi;
2177 for (i = 0; ; i++) {
2178 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
2179 (struct acpi_table_header **)&spmi);
2180 if (status != AE_OK)
2183 try_init_spmi(spmi);
2187 static int ipmi_pnp_probe(struct pnp_dev *dev,
2188 const struct pnp_device_id *dev_id)
2190 struct acpi_device *acpi_dev;
2191 struct smi_info *info;
2192 struct resource *res, *res_second;
2195 unsigned long long tmp;
2198 acpi_dev = pnp_acpi_device(dev);
2202 info = smi_info_alloc();
2206 info->addr_source = SI_ACPI;
2207 printk(KERN_INFO PFX "probing via ACPI\n");
2209 handle = acpi_dev->handle;
2210 info->addr_info.acpi_info.acpi_handle = handle;
2212 /* _IFT tells us the interface type: KCS, BT, etc */
2213 status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
2214 if (ACPI_FAILURE(status))
2219 info->si_type = SI_KCS;
2222 info->si_type = SI_SMIC;
2225 info->si_type = SI_BT;
2228 dev_info(&dev->dev, "unknown IPMI type %lld\n", tmp);
2232 res = pnp_get_resource(dev, IORESOURCE_IO, 0);
2234 info->io_setup = port_setup;
2235 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2237 res = pnp_get_resource(dev, IORESOURCE_MEM, 0);
2239 info->io_setup = mem_setup;
2240 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2244 dev_err(&dev->dev, "no I/O or memory address\n");
2247 info->io.addr_data = res->start;
2249 info->io.regspacing = DEFAULT_REGSPACING;
2250 res_second = pnp_get_resource(dev,
2251 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ?
2252 IORESOURCE_IO : IORESOURCE_MEM,
2255 if (res_second->start > info->io.addr_data)
2256 info->io.regspacing = res_second->start - info->io.addr_data;
2258 info->io.regsize = DEFAULT_REGSPACING;
2259 info->io.regshift = 0;
2261 /* If _GPE exists, use it; otherwise use standard interrupts */
2262 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
2263 if (ACPI_SUCCESS(status)) {
2265 info->irq_setup = acpi_gpe_irq_setup;
2266 } else if (pnp_irq_valid(dev, 0)) {
2267 info->irq = pnp_irq(dev, 0);
2268 info->irq_setup = std_irq_setup;
2271 info->dev = &dev->dev;
2272 pnp_set_drvdata(dev, info);
2274 dev_info(info->dev, "%pR regsize %d spacing %d irq %d\n",
2275 res, info->io.regsize, info->io.regspacing,
2289 static void ipmi_pnp_remove(struct pnp_dev *dev)
2291 struct smi_info *info = pnp_get_drvdata(dev);
2293 cleanup_one_si(info);
2296 static const struct pnp_device_id pnp_dev_table[] = {
2301 static struct pnp_driver ipmi_pnp_driver = {
2302 .name = DEVICE_NAME,
2303 .probe = ipmi_pnp_probe,
2304 .remove = ipmi_pnp_remove,
2305 .id_table = pnp_dev_table,
2308 MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
2312 struct dmi_ipmi_data {
2315 unsigned long base_addr;
2321 static int decode_dmi(const struct dmi_header *dm,
2322 struct dmi_ipmi_data *dmi)
2324 const u8 *data = (const u8 *)dm;
2325 unsigned long base_addr;
2327 u8 len = dm->length;
2329 dmi->type = data[4];
2331 memcpy(&base_addr, data+8, sizeof(unsigned long));
2333 if (base_addr & 1) {
2335 base_addr &= 0xFFFE;
2336 dmi->addr_space = IPMI_IO_ADDR_SPACE;
2339 dmi->addr_space = IPMI_MEM_ADDR_SPACE;
2341 /* If bit 4 of byte 0x10 is set, then the lsb for the address
2343 dmi->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
2345 dmi->irq = data[0x11];
2347 /* The top two bits of byte 0x10 hold the register spacing. */
2348 reg_spacing = (data[0x10] & 0xC0) >> 6;
2349 switch (reg_spacing) {
2350 case 0x00: /* Byte boundaries */
2353 case 0x01: /* 32-bit boundaries */
2356 case 0x02: /* 16-byte boundaries */
2360 /* Some other interface, just ignore it. */
2366 * Note that technically, the lower bit of the base
2367 * address should be 1 if the address is I/O and 0 if
2368 * the address is in memory. So many systems get that
2369 * wrong (and all that I have seen are I/O) so we just
2370 * ignore that bit and assume I/O. Systems that use
2371 * memory should use the newer spec, anyway.
2373 dmi->base_addr = base_addr & 0xfffe;
2374 dmi->addr_space = IPMI_IO_ADDR_SPACE;
2378 dmi->slave_addr = data[6];
2383 static void try_init_dmi(struct dmi_ipmi_data *ipmi_data)
2385 struct smi_info *info;
2387 info = smi_info_alloc();
2389 printk(KERN_ERR PFX "Could not allocate SI data\n");
2393 info->addr_source = SI_SMBIOS;
2394 printk(KERN_INFO PFX "probing via SMBIOS\n");
2396 switch (ipmi_data->type) {
2397 case 0x01: /* KCS */
2398 info->si_type = SI_KCS;
2400 case 0x02: /* SMIC */
2401 info->si_type = SI_SMIC;
2404 info->si_type = SI_BT;
2411 switch (ipmi_data->addr_space) {
2412 case IPMI_MEM_ADDR_SPACE:
2413 info->io_setup = mem_setup;
2414 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2417 case IPMI_IO_ADDR_SPACE:
2418 info->io_setup = port_setup;
2419 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2424 printk(KERN_WARNING PFX "Unknown SMBIOS I/O Address type: %d\n",
2425 ipmi_data->addr_space);
2428 info->io.addr_data = ipmi_data->base_addr;
2430 info->io.regspacing = ipmi_data->offset;
2431 if (!info->io.regspacing)
2432 info->io.regspacing = DEFAULT_REGSPACING;
2433 info->io.regsize = DEFAULT_REGSPACING;
2434 info->io.regshift = 0;
2436 info->slave_addr = ipmi_data->slave_addr;
2438 info->irq = ipmi_data->irq;
2440 info->irq_setup = std_irq_setup;
2442 pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
2443 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2444 info->io.addr_data, info->io.regsize, info->io.regspacing,
2451 static void dmi_find_bmc(void)
2453 const struct dmi_device *dev = NULL;
2454 struct dmi_ipmi_data data;
2457 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
2458 memset(&data, 0, sizeof(data));
2459 rv = decode_dmi((const struct dmi_header *) dev->device_data,
2462 try_init_dmi(&data);
2465 #endif /* CONFIG_DMI */
2469 #define PCI_ERMC_CLASSCODE 0x0C0700
2470 #define PCI_ERMC_CLASSCODE_MASK 0xffffff00
2471 #define PCI_ERMC_CLASSCODE_TYPE_MASK 0xff
2472 #define PCI_ERMC_CLASSCODE_TYPE_SMIC 0x00
2473 #define PCI_ERMC_CLASSCODE_TYPE_KCS 0x01
2474 #define PCI_ERMC_CLASSCODE_TYPE_BT 0x02
2476 #define PCI_HP_VENDOR_ID 0x103C
2477 #define PCI_MMC_DEVICE_ID 0x121A
2478 #define PCI_MMC_ADDR_CW 0x10
2480 static void ipmi_pci_cleanup(struct smi_info *info)
2482 struct pci_dev *pdev = info->addr_source_data;
2484 pci_disable_device(pdev);
2487 static int ipmi_pci_probe_regspacing(struct smi_info *info)
2489 if (info->si_type == SI_KCS) {
2490 unsigned char status;
2493 info->io.regsize = DEFAULT_REGSIZE;
2494 info->io.regshift = 0;
2496 info->handlers = &kcs_smi_handlers;
2498 /* detect 1, 4, 16byte spacing */
2499 for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
2500 info->io.regspacing = regspacing;
2501 if (info->io_setup(info)) {
2503 "Could not setup I/O space\n");
2504 return DEFAULT_REGSPACING;
2506 /* write invalid cmd */
2507 info->io.outputb(&info->io, 1, 0x10);
2508 /* read status back */
2509 status = info->io.inputb(&info->io, 1);
2510 info->io_cleanup(info);
2516 return DEFAULT_REGSPACING;
2519 static int ipmi_pci_probe(struct pci_dev *pdev,
2520 const struct pci_device_id *ent)
2523 int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
2524 struct smi_info *info;
2526 info = smi_info_alloc();
2530 info->addr_source = SI_PCI;
2531 dev_info(&pdev->dev, "probing via PCI");
2533 switch (class_type) {
2534 case PCI_ERMC_CLASSCODE_TYPE_SMIC:
2535 info->si_type = SI_SMIC;
2538 case PCI_ERMC_CLASSCODE_TYPE_KCS:
2539 info->si_type = SI_KCS;
2542 case PCI_ERMC_CLASSCODE_TYPE_BT:
2543 info->si_type = SI_BT;
2548 dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);
2552 rv = pci_enable_device(pdev);
2554 dev_err(&pdev->dev, "couldn't enable PCI device\n");
2559 info->addr_source_cleanup = ipmi_pci_cleanup;
2560 info->addr_source_data = pdev;
2562 if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
2563 info->io_setup = port_setup;
2564 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2566 info->io_setup = mem_setup;
2567 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2569 info->io.addr_data = pci_resource_start(pdev, 0);
2571 info->io.regspacing = ipmi_pci_probe_regspacing(info);
2572 info->io.regsize = DEFAULT_REGSIZE;
2573 info->io.regshift = 0;
2575 info->irq = pdev->irq;
2577 info->irq_setup = std_irq_setup;
2579 info->dev = &pdev->dev;
2580 pci_set_drvdata(pdev, info);
2582 dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
2583 &pdev->resource[0], info->io.regsize, info->io.regspacing,
2589 pci_disable_device(pdev);
2595 static void ipmi_pci_remove(struct pci_dev *pdev)
2597 struct smi_info *info = pci_get_drvdata(pdev);
2598 cleanup_one_si(info);
2599 pci_disable_device(pdev);
2602 static struct pci_device_id ipmi_pci_devices[] = {
2603 { PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },
2604 { PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE_MASK) },
2607 MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
2609 static struct pci_driver ipmi_pci_driver = {
2610 .name = DEVICE_NAME,
2611 .id_table = ipmi_pci_devices,
2612 .probe = ipmi_pci_probe,
2613 .remove = ipmi_pci_remove,
2615 #endif /* CONFIG_PCI */
2617 static struct of_device_id ipmi_match[];
2618 static int ipmi_probe(struct platform_device *dev)
2621 const struct of_device_id *match;
2622 struct smi_info *info;
2623 struct resource resource;
2624 const __be32 *regsize, *regspacing, *regshift;
2625 struct device_node *np = dev->dev.of_node;
2629 dev_info(&dev->dev, "probing via device tree\n");
2631 match = of_match_device(ipmi_match, &dev->dev);
2635 ret = of_address_to_resource(np, 0, &resource);
2637 dev_warn(&dev->dev, PFX "invalid address from OF\n");
2641 regsize = of_get_property(np, "reg-size", &proplen);
2642 if (regsize && proplen != 4) {
2643 dev_warn(&dev->dev, PFX "invalid regsize from OF\n");
2647 regspacing = of_get_property(np, "reg-spacing", &proplen);
2648 if (regspacing && proplen != 4) {
2649 dev_warn(&dev->dev, PFX "invalid regspacing from OF\n");
2653 regshift = of_get_property(np, "reg-shift", &proplen);
2654 if (regshift && proplen != 4) {
2655 dev_warn(&dev->dev, PFX "invalid regshift from OF\n");
2659 info = smi_info_alloc();
2663 "could not allocate memory for OF probe\n");
2667 info->si_type = (enum si_type) match->data;
2668 info->addr_source = SI_DEVICETREE;
2669 info->irq_setup = std_irq_setup;
2671 if (resource.flags & IORESOURCE_IO) {
2672 info->io_setup = port_setup;
2673 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2675 info->io_setup = mem_setup;
2676 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2679 info->io.addr_data = resource.start;
2681 info->io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
2682 info->io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
2683 info->io.regshift = regshift ? be32_to_cpup(regshift) : 0;
2685 info->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
2686 info->dev = &dev->dev;
2688 dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
2689 info->io.addr_data, info->io.regsize, info->io.regspacing,
2692 dev_set_drvdata(&dev->dev, info);
2694 ret = add_smi(info);
2703 static int ipmi_remove(struct platform_device *dev)
2706 cleanup_one_si(dev_get_drvdata(&dev->dev));
2711 static struct of_device_id ipmi_match[] =
2713 { .type = "ipmi", .compatible = "ipmi-kcs",
2714 .data = (void *)(unsigned long) SI_KCS },
2715 { .type = "ipmi", .compatible = "ipmi-smic",
2716 .data = (void *)(unsigned long) SI_SMIC },
2717 { .type = "ipmi", .compatible = "ipmi-bt",
2718 .data = (void *)(unsigned long) SI_BT },
2722 static struct platform_driver ipmi_driver = {
2724 .name = DEVICE_NAME,
2725 .owner = THIS_MODULE,
2726 .of_match_table = ipmi_match,
2728 .probe = ipmi_probe,
2729 .remove = ipmi_remove,
2732 #ifdef CONFIG_PARISC
2733 static int ipmi_parisc_probe(struct parisc_device *dev)
2735 struct smi_info *info;
2738 info = smi_info_alloc();
2742 "could not allocate memory for PARISC probe\n");
2746 info->si_type = SI_KCS;
2747 info->addr_source = SI_DEVICETREE;
2748 info->io_setup = mem_setup;
2749 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2750 info->io.addr_data = dev->hpa.start;
2751 info->io.regsize = 1;
2752 info->io.regspacing = 1;
2753 info->io.regshift = 0;
2754 info->irq = 0; /* no interrupt */
2755 info->irq_setup = NULL;
2756 info->dev = &dev->dev;
2758 dev_dbg(&dev->dev, "addr 0x%lx\n", info->io.addr_data);
2760 dev_set_drvdata(&dev->dev, info);
2771 static int ipmi_parisc_remove(struct parisc_device *dev)
2773 cleanup_one_si(dev_get_drvdata(&dev->dev));
2777 static struct parisc_device_id ipmi_parisc_tbl[] = {
2778 { HPHW_MC, HVERSION_REV_ANY_ID, 0x004, 0xC0 },
2782 static struct parisc_driver ipmi_parisc_driver = {
2784 .id_table = ipmi_parisc_tbl,
2785 .probe = ipmi_parisc_probe,
2786 .remove = ipmi_parisc_remove,
2788 #endif /* CONFIG_PARISC */
2790 static int wait_for_msg_done(struct smi_info *smi_info)
2792 enum si_sm_result smi_result;
2794 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
2796 if (smi_result == SI_SM_CALL_WITH_DELAY ||
2797 smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
2798 schedule_timeout_uninterruptible(1);
2799 smi_result = smi_info->handlers->event(
2800 smi_info->si_sm, jiffies_to_usecs(1));
2801 } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
2802 smi_result = smi_info->handlers->event(
2803 smi_info->si_sm, 0);
2807 if (smi_result == SI_SM_HOSED)
2809 * We couldn't get the state machine to run, so whatever's at
2810 * the port is probably not an IPMI SMI interface.
2817 static int try_get_dev_id(struct smi_info *smi_info)
2819 unsigned char msg[2];
2820 unsigned char *resp;
2821 unsigned long resp_len;
2824 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
2829 * Do a Get Device ID command, since it comes back with some
2832 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2833 msg[1] = IPMI_GET_DEVICE_ID_CMD;
2834 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
2836 rv = wait_for_msg_done(smi_info);
2840 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2841 resp, IPMI_MAX_MSG_LENGTH);
2843 /* Check and record info from the get device id, in case we need it. */
2844 rv = ipmi_demangle_device_id(resp, resp_len, &smi_info->device_id);
2851 static int try_enable_event_buffer(struct smi_info *smi_info)
2853 unsigned char msg[3];
2854 unsigned char *resp;
2855 unsigned long resp_len;
2858 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
2862 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2863 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
2864 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
2866 rv = wait_for_msg_done(smi_info);
2868 printk(KERN_WARNING PFX "Error getting response from get"
2869 " global enables command, the event buffer is not"
2874 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2875 resp, IPMI_MAX_MSG_LENGTH);
2878 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2879 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
2881 printk(KERN_WARNING PFX "Invalid return from get global"
2882 " enables command, cannot enable the event buffer.\n");
2887 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF)
2888 /* buffer is already enabled, nothing to do. */
2891 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2892 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
2893 msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
2894 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
2896 rv = wait_for_msg_done(smi_info);
2898 printk(KERN_WARNING PFX "Error getting response from set"
2899 " global, enables command, the event buffer is not"
2904 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2905 resp, IPMI_MAX_MSG_LENGTH);
2908 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2909 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
2910 printk(KERN_WARNING PFX "Invalid return from get global,"
2911 "enables command, not enable the event buffer.\n");
2918 * An error when setting the event buffer bit means
2919 * that the event buffer is not supported.
2927 static int smi_type_proc_show(struct seq_file *m, void *v)
2929 struct smi_info *smi = m->private;
2931 return seq_printf(m, "%s\n", si_to_str[smi->si_type]);
2934 static int smi_type_proc_open(struct inode *inode, struct file *file)
2936 return single_open(file, smi_type_proc_show, PDE_DATA(inode));
2939 static const struct file_operations smi_type_proc_ops = {
2940 .open = smi_type_proc_open,
2942 .llseek = seq_lseek,
2943 .release = single_release,
2946 static int smi_si_stats_proc_show(struct seq_file *m, void *v)
2948 struct smi_info *smi = m->private;
2950 seq_printf(m, "interrupts_enabled: %d\n",
2951 smi->irq && !smi->interrupt_disabled);
2952 seq_printf(m, "short_timeouts: %u\n",
2953 smi_get_stat(smi, short_timeouts));
2954 seq_printf(m, "long_timeouts: %u\n",
2955 smi_get_stat(smi, long_timeouts));
2956 seq_printf(m, "idles: %u\n",
2957 smi_get_stat(smi, idles));
2958 seq_printf(m, "interrupts: %u\n",
2959 smi_get_stat(smi, interrupts));
2960 seq_printf(m, "attentions: %u\n",
2961 smi_get_stat(smi, attentions));
2962 seq_printf(m, "flag_fetches: %u\n",
2963 smi_get_stat(smi, flag_fetches));
2964 seq_printf(m, "hosed_count: %u\n",
2965 smi_get_stat(smi, hosed_count));
2966 seq_printf(m, "complete_transactions: %u\n",
2967 smi_get_stat(smi, complete_transactions));
2968 seq_printf(m, "events: %u\n",
2969 smi_get_stat(smi, events));
2970 seq_printf(m, "watchdog_pretimeouts: %u\n",
2971 smi_get_stat(smi, watchdog_pretimeouts));
2972 seq_printf(m, "incoming_messages: %u\n",
2973 smi_get_stat(smi, incoming_messages));
2977 static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
2979 return single_open(file, smi_si_stats_proc_show, PDE_DATA(inode));
2982 static const struct file_operations smi_si_stats_proc_ops = {
2983 .open = smi_si_stats_proc_open,
2985 .llseek = seq_lseek,
2986 .release = single_release,
2989 static int smi_params_proc_show(struct seq_file *m, void *v)
2991 struct smi_info *smi = m->private;
2993 return seq_printf(m,
2994 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
2995 si_to_str[smi->si_type],
2996 addr_space_to_str[smi->io.addr_type],
3005 static int smi_params_proc_open(struct inode *inode, struct file *file)
3007 return single_open(file, smi_params_proc_show, PDE_DATA(inode));
3010 static const struct file_operations smi_params_proc_ops = {
3011 .open = smi_params_proc_open,
3013 .llseek = seq_lseek,
3014 .release = single_release,
3018 * oem_data_avail_to_receive_msg_avail
3019 * @info - smi_info structure with msg_flags set
3021 * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
3022 * Returns 1 indicating need to re-run handle_flags().
3024 static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
3026 smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
3032 * setup_dell_poweredge_oem_data_handler
3033 * @info - smi_info.device_id must be populated
3035 * Systems that match, but have firmware version < 1.40 may assert
3036 * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
3037 * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
3038 * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
3039 * as RECEIVE_MSG_AVAIL instead.
3041 * As Dell has no plans to release IPMI 1.5 firmware that *ever*
3042 * assert the OEM[012] bits, and if it did, the driver would have to
3043 * change to handle that properly, we don't actually check for the
3045 * Device ID = 0x20 BMC on PowerEdge 8G servers
3046 * Device Revision = 0x80
3047 * Firmware Revision1 = 0x01 BMC version 1.40
3048 * Firmware Revision2 = 0x40 BCD encoded
3049 * IPMI Version = 0x51 IPMI 1.5
3050 * Manufacturer ID = A2 02 00 Dell IANA
3052 * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
3053 * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
3056 #define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
3057 #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
3058 #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
3059 #define DELL_IANA_MFR_ID 0x0002a2
3060 static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
3062 struct ipmi_device_id *id = &smi_info->device_id;
3063 if (id->manufacturer_id == DELL_IANA_MFR_ID) {
3064 if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
3065 id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
3066 id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
3067 smi_info->oem_data_avail_handler =
3068 oem_data_avail_to_receive_msg_avail;
3069 } else if (ipmi_version_major(id) < 1 ||
3070 (ipmi_version_major(id) == 1 &&
3071 ipmi_version_minor(id) < 5)) {
3072 smi_info->oem_data_avail_handler =
3073 oem_data_avail_to_receive_msg_avail;
3078 #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
3079 static void return_hosed_msg_badsize(struct smi_info *smi_info)
3081 struct ipmi_smi_msg *msg = smi_info->curr_msg;
3083 /* Make it a response */
3084 msg->rsp[0] = msg->data[0] | 4;
3085 msg->rsp[1] = msg->data[1];
3086 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
3088 smi_info->curr_msg = NULL;
3089 deliver_recv_msg(smi_info, msg);
3093 * dell_poweredge_bt_xaction_handler
3094 * @info - smi_info.device_id must be populated
3096 * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
3097 * not respond to a Get SDR command if the length of the data
3098 * requested is exactly 0x3A, which leads to command timeouts and no
3099 * data returned. This intercepts such commands, and causes userspace
3100 * callers to try again with a different-sized buffer, which succeeds.
3103 #define STORAGE_NETFN 0x0A
3104 #define STORAGE_CMD_GET_SDR 0x23
3105 static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
3106 unsigned long unused,
3109 struct smi_info *smi_info = in;
3110 unsigned char *data = smi_info->curr_msg->data;
3111 unsigned int size = smi_info->curr_msg->data_size;
3113 (data[0]>>2) == STORAGE_NETFN &&
3114 data[1] == STORAGE_CMD_GET_SDR &&
3116 return_hosed_msg_badsize(smi_info);
3122 static struct notifier_block dell_poweredge_bt_xaction_notifier = {
3123 .notifier_call = dell_poweredge_bt_xaction_handler,
3127 * setup_dell_poweredge_bt_xaction_handler
3128 * @info - smi_info.device_id must be filled in already
3130 * Fills in smi_info.device_id.start_transaction_pre_hook
3131 * when we know what function to use there.
3134 setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
3136 struct ipmi_device_id *id = &smi_info->device_id;
3137 if (id->manufacturer_id == DELL_IANA_MFR_ID &&
3138 smi_info->si_type == SI_BT)
3139 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
3143 * setup_oem_data_handler
3144 * @info - smi_info.device_id must be filled in already
3146 * Fills in smi_info.device_id.oem_data_available_handler
3147 * when we know what function to use there.
3150 static void setup_oem_data_handler(struct smi_info *smi_info)
3152 setup_dell_poweredge_oem_data_handler(smi_info);
3155 static void setup_xaction_handlers(struct smi_info *smi_info)
3157 setup_dell_poweredge_bt_xaction_handler(smi_info);
3160 static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
3162 if (smi_info->intf) {
3164 * The timer and thread are only running if the
3165 * interface has been started up and registered.
3167 if (smi_info->thread != NULL)
3168 kthread_stop(smi_info->thread);
3169 del_timer_sync(&smi_info->si_timer);
3173 static struct ipmi_default_vals
3179 { .type = SI_KCS, .port = 0xca2 },
3180 { .type = SI_SMIC, .port = 0xca9 },
3181 { .type = SI_BT, .port = 0xe4 },
3185 static void default_find_bmc(void)
3187 struct smi_info *info;
3190 for (i = 0; ; i++) {
3191 if (!ipmi_defaults[i].port)
3194 if (check_legacy_ioport(ipmi_defaults[i].port))
3197 info = smi_info_alloc();
3201 info->addr_source = SI_DEFAULT;
3203 info->si_type = ipmi_defaults[i].type;
3204 info->io_setup = port_setup;
3205 info->io.addr_data = ipmi_defaults[i].port;
3206 info->io.addr_type = IPMI_IO_ADDR_SPACE;
3208 info->io.addr = NULL;
3209 info->io.regspacing = DEFAULT_REGSPACING;
3210 info->io.regsize = DEFAULT_REGSPACING;
3211 info->io.regshift = 0;
3213 if (add_smi(info) == 0) {
3214 if ((try_smi_init(info)) == 0) {
3216 printk(KERN_INFO PFX "Found default %s"
3217 " state machine at %s address 0x%lx\n",
3218 si_to_str[info->si_type],
3219 addr_space_to_str[info->io.addr_type],
3220 info->io.addr_data);
3222 cleanup_one_si(info);
3229 static int is_new_interface(struct smi_info *info)
3233 list_for_each_entry(e, &smi_infos, link) {
3234 if (e->io.addr_type != info->io.addr_type)
3236 if (e->io.addr_data == info->io.addr_data)
3243 static int add_smi(struct smi_info *new_smi)
3247 printk(KERN_INFO PFX "Adding %s-specified %s state machine",
3248 ipmi_addr_src_to_str[new_smi->addr_source],
3249 si_to_str[new_smi->si_type]);
3250 mutex_lock(&smi_infos_lock);
3251 if (!is_new_interface(new_smi)) {
3252 printk(KERN_CONT " duplicate interface\n");
3257 printk(KERN_CONT "\n");
3259 /* So we know not to free it unless we have allocated one. */
3260 new_smi->intf = NULL;
3261 new_smi->si_sm = NULL;
3262 new_smi->handlers = NULL;
3264 list_add_tail(&new_smi->link, &smi_infos);
3267 mutex_unlock(&smi_infos_lock);
3271 static int try_smi_init(struct smi_info *new_smi)
3276 printk(KERN_INFO PFX "Trying %s-specified %s state"
3277 " machine at %s address 0x%lx, slave address 0x%x,"
3279 ipmi_addr_src_to_str[new_smi->addr_source],
3280 si_to_str[new_smi->si_type],
3281 addr_space_to_str[new_smi->io.addr_type],
3282 new_smi->io.addr_data,
3283 new_smi->slave_addr, new_smi->irq);
3285 switch (new_smi->si_type) {
3287 new_smi->handlers = &kcs_smi_handlers;
3291 new_smi->handlers = &smic_smi_handlers;
3295 new_smi->handlers = &bt_smi_handlers;
3299 /* No support for anything else yet. */
3304 /* Allocate the state machine's data and initialize it. */
3305 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
3306 if (!new_smi->si_sm) {
3308 "Could not allocate state machine memory\n");
3312 new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
3315 /* Now that we know the I/O size, we can set up the I/O. */
3316 rv = new_smi->io_setup(new_smi);
3318 printk(KERN_ERR PFX "Could not set up I/O space\n");
3322 /* Do low-level detection first. */
3323 if (new_smi->handlers->detect(new_smi->si_sm)) {
3324 if (new_smi->addr_source)
3325 printk(KERN_INFO PFX "Interface detection failed\n");
3331 * Attempt a get device id command. If it fails, we probably
3332 * don't have a BMC here.
3334 rv = try_get_dev_id(new_smi);
3336 if (new_smi->addr_source)
3337 printk(KERN_INFO PFX "There appears to be no BMC"
3338 " at this location\n");
3342 setup_oem_data_handler(new_smi);
3343 setup_xaction_handlers(new_smi);
3345 INIT_LIST_HEAD(&(new_smi->xmit_msgs));
3346 INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
3347 new_smi->curr_msg = NULL;
3348 atomic_set(&new_smi->req_events, 0);
3349 new_smi->run_to_completion = 0;
3350 for (i = 0; i < SI_NUM_STATS; i++)
3351 atomic_set(&new_smi->stats[i], 0);
3353 new_smi->interrupt_disabled = 1;
3354 atomic_set(&new_smi->stop_operation, 0);
3355 new_smi->intf_num = smi_num;
3358 rv = try_enable_event_buffer(new_smi);
3360 new_smi->has_event_buffer = 1;
3363 * Start clearing the flags before we enable interrupts or the
3364 * timer to avoid racing with the timer.
3366 start_clear_flags(new_smi);
3367 /* IRQ is defined to be set when non-zero. */
3369 new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
3371 if (!new_smi->dev) {
3373 * If we don't already have a device from something
3374 * else (like PCI), then register a new one.
3376 new_smi->pdev = platform_device_alloc("ipmi_si",
3378 if (!new_smi->pdev) {
3380 "Unable to allocate platform device\n");
3383 new_smi->dev = &new_smi->pdev->dev;
3384 new_smi->dev->driver = &ipmi_driver.driver;
3386 rv = platform_device_add(new_smi->pdev);
3389 "Unable to register system interface device:"
3394 new_smi->dev_registered = 1;
3397 rv = ipmi_register_smi(&handlers,
3399 &new_smi->device_id,
3402 new_smi->slave_addr);
3404 dev_err(new_smi->dev, "Unable to register device: error %d\n",
3406 goto out_err_stop_timer;
3409 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
3413 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
3414 goto out_err_stop_timer;
3417 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
3418 &smi_si_stats_proc_ops,
3421 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
3422 goto out_err_stop_timer;
3425 rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
3426 &smi_params_proc_ops,
3429 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
3430 goto out_err_stop_timer;
3433 dev_info(new_smi->dev, "IPMI %s interface initialized\n",
3434 si_to_str[new_smi->si_type]);
3439 atomic_inc(&new_smi->stop_operation);
3440 wait_for_timer_and_thread(new_smi);
3443 new_smi->interrupt_disabled = 1;
3445 if (new_smi->intf) {
3446 ipmi_unregister_smi(new_smi->intf);
3447 new_smi->intf = NULL;
3450 if (new_smi->irq_cleanup) {
3451 new_smi->irq_cleanup(new_smi);
3452 new_smi->irq_cleanup = NULL;
3456 * Wait until we know that we are out of any interrupt
3457 * handlers might have been running before we freed the
3460 synchronize_sched();
3462 if (new_smi->si_sm) {
3463 if (new_smi->handlers)
3464 new_smi->handlers->cleanup(new_smi->si_sm);
3465 kfree(new_smi->si_sm);
3466 new_smi->si_sm = NULL;
3468 if (new_smi->addr_source_cleanup) {
3469 new_smi->addr_source_cleanup(new_smi);
3470 new_smi->addr_source_cleanup = NULL;
3472 if (new_smi->io_cleanup) {
3473 new_smi->io_cleanup(new_smi);
3474 new_smi->io_cleanup = NULL;
3477 if (new_smi->dev_registered) {
3478 platform_device_unregister(new_smi->pdev);
3479 new_smi->dev_registered = 0;
3485 static int init_ipmi_si(void)
3491 enum ipmi_addr_src type = SI_INVALID;
3497 if (si_tryplatform) {
3498 rv = platform_driver_register(&ipmi_driver);
3500 printk(KERN_ERR PFX "Unable to register "
3501 "driver: %d\n", rv);
3506 /* Parse out the si_type string into its components. */
3509 for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
3511 str = strchr(str, ',');
3521 printk(KERN_INFO "IPMI System Interface driver.\n");
3523 /* If the user gave us a device, they presumably want us to use it */
3524 if (!hardcode_find_bmc())
3529 rv = pci_register_driver(&ipmi_pci_driver);
3531 printk(KERN_ERR PFX "Unable to register "
3532 "PCI driver: %d\n", rv);
3540 pnp_register_driver(&ipmi_pnp_driver);
3555 #ifdef CONFIG_PARISC
3556 register_parisc_driver(&ipmi_parisc_driver);
3557 parisc_registered = 1;
3558 /* poking PC IO addresses will crash machine, don't do it */
3562 /* We prefer devices with interrupts, but in the case of a machine
3563 with multiple BMCs we assume that there will be several instances
3564 of a given type so if we succeed in registering a type then also
3565 try to register everything else of the same type */
3567 mutex_lock(&smi_infos_lock);
3568 list_for_each_entry(e, &smi_infos, link) {
3569 /* Try to register a device if it has an IRQ and we either
3570 haven't successfully registered a device yet or this
3571 device has the same type as one we successfully registered */
3572 if (e->irq && (!type || e->addr_source == type)) {
3573 if (!try_smi_init(e)) {
3574 type = e->addr_source;
3579 /* type will only have been set if we successfully registered an si */
3581 mutex_unlock(&smi_infos_lock);
3585 /* Fall back to the preferred device */
3587 list_for_each_entry(e, &smi_infos, link) {
3588 if (!e->irq && (!type || e->addr_source == type)) {
3589 if (!try_smi_init(e)) {
3590 type = e->addr_source;
3594 mutex_unlock(&smi_infos_lock);
3599 if (si_trydefaults) {
3600 mutex_lock(&smi_infos_lock);
3601 if (list_empty(&smi_infos)) {
3602 /* No BMC was found, try defaults. */
3603 mutex_unlock(&smi_infos_lock);
3606 mutex_unlock(&smi_infos_lock);
3609 mutex_lock(&smi_infos_lock);
3610 if (unload_when_empty && list_empty(&smi_infos)) {
3611 mutex_unlock(&smi_infos_lock);
3613 printk(KERN_WARNING PFX
3614 "Unable to find any System Interface(s)\n");
3617 mutex_unlock(&smi_infos_lock);
3621 module_init(init_ipmi_si);
3623 static void cleanup_one_si(struct smi_info *to_clean)
3626 unsigned long flags;
3631 list_del(&to_clean->link);
3633 /* Tell the driver that we are shutting down. */
3634 atomic_inc(&to_clean->stop_operation);
3637 * Make sure the timer and thread are stopped and will not run
3640 wait_for_timer_and_thread(to_clean);
3643 * Timeouts are stopped, now make sure the interrupts are off
3644 * for the device. A little tricky with locks to make sure
3645 * there are no races.
3647 spin_lock_irqsave(&to_clean->si_lock, flags);
3648 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3649 spin_unlock_irqrestore(&to_clean->si_lock, flags);
3651 schedule_timeout_uninterruptible(1);
3652 spin_lock_irqsave(&to_clean->si_lock, flags);
3654 disable_si_irq(to_clean);
3655 spin_unlock_irqrestore(&to_clean->si_lock, flags);
3656 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3658 schedule_timeout_uninterruptible(1);
3661 /* Clean up interrupts and make sure that everything is done. */
3662 if (to_clean->irq_cleanup)
3663 to_clean->irq_cleanup(to_clean);
3664 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3666 schedule_timeout_uninterruptible(1);
3670 rv = ipmi_unregister_smi(to_clean->intf);
3673 printk(KERN_ERR PFX "Unable to unregister device: errno=%d\n",
3677 if (to_clean->handlers)
3678 to_clean->handlers->cleanup(to_clean->si_sm);
3680 kfree(to_clean->si_sm);
3682 if (to_clean->addr_source_cleanup)
3683 to_clean->addr_source_cleanup(to_clean);
3684 if (to_clean->io_cleanup)
3685 to_clean->io_cleanup(to_clean);
3687 if (to_clean->dev_registered)
3688 platform_device_unregister(to_clean->pdev);
3693 static void cleanup_ipmi_si(void)
3695 struct smi_info *e, *tmp_e;
3702 pci_unregister_driver(&ipmi_pci_driver);
3706 pnp_unregister_driver(&ipmi_pnp_driver);
3708 #ifdef CONFIG_PARISC
3709 if (parisc_registered)
3710 unregister_parisc_driver(&ipmi_parisc_driver);
3713 platform_driver_unregister(&ipmi_driver);
3715 mutex_lock(&smi_infos_lock);
3716 list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
3718 mutex_unlock(&smi_infos_lock);
3720 module_exit(cleanup_ipmi_si);
3722 MODULE_LICENSE("GPL");
3723 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
3724 MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
3725 " system interfaces.");