ipmi: make sure drivers were registered before unregistering them
[sfrench/cifs-2.6.git] / drivers / char / ipmi / ipmi_si_intf.c
index 4462b113ba3f1e134a1eb77b650af5e2732eb9af..311f85b67cc1b66b9caa6850d92fa1fd62c65185 100644 (file)
@@ -107,6 +107,14 @@ enum si_type {
 };
 static char *si_to_str[] = { "kcs", "smic", "bt" };
 
+enum ipmi_addr_src {
+       SI_INVALID = 0, SI_HOTMOD, SI_HARDCODED, SI_SPMI, SI_ACPI, SI_SMBIOS,
+       SI_PCI, SI_DEVICETREE, SI_DEFAULT
+};
+static char *ipmi_addr_src_to_str[] = { NULL, "hotmod", "hardcoded", "SPMI",
+                                       "ACPI", "SMBIOS", "PCI",
+                                       "device-tree", "default" };
+
 #define DEVICE_NAME "ipmi_si"
 
 static struct platform_driver ipmi_driver = {
@@ -188,7 +196,7 @@ struct smi_info {
        int (*irq_setup)(struct smi_info *info);
        void (*irq_cleanup)(struct smi_info *info);
        unsigned int io_size;
-       char *addr_source; /* ACPI, PCI, SMBIOS, hardcode, default. */
+       enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
        void (*addr_source_cleanup)(struct smi_info *info);
        void *addr_source_data;
 
@@ -294,12 +302,19 @@ struct smi_info {
 
 static int force_kipmid[SI_MAX_PARMS];
 static int num_force_kipmid;
+#ifdef CONFIG_PCI
+static int pci_registered;
+#endif
+#ifdef CONFIG_PPC_OF
+static int of_registered;
+#endif
 
 static unsigned int kipmid_max_busy_us[SI_MAX_PARMS];
 static int num_max_busy_us;
 
 static int unload_when_empty = 1;
 
+static int add_smi(struct smi_info *smi);
 static int try_smi_init(struct smi_info *smi);
 static void cleanup_one_si(struct smi_info *to_clean);
 
@@ -314,9 +329,14 @@ static void deliver_recv_msg(struct smi_info *smi_info,
 {
        /* Deliver the message to the upper layer with the lock
           released. */
-       spin_unlock(&(smi_info->si_lock));
-       ipmi_smi_msg_received(smi_info->intf, msg);
-       spin_lock(&(smi_info->si_lock));
+
+       if (smi_info->run_to_completion) {
+               ipmi_smi_msg_received(smi_info->intf, msg);
+       } else {
+               spin_unlock(&(smi_info->si_lock));
+               ipmi_smi_msg_received(smi_info->intf, msg);
+               spin_lock(&(smi_info->si_lock));
+       }
 }
 
 static void return_hosed_msg(struct smi_info *smi_info, int cCode)
@@ -445,6 +465,9 @@ static inline void disable_si_irq(struct smi_info *smi_info)
        if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
                start_disable_irq(smi_info);
                smi_info->interrupt_disabled = 1;
+               if (!atomic_read(&smi_info->stop_operation))
+                       mod_timer(&smi_info->si_timer,
+                                 jiffies + SI_TIMEOUT_JIFFIES);
        }
 }
 
@@ -576,9 +599,8 @@ static void handle_transaction_done(struct smi_info *smi_info)
                smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
                if (msg[2] != 0) {
                        /* Error clearing flags */
-                       printk(KERN_WARNING
-                              "ipmi_si: Error clearing flags: %2.2x\n",
-                              msg[2]);
+                       dev_warn(smi_info->dev,
+                                "Error clearing flags: %2.2x\n", msg[2]);
                }
                if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
                        start_enable_irq(smi_info);
@@ -670,9 +692,8 @@ static void handle_transaction_done(struct smi_info *smi_info)
                /* We got the flags from the SMI, now handle them. */
                smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
                if (msg[2] != 0) {
-                       printk(KERN_WARNING
-                              "ipmi_si: Could not enable interrupts"
-                              ", failed get, using polled mode.\n");
+                       dev_warn(smi_info->dev, "Could not enable interrupts"
+                                ", failed get, using polled mode.\n");
                        smi_info->si_state = SI_NORMAL;
                } else {
                        msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
@@ -693,11 +714,11 @@ static void handle_transaction_done(struct smi_info *smi_info)
 
                /* We got the flags from the SMI, now handle them. */
                smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
-               if (msg[2] != 0) {
-                       printk(KERN_WARNING
-                              "ipmi_si: Could not enable interrupts"
-                              ", failed set, using polled mode.\n");
-               }
+               if (msg[2] != 0)
+                       dev_warn(smi_info->dev, "Could not enable interrupts"
+                                ", failed set, using polled mode.\n");
+               else
+                       smi_info->interrupt_disabled = 0;
                smi_info->si_state = SI_NORMAL;
                break;
        }
@@ -709,9 +730,8 @@ static void handle_transaction_done(struct smi_info *smi_info)
                /* We got the flags from the SMI, now handle them. */
                smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
                if (msg[2] != 0) {
-                       printk(KERN_WARNING
-                              "ipmi_si: Could not disable interrupts"
-                              ", failed get.\n");
+                       dev_warn(smi_info->dev, "Could not disable interrupts"
+                                ", failed get.\n");
                        smi_info->si_state = SI_NORMAL;
                } else {
                        msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
@@ -733,9 +753,8 @@ static void handle_transaction_done(struct smi_info *smi_info)
                /* We got the flags from the SMI, now handle them. */
                smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
                if (msg[2] != 0) {
-                       printk(KERN_WARNING
-                              "ipmi_si: Could not disable interrupts"
-                              ", failed set.\n");
+                       dev_warn(smi_info->dev, "Could not disable interrupts"
+                                ", failed set.\n");
                }
                smi_info->si_state = SI_NORMAL;
                break;
@@ -877,6 +896,11 @@ static void sender(void                *send_info,
        printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
 #endif
 
+       mod_timer(&smi_info->si_timer, jiffies + SI_TIMEOUT_JIFFIES);
+
+       if (smi_info->thread)
+               wake_up_process(smi_info->thread);
+
        if (smi_info->run_to_completion) {
                /*
                 * If we are running to completion, then throw it in
@@ -997,6 +1021,8 @@ static int ipmi_thread(void *data)
                        ; /* do nothing */
                else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
                        schedule();
+               else if (smi_result == SI_SM_IDLE)
+                       schedule_timeout_interruptible(100);
                else
                        schedule_timeout_interruptible(0);
        }
@@ -1039,6 +1065,7 @@ static void smi_timeout(unsigned long data)
        unsigned long     flags;
        unsigned long     jiffies_now;
        long              time_diff;
+       long              timeout;
 #ifdef DEBUG_TIMING
        struct timeval    t;
 #endif
@@ -1059,9 +1086,9 @@ static void smi_timeout(unsigned long data)
 
        if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
                /* Running with interrupts, only do long timeouts. */
-               smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
+               timeout = jiffies + SI_TIMEOUT_JIFFIES;
                smi_inc_stat(smi_info, long_timeouts);
-               goto do_add_timer;
+               goto do_mod_timer;
        }
 
        /*
@@ -1070,14 +1097,15 @@ static void smi_timeout(unsigned long data)
         */
        if (smi_result == SI_SM_CALL_WITH_DELAY) {
                smi_inc_stat(smi_info, short_timeouts);
-               smi_info->si_timer.expires = jiffies + 1;
+               timeout = jiffies + 1;
        } else {
                smi_inc_stat(smi_info, long_timeouts);
-               smi_info->si_timer.expires = jiffies + SI_TIMEOUT_JIFFIES;
+               timeout = jiffies + SI_TIMEOUT_JIFFIES;
        }
 
- do_add_timer:
-       add_timer(&(smi_info->si_timer));
+ do_mod_timer:
+       if (smi_result != SI_SM_IDLE)
+               mod_timer(&(smi_info->si_timer), timeout);
 }
 
 static irqreturn_t si_irq_handler(int irq, void *data)
@@ -1144,10 +1172,10 @@ static int smi_start_processing(void       *send_info,
                new_smi->thread = kthread_run(ipmi_thread, new_smi,
                                              "kipmi%d", new_smi->intf_num);
                if (IS_ERR(new_smi->thread)) {
-                       printk(KERN_NOTICE "ipmi_si_intf: Could not start"
-                              " kernel thread due to error %ld, only using"
-                              " timers to drive the interface\n",
-                              PTR_ERR(new_smi->thread));
+                       dev_notice(new_smi->dev, "Could not start"
+                                  " kernel thread due to error %ld, only using"
+                                  " timers to drive the interface\n",
+                                  PTR_ERR(new_smi->thread));
                        new_smi->thread = NULL;
                }
        }
@@ -1308,14 +1336,13 @@ static int std_irq_setup(struct smi_info *info)
                                 DEVICE_NAME,
                                 info);
        if (rv) {
-               printk(KERN_WARNING
-                      "ipmi_si: %s unable to claim interrupt %d,"
-                      " running polled\n",
-                      DEVICE_NAME, info->irq);
+               dev_warn(info->dev, "%s unable to claim interrupt %d,"
+                        " running polled\n",
+                        DEVICE_NAME, info->irq);
                info->irq = 0;
        } else {
                info->irq_cleanup = std_irq_cleanup;
-               printk("  Using irq %d\n", info->irq);
+               dev_info(info->dev, "Using irq %d\n", info->irq);
        }
 
        return rv;
@@ -1406,8 +1433,8 @@ static int port_setup(struct smi_info *info)
                info->io.outputb = port_outl;
                break;
        default:
-               printk(KERN_WARNING "ipmi_si: Invalid register size: %d\n",
-                      info->io.regsize);
+               dev_warn(info->dev, "Invalid register size: %d\n",
+                        info->io.regsize);
                return -EINVAL;
        }
 
