]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/firewire/fw-sbp2.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[linux-2.6-omap-h63xx.git] / drivers / firewire / fw-sbp2.c
index 3308bc089beb18c1658a7d30dca42174678626e5..a98d3915e26f33f926d8e0c383842cb5330018db 100644 (file)
@@ -74,7 +74,6 @@ struct sbp2_device {
 
        int retries;
        struct delayed_work work;
-       struct Scsi_Host *scsi_host;
 };
 
 #define SBP2_MAX_SG_ELEMENT_LENGTH     0xf000
@@ -123,14 +122,14 @@ struct sbp2_device {
 #define SBP2_STATUS_ILLEGAL_REQUEST    0x2
 #define SBP2_STATUS_VENDOR_DEPENDENT   0x3
 
-#define status_get_orb_high(v)         ((v).status & 0xffff)
-#define status_get_sbp_status(v)       (((v).status >> 16) & 0xff)
-#define status_get_len(v)              (((v).status >> 24) & 0x07)
-#define status_get_dead(v)             (((v).status >> 27) & 0x01)
-#define status_get_response(v)         (((v).status >> 28) & 0x03)
-#define status_get_source(v)           (((v).status >> 30) & 0x03)
-#define status_get_orb_low(v)          ((v).orb_low)
-#define status_get_data(v)             ((v).data)
+#define STATUS_GET_ORB_HIGH(v)         ((v).status & 0xffff)
+#define STATUS_GET_SBP_STATUS(v)       (((v).status >> 16) & 0xff)
+#define STATUS_GET_LEN(v)              (((v).status >> 24) & 0x07)
+#define STATUS_GET_DEAD(v)             (((v).status >> 27) & 0x01)
+#define STATUS_GET_RESPONSE(v)         (((v).status >> 28) & 0x03)
+#define STATUS_GET_SOURCE(v)           (((v).status >> 30) & 0x03)
+#define STATUS_GET_ORB_LOW(v)          ((v).orb_low)
+#define STATUS_GET_DATA(v)             ((v).data)
 
 struct sbp2_status {
        u32 status;
@@ -152,15 +151,15 @@ struct sbp2_orb {
        struct list_head link;
 };
 
-#define management_orb_lun(v)                  ((v))
-#define management_orb_function(v)             ((v) << 16)
-#define management_orb_reconnect(v)            ((v) << 20)
-#define management_orb_exclusive               ((1) << 28)
-#define management_orb_request_format(v)       ((v) << 29)
-#define management_orb_notify                  ((1) << 31)
+#define MANAGEMENT_ORB_LUN(v)                  ((v))
+#define MANAGEMENT_ORB_FUNCTION(v)             ((v) << 16)
+#define MANAGEMENT_ORB_RECONNECT(v)            ((v) << 20)
+#define MANAGEMENT_ORB_EXCLUSIVE               ((1) << 28)
+#define MANAGEMENT_ORB_REQUEST_FORMAT(v)       ((v) << 29)
+#define MANAGEMENT_ORB_NOTIFY                  ((1) << 31)
 
-#define management_orb_response_length(v)      ((v))
-#define management_orb_password_length(v)      ((v) << 16)
+#define MANAGEMENT_ORB_RESPONSE_LENGTH(v)      ((v))
+#define MANAGEMENT_ORB_PASSWORD_LENGTH(v)      ((v) << 16)
 
 struct sbp2_management_orb {
        struct sbp2_orb base;
@@ -177,23 +176,22 @@ struct sbp2_management_orb {
        struct sbp2_status status;
 };
 
-#define login_response_get_login_id(v) ((v).misc & 0xffff)
-#define login_response_get_length(v)   (((v).misc >> 16) & 0xffff)
+#define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff)
+#define LOGIN_RESPONSE_GET_LENGTH(v)   (((v).misc >> 16) & 0xffff)
 
 struct sbp2_login_response {
        u32 misc;
        struct sbp2_pointer command_block_agent;
        u32 reconnect_hold;
 };
-
-#define command_orb_data_size(v)       ((v))
-#define command_orb_page_size(v)       ((v) << 16)
-#define command_orb_page_table_present ((1) << 19)
-#define command_orb_max_payload(v)     ((v) << 20)
-#define command_orb_speed(v)           ((v) << 24)
-#define command_orb_direction(v)       ((v) << 27)
-#define command_orb_request_format(v)  ((v) << 29)
-#define command_orb_notify             ((1) << 31)
+#define COMMAND_ORB_DATA_SIZE(v)       ((v))
+#define COMMAND_ORB_PAGE_SIZE(v)       ((v) << 16)
+#define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
+#define COMMAND_ORB_MAX_PAYLOAD(v)     ((v) << 20)
+#define COMMAND_ORB_SPEED(v)           ((v) << 24)
+#define COMMAND_ORB_DIRECTION(v)       ((v) << 27)
+#define COMMAND_ORB_REQUEST_FORMAT(v)  ((v) << 29)
+#define COMMAND_ORB_NOTIFY             ((1) << 31)
 
 struct sbp2_command_orb {
        struct sbp2_orb base;
@@ -281,7 +279,7 @@ sbp2_status_write(struct fw_card *card, struct fw_request *request,
        unsigned long flags;
 
        if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
-           length == 0 || length > sizeof status) {
+           length == 0 || length > sizeof(status)) {
                fw_send_response(card, request, RCODE_TYPE_ERROR);
                return;
        }
@@ -290,7 +288,7 @@ sbp2_status_write(struct fw_card *card, struct fw_request *request,
        fw_memcpy_from_be32(&status, payload, header_size);
        if (length > header_size)
                memcpy(status.data, payload + 8, length - header_size);
-       if (status_get_source(status) == 2 || status_get_source(status) == 3) {
+       if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
                fw_notify("non-orb related status write, not handled\n");
                fw_send_response(card, request, RCODE_COMPLETE);
                return;
@@ -299,8 +297,8 @@ sbp2_status_write(struct fw_card *card, struct fw_request *request,
        /* Lookup the orb corresponding to this status write. */
        spin_lock_irqsave(&card->lock, flags);
        list_for_each_entry(orb, &sd->orb_list, link) {
-               if (status_get_orb_high(status) == 0 &&
-                   status_get_orb_low(status) == orb->request_bus &&
+               if (STATUS_GET_ORB_HIGH(status) == 0 &&
+                   STATUS_GET_ORB_LOW(status) == orb->request_bus &&
                    orb->rcode == RCODE_COMPLETE) {
                        list_del(&orb->link);
                        break;
@@ -342,7 +340,7 @@ sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
 
        orb->pointer.high = 0;
        orb->pointer.low = orb->request_bus;
-       fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof orb->pointer);
+       fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer));
 
        spin_lock_irqsave(&device->card->lock, flags);
        list_add_tail(&orb->link, &sd->orb_list);
@@ -351,7 +349,7 @@ sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
        fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
                        node_id, generation,
                        device->node->max_speed, offset,
-                       &orb->pointer, sizeof orb->pointer,
+                       &orb->pointer, sizeof(orb->pointer),
                        complete_transaction, orb);
 }
 
@@ -388,7 +386,7 @@ complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
            (struct sbp2_management_orb *)base_orb;
 
        if (status)
-               memcpy(&orb->status, status, sizeof *status);
+               memcpy(&orb->status, status, sizeof(*status));
        complete(&orb->done);
 }
 
@@ -401,7 +399,7 @@ sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
        struct sbp2_management_orb *orb;
        int retval = -ENOMEM;
 
-       orb = kzalloc(sizeof *orb, GFP_ATOMIC);
+       orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
        if (orb == NULL)
                return -ENOMEM;
 
@@ -411,13 +409,13 @@ sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
         */
        orb->base.request_bus =
                dma_map_single(device->card->device, &orb->request,
-                              sizeof orb->request, DMA_TO_DEVICE);
+                              sizeof(orb->request), DMA_TO_DEVICE);
        if (dma_mapping_error(orb->base.request_bus))
                goto out;
 
        orb->response_bus =
                dma_map_single(device->card->device, &orb->response,
-                              sizeof orb->response, DMA_FROM_DEVICE);
+                              sizeof(orb->response), DMA_FROM_DEVICE);
        if (dma_mapping_error(orb->response_bus))
                goto out;
 
@@ -425,11 +423,11 @@ sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
        orb->request.response.low     = orb->response_bus;
 
        orb->request.misc =
-               management_orb_notify |
-               management_orb_function(function) |
-               management_orb_lun(lun);
+               MANAGEMENT_ORB_NOTIFY |
+               MANAGEMENT_ORB_FUNCTION(function) |
+               MANAGEMENT_ORB_LUN(lun);
        orb->request.length =
-               management_orb_response_length(sizeof orb->response);
+               MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response));
 
        orb->request.status_fifo.high = sd->address_handler.offset >> 32;
        orb->request.status_fifo.low  = sd->address_handler.offset;
