input: touchscreen: k29_i2c_goodix: fix compilation warning
[firefly-linux-kernel-4.4.55.git] / drivers / firewire / sbp2.c
index 41841a3e3f99c9acd4c4fc72c972626d8dffdcee..1b1c37dd830b7ac77cd97e2f634f51fb68813359 100644 (file)
@@ -52,7 +52,6 @@
 #include <linux/workqueue.h>
 
 #include <asm/byteorder.h>
-#include <asm/system.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
@@ -66,7 +65,7 @@
  *
  * Concurrent logins are useful together with cluster filesystems.
  */
-static int sbp2_param_exclusive_login = 1;
+static bool sbp2_param_exclusive_login = 1;
 module_param_named(exclusive_login, sbp2_param_exclusive_login, bool, 0644);
 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
                 "(default = Y, use N for concurrent initiators)");
@@ -125,8 +124,6 @@ MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
        ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
        ", or a combination)");
 
-static const char sbp2_driver_name[] = "sbp2";
-
 /*
  * We create one struct sbp2_logical_unit per SBP-2 Logical Unit Number Entry
  * and one struct scsi_device per sbp2_logical_unit.
@@ -149,19 +146,23 @@ struct sbp2_logical_unit {
         */
        int generation;
        int retries;
+       work_func_t workfn;
        struct delayed_work work;
        bool has_sdev;
        bool blocked;
 };
 