@@ -1529,8 +1556,8 @@ static int mem_setup(struct smi_info *info)
                break;
 #endif
        default:
-               printk(KERN_WARNING "ipmi_si: Invalid register size: %d\n",
-                      info->io.regsize);
+               dev_warn(info->dev, "Invalid register size: %d\n",
+                        info->io.regsize);
                return -EINVAL;
        }
 
@@ -1755,7 +1782,7 @@ static int hotmod_handler(const char *val, struct kernel_param *kp)
                                goto out;
                        }
 
-                       info->addr_source = "hotmod";
+                       info->addr_source = SI_HOTMOD;
                        info->si_type = si_type;
                        info->io.addr_data = addr;
                        info->io.addr_type = addr_space;
@@ -1777,7 +1804,9 @@ static int hotmod_handler(const char *val, struct kernel_param *kp)
                                info->irq_setup = std_irq_setup;
                        info->slave_addr = ipmb;
 
-                       try_smi_init(info);
+                       if (!add_smi(info))
+                               if (try_smi_init(info))
+                                       cleanup_one_si(info);
                } else {
                        /* remove */
                        struct smi_info *e, *tmp_e;
@@ -1813,7 +1842,8 @@ static __devinit void hardcode_find_bmc(void)
                if (!info)
                        return;
 
-               info->addr_source = "hardcoded";
+               info->addr_source = SI_HARDCODED;
+               printk(KERN_INFO PFX "probing via hardcoded address\n");
 
                if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
                        info->si_type = SI_KCS;
@@ -1822,8 +1852,7 @@ static __devinit void hardcode_find_bmc(void)
                } else if (strcmp(si_type[i], "bt") == 0) {
                        info->si_type = SI_BT;
                } else {
-                       printk(KERN_WARNING
-                              "ipmi_si: Interface type specified "
+                       printk(KERN_WARNING PFX "Interface type specified "
                               "for interface %d, was invalid: %s\n",
                               i, si_type[i]);
                        kfree(info);
@@ -1841,11 +1870,9 @@ static __devinit void hardcode_find_bmc(void)
                        info->io.addr_data = addrs[i];
                        info->io.addr_type = IPMI_MEM_ADDR_SPACE;
                } else {
-                       printk(KERN_WARNING
-                              "ipmi_si: Interface type specified "
-                              "for interface %d, "
-                              "but port and address were not set or "
-                              "set to zero.\n", i);
+                       printk(KERN_WARNING PFX "Interface type specified "
+                              "for interface %d, but port and address were "
+                              "not set or set to zero.\n", i);
                        kfree(info);
                        continue;
                }
@@ -1863,7 +1890,9 @@ static __devinit void hardcode_find_bmc(void)
                        info->irq_setup = std_irq_setup;
                info->slave_addr = slave_addrs[i];
 
-               try_smi_init(info);
+               if (!add_smi(info))
+                       if (try_smi_init(info))
+                               cleanup_one_si(info);
        }
 }
 
