#include <linux/module.h>
 #include <linux/errno.h>
+#include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/mutex.h>
 #include <linux/crc-itu-t.h>
        static atomic_t index = ATOMIC_INIT(-1);
 
        kref_init(&card->kref);
+       atomic_set(&card->device_count, 0);
        card->index = atomic_inc_return(&index);
        card->driver = driver;
        card->device = device;
        card->driver = &dummy_driver;
 
        fw_destroy_nodes(card);
-       flush_scheduled_work();
+       /*
+        * Wait for all device workqueue jobs to finish.  Otherwise the
+        * firewire-core module could be unloaded before the jobs ran.
+        */
+       while (atomic_read(&card->device_count) > 0)
+               msleep(100);
 
+       cancel_delayed_work_sync(&card->work);
        fw_flush_transactions(card);
        del_timer_sync(&card->flush_timer);
 
 
 };
 EXPORT_SYMBOL(fw_bus_type);
 
-struct fw_device *fw_device_get(struct fw_device *device)
-{
-       get_device(&device->device);
-
-       return device;
-}
-
-void fw_device_put(struct fw_device *device)
-{
-       put_device(&device->device);
-}
-
 static void fw_device_release(struct device *dev)
 {
        struct fw_device *device = fw_device(dev);
+       struct fw_card *card = device->card;
        unsigned long flags;
 
        /*
        spin_unlock_irqrestore(&device->card->lock, flags);
 
        fw_node_put(device->node);
-       fw_card_put(device->card);
        kfree(device->config_rom);
        kfree(device);
+       atomic_dec(&card->device_count);
 }
 
 int fw_device_enable_phys_dma(struct fw_device *device)
         */
 
        if (read_bus_info_block(device, device->generation) < 0) {
-               if (device->config_rom_retries < MAX_RETRIES) {
+               if (device->config_rom_retries < MAX_RETRIES &&
+                   atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
                        device->config_rom_retries++;
                        schedule_delayed_work(&device->work, RETRY_DELAY);
                } else {
                 */
                device_initialize(&device->device);
                atomic_set(&device->state, FW_DEVICE_INITIALIZING);
-               device->card = fw_card_get(card);
+               atomic_inc(&card->device_count);
+               device->card = card;
                device->node = fw_node_get(node);
                device->node_id = node->node_id;
                device->generation = card->generation;
 
        return atomic_read(&device->state) == FW_DEVICE_SHUTDOWN;
 }
 
-struct fw_device *fw_device_get(struct fw_device *device);
+static inline struct fw_device *
+fw_device_get(struct fw_device *device)
+{
+       get_device(&device->device);
+
+       return device;
+}
+
+static inline void
+fw_device_put(struct fw_device *device)
+{
+       put_device(&device->device);
+}
+
 struct fw_device *fw_device_get_by_devt(dev_t devt);
-void fw_device_put(struct fw_device *device);
 int fw_device_enable_phys_dma(struct fw_device *device);
 
 void fw_device_cdev_update(struct fw_device *device);
 
        struct Scsi_Host *shost =
                container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
        struct scsi_device *sdev;
+       struct fw_device *device = fw_device(tgt->unit->device.parent);
 
        /* prevent deadlocks */
        sbp2_unblock(tgt);
 
        put_device(&tgt->unit->device);
        scsi_host_put(shost);
+       fw_device_put(device);
 }
 
 static struct workqueue_struct *sbp2_wq;
        if (scsi_add_host(shost, &unit->device) < 0)
                goto fail_shost_put;
 
+       fw_device_get(device);
+
        /* Initialize to values that won't match anything in our table. */
        firmware_revision = 0xff000000;
        model = 0xff000000;
 
 #include <linux/fs.h>
 #include <linux/dma-mapping.h>
 #include <linux/firewire-constants.h>
+#include <asm/atomic.h>
 
 #define TCODE_IS_READ_REQUEST(tcode)   (((tcode) & ~1) == 4)
 #define TCODE_IS_BLOCK_PACKET(tcode)   (((tcode) &  1) != 0)
 struct fw_card {
        const struct fw_card_driver *driver;
        struct device *device;
+       atomic_t device_count;
        struct kref kref;
 
        int node_id;