@@ -441,11 +439,11 @@ sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
         */
        if (function == SBP2_LOGIN_REQUEST) {
                orb->request.misc |=
-                       management_orb_exclusive |
-                       management_orb_reconnect(0);
+                       MANAGEMENT_ORB_EXCLUSIVE |
+                       MANAGEMENT_ORB_RECONNECT(0);
        }
 
-       fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
+       fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
 
        init_completion(&orb->done);
        orb->base.callback = complete_management_orb;
@@ -469,24 +467,24 @@ sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
                goto out;
        }
 
-       if (status_get_response(orb->status) != 0 ||
-           status_get_sbp_status(orb->status) != 0) {
+       if (STATUS_GET_RESPONSE(orb->status) != 0 ||
+           STATUS_GET_SBP_STATUS(orb->status) != 0) {
                fw_error("error status: %d:%d\n",
-                        status_get_response(orb->status),
-                        status_get_sbp_status(orb->status));
+                        STATUS_GET_RESPONSE(orb->status),
+                        STATUS_GET_SBP_STATUS(orb->status));
                goto out;
        }
 
        retval = 0;
  out:
        dma_unmap_single(device->card->device, orb->base.request_bus,
-                        sizeof orb->request, DMA_TO_DEVICE);
+                        sizeof(orb->request), DMA_TO_DEVICE);
        dma_unmap_single(device->card->device, orb->response_bus,
-                        sizeof orb->response, DMA_FROM_DEVICE);
+                        sizeof(orb->response), DMA_FROM_DEVICE);
 
        if (response)
                fw_memcpy_from_be32(response,
-                                   orb->response, sizeof orb->response);
+                                   orb->response, sizeof(orb->response));
        kfree(orb);
 
        return retval;
@@ -508,42 +506,44 @@ static int sbp2_agent_reset(struct fw_unit *unit)
        struct fw_transaction *t;
        static u32 zero;
 
-       t = kzalloc(sizeof *t, GFP_ATOMIC);
+       t = kzalloc(sizeof(*t), GFP_ATOMIC);
        if (t == NULL)
                return -ENOMEM;
 
        fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
                        sd->node_id, sd->generation, SCODE_400,
                        sd->command_block_agent_address + SBP2_AGENT_RESET,
-                       &zero, sizeof zero, complete_agent_reset_write, t);
+                       &zero, sizeof(zero), complete_agent_reset_write, t);
 
        return 0;
 }
 
-static int add_scsi_devices(struct fw_unit *unit);
-static void remove_scsi_devices(struct fw_unit *unit);
 static void sbp2_reconnect(struct work_struct *work);
+static struct scsi_host_template scsi_driver_template;
 
 static void
 release_sbp2_device(struct kref *kref)
 {
        struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref);
+       struct Scsi_Host *host =
+               container_of((void *)sd, struct Scsi_Host, hostdata[0]);
 
        sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation,
                                 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
 
-       remove_scsi_devices(sd->unit);
-
+       scsi_remove_host(host);
        fw_core_remove_address_handler(&sd->address_handler);
        fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id);
        put_device(&sd->unit->device);
-       kfree(sd);
+       scsi_host_put(host);
 }
 
 static void sbp2_login(struct work_struct *work)
 {
        struct sbp2_device *sd =
                container_of(work, struct sbp2_device, work.work);
+       struct Scsi_Host *host =
+               container_of((void *)sd, struct Scsi_Host, hostdata[0]);
        struct fw_unit *unit = sd->unit;
        struct fw_device *device = fw_device(unit->device.parent);
        struct sbp2_login_response response;
@@ -563,7 +563,6 @@ static void sbp2_login(struct work_struct *work)
                } else {
                        fw_error("failed to login to %s\n",
                                 unit->device.bus_id);
-                       remove_scsi_devices(unit);
                        kref_put(&sd->kref, release_sbp2_device);
                }
                return;
@@ -577,7 +576,7 @@ static void sbp2_login(struct work_struct *work)
        sd->command_block_agent_address =
                ((u64) (response.command_block_agent.high & 0xffff) << 32) |
                response.command_block_agent.low;
-       sd->login_id = login_response_get_login_id(response);
+       sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
 
        fw_notify("logged in to sbp2 unit %s (%d retries)\n",
                  unit->device.bus_id, sd->retries);
@@ -596,7 +595,9 @@ static void sbp2_login(struct work_struct *work)
        PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
        sbp2_agent_reset(unit);
 
-       retval = add_scsi_devices(unit);
+       /* FIXME: Loop over luns here. */
+       lun = 0;
+       retval = scsi_add_device(host, 0, 0, lun);
        if (retval < 0) {
                sbp2_send_management_orb(unit, sd->node_id, sd->generation,
                                         SBP2_LOGOUT_REQUEST, sd->login_id,
@@ -616,13 +617,16 @@ static int sbp2_probe(struct device *dev)
        struct fw_device *device = fw_device(unit->device.parent);
        struct sbp2_device *sd;
        struct fw_csr_iterator ci;
-       int i, key, value;
+       struct Scsi_Host *host;
+       int i, key, value, err;
        u32 model, firmware_revision;
 
-       sd = kzalloc(sizeof *sd, GFP_KERNEL);
-       if (sd == NULL)
-               return -ENOMEM;
+       err = -ENOMEM;
+       host = scsi_host_alloc(&scsi_driver_template, sizeof(*sd));
+       if (host == NULL)
+               goto fail;
 
+       sd = (struct sbp2_device *) host->hostdata;
        unit->device.driver_data = sd;
        sd->unit = unit;
        INIT_LIST_HEAD(&sd->orb_list);
@@ -632,17 +636,18 @@ static int sbp2_probe(struct device *dev)
        sd->address_handler.address_callback = sbp2_status_write;
        sd->address_handler.callback_data = sd;
 
-       if (fw_core_add_address_handler(&sd->address_handler,
-                                       &fw_high_memory_region) < 0) {
-               kfree(sd);
-               return -EBUSY;
-       }
+       err = fw_core_add_address_handler(&sd->address_handler,
+                                         &fw_high_memory_region);
+       if (err < 0)
+               goto fail_host;
 
-       if (fw_device_enable_phys_dma(device) < 0) {
-               fw_core_remove_address_handler(&sd->address_handler);
-               kfree(sd);
-               return -EBUSY;
-       }
+       err = fw_device_enable_phys_dma(device);
+       if (err < 0)
+               goto fail_address_handler;
+
+       err = scsi_add_host(host, &unit->device);
+       if (err < 0)
+               goto fail_address_handler;
 
        /*
         * Scan unit directory to get management agent address,
@@ -696,6 +701,13 @@ static int sbp2_probe(struct device *dev)
                kref_get(&sd->kref);
 
        return 0;
+
+ fail_address_handler:
+       fw_core_remove_address_handler(&sd->address_handler);
+ fail_host:
+       scsi_host_put(host);
+ fail:
+       return err;
 }
 
 static int sbp2_remove(struct device *dev)
@@ -828,10 +840,10 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
        int result;
 
        if (status != NULL) {
-               if (status_get_dead(*status))
+               if (STATUS_GET_DEAD(*status))
                        sbp2_agent_reset(unit);
 
-               switch (status_get_response(*status)) {
+               switch (STATUS_GET_RESPONSE(*status)) {
                case SBP2_STATUS_REQUEST_COMPLETE:
                        result = DID_OK << 16;
                        break;
@@ -845,8 +857,8 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
                        break;
                }
 
-               if (result == DID_OK << 16 && status_get_len(*status) > 1)
-                       result = sbp2_status_to_sense_data(status_get_data(*status),
+               if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
+                       result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
                                                           orb->cmd->sense_buffer);
        } else {
                /*
@@ -858,7 +870,7 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
        }
 
        dma_unmap_single(device->card->device, orb->base.request_bus,
-                        sizeof orb->request, DMA_TO_DEVICE);
+                        sizeof(orb->request), DMA_TO_DEVICE);
 
        if (orb->cmd->use_sg > 0) {
                sg = (struct scatterlist *)orb->cmd->request_buffer;
@@ -868,11 +880,11 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
 
        if (orb->page_table_bus != 0)
                dma_unmap_single(device->card->device, orb->page_table_bus,
-                                sizeof orb->page_table_bus, DMA_TO_DEVICE);
+                                sizeof(orb->page_table_bus), DMA_TO_DEVICE);
 
        if (orb->request_buffer_bus != 0)
                dma_unmap_single(device->card->device, orb->request_buffer_bus,
-                                sizeof orb->request_buffer_bus,
+                                sizeof(orb->request_buffer_bus),
                                 DMA_FROM_DEVICE);
 
        orb->cmd->result = result;
@@ -880,12 +892,12 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
        kfree(orb);
 }
 
-static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
+static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
 {
-       struct fw_unit *unit =
-               (struct fw_unit *)orb->cmd->device->host->hostdata[0];
+       struct sbp2_device *sd =
+               (struct sbp2_device *)orb->cmd->device->host->hostdata;
+       struct fw_unit *unit = sd->unit;
        struct fw_device *device = fw_device(unit->device.parent);
-       struct sbp2_device *sd = unit->device.driver_data;
        struct scatterlist *sg;
        int sg_len, l, i, j, count;
        size_t size;
@@ -894,6 +906,8 @@ static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
        sg = (struct scatterlist *)orb->cmd->request_buffer;
        count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
                           orb->cmd->sc_data_direction);
+       if (count == 0)
+               goto fail;
 
        /*
         * Handle the special case where there is only one element in
@@ -906,13 +920,16 @@ static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
                orb->request.data_descriptor.high = sd->address_high;
                orb->request.data_descriptor.low  = sg_dma_address(sg);
                orb->request.misc |=
-                       command_orb_data_size(sg_dma_len(sg));
-               return;
+                       COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
+               return 0;
        }
 
        /*
         * Convert the scatterlist to an sbp2 page table.  If any
-        * scatterlist entries are too big for sbp2 we split the as we go.
+        * scatterlist entries are too big for sbp2, we split them as we
+        * go.  Even if we ask the block I/O layer to not give us sg
+        * elements larger than 65535 bytes, some IOMMUs may merge sg elements
+        * during DMA mapping, and Linux currently doesn't prevent this.
         */
        for (i = 0, j = 0; i < count; i++) {
                sg_len = sg_dma_len(sg + i);
@@ -927,7 +944,7 @@ static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
                }
        }
 
-       size = sizeof orb->page_table[0] * j;
+       size = sizeof(orb->page_table[0]) * j;
 
        /*
         * The data_descriptor pointer is the one case where we need
@@ -940,45 +957,33 @@ static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
        orb->page_table_bus =
                dma_map_single(device->card->device, orb->page_table,
                               size, DMA_TO_DEVICE);
+       if (dma_mapping_error(orb->page_table_bus))
+               goto fail_page_table;
        orb->request.data_descriptor.high = sd->address_high;
        orb->request.data_descriptor.low  = orb->page_table_bus;
        orb->request.misc |=
-               command_orb_page_table_present |
-               command_orb_data_size(j);
+               COMMAND_ORB_PAGE_TABLE_PRESENT |
+               COMMAND_ORB_DATA_SIZE(j);
 
        fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
-}
 
-static void sbp2_command_orb_map_buffer(struct sbp2_command_orb *orb)
-{
-       struct fw_unit *unit =
-               (struct fw_unit *)orb->cmd->device->host->hostdata[0];
-       struct fw_device *device = fw_device(unit->device.parent);
-       struct sbp2_device *sd = unit->device.driver_data;
-
-       /*
-        * As for map_scatterlist, we need to fill in the high bits of
-        * the data_descriptor pointer.
-        */
+       return 0;
 
-       orb->request_buffer_bus =
-               dma_map_single(device->card->device,
-                              orb->cmd->request_buffer,
-                              orb->cmd->request_bufflen,
-                              orb->cmd->sc_data_direction);
-       orb->request.data_descriptor.high = sd->address_high;
-       orb->request.data_descriptor.low  = orb->request_buffer_bus;
-       orb->request.misc |=
-               command_orb_data_size(orb->cmd->request_bufflen);
+ fail_page_table:
+       dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
+                    orb->cmd->sc_data_direction);
+ fail:
+       return -ENOMEM;
 }
 
 /* SCSI stack integration */
 
 static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
 {
-       struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
+       struct sbp2_device *sd =
+               (struct sbp2_device *)cmd->device->host->hostdata;
+       struct fw_unit *unit = sd->unit;
        struct fw_device *device = fw_device(unit->device.parent);
-       struct sbp2_device *sd = unit->device.driver_data;
        struct sbp2_command_orb *orb;
 
        /*
@@ -987,10 +992,12 @@ static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
         */
        if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
                fw_error("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
-               goto fail_alloc;
+               cmd->result = DID_ERROR << 16;
+               done(cmd);
+               return 0;
        }
 
-       orb = kzalloc(sizeof *orb, GFP_ATOMIC);
+       orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
        if (orb == NULL) {
                fw_notify("failed to alloc orb\n");
                goto fail_alloc;
@@ -1000,7 +1007,7 @@ static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
        orb->base.rcode = -1;
        orb->base.request_bus =
                dma_map_single(device->card->device, &orb->request,
-                              sizeof orb->request, DMA_TO_DEVICE);
+                              sizeof(orb->request), DMA_TO_DEVICE);
        if (dma_mapping_error(orb->base.request_bus))
                goto fail_mapping;
 
@@ -1017,35 +1024,24 @@ static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
         * if we set this to max_speed + 7, we get the right value.
         */
        orb->request.misc =
-               command_orb_max_payload(device->node->max_speed + 7) |
-               command_orb_speed(device->node->max_speed) |
-               command_orb_notify;
+               COMMAND_ORB_MAX_PAYLOAD(device->node->max_speed + 7) |
+               COMMAND_ORB_SPEED(device->node->max_speed) |
+               COMMAND_ORB_NOTIFY;
 
        if (cmd->sc_data_direction == DMA_FROM_DEVICE)
                orb->request.misc |=
-                       command_orb_direction(SBP2_DIRECTION_FROM_MEDIA);
+                       COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA);
        else if (cmd->sc_data_direction == DMA_TO_DEVICE)
                orb->request.misc |=
-                       command_orb_direction(SBP2_DIRECTION_TO_MEDIA);
+                       COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA);
 
-       if (cmd->use_sg) {
-               sbp2_command_orb_map_scatterlist(orb);
-       } else if (cmd->request_bufflen > SBP2_MAX_SG_ELEMENT_LENGTH) {
-               /*
-                * FIXME: Need to split this into a sg list... but
-                * could we get the scsi or blk layer to do that by
-                * reporting our max supported block size?
-                */
-               fw_error("command > 64k\n");
-               goto fail_bufflen;
-       } else if (cmd->request_bufflen > 0) {
-               sbp2_command_orb_map_buffer(orb);
-       }
+       if (cmd->use_sg && sbp2_command_orb_map_scatterlist(orb) < 0)
+               goto fail_map_payload;
 
-       fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
+       fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
 
        memset(orb->request.command_block,
-              0, sizeof orb->request.command_block);
+              0, sizeof(orb->request.command_block));
        memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
 
        orb->base.callback = complete_command_orb;
@@ -1055,21 +1051,18 @@ static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
 
        return 0;
 
- fail_bufflen:
+ fail_map_payload:
        dma_unmap_single(device->card->device, orb->base.request_bus,
-                        sizeof orb->request, DMA_TO_DEVICE);
+                        sizeof(orb->request), DMA_TO_DEVICE);
  fail_mapping:
        kfree(orb);
  fail_alloc:
-       cmd->result = DID_ERROR << 16;
-       done(cmd);
-       return 0;
+       return SCSI_MLQUEUE_HOST_BUSY;
 }
 
 static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
 {
-       struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
-       struct sbp2_device *sd = unit->device.driver_data;
+       struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
 
        sdev->allow_restart = 1;
 
@@ -1080,8 +1073,8 @@ static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
 
 static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
 {
-       struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
-       struct sbp2_device *sd = unit->device.driver_data;
+       struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
+       struct fw_unit *unit = sd->unit;
 
        sdev->use_10_for_rw = 1;
 
@@ -1104,7 +1097,9 @@ static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
  */
 static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
 {
-       struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
+       struct sbp2_device *sd =
+               (struct sbp2_device *)cmd->device->host->hostdata;
+       struct fw_unit *unit = sd->unit;
 
        fw_notify("sbp2_scsi_abort\n");
        sbp2_agent_reset(unit);
@@ -1113,6 +1108,58 @@ static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
        return SUCCESS;
 }
 
+/*
+ * Format of /sys/bus/scsi/devices/.../ieee1394_id:
+ * u64 EUI-64 : u24 directory_ID : u16 LUN  (all printed in hexadecimal)
+ *
+ * This is the concatenation of target port identifier and logical unit
+ * identifier as per SAM-2...SAM-4 annex A.
+ */
+static ssize_t
+sbp2_sysfs_ieee1394_id_show(struct device *dev, struct device_attribute *attr,
+                           char *buf)
+{
+       struct scsi_device *sdev = to_scsi_device(dev);
+       struct sbp2_device *sd;
+       struct fw_unit *unit;
+       struct fw_device *device;
+       u32 directory_id;
+       struct fw_csr_iterator ci;
+       int key, value, lun;
+
+       if (!sdev)
+               return 0;
+       sd = (struct sbp2_device *)sdev->host->hostdata;
+       unit = sd->unit;
+       device = fw_device(unit->device.parent);
+
+       /* implicit directory ID */
+       directory_id = ((unit->directory - device->config_rom) * 4
+                       + CSR_CONFIG_ROM) & 0xffffff;
+
+       /* explicit directory ID, overrides implicit ID if present */
+       fw_csr_iterator_init(&ci, unit->directory);
+       while (fw_csr_iterator_next(&ci, &key, &value))
+               if (key == CSR_DIRECTORY_ID) {
+                       directory_id = value;
+                       break;
+               }
+
+       /* FIXME: Make this work for multi-lun devices. */
+       lun = 0;
+
+       return sprintf(buf, "%08x%08x:%06x:%04x\n",
+                       device->config_rom[3], device->config_rom[4],
+                       directory_id, lun);
+}
+
+static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
+
+static struct device_attribute *sbp2_scsi_sysfs_attrs[] = {
+       &dev_attr_ieee1394_id,
+       NULL
+};
+
 static struct scsi_host_template scsi_driver_template = {
        .module                 = THIS_MODULE,
        .name                   = "SBP-2 IEEE-1394",
@@ -1126,62 +1173,19 @@ static struct scsi_host_template scsi_driver_template = {
        .use_clustering         = ENABLE_CLUSTERING,
        .cmd_per_lun            = 1,
        .can_queue              = 1,
+       .sdev_attrs             = sbp2_scsi_sysfs_attrs,
 };
 
-static int add_scsi_devices(struct fw_unit *unit)
-{
-       struct sbp2_device *sd = unit->device.driver_data;
-       int retval, lun;
-
-       if (sd->scsi_host != NULL)
-               return 0;
-
-       sd->scsi_host = scsi_host_alloc(&scsi_driver_template,
-                                       sizeof(unsigned long));
-       if (sd->scsi_host == NULL) {
-               fw_error("failed to register scsi host\n");
-               return -1;
-       }
-
-       sd->scsi_host->hostdata[0] = (unsigned long)unit;
-       retval = scsi_add_host(sd->scsi_host, &unit->device);
-       if (retval < 0) {
-               fw_error("failed to add scsi host\n");
-               scsi_host_put(sd->scsi_host);
-               sd->scsi_host = NULL;
-               return retval;
-       }
-
-       /* FIXME: Loop over luns here. */
-       lun = 0;
-       retval = scsi_add_device(sd->scsi_host, 0, 0, lun);
-       if (retval < 0) {
-               fw_error("failed to add scsi device\n");
-               scsi_remove_host(sd->scsi_host);
-               scsi_host_put(sd->scsi_host);
-               sd->scsi_host = NULL;
-               return retval;
-       }
-
-       return 0;
-}
-
-static void remove_scsi_devices(struct fw_unit *unit)
-{
-       struct sbp2_device *sd = unit->device.driver_data;
-
-       if (sd->scsi_host != NULL) {
-               scsi_remove_host(sd->scsi_host);
-               scsi_host_put(sd->scsi_host);
-       }
-       sd->scsi_host = NULL;
-}
-
 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
 MODULE_DESCRIPTION("SCSI over IEEE1394");
 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)
 {
        return driver_register(&sbp2_driver.driver);