@@ -1923,15 +1952,13 @@ static int acpi_gpe_irq_setup(struct smi_info *info)
                                          &ipmi_acpi_gpe,
                                          info);
        if (status != AE_OK) {
-               printk(KERN_WARNING
-                      "ipmi_si: %s unable to claim ACPI GPE %d,"
-                      " running polled\n",
-                      DEVICE_NAME, info->irq);
+               dev_warn(info->dev, "%s unable to claim ACPI GPE %d,"
+                        " running polled\n", DEVICE_NAME, info->irq);
                info->irq = 0;
                return -EINVAL;
        } else {
                info->irq_cleanup = acpi_gpe_irq_cleanup;
-               printk("  Using ACPI GPE %d\n", info->irq);
+               dev_info(info->dev, "Using ACPI GPE %d\n", info->irq);
                return 0;
        }
 }
@@ -1989,8 +2016,8 @@ static __devinit int try_init_spmi(struct SPMITable *spmi)
        u8               addr_space;
 
        if (spmi->IPMIlegacy != 1) {
-           printk(KERN_INFO "IPMI: Bad SPMI legacy %d\n", spmi->IPMIlegacy);
-           return -ENODEV;
+               printk(KERN_INFO PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);
+               return -ENODEV;
        }
 
        if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
@@ -2000,11 +2027,12 @@ static __devinit int try_init_spmi(struct SPMITable *spmi)
 
        info = kzalloc(sizeof(*info), GFP_KERNEL);
        if (!info) {
-               printk(KERN_ERR "ipmi_si: Could not allocate SI data (3)\n");
+               printk(KERN_ERR PFX "Could not allocate SI data (3)\n");
                return -ENOMEM;
        }
 
-       info->addr_source = "SPMI";
+       info->addr_source = SI_SPMI;
+       printk(KERN_INFO PFX "probing via SPMI\n");
 
        /* Figure out the interface type. */
        switch (spmi->InterfaceType) {
@@ -2018,8 +2046,8 @@ static __devinit int try_init_spmi(struct SPMITable *spmi)
                info->si_type = SI_BT;
                break;
        default:
-               printk(KERN_INFO "ipmi_si: Unknown ACPI/SPMI SI type %d\n",
-                       spmi->InterfaceType);
+               printk(KERN_INFO PFX "Unknown ACPI/SPMI SI type %d\n",
+                      spmi->InterfaceType);
                kfree(info);
                return -EIO;
        }
@@ -2055,13 +2083,12 @@ static __devinit int try_init_spmi(struct SPMITable *spmi)
                info->io.addr_type = IPMI_IO_ADDR_SPACE;
        } else {
                kfree(info);
-               printk(KERN_WARNING
-                      "ipmi_si: Unknown ACPI I/O Address type\n");
+               printk(KERN_WARNING PFX "Unknown ACPI I/O Address type\n");
                return -EIO;
        }
        info->io.addr_data = spmi->addr.address;
 
-       try_smi_init(info);
+       add_smi(info);
 
        return 0;
 }