+static void sbp2_queue_work(struct sbp2_logical_unit *lu, unsigned long delay)
+{
+       queue_delayed_work(fw_workqueue, &lu->work, delay);
+}
+
 /*
  * We create one struct sbp2_target per IEEE 1212 Unit Directory
  * and one struct Scsi_Host per sbp2_target.
  */
 struct sbp2_target {
-       struct kref kref;
        struct fw_unit *unit;
-       const char *bus_id;
        struct list_head lu_list;
 
        u64 management_agent_address;
@@ -177,11 +178,21 @@ struct sbp2_target {
        int blocked;    /* ditto */
 };
 
-static struct fw_device *target_device(struct sbp2_target *tgt)
+static struct fw_device *target_parent_device(struct sbp2_target *tgt)
 {
        return fw_parent_device(tgt->unit);
 }
 
+static const struct device *tgt_dev(const struct sbp2_target *tgt)
+{
+       return &tgt->unit->device;
+}
+
+static const struct device *lu_dev(const struct sbp2_logical_unit *lu)
+{
+       return &lu->tgt->unit->device;
+}
+
 /* Impossible login_id, to detect logout attempt before successful login */
 #define INVALID_LOGIN_ID 0x10000
 
@@ -197,9 +208,8 @@ static struct fw_device *target_device(struct sbp2_target *tgt)
 #define SBP2_MAX_CDB_SIZE              16
 
 /*
- * The default maximum s/g segment size of a FireWire controller is
- * usually 0x10000, but SBP-2 only allows 0xffff. Since buffers have to
- * be quadlet-aligned, we set the length limit to 0xffff & ~3.
+ * The maximum SBP-2 data buffer size is 0xffff.  We quadlet-align this
+ * for compatibility with earlier versions of this driver.
  */
 #define SBP2_MAX_SEG_SIZE              0xfffc
 
@@ -207,6 +217,7 @@ static struct fw_device *target_device(struct sbp2_target *tgt)
 #define SBP2_CSR_UNIT_CHARACTERISTICS  0x3a
 #define SBP2_CSR_FIRMWARE_REVISION     0x3c
 #define SBP2_CSR_LOGICAL_UNIT_NUMBER   0x14
+#define SBP2_CSR_UNIT_UNIQUE_ID                0x8d
 #define SBP2_CSR_LOGICAL_UNIT_DIRECTORY        0xd4
 
 /* Management orb opcodes */
@@ -426,7 +437,8 @@ static void sbp2_status_write(struct fw_card *card, struct fw_request *request,
                memcpy(status.data, payload + 8, length - 8);
 
        if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
-               fw_notify("non-orb related status write, not handled\n");
+               dev_notice(lu_dev(lu),
+                          "non-ORB related status write, not handled\n");
                fw_send_response(card, request, RCODE_COMPLETE);
                return;
        }
@@ -447,7 +459,7 @@ static void sbp2_status_write(struct fw_card *card, struct fw_request *request,
                orb->callback(orb, &status);
                kref_put(&orb->kref, free_orb); /* orb callback reference */
        } else {
-               fw_error("status write for unknown orb\n");
+               dev_err(lu_dev(lu), "status write for unknown ORB\n");
        }
 
        fw_send_response(card, request, RCODE_COMPLETE);
@@ -488,7 +500,7 @@ static void complete_transaction(struct fw_card *card, int rcode,
 static void sbp2_send_orb(struct sbp2_orb *orb, struct sbp2_logical_unit *lu,
                          int node_id, int generation, u64 offset)
 {
-       struct fw_device *device = target_device(lu->tgt);
+       struct fw_device *device = target_parent_device(lu->tgt);
        struct sbp2_pointer orb_pointer;
        unsigned long flags;
 
@@ -509,7 +521,7 @@ static void sbp2_send_orb(struct sbp2_orb *orb, struct sbp2_logical_unit *lu,
 
 static int sbp2_cancel_orbs(struct sbp2_logical_unit *lu)
 {
-       struct fw_device *device = target_device(lu->tgt);
+       struct fw_device *device = target_parent_device(lu->tgt);
        struct sbp2_orb *orb, *next;
        struct list_head list;
        unsigned long flags;
@@ -548,7 +560,7 @@ static int sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
                                    int generation, int function,
                                    int lun_or_login_id, void *response)
 {
-       struct fw_device *device = target_device(lu->tgt);
+       struct fw_device *device = target_parent_device(lu->tgt);
        struct sbp2_management_orb *orb;
        unsigned int timeout;
        int retval = -ENOMEM;
@@ -556,7 +568,7 @@ static int sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
        if (function == SBP2_LOGOUT_REQUEST && fw_device_is_shutdown(device))
                return 0;
 
-       orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
+       orb = kzalloc(sizeof(*orb), GFP_NOIO);
        if (orb == NULL)
                return -ENOMEM;
 
@@ -608,20 +620,20 @@ static int sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
 
        retval = -EIO;
        if (sbp2_cancel_orbs(lu) == 0) {
-               fw_error("%s: orb reply timed out, rcode=0x%02x\n",
-                        lu->tgt->bus_id, orb->base.rcode);
+               dev_err(lu_dev(lu), "ORB reply timed out, rcode 0x%02x\n",
+                       orb->base.rcode);
                goto out;
        }
 
        if (orb->base.rcode != RCODE_COMPLETE) {
-               fw_error("%s: management write failed, rcode 0x%02x\n",
-                        lu->tgt->bus_id, orb->base.rcode);
+               dev_err(lu_dev(lu), "management write failed, rcode 0x%02x\n",
+                       orb->base.rcode);
                goto out;
        }
 
        if (STATUS_GET_RESPONSE(orb->status) != 0 ||
            STATUS_GET_SBP_STATUS(orb->status) != 0) {
-               fw_error("%s: error status: %d:%d\n", lu->tgt->bus_id,
+               dev_err(lu_dev(lu), "error status: %d:%d\n",
                         STATUS_GET_RESPONSE(orb->status),
                         STATUS_GET_SBP_STATUS(orb->status));
                goto out;
@@ -644,7 +656,7 @@ static int sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
 
 static void sbp2_agent_reset(struct sbp2_logical_unit *lu)
 {
-       struct fw_device *device = target_device(lu->tgt);
+       struct fw_device *device = target_parent_device(lu->tgt);
        __be32 d = 0;
 
        fw_run_transaction(device->card, TCODE_WRITE_QUADLET_REQUEST,
@@ -661,7 +673,7 @@ static void complete_agent_reset_write_no_wait(struct fw_card *card,
 
 static void sbp2_agent_reset_no_wait(struct sbp2_logical_unit *lu)
 {
-       struct fw_device *device = target_device(lu->tgt);
+       struct fw_device *device = target_parent_device(lu->tgt);
        struct fw_transaction *t;
        static __be32 d;
 
@@ -700,7 +712,7 @@ static inline void sbp2_allow_block(struct sbp2_logical_unit *lu)
 static void sbp2_conditionally_block(struct sbp2_logical_unit *lu)
 {
        struct sbp2_target *tgt = lu->tgt;
-       struct fw_card *card = target_device(tgt)->card;
+       struct fw_card *card = target_parent_device(tgt)->card;
        struct Scsi_Host *shost =
                container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
        unsigned long flags;
@@ -724,7 +736,7 @@ static void sbp2_conditionally_block(struct sbp2_logical_unit *lu)
 static void sbp2_conditionally_unblock(struct sbp2_logical_unit *lu)
 {
        struct sbp2_target *tgt = lu->tgt;
-       struct fw_card *card = target_device(tgt)->card;
+       struct fw_card *card = target_parent_device(tgt)->card;
        struct Scsi_Host *shost =
                container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
        unsigned long flags;
@@ -749,7 +761,7 @@ static void sbp2_conditionally_unblock(struct sbp2_logical_unit *lu)
  */
 static void sbp2_unblock(struct sbp2_target *tgt)
 {
-       struct fw_card *card = target_device(tgt)->card;
+       struct fw_card *card = target_parent_device(tgt)->card;
        struct Scsi_Host *shost =
                container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
        unsigned long flags;
@@ -772,71 +784,6 @@ static int sbp2_lun2int(u16 lun)
        return scsilun_to_int(&eight_bytes_lun);
 }
 
-static void sbp2_release_target(struct kref *kref)
-{
-       struct sbp2_target *tgt = container_of(kref, struct sbp2_target, kref);
-       struct sbp2_logical_unit *lu, *next;
-       struct Scsi_Host *shost =
-               container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
-       struct scsi_device *sdev;
-       struct fw_device *device = target_device(tgt);
-
-       /* prevent deadlocks */
-       sbp2_unblock(tgt);
-
-       list_for_each_entry_safe(lu, next, &tgt->lu_list, link) {
-               sdev = scsi_device_lookup(shost, 0, 0, sbp2_lun2int(lu->lun));
-               if (sdev) {
-                       scsi_remove_device(sdev);
-                       scsi_device_put(sdev);
-               }
-               if (lu->login_id != INVALID_LOGIN_ID) {
-                       int generation, node_id;
-                       /*
-                        * tgt->node_id may be obsolete here if we failed
-                        * during initial login or after a bus reset where
-                        * the topology changed.
-                        */
-                       generation = device->generation;
-                       smp_rmb(); /* node_id vs. generation */
-                       node_id    = device->node_id;
-                       sbp2_send_management_orb(lu, node_id, generation,
-                                                SBP2_LOGOUT_REQUEST,
-                                                lu->login_id, NULL);
-               }
-               fw_core_remove_address_handler(&lu->address_handler);
-               list_del(&lu->link);
-               kfree(lu);
-       }
-       scsi_remove_host(shost);
-       fw_notify("released %s, target %d:0:0\n", tgt->bus_id, shost->host_no);
-
-       fw_unit_put(tgt->unit);
-       scsi_host_put(shost);
-       fw_device_put(device);
-}
-
-static void sbp2_target_get(struct sbp2_target *tgt)
-{
-       kref_get(&tgt->kref);
-}
-
-static void sbp2_target_put(struct sbp2_target *tgt)
-{
-       kref_put(&tgt->kref, sbp2_release_target);
-}
-
-/*
- * Always get the target's kref when scheduling work on one its units.
- * Each workqueue job is responsible to call sbp2_target_put() upon return.
- */
-static void sbp2_queue_work(struct sbp2_logical_unit *lu, unsigned long delay)
-{
-       sbp2_target_get(lu->tgt);
-       if (!queue_delayed_work(fw_workqueue, &lu->work, delay))
-               sbp2_target_put(lu->tgt);
-}
-
 /*
  * Write retransmit retry values into the BUSY_TIMEOUT register.
  * - The single-phase retry protocol is supported by all SBP-2 devices, but the
@@ -855,7 +802,7 @@ static void sbp2_queue_work(struct sbp2_logical_unit *lu, unsigned long delay)
  */
 static void sbp2_set_busy_timeout(struct sbp2_logical_unit *lu)
 {
-       struct fw_device *device = target_device(lu->tgt);
+       struct fw_device *device = target_parent_device(lu->tgt);
        __be32 d = cpu_to_be32(SBP2_CYCLE_LIMIT | SBP2_RETRY_LIMIT);
 
        fw_run_transaction(device->card, TCODE_WRITE_QUADLET_REQUEST,
@@ -870,14 +817,14 @@ static void sbp2_login(struct work_struct *work)
        struct sbp2_logical_unit *lu =
                container_of(work, struct sbp2_logical_unit, work.work);
        struct sbp2_target *tgt = lu->tgt;
-       struct fw_device *device = target_device(tgt);
+       struct fw_device *device = target_parent_device(tgt);
        struct Scsi_Host *shost;
        struct scsi_device *sdev;
        struct sbp2_login_response response;
        int generation, node_id, local_node_id;
 
        if (fw_device_is_shutdown(device))
-               goto out;
+               return;
 
        generation    = device->generation;
        smp_rmb();    /* node IDs must not be older than generation */
@@ -894,12 +841,12 @@ static void sbp2_login(struct work_struct *work)
                if (lu->retries++ < 5) {
                        sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
                } else {
-                       fw_error("%s: failed to login to LUN %04x\n",
-                                tgt->bus_id, lu->lun);
+                       dev_err(tgt_dev(tgt), "failed to login to LUN %04x\n",
+                               lu->lun);
                        /* Let any waiting I/O fail from now on. */
                        sbp2_unblock(lu->tgt);
                }
-               goto out;
+               return;
        }
 
        tgt->node_id      = node_id;
@@ -912,20 +859,21 @@ static void sbp2_login(struct work_struct *work)
                      << 32) | be32_to_cpu(response.command_block_agent.low);
        lu->login_id = be32_to_cpu(response.misc) & 0xffff;
 
-       fw_notify("%s: logged in to LUN %04x (%d retries)\n",
-                 tgt->bus_id, lu->lun, lu->retries);
+       dev_notice(tgt_dev(tgt), "logged in to LUN %04x (%d retries)\n",
+                  lu->lun, lu->retries);
 
        /* set appropriate retry limit(s) in BUSY_TIMEOUT register */
        sbp2_set_busy_timeout(lu);
 
-       PREPARE_DELAYED_WORK(&lu->work, sbp2_reconnect);
+       lu->workfn = sbp2_reconnect;
        sbp2_agent_reset(lu);
 
        /* This was a re-login. */
        if (lu->has_sdev) {
                sbp2_cancel_orbs(lu);
                sbp2_conditionally_unblock(lu);
-               goto out;
+
+               return;
        }
 
        if (lu->tgt->workarounds & SBP2_WORKAROUND_DELAY_INQUIRY)
@@ -957,7 +905,8 @@ static void sbp2_login(struct work_struct *work)
        lu->has_sdev = true;
        scsi_device_put(sdev);
        sbp2_allow_block(lu);
-       goto out;
+
+       return;
 
  out_logout_login:
        smp_rmb(); /* generation may have changed */
@@ -970,9 +919,65 @@ static void sbp2_login(struct work_struct *work)
         * If a bus reset happened, sbp2_update will have requeued
         * lu->work already.  Reset the work from reconnect to login.
         */
-       PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
- out:
-       sbp2_target_put(tgt);
+       lu->workfn = sbp2_login;
+}
+
+static void sbp2_reconnect(struct work_struct *work)
+{
+       struct sbp2_logical_unit *lu =
+               container_of(work, struct sbp2_logical_unit, work.work);
+       struct sbp2_target *tgt = lu->tgt;
+       struct fw_device *device = target_parent_device(tgt);
+       int generation, node_id, local_node_id;
+
+       if (fw_device_is_shutdown(device))
+               return;
+
+       generation    = device->generation;
+       smp_rmb();    /* node IDs must not be older than generation */
+       node_id       = device->node_id;
+       local_node_id = device->card->node_id;
+
+       if (sbp2_send_management_orb(lu, node_id, generation,
+                                    SBP2_RECONNECT_REQUEST,
+                                    lu->login_id, NULL) < 0) {
+               /*
+                * If reconnect was impossible even though we are in the
+                * current generation, fall back and try to log in again.
+                *
+                * We could check for "Function rejected" status, but
+                * looking at the bus generation as simpler and more general.
+                */
+               smp_rmb(); /* get current card generation */
+               if (generation == device->card->generation ||
+                   lu->retries++ >= 5) {
+                       dev_err(tgt_dev(tgt), "failed to reconnect\n");
+                       lu->retries = 0;
+                       lu->workfn = sbp2_login;
+               }
+               sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
+
+               return;
+       }
+
+       tgt->node_id      = node_id;
+       tgt->address_high = local_node_id << 16;
+       smp_wmb();        /* node IDs must not be older than generation */
+       lu->generation    = generation;
+
+       dev_notice(tgt_dev(tgt), "reconnected to LUN %04x (%d retries)\n",
+                  lu->lun, lu->retries);
+
+       sbp2_agent_reset(lu);
+       sbp2_cancel_orbs(lu);
+       sbp2_conditionally_unblock(lu);
+}
+
+static void sbp2_lu_workfn(struct work_struct *work)
+{
+       struct sbp2_logical_unit *lu = container_of(to_delayed_work(work),
+                                               struct sbp2_logical_unit, work);
+       lu->workfn(work);
 }
 
 static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
@@ -1001,12 +1006,20 @@ static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
        lu->blocked  = false;
        ++tgt->dont_block;
        INIT_LIST_HEAD(&lu->orb_list);
-       INIT_DELAYED_WORK(&lu->work, sbp2_login);
+       lu->workfn = sbp2_login;
+       INIT_DELAYED_WORK(&lu->work, sbp2_lu_workfn);
 
        list_add_tail(&lu->link, &tgt->lu_list);
        return 0;
 }
 
+static void sbp2_get_unit_unique_id(struct sbp2_target *tgt,
+                                   const u32 *leaf)
+{
+       if ((leaf[0] & 0xffff0000) == 0x00020000)
+               tgt->guid = (u64)leaf[1] << 32 | leaf[2];
+}
+
 static int sbp2_scan_logical_unit_dir(struct sbp2_target *tgt,
                                      const u32 *directory)
 {
@@ -1058,6 +1071,10 @@ static int sbp2_scan_unit_dir(struct sbp2_target *tgt, const u32 *directory,
                                return -ENOMEM;
                        break;
 
+               case SBP2_CSR_UNIT_UNIQUE_ID:
+                       sbp2_get_unit_unique_id(tgt, ci.p - 1 + value);
+                       break;
+
                case SBP2_CSR_LOGICAL_UNIT_DIRECTORY:
                        /* Adjust for the increment in the iterator */
                        if (sbp2_scan_logical_unit_dir(tgt, ci.p - 1 + value) < 0)
@@ -1078,8 +1095,8 @@ static void sbp2_clamp_management_orb_timeout(struct sbp2_target *tgt)
        unsigned int timeout = tgt->mgt_orb_timeout;
 
        if (timeout > 40000)
-               fw_notify("%s: %ds mgt_ORB_timeout limited to 40s\n",
-                         tgt->bus_id, timeout / 1000);
+               dev_notice(tgt_dev(tgt), "%ds mgt_ORB_timeout limited to 40s\n",
+                          timeout / 1000);
 
        tgt->mgt_orb_timeout = clamp_val(timeout, 5000, 40000);
 }
@@ -1091,9 +1108,9 @@ static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
        unsigned int w = sbp2_param_workarounds;
 
        if (w)
-               fw_notify("Please notify linux1394-devel@lists.sourceforge.net "
-                         "if you need the workarounds parameter for %s\n",
-                         tgt->bus_id);
+               dev_notice(tgt_dev(tgt),
+                          "Please notify linux1394-devel@lists.sf.net "
+                          "if you need the workarounds parameter\n");
 
        if (w & SBP2_WORKAROUND_OVERRIDE)
                goto out;
@@ -1113,13 +1130,14 @@ static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
        }
  out:
        if (w)
-               fw_notify("Workarounds for %s: 0x%x "
-                         "(firmware_revision 0x%06x, model_id 0x%06x)\n",
-                         tgt->bus_id, w, firmware_revision, model);
+               dev_notice(tgt_dev(tgt), "workarounds 0x%x "
+                          "(firmware_revision 0x%06x, model_id 0x%06x)\n",
+                          w, firmware_revision, model);
        tgt->workarounds = w;
 }
 
 static struct scsi_host_template scsi_driver_template;
+static int sbp2_remove(struct device *dev);
 
 static int sbp2_probe(struct device *dev)
 {
@@ -1130,9 +1148,13 @@ static int sbp2_probe(struct device *dev)
        struct Scsi_Host *shost;
        u32 model, firmware_revision;
 
+       /* cannot (or should not) handle targets on the local node */
+       if (device->is_local)
+               return -ENODEV;
+
        if (dma_get_max_seg_size(device->card->device) > SBP2_MAX_SEG_SIZE)
-               BUG_ON(dma_set_max_seg_size(device->card->device,
-                                           SBP2_MAX_SEG_SIZE));
+               WARN_ON(dma_set_max_seg_size(device->card->device,
+                                            SBP2_MAX_SEG_SIZE));
 
        shost = scsi_host_alloc(&scsi_driver_template, sizeof(*tgt));
        if (shost == NULL)
@@ -1141,9 +1163,7 @@ static int sbp2_probe(struct device *dev)
        tgt = (struct sbp2_target *)shost->hostdata;
        dev_set_drvdata(&unit->device, tgt);
        tgt->unit = unit;
-       kref_init(&tgt->kref);
        INIT_LIST_HEAD(&tgt->lu_list);
-       tgt->bus_id = dev_name(&unit->device);
        tgt->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
 
        if (fw_device_enable_phys_dma(device) < 0)
@@ -1151,12 +1171,10 @@ static int sbp2_probe(struct device *dev)
 
        shost->max_cmd_len = SBP2_MAX_CDB_SIZE;
 
-       if (scsi_add_host(shost, &unit->device) < 0)
+       if (scsi_add_host_with_dma(shost, &unit->device,
+                                  device->card->device) < 0)
                goto fail_shost_put;
 
-       fw_device_get(device);
-       fw_unit_get(unit);
-
        /* implicit directory ID */
        tgt->directory_id = ((unit->directory - device->config_rom) * 4
                             + CSR_CONFIG_ROM) & 0xffffff;
@@ -1166,7 +1184,7 @@ static int sbp2_probe(struct device *dev)
 
        if (sbp2_scan_unit_dir(tgt, unit->directory, &model,
                               &firmware_revision) < 0)
-               goto fail_tgt_put;
+               goto fail_remove;
 
        sbp2_clamp_management_orb_timeout(tgt);
        sbp2_init_workarounds(tgt, model, firmware_revision);
@@ -1177,16 +1195,17 @@ static int sbp2_probe(struct device *dev)
         * specifies the max payload size as 2 ^ (max_payload + 2), so
         * if we set this to max_speed + 7, we get the right value.
         */
-       tgt->max_payload = min(device->max_speed + 7, 10U);
-       tgt->max_payload = min(tgt->max_payload, device->card->max_receive - 1);
+       tgt->max_payload = min3(device->max_speed + 7, 10U,
+                               device->card->max_receive - 1);
 
        /* Do the login in a workqueue so we can easily reschedule retries. */
        list_for_each_entry(lu, &tgt->lu_list, link)
                sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
+
        return 0;
 
- fail_tgt_put:
-       sbp2_target_put(tgt);
+ fail_remove:
+       sbp2_remove(dev);
        return -ENOMEM;
 
  fail_shost_put:
@@ -1194,67 +1213,6 @@ static int sbp2_probe(struct device *dev)
        return -ENOMEM;
 }
 
-static int sbp2_remove(struct device *dev)
-{
-       struct fw_unit *unit = fw_unit(dev);
-       struct sbp2_target *tgt = dev_get_drvdata(&unit->device);
-
-       sbp2_target_put(tgt);
-       return 0;
-}
-
-static void sbp2_reconnect(struct work_struct *work)
-{
-       struct sbp2_logical_unit *lu =
-               container_of(work, struct sbp2_logical_unit, work.work);
-       struct sbp2_target *tgt = lu->tgt;
-       struct fw_device *device = target_device(tgt);
-       int generation, node_id, local_node_id;
-
-       if (fw_device_is_shutdown(device))
-               goto out;
-
-       generation    = device->generation;
-       smp_rmb();    /* node IDs must not be older than generation */
-       node_id       = device->node_id;
-       local_node_id = device->card->node_id;
-
-       if (sbp2_send_management_orb(lu, node_id, generation,
-                                    SBP2_RECONNECT_REQUEST,
-                                    lu->login_id, NULL) < 0) {
-               /*
-                * If reconnect was impossible even though we are in the
-                * current generation, fall back and try to log in again.
-                *
-                * We could check for "Function rejected" status, but
-                * looking at the bus generation as simpler and more general.
-                */
-               smp_rmb(); /* get current card generation */
-               if (generation == device->card->generation ||
-                   lu->retries++ >= 5) {
-                       fw_error("%s: failed to reconnect\n", tgt->bus_id);
-                       lu->retries = 0;
-                       PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
-               }
-               sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
-               goto out;
-       }
-
-       tgt->node_id      = node_id;
-       tgt->address_high = local_node_id << 16;
-       smp_wmb();        /* node IDs must not be older than generation */
-       lu->generation    = generation;
-
-       fw_notify("%s: reconnected to LUN %04x (%d retries)\n",
-                 tgt->bus_id, lu->lun, lu->retries);
-
-       sbp2_agent_reset(lu);
-       sbp2_cancel_orbs(lu);
-       sbp2_conditionally_unblock(lu);
- out:
-       sbp2_target_put(tgt);
-}
-
 static void sbp2_update(struct fw_unit *unit)
 {
        struct sbp2_target *tgt = dev_get_drvdata(&unit->device);
@@ -1273,6 +1231,51 @@ static void sbp2_update(struct fw_unit *unit)
        }
 }
 
+static int sbp2_remove(struct device *dev)
+{
+       struct fw_unit *unit = fw_unit(dev);
+       struct fw_device *device = fw_parent_device(unit);
+       struct sbp2_target *tgt = dev_get_drvdata(&unit->device);
+       struct sbp2_logical_unit *lu, *next;
+       struct Scsi_Host *shost =
+               container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
+       struct scsi_device *sdev;
+
+       /* prevent deadlocks */
+       sbp2_unblock(tgt);
+
+       list_for_each_entry_safe(lu, next, &tgt->lu_list, link) {
+               cancel_delayed_work_sync(&lu->work);
+               sdev = scsi_device_lookup(shost, 0, 0, sbp2_lun2int(lu->lun));
+               if (sdev) {
+                       scsi_remove_device(sdev);
+                       scsi_device_put(sdev);
+               }
+               if (lu->login_id != INVALID_LOGIN_ID) {
+                       int generation, node_id;
+                       /*
+                        * tgt->node_id may be obsolete here if we failed
+                        * during initial login or after a bus reset where
+                        * the topology changed.
+                        */
+                       generation = device->generation;
+                       smp_rmb(); /* node_id vs. generation */
+                       node_id    = device->node_id;
+                       sbp2_send_management_orb(lu, node_id, generation,
+                                                SBP2_LOGOUT_REQUEST,
+                                                lu->login_id, NULL);
+               }
+               fw_core_remove_address_handler(&lu->address_handler);
+               list_del(&lu->link);
+               kfree(lu);
+       }
+       scsi_remove_host(shost);
+       dev_notice(dev, "released target %d:0:0\n", shost->host_no);
+
+       scsi_host_put(shost);
+       return 0;
+}
+
 #define SBP2_UNIT_SPEC_ID_ENTRY        0x0000609e
 #define SBP2_SW_VERSION_ENTRY  0x00010483
 
@@ -1289,7 +1292,7 @@ static const struct ieee1394_device_id sbp2_id_table[] = {
 static struct fw_driver sbp2_driver = {
        .driver   = {
                .owner  = THIS_MODULE,
-               .name   = sbp2_driver_name,
+               .name   = KBUILD_MODNAME,
                .bus    = &fw_bus_type,
                .probe  = sbp2_probe,
                .remove = sbp2_remove,
@@ -1301,10 +1304,7 @@ static struct fw_driver sbp2_driver = {
 static void sbp2_unmap_scatterlist(struct device *card_device,
                                   struct sbp2_command_orb *orb)
 {
-       if (scsi_sg_count(orb->cmd))
-               dma_unmap_sg(card_device, scsi_sglist(orb->cmd),
-                            scsi_sg_count(orb->cmd),
-                            orb->cmd->sc_data_direction);
+       scsi_dma_unmap(orb->cmd);
 
        if (orb->request.misc & cpu_to_be32(COMMAND_ORB_PAGE_TABLE_PRESENT))
                dma_unmap_single(card_device, orb->page_table_bus,
@@ -1314,10 +1314,19 @@ static void sbp2_unmap_scatterlist(struct device *card_device,
 static unsigned int sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
 {
        int sam_status;
+       int sfmt = (sbp2_status[0] >> 6) & 0x03;
+
+       if (sfmt == 2 || sfmt == 3) {
+               /*
+                * Reserved for future standardization (2) or
+                * Status block format vendor-dependent (3)
+                */
+               return DID_ERROR << 16;
+       }
 
-       sense_data[0] = 0x70;
+       sense_data[0] = 0x70 | sfmt | (sbp2_status[1] & 0x80);
        sense_data[1] = 0x0;
-       sense_data[2] = sbp2_status[1];
+       sense_data[2] = ((sbp2_status[1] << 1) & 0xe0) | (sbp2_status[1] & 0x0f);
        sense_data[3] = sbp2_status[4];
        sense_data[4] = sbp2_status[5];
        sense_data[5] = sbp2_status[6];
@@ -1353,7 +1362,7 @@ static void complete_command_orb(struct sbp2_orb *base_orb,
 {
        struct sbp2_command_orb *orb =
                container_of(base_orb, struct sbp2_command_orb, base);
-       struct fw_device *device = target_device(orb->lu->tgt);
+       struct fw_device *device = target_parent_device(orb->lu->tgt);
        int result;
 
        if (status != NULL) {
@@ -1401,9 +1410,8 @@ static int sbp2_map_scatterlist(struct sbp2_command_orb *orb,
        struct scatterlist *sg = scsi_sglist(orb->cmd);
        int i, n;
 
-       n = dma_map_sg(device->card->device, sg, scsi_sg_count(orb->cmd),
-                      orb->cmd->sc_data_direction);
-       if (n == 0)
+       n = scsi_dma_map(orb->cmd);
+       if (n <= 0)
                goto fail;
 
        /*
@@ -1449,8 +1457,7 @@ static int sbp2_map_scatterlist(struct sbp2_command_orb *orb,
        return 0;
 
  fail_page_table:
-       dma_unmap_sg(device->card->device, scsi_sglist(orb->cmd),
-                    scsi_sg_count(orb->cmd), orb->cmd->sc_data_direction);
+       scsi_dma_unmap(orb->cmd);
  fail:
        return -ENOMEM;
 }
@@ -1461,7 +1468,7 @@ static int sbp2_scsi_queuecommand(struct Scsi_Host *shost,
                                  struct scsi_cmnd *cmd)
 {
        struct sbp2_logical_unit *lu = cmd->device->hostdata;
-       struct fw_device *device = target_device(lu->tgt);
+       struct fw_device *device = target_parent_device(lu->tgt);
        struct sbp2_command_orb *orb;
        int generation, retval = SCSI_MLQUEUE_HOST_BUSY;
 
@@ -1470,17 +1477,15 @@ static int sbp2_scsi_queuecommand(struct Scsi_Host *shost,
         * transfer direction not handled.
         */
        if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
-               fw_error("Can't handle DMA_BIDIRECTIONAL, rejecting command\n");
+               dev_err(lu_dev(lu), "cannot handle bidirectional command\n");
                cmd->result = DID_ERROR << 16;
                cmd->scsi_done(cmd);
                return 0;
        }
 
        orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
-       if (orb == NULL) {
-               fw_notify("failed to alloc orb\n");
+       if (orb == NULL)
                return SCSI_MLQUEUE_HOST_BUSY;
-       }
 
        /* Initialize rcode to something not RCODE_COMPLETE. */
        orb->base.rcode = -1;
@@ -1531,7 +1536,10 @@ static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
 
        sdev->allow_restart = 1;
 
-       /* SBP-2 requires quadlet alignment of the data buffers. */
+       /*
+        * SBP-2 does not require any alignment, but we set it anyway
+        * for compatibility with earlier versions of this driver.
+        */
        blk_queue_update_dma_alignment(sdev->request_queue, 4 - 1);
 
        if (lu->tgt->workarounds & SBP2_WORKAROUND_INQUIRY_36)
@@ -1565,8 +1573,6 @@ static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
        if (lu->tgt->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
                blk_queue_max_hw_sectors(sdev->request_queue, 128 * 1024 / 512);
 
-       blk_queue_max_segment_size(sdev->request_queue, SBP2_MAX_SEG_SIZE);
-
        return 0;
 }
 
@@ -1578,7 +1584,7 @@ static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
 {
        struct sbp2_logical_unit *lu = cmd->device->hostdata;
 
-       fw_notify("%s: sbp2_scsi_abort\n", lu->tgt->bus_id);
+       dev_notice(lu_dev(lu), "sbp2_scsi_abort\n");
        sbp2_agent_reset(lu);
        sbp2_cancel_orbs(lu);
 
@@ -1618,7 +1624,7 @@ static struct device_attribute *sbp2_scsi_sysfs_attrs[] = {
 static struct scsi_host_template scsi_driver_template = {
        .module                 = THIS_MODULE,
        .name                   = "SBP-2 IEEE-1394",
-       .proc_name              = sbp2_driver_name,
+       .proc_name              = "sbp2",
        .queuecommand           = sbp2_scsi_queuecommand,
        .slave_alloc            = sbp2_scsi_slave_alloc,
        .slave_configure        = sbp2_scsi_slave_configure,
@@ -1637,9 +1643,7 @@ MODULE_LICENSE("GPL");
 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
 
 /* Provide a module alias so root-on-sbp2 initrds don't break. */
-#ifndef CONFIG_IEEE1394_SBP2_MODULE
 MODULE_ALIAS("sbp2");
-#endif
 
 static int __init sbp2_init(void)
 {