@@ -2093,6 +2120,7 @@ static int __devinit ipmi_pnp_probe(struct pnp_dev *dev,
 {
        struct acpi_device *acpi_dev;
        struct smi_info *info;
+       struct resource *res;
        acpi_handle handle;
        acpi_status status;
        unsigned long long tmp;
@@ -2105,7 +2133,8 @@ static int __devinit ipmi_pnp_probe(struct pnp_dev *dev,
        if (!info)
                return -ENOMEM;
 
-       info->addr_source = "ACPI";
+       info->addr_source = SI_ACPI;
+       printk(KERN_INFO PFX "probing via ACPI\n");
 
        handle = acpi_dev->handle;
 
@@ -2125,22 +2154,26 @@ static int __devinit ipmi_pnp_probe(struct pnp_dev *dev,
                info->si_type = SI_BT;
                break;
        default:
-               dev_info(&dev->dev, "unknown interface type %lld\n", tmp);
+               dev_info(&dev->dev, "unknown IPMI type %lld\n", tmp);
                goto err_free;
        }
 
-       if (pnp_port_valid(dev, 0)) {
+       res = pnp_get_resource(dev, IORESOURCE_IO, 0);
+       if (res) {
                info->io_setup = port_setup;
                info->io.addr_type = IPMI_IO_ADDR_SPACE;
-               info->io.addr_data = pnp_port_start(dev, 0);
-       } else if (pnp_mem_valid(dev, 0)) {
-               info->io_setup = mem_setup;
-               info->io.addr_type = IPMI_MEM_ADDR_SPACE;
-               info->io.addr_data = pnp_mem_start(dev, 0);
        } else {
+               res = pnp_get_resource(dev, IORESOURCE_MEM, 0);
+               if (res) {
+                       info->io_setup = mem_setup;
+                       info->io.addr_type = IPMI_MEM_ADDR_SPACE;
+               }
+       }
+       if (!res) {
                dev_err(&dev->dev, "no I/O or memory address\n");
                goto err_free;
        }
+       info->io.addr_data = res->start;
 
        info->io.regspacing = DEFAULT_REGSPACING;
        info->io.regsize = DEFAULT_REGSPACING;
@@ -2156,10 +2189,14 @@ static int __devinit ipmi_pnp_probe(struct pnp_dev *dev,
                info->irq_setup = std_irq_setup;
        }
 
-       info->dev = &acpi_dev->dev;
+       info->dev = &dev->dev;
        pnp_set_drvdata(dev, info);
 
-       return try_smi_init(info);
+       dev_info(info->dev, "%pR regsize %d spacing %d irq %d\n",
+                res, info->io.regsize, info->io.regspacing,
+                info->irq);
+
+       return add_smi(info);
 
 err_free:
        kfree(info);
@@ -2264,12 +2301,12 @@ static __devinit void try_init_dmi(struct dmi_ipmi_data *ipmi_data)
 
        info = kzalloc(sizeof(*info), GFP_KERNEL);
        if (!info) {
-               printk(KERN_ERR
-                      "ipmi_si: Could not allocate SI data\n");
+               printk(KERN_ERR PFX "Could not allocate SI data\n");
                return;
        }
 
-       info->addr_source = "SMBIOS";
+       info->addr_source = SI_SMBIOS;
+       printk(KERN_INFO PFX "probing via SMBIOS\n");
 
        switch (ipmi_data->type) {
        case 0x01: /* KCS */
@@ -2299,8 +2336,7 @@ static __devinit void try_init_dmi(struct dmi_ipmi_data *ipmi_data)
 
        default:
                kfree(info);
-               printk(KERN_WARNING
-                      "ipmi_si: Unknown SMBIOS I/O Address type: %d.\n",
+               printk(KERN_WARNING PFX "Unknown SMBIOS I/O Address type: %d\n",
                       ipmi_data->addr_space);
                return;
        }
@@ -2318,7 +2354,7 @@ static __devinit void try_init_dmi(struct dmi_ipmi_data *ipmi_data)
        if (info->irq)
                info->irq_setup = std_irq_setup;
 
-       try_smi_init(info);
+       add_smi(info);
 }
 
 static void __devinit dmi_find_bmc(void)
@@ -2368,7 +2404,8 @@ static int __devinit ipmi_pci_probe(struct pci_dev *pdev,
        if (!info)
                return -ENOMEM;
 
-       info->addr_source = "PCI";
+       info->addr_source = SI_PCI;
+       dev_info(&pdev->dev, "probing via PCI");
 
        switch (class_type) {
        case PCI_ERMC_CLASSCODE_TYPE_SMIC:
@@ -2385,15 +2422,13 @@ static int __devinit ipmi_pci_probe(struct pci_dev *pdev,
 
        default:
                kfree(info);
-               printk(KERN_INFO "ipmi_si: %s: Unknown IPMI type: %d\n",
-                      pci_name(pdev), class_type);
+               dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);
                return -ENOMEM;
        }
 
        rv = pci_enable_device(pdev);
        if (rv) {
-               printk(KERN_ERR "ipmi_si: %s: couldn't enable PCI device\n",
-                      pci_name(pdev));
+               dev_err(&pdev->dev, "couldn't enable PCI device\n");
                kfree(info);
                return rv;
        }
@@ -2421,7 +2456,11 @@ static int __devinit ipmi_pci_probe(struct pci_dev *pdev,
        info->dev = &pdev->dev;
        pci_set_drvdata(pdev, info);
 
-       return try_smi_init(info);
+       dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
+               &pdev->resource[0], info->io.regsize, info->io.regspacing,
+               info->irq);
+
+       return add_smi(info);
 }
 
 static void __devexit ipmi_pci_remove(struct pci_dev *pdev)
@@ -2469,11 +2508,11 @@ static int __devinit ipmi_of_probe(struct of_device *dev,
        struct smi_info *info;
        struct resource resource;
        const int *regsize, *regspacing, *regshift;
-       struct device_node *np = dev->node;
+       struct device_node *np = dev->dev.of_node;
        int ret;
        int proplen;
 
-       dev_info(&dev->dev, PFX "probing via device tree\n");
+       dev_info(&dev->dev, "probing via device tree\n");
 
        ret = of_address_to_resource(np, 0, &resource);
        if (ret) {
@@ -2503,12 +2542,12 @@ static int __devinit ipmi_of_probe(struct of_device *dev,
 
        if (!info) {
                dev_err(&dev->dev,
-                       PFX "could not allocate memory for OF probe\n");
+                       "could not allocate memory for OF probe\n");
                return -ENOMEM;
        }
 
        info->si_type           = (enum si_type) match->data;
-       info->addr_source       = "device-tree";
+       info->addr_source       = SI_DEVICETREE;
        info->irq_setup         = std_irq_setup;
 
        if (resource.flags & IORESOURCE_IO) {
@@ -2525,16 +2564,16 @@ static int __devinit ipmi_of_probe(struct of_device *dev,
        info->io.regspacing     = regspacing ? *regspacing : DEFAULT_REGSPACING;
        info->io.regshift       = regshift ? *regshift : 0;
 
-       info->irq               = irq_of_parse_and_map(dev->node, 0);
+       info->irq               = irq_of_parse_and_map(dev->dev.of_node, 0);
        info->dev               = &dev->dev;
 
-       dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %x\n",
+       dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
                info->io.addr_data, info->io.regsize, info->io.regspacing,
                info->irq);
 
        dev_set_drvdata(&dev->dev, info);
 
-       return try_smi_init(info);
+       return add_smi(info);
 }
 
 static int __devexit ipmi_of_remove(struct of_device *dev)
@@ -2555,8 +2594,11 @@ static struct of_device_id ipmi_match[] =
 };
 
 static struct of_platform_driver ipmi_of_platform_driver = {
-       .name           = "ipmi",
-       .match_table    = ipmi_match,
+       .driver = {
+               .name = "ipmi",
+               .owner = THIS_MODULE,
+               .of_match_table = ipmi_match,
+       },
        .probe          = ipmi_of_probe,
        .remove         = __devexit_p(ipmi_of_remove),
 };
@@ -2640,9 +2682,8 @@ static int try_enable_event_buffer(struct smi_info *smi_info)
 
        rv = wait_for_msg_done(smi_info);
        if (rv) {
-               printk(KERN_WARNING
-                      "ipmi_si: Error getting response from get global,"
-                      " enables command, the event buffer is not"
+               printk(KERN_WARNING PFX "Error getting response from get"
+                      " global enables command, the event buffer is not"
                       " enabled.\n");
                goto out;
        }
@@ -2654,10 +2695,8 @@ static int try_enable_event_buffer(struct smi_info *smi_info)
                        resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
                        resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD   ||
                        resp[2] != 0) {
-               printk(KERN_WARNING
-                      "ipmi_si: Invalid return from get global"
-                      " enables command, cannot enable the event"
-                      " buffer.\n");
+               printk(KERN_WARNING PFX "Invalid return from get global"
+                      " enables command, cannot enable the event buffer.\n");
                rv = -EINVAL;
                goto out;
        }
@@ -2673,9 +2712,8 @@ static int try_enable_event_buffer(struct smi_info *smi_info)
 
        rv = wait_for_msg_done(smi_info);
        if (rv) {
-               printk(KERN_WARNING
-                      "ipmi_si: Error getting response from set global,"
-                      " enables command, the event buffer is not"
+               printk(KERN_WARNING PFX "Error getting response from set"
+                      " global, enables command, the event buffer is not"
                       " enabled.\n");
                goto out;
        }
@@ -2686,10 +2724,8 @@ static int try_enable_event_buffer(struct smi_info *smi_info)
        if (resp_len < 3 ||
                        resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
                        resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
-               printk(KERN_WARNING
-                      "ipmi_si: Invalid return from get global,"
-                      "enables command, not enable the event"
-                      " buffer.\n");
+               printk(KERN_WARNING PFX "Invalid return from get global,"
+                      "enables command, not enable the event buffer.\n");
                rv = -EINVAL;
                goto out;
        }
@@ -2948,7 +2984,7 @@ static __devinit void default_find_bmc(void)
                if (!info)
                        return;
 
-               info->addr_source = NULL;
+               info->addr_source = SI_DEFAULT;
 
                info->si_type = ipmi_defaults[i].type;
                info->io_setup = port_setup;
@@ -2960,14 +2996,16 @@ static __devinit void default_find_bmc(void)
                info->io.regsize = DEFAULT_REGSPACING;
                info->io.regshift = 0;
 
-               if (try_smi_init(info) == 0) {
-                       /* Found one... */
-                       printk(KERN_INFO "ipmi_si: Found default %s state"
-                              " machine at %s address 0x%lx\n",
-                              si_to_str[info->si_type],
-                              addr_space_to_str[info->io.addr_type],
-                              info->io.addr_data);
-                       return;
+               if (add_smi(info) == 0) {
+                       if ((try_smi_init(info)) == 0) {
+                               /* Found one... */
+                               printk(KERN_INFO PFX "Found default %s"
+                               " state machine at %s address 0x%lx\n",
+                               si_to_str[info->si_type],
+                               addr_space_to_str[info->io.addr_type],
+                               info->io.addr_data);
+                       } else
+                               cleanup_one_si(info);
                }
        }
 }
@@ -2986,34 +3024,48 @@ static int is_new_interface(struct smi_info *info)
        return 1;
 }
 
-static int try_smi_init(struct smi_info *new_smi)
+static int add_smi(struct smi_info *new_smi)
 {
-       int rv;
-       int i;
-
-       if (new_smi->addr_source) {
-               printk(KERN_INFO "ipmi_si: Trying %s-specified %s state"
-                      " machine at %s address 0x%lx, slave address 0x%x,"
-                      " irq %d\n",
-                      new_smi->addr_source,
-                      si_to_str[new_smi->si_type],
-                      addr_space_to_str[new_smi->io.addr_type],
-                      new_smi->io.addr_data,
-                      new_smi->slave_addr, new_smi->irq);
-       }
+       int rv = 0;
 
+       printk(KERN_INFO PFX "Adding %s-specified %s state machine",
+                       ipmi_addr_src_to_str[new_smi->addr_source],
+                       si_to_str[new_smi->si_type]);
        mutex_lock(&smi_infos_lock);
        if (!is_new_interface(new_smi)) {
-               printk(KERN_WARNING "ipmi_si: duplicate interface\n");
+               printk(KERN_CONT PFX "duplicate interface\n");
                rv = -EBUSY;
                goto out_err;
        }
 
+       printk(KERN_CONT "\n");
+
        /* So we know not to free it unless we have allocated one. */
        new_smi->intf = NULL;
        new_smi->si_sm = NULL;
        new_smi->handlers = NULL;
 
+       list_add_tail(&new_smi->link, &smi_infos);
+
+out_err:
+       mutex_unlock(&smi_infos_lock);
+       return rv;
+}
+
+static int try_smi_init(struct smi_info *new_smi)
+{
+       int rv = 0;
+       int i;
+
+       printk(KERN_INFO PFX "Trying %s-specified %s state"
+              " machine at %s address 0x%lx, slave address 0x%x,"
+              " irq %d\n",
+              ipmi_addr_src_to_str[new_smi->addr_source],
+              si_to_str[new_smi->si_type],
+              addr_space_to_str[new_smi->io.addr_type],
+              new_smi->io.addr_data,
+              new_smi->slave_addr, new_smi->irq);
+
        switch (new_smi->si_type) {
        case SI_KCS:
                new_smi->handlers = &kcs_smi_handlers;
@@ -3036,7 +3088,8 @@ static int try_smi_init(struct smi_info *new_smi)
        /* Allocate the state machine's data and initialize it. */
        new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
        if (!new_smi->si_sm) {
-               printk(KERN_ERR "Could not allocate state machine memory\n");
+               printk(KERN_ERR PFX
+                      "Could not allocate state machine memory\n");
                rv = -ENOMEM;
                goto out_err;
        }
@@ -3046,7 +3099,7 @@ static int try_smi_init(struct smi_info *new_smi)
        /* Now that we know the I/O size, we can set up the I/O. */
        rv = new_smi->io_setup(new_smi);
        if (rv) {
-               printk(KERN_ERR "Could not set up I/O space\n");
+               printk(KERN_ERR PFX "Could not set up I/O space\n");
                goto out_err;
        }
 
@@ -3056,8 +3109,7 @@ static int try_smi_init(struct smi_info *new_smi)
        /* Do low-level detection first. */
        if (new_smi->handlers->detect(new_smi->si_sm)) {
                if (new_smi->addr_source)
-                       printk(KERN_INFO "ipmi_si: Interface detection"
-                              " failed\n");
+                       printk(KERN_INFO PFX "Interface detection failed\n");
                rv = -ENODEV;
                goto out_err;
        }
@@ -3069,7 +3121,7 @@ static int try_smi_init(struct smi_info *new_smi)
        rv = try_get_dev_id(new_smi);
        if (rv) {
                if (new_smi->addr_source)
-                       printk(KERN_INFO "ipmi_si: There appears to be no BMC"
+                       printk(KERN_INFO PFX "There appears to be no BMC"
                               " at this location\n");
                goto out_err;
        }
@@ -3085,7 +3137,7 @@ static int try_smi_init(struct smi_info *new_smi)
        for (i = 0; i < SI_NUM_STATS; i++)
                atomic_set(&new_smi->stats[i], 0);
 
-       new_smi->interrupt_disabled = 0;
+       new_smi->interrupt_disabled = 1;
        atomic_set(&new_smi->stop_operation, 0);
        new_smi->intf_num = smi_num;
        smi_num++;
@@ -3111,9 +3163,8 @@ static int try_smi_init(struct smi_info *new_smi)
                new_smi->pdev = platform_device_alloc("ipmi_si",
                                                      new_smi->intf_num);
                if (!new_smi->pdev) {
-                       printk(KERN_ERR
-                              "ipmi_si_intf:"
-                              " Unable to allocate platform device\n");
+                       printk(KERN_ERR PFX
+                              "Unable to allocate platform device\n");
                        goto out_err;
                }
                new_smi->dev = &new_smi->pdev->dev;
@@ -3121,9 +3172,8 @@ static int try_smi_init(struct smi_info *new_smi)
 
                rv = platform_device_add(new_smi->pdev);
                if (rv) {
-                       printk(KERN_ERR
-                              "ipmi_si_intf:"
-                              " Unable to register system interface device:"
+                       printk(KERN_ERR PFX
+                              "Unable to register system interface device:"
                               " %d\n",
                               rv);
                        goto out_err;
@@ -3138,9 +3188,8 @@ static int try_smi_init(struct smi_info *new_smi)
                               "bmc",
                               new_smi->slave_addr);
        if (rv) {
-               printk(KERN_ERR
-                      "ipmi_si: Unable to register device: error %d\n",
-                      rv);
+               dev_err(new_smi->dev, "Unable to register device: error %d\n",
+                       rv);
                goto out_err_stop_timer;
        }
 
@@ -3148,9 +3197,7 @@ static int try_smi_init(struct smi_info *new_smi)
                                     type_file_read_proc,
                                     new_smi);
        if (rv) {
-               printk(KERN_ERR
-                      "ipmi_si: Unable to create proc entry: %d\n",
-                      rv);
+               dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
                goto out_err_stop_timer;
        }
 
@@ -3158,9 +3205,7 @@ static int try_smi_init(struct smi_info *new_smi)
                                     stat_file_read_proc,
                                     new_smi);
        if (rv) {
-               printk(KERN_ERR
-                      "ipmi_si: Unable to create proc entry: %d\n",
-                      rv);
+               dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
                goto out_err_stop_timer;
        }
 
@@ -3168,18 +3213,12 @@ static int try_smi_init(struct smi_info *new_smi)
                                     param_read_proc,
                                     new_smi);
        if (rv) {
-               printk(KERN_ERR
-                      "ipmi_si: Unable to create proc entry: %d\n",
-                      rv);
+               dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
                goto out_err_stop_timer;
        }
 
-       list_add_tail(&new_smi->link, &smi_infos);
-
-       mutex_unlock(&smi_infos_lock);
-
-       printk(KERN_INFO "IPMI %s interface initialized\n",
-              si_to_str[new_smi->si_type]);
+       dev_info(new_smi->dev, "IPMI %s interface initialized\n",
+                si_to_str[new_smi->si_type]);
 
        return 0;
 
@@ -3188,11 +3227,17 @@ static int try_smi_init(struct smi_info *new_smi)
        wait_for_timer_and_thread(new_smi);
 
  out_err:
-       if (new_smi->intf)
+       new_smi->interrupt_disabled = 1;
+
+       if (new_smi->intf) {
                ipmi_unregister_smi(new_smi->intf);
+               new_smi->intf = NULL;
+       }
 
-       if (new_smi->irq_cleanup)
+       if (new_smi->irq_cleanup) {
                new_smi->irq_cleanup(new_smi);
+               new_smi->irq_cleanup = NULL;
+       }
 
        /*
         * Wait until we know that we are out of any interrupt
@@ -3205,18 +3250,21 @@ static int try_smi_init(struct smi_info *new_smi)
                if (new_smi->handlers)
                        new_smi->handlers->cleanup(new_smi->si_sm);
                kfree(new_smi->si_sm);
+               new_smi->si_sm = NULL;
        }
-       if (new_smi->addr_source_cleanup)
+       if (new_smi->addr_source_cleanup) {
                new_smi->addr_source_cleanup(new_smi);
-       if (new_smi->io_cleanup)
+               new_smi->addr_source_cleanup = NULL;
+       }
+       if (new_smi->io_cleanup) {
                new_smi->io_cleanup(new_smi);
+               new_smi->io_cleanup = NULL;
+       }
 
-       if (new_smi->dev_registered)
+       if (new_smi->dev_registered) {
                platform_device_unregister(new_smi->pdev);
-
-       kfree(new_smi);
-
-       mutex_unlock(&smi_infos_lock);
+               new_smi->dev_registered = 0;
+       }
 
        return rv;
 }
@@ -3226,6 +3274,8 @@ static __devinit int init_ipmi_si(void)
        int  i;
        char *str;
        int  rv;
+       struct smi_info *e;
+       enum ipmi_addr_src type = SI_INVALID;
 
        if (initialized)
                return 0;
@@ -3234,9 +3284,7 @@ static __devinit int init_ipmi_si(void)
        /* Register the device drivers. */
        rv = driver_register(&ipmi_driver.driver);
        if (rv) {
-               printk(KERN_ERR
-                      "init_ipmi_si: Unable to register driver: %d\n",
-                      rv);
+               printk(KERN_ERR PFX "Unable to register driver: %d\n", rv);
                return rv;
        }
 
@@ -3260,53 +3308,101 @@ static __devinit int init_ipmi_si(void)
 
        hardcode_find_bmc();
 
-#ifdef CONFIG_DMI
-       dmi_find_bmc();
-#endif
+       /* If the user gave us a device, they presumably want us to use it */
+       mutex_lock(&smi_infos_lock);
+       if (!list_empty(&smi_infos)) {
+               mutex_unlock(&smi_infos_lock);
+               return 0;
+       }
+       mutex_unlock(&smi_infos_lock);
 
-#ifdef CONFIG_ACPI
-       spmi_find_bmc();
+#ifdef CONFIG_PCI
+       rv = pci_register_driver(&ipmi_pci_driver);
+       if (rv)
+               printk(KERN_ERR PFX "Unable to register PCI driver: %d\n", rv);
+       else
+               pci_registered = 1;
 #endif
+
 #ifdef CONFIG_ACPI
        pnp_register_driver(&ipmi_pnp_driver);
 #endif
 
-#ifdef CONFIG_PCI
-       rv = pci_register_driver(&ipmi_pci_driver);
-       if (rv)
-               printk(KERN_ERR
-                      "init_ipmi_si: Unable to register PCI driver: %d\n",
-                      rv);
+#ifdef CONFIG_DMI
+       dmi_find_bmc();
+#endif
+
+#ifdef CONFIG_ACPI
+       spmi_find_bmc();
 #endif
 
 #ifdef CONFIG_PPC_OF
        of_register_platform_driver(&ipmi_of_platform_driver);
+       of_registered = 1;
 #endif
 
+       /* We prefer devices with interrupts, but in the case of a machine
+          with multiple BMCs we assume that there will be several instances
+          of a given type so if we succeed in registering a type then also
+          try to register everything else of the same type */
+
+       mutex_lock(&smi_infos_lock);
+       list_for_each_entry(e, &smi_infos, link) {
+               /* Try to register a device if it has an IRQ and we either
+                  haven't successfully registered a device yet or this
+                  device has the same type as one we successfully registered */
+               if (e->irq && (!type || e->addr_source == type)) {
+                       if (!try_smi_init(e)) {
+                               type = e->addr_source;
+                       }
+               }
+       }
+
+       /* type will only have been set if we successfully registered an si */
+       if (type) {
+               mutex_unlock(&smi_infos_lock);
+               return 0;
+       }
+
+       /* Fall back to the preferred device */
+
+       list_for_each_entry(e, &smi_infos, link) {
+               if (!e->irq && (!type || e->addr_source == type)) {
+                       if (!try_smi_init(e)) {
+                               type = e->addr_source;
+                       }
+               }
+       }
+       mutex_unlock(&smi_infos_lock);
+
+       if (type)
+               return 0;
+
        if (si_trydefaults) {
                mutex_lock(&smi_infos_lock);
                if (list_empty(&smi_infos)) {
                        /* No BMC was found, try defaults. */
                        mutex_unlock(&smi_infos_lock);
                        default_find_bmc();
-               } else {
+               } else
                        mutex_unlock(&smi_infos_lock);
-               }
        }
 
        mutex_lock(&smi_infos_lock);
        if (unload_when_empty && list_empty(&smi_infos)) {
                mutex_unlock(&smi_infos_lock);
 #ifdef CONFIG_PCI
-               pci_unregister_driver(&ipmi_pci_driver);
+               if (pci_registered)
+                       pci_unregister_driver(&ipmi_pci_driver);
 #endif
 
 #ifdef CONFIG_PPC_OF
-               of_unregister_platform_driver(&ipmi_of_platform_driver);
+               if (of_registered)
+                       of_unregister_platform_driver(&ipmi_of_platform_driver);
 #endif
                driver_unregister(&ipmi_driver.driver);
-               printk(KERN_WARNING
-                      "ipmi_si: Unable to find any System Interface(s)\n");
+               printk(KERN_WARNING PFX
+                      "Unable to find any System Interface(s)\n");
                return -ENODEV;
        } else {
                mutex_unlock(&smi_infos_lock);
@@ -3317,7 +3413,7 @@ module_init(init_ipmi_si);
 
 static void cleanup_one_si(struct smi_info *to_clean)
 {
-       int           rv;
+       int           rv = 0;
        unsigned long flags;
 
        if (!to_clean)
@@ -3361,14 +3457,16 @@ static void cleanup_one_si(struct smi_info *to_clean)
                schedule_timeout_uninterruptible(1);
        }
 
-       rv = ipmi_unregister_smi(to_clean->intf);
+       if (to_clean->intf)
+               rv = ipmi_unregister_smi(to_clean->intf);
+
        if (rv) {
-               printk(KERN_ERR
-                      "ipmi_si: Unable to unregister device: errno=%d\n",
+               printk(KERN_ERR PFX "Unable to unregister device: errno=%d\n",
                       rv);
        }
 
-       to_clean->handlers->cleanup(to_clean->si_sm);
+       if (to_clean->handlers)
+               to_clean->handlers->cleanup(to_clean->si_sm);
 
        kfree(to_clean->si_sm);
 
@@ -3391,14 +3489,16 @@ static __exit void cleanup_ipmi_si(void)
                return;
 
 #ifdef CONFIG_PCI
-       pci_unregister_driver(&ipmi_pci_driver);
+       if (pci_registered)
+               pci_unregister_driver(&ipmi_pci_driver);
 #endif
 #ifdef CONFIG_ACPI
        pnp_unregister_driver(&ipmi_pnp_driver);
 #endif
 
 #ifdef CONFIG_PPC_OF
-       of_unregister_platform_driver(&ipmi_of_platform_driver);
+       if (of_registered)
+               of_unregister_platform_driver(&ipmi_of_platform_driver);
 #endif
 
        mutex_lock(&smi_infos_lock);