ieee1394: sbp2: check for DMA mapping failures
[pandora-kernel.git] / drivers / ieee1394 / sbp2.c
index a5ceff2..0037305 100644 (file)
@@ -186,6 +186,11 @@ MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
  * - delay inquiry
  *   Wait extra SBP2_INQUIRY_DELAY seconds after login before SCSI inquiry.
  *
+ * - power condition
+ *   Set the power condition field in the START STOP UNIT commands sent by
+ *   sd_mod on suspend, resume, and shutdown (if manage_start_stop is on).
+ *   Some disks need this to spin down or to resume properly.
+ *
  * - override internal blacklist
  *   Instead of adding to the built-in blacklist, use only the workarounds
  *   specified in the module load parameter.
@@ -199,6 +204,8 @@ MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
        ", skip mode page 8 = "   __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
        ", fix capacity = "       __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
        ", delay inquiry = "      __stringify(SBP2_WORKAROUND_DELAY_INQUIRY)
+       ", set power condition in start stop unit = "
+                                 __stringify(SBP2_WORKAROUND_POWER_CONDITION)
        ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
        ", or a combination)");
 
@@ -359,18 +366,25 @@ static const struct {
                .firmware_revision      = 0x002800,
                .model_id               = 0x001010,
                .workarounds            = SBP2_WORKAROUND_INQUIRY_36 |
-                                         SBP2_WORKAROUND_MODE_SENSE_8,
+                                         SBP2_WORKAROUND_MODE_SENSE_8 |
+                                         SBP2_WORKAROUND_POWER_CONDITION,
        },
        /* DViCO Momobay FX-3A with TSB42AA9A bridge */ {
                .firmware_revision      = 0x002800,
                .model_id               = 0x000000,
-               .workarounds            = SBP2_WORKAROUND_DELAY_INQUIRY,
+               .workarounds            = SBP2_WORKAROUND_DELAY_INQUIRY |
+                                         SBP2_WORKAROUND_POWER_CONDITION,
        },
        /* Initio bridges, actually only needed for some older ones */ {
                .firmware_revision      = 0x000200,
                .model_id               = SBP2_ROM_VALUE_WILDCARD,
                .workarounds            = SBP2_WORKAROUND_INQUIRY_36,
        },
+       /* PL-3507 bridge with Prolific firmware */ {
+               .firmware_revision      = 0x012800,
+               .model_id               = SBP2_ROM_VALUE_WILDCARD,
+               .workarounds            = SBP2_WORKAROUND_POWER_CONDITION,
+       },
        /* Symbios bridge */ {
                .firmware_revision      = 0xa0b800,
                .model_id               = SBP2_ROM_VALUE_WILDCARD,
@@ -512,26 +526,41 @@ static void sbp2util_write_doorbell(struct work_struct *work)
 
 static int sbp2util_create_command_orb_pool(struct sbp2_lu *lu)
 {
-       struct sbp2_fwhost_info *hi = lu->hi;
        struct sbp2_command_info *cmd;
+       struct device *dmadev = lu->hi->host->device.parent;
        int i, orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
 
        for (i = 0; i < orbs; i++) {
                cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
                if (!cmd)
-                       return -ENOMEM;
-               cmd->command_orb_dma = dma_map_single(hi->host->device.parent,
-                                               &cmd->command_orb,
-                                               sizeof(struct sbp2_command_orb),
-                                               DMA_TO_DEVICE);
-               cmd->sge_dma = dma_map_single(hi->host->device.parent,
-                                       &cmd->scatter_gather_element,
-                                       sizeof(cmd->scatter_gather_element),
-                                       DMA_TO_DEVICE);
+                       goto failed_alloc;
+
+               cmd->command_orb_dma =
+                   dma_map_single(dmadev, &cmd->command_orb,
+                                  sizeof(struct sbp2_command_orb),
+                                  DMA_TO_DEVICE);
+               if (dma_mapping_error(dmadev, cmd->command_orb_dma))
+                       goto failed_orb;
+
+               cmd->sge_dma =
+                   dma_map_single(dmadev, &cmd->scatter_gather_element,
+                                  sizeof(cmd->scatter_gather_element),
+                                  DMA_TO_DEVICE);
+               if (dma_mapping_error(dmadev, cmd->sge_dma))
+                       goto failed_sge;
+
                INIT_LIST_HEAD(&cmd->list);
                list_add_tail(&cmd->list, &lu->cmd_orb_completed);
        }
        return 0;
+
+failed_sge:
+       dma_unmap_single(dmadev, cmd->command_orb_dma,
+                        sizeof(struct sbp2_command_orb), DMA_TO_DEVICE);
+failed_orb:
+       kfree(cmd);
+failed_alloc:
+       return -ENOMEM;
 }
 
 static void sbp2util_remove_command_orb_pool(struct sbp2_lu *lu,
@@ -717,15 +746,26 @@ static int sbp2_update(struct unit_directory *ud)
 {
        struct sbp2_lu *lu = ud->device.driver_data;
 
-       if (sbp2_reconnect_device(lu)) {
-               /* Reconnect has failed. Perhaps we didn't reconnect fast
-                * enough. Try a regular login, but first log out just in
-                * case of any weirdness. */
+       if (sbp2_reconnect_device(lu) != 0) {
+               /*
+                * Reconnect failed.  If another bus reset happened,
+                * let nodemgr proceed and call sbp2_update again later
+                * (or sbp2_remove if this node went away).
+                */
+               if (!hpsb_node_entry_valid(lu->ne))
+                       return 0;
+               /*
+                * Or the target rejected the reconnect because we weren't
+                * fast enough.  Try a regular login, but first log out
+                * just in case of any weirdness.
+                */
                sbp2_logout_device(lu);
 
-               if (sbp2_login_device(lu)) {
-                       /* Login failed too, just fail, and the backend
-                        * will call our sbp2_remove for us */
+               if (sbp2_login_device(lu) != 0) {
+                       if (!hpsb_node_entry_valid(lu->ne))
+                               return 0;
+
+                       /* Maybe another initiator won the login. */
                        SBP2_ERR("Failed to reconnect to sbp2 device!");
                        return -EBUSY;
                }
@@ -1469,14 +1509,16 @@ static int sbp2_agent_reset(struct sbp2_lu *lu, int wait)
        return 0;
 }
 
-static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
-                                    struct sbp2_fwhost_info *hi,
-                                    struct sbp2_command_info *cmd,
-                                    unsigned int scsi_use_sg,
-                                    struct scatterlist *sg,
-                                    u32 orb_direction,
-                                    enum dma_data_direction dma_dir)
+static int sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
+                                   struct sbp2_fwhost_info *hi,
+                                   struct sbp2_command_info *cmd,
+                                   unsigned int scsi_use_sg,
+                                   struct scatterlist *sg,
+                                   u32 orb_direction,
+                                   enum dma_data_direction dma_dir)
 {
+       struct device *dmadev = hi->host->device.parent;
+
        cmd->dma_dir = dma_dir;
        orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
        orb->misc |= ORB_SET_DIRECTION(orb_direction);
@@ -1486,9 +1528,12 @@ static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
 
                cmd->dma_size = sg->length;
                cmd->dma_type = CMD_DMA_PAGE;
-               cmd->cmd_dma = dma_map_page(hi->host->device.parent,
-                                           sg_page(sg), sg->offset,
+               cmd->cmd_dma = dma_map_page(dmadev, sg_page(sg), sg->offset,
                                            cmd->dma_size, cmd->dma_dir);
+               if (dma_mapping_error(dmadev, cmd->cmd_dma)) {
+                       cmd->cmd_dma = 0;
+                       return -ENOMEM;
+               }
 
                orb->data_descriptor_lo = cmd->cmd_dma;
                orb->misc |= ORB_SET_DATA_SIZE(cmd->dma_size);
@@ -1498,8 +1543,7 @@ static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
                                                &cmd->scatter_gather_element[0];
                u32 sg_count, sg_len;
                dma_addr_t sg_addr;
-               int i, count = dma_map_sg(hi->host->device.parent, sg,
-                                         scsi_use_sg, dma_dir);
+               int i, count = dma_map_sg(dmadev, sg, scsi_use_sg, dma_dir);
 
                cmd->dma_size = scsi_use_sg;
                cmd->sge_buffer = sg;
@@ -1508,6 +1552,10 @@ static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
                orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
                orb->data_descriptor_lo = cmd->sge_dma;
 
+               dma_sync_single_for_cpu(dmadev, cmd->sge_dma,
+                                       sizeof(cmd->scatter_gather_element),
+                                       DMA_TO_DEVICE);
+
                /* loop through and fill out our SBP-2 page tables
                 * (and split up anything too large) */
                for (i = 0, sg_count = 0; i < count; i++, sg = sg_next(sg)) {
@@ -1534,19 +1582,27 @@ static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
                sbp2util_cpu_to_be32_buffer(sg_element,
                                (sizeof(struct sbp2_unrestricted_page_table)) *
                                sg_count);
+
+               dma_sync_single_for_device(dmadev, cmd->sge_dma,
+                                          sizeof(cmd->scatter_gather_element),
+                                          DMA_TO_DEVICE);
        }
+       return 0;
 }
 
-static void sbp2_create_command_orb(struct sbp2_lu *lu,
-                                   struct sbp2_command_info *cmd,
-                                   struct scsi_cmnd *SCpnt)
+static int sbp2_create_command_orb(struct sbp2_lu *lu,
+                                  struct sbp2_command_info *cmd,
+                                  struct scsi_cmnd *SCpnt)
 {
-       struct sbp2_fwhost_info *hi = lu->hi;
+       struct device *dmadev = lu->hi->host->device.parent;
        struct sbp2_command_orb *orb = &cmd->command_orb;
-       u32 orb_direction;
        unsigned int scsi_request_bufflen = scsi_bufflen(SCpnt);
        enum dma_data_direction dma_dir = SCpnt->sc_data_direction;
+       u32 orb_direction;
+       int ret;
 
+       dma_sync_single_for_cpu(dmadev, cmd->command_orb_dma,
+                               sizeof(struct sbp2_command_orb), DMA_TO_DEVICE);
        /*
         * Set-up our command ORB.
         *
@@ -1577,15 +1633,21 @@ static void sbp2_create_command_orb(struct sbp2_lu *lu,
                orb->data_descriptor_hi = 0x0;
                orb->data_descriptor_lo = 0x0;
                orb->misc |= ORB_SET_DIRECTION(1);
-       } else
-               sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_sg_count(SCpnt),
-                                        scsi_sglist(SCpnt),
-                                        orb_direction, dma_dir);
-
+               ret = 0;
+       } else {
+               ret = sbp2_prep_command_orb_sg(orb, lu->hi, cmd,
+                                              scsi_sg_count(SCpnt),
+                                              scsi_sglist(SCpnt),
+                                              orb_direction, dma_dir);
+       }
        sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
 
        memset(orb->cdb, 0, sizeof(orb->cdb));
        memcpy(orb->cdb, SCpnt->cmnd, SCpnt->cmd_len);
+
+       dma_sync_single_for_device(dmadev, cmd->command_orb_dma,
+                       sizeof(struct sbp2_command_orb), DMA_TO_DEVICE);
+       return ret;
 }
 
 static void sbp2_link_orb_command(struct sbp2_lu *lu,
@@ -1599,14 +1661,6 @@ static void sbp2_link_orb_command(struct sbp2_lu *lu,
        size_t length;
        unsigned long flags;
 
-       dma_sync_single_for_device(hi->host->device.parent,
-                                  cmd->command_orb_dma,
-                                  sizeof(struct sbp2_command_orb),
-                                  DMA_TO_DEVICE);
-       dma_sync_single_for_device(hi->host->device.parent, cmd->sge_dma,
-                                  sizeof(cmd->scatter_gather_element),
-                                  DMA_TO_DEVICE);
-
        /* check to see if there are any previous orbs to use */
        spin_lock_irqsave(&lu->cmd_orb_lock, flags);
        last_orb = lu->last_orb;
@@ -1674,9 +1728,10 @@ static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
        if (!cmd)
                return -EIO;
 
-       sbp2_create_command_orb(lu, cmd, SCpnt);
-       sbp2_link_orb_command(lu, cmd);
+       if (sbp2_create_command_orb(lu, cmd, SCpnt))
+               return -ENOMEM;
 
+       sbp2_link_orb_command(lu, cmd);
        return 0;
 }
 
@@ -1764,13 +1819,6 @@ static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
        else
                cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
        if (cmd) {
-               dma_sync_single_for_cpu(hi->host->device.parent,
-                                       cmd->command_orb_dma,
-                                       sizeof(struct sbp2_command_orb),
-                                       DMA_TO_DEVICE);
-               dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
-                                       sizeof(cmd->scatter_gather_element),
-                                       DMA_TO_DEVICE);
                /* Grab SCSI command pointers and check status. */
                /*
                 * FIXME: If the src field in the status is 1, the ORB DMA must
@@ -1887,7 +1935,6 @@ done:
 
 static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
 {
-       struct sbp2_fwhost_info *hi = lu->hi;
        struct list_head *lh;
        struct sbp2_command_info *cmd;
        unsigned long flags;
@@ -1896,13 +1943,6 @@ static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
        while (!list_empty(&lu->cmd_orb_inuse)) {
                lh = lu->cmd_orb_inuse.next;
                cmd = list_entry(lh, struct sbp2_command_info, list);
-               dma_sync_single_for_cpu(hi->host->device.parent,
-                                       cmd->command_orb_dma,
-                                       sizeof(struct sbp2_command_orb),
-                                       DMA_TO_DEVICE);
-               dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
-                                       sizeof(cmd->scatter_gather_element),
-                                       DMA_TO_DEVICE);
                sbp2util_mark_command_completed(lu, cmd);
                if (cmd->Current_SCpnt) {
                        cmd->Current_SCpnt->result = status << 16;
@@ -1995,6 +2035,8 @@ static int sbp2scsi_slave_configure(struct scsi_device *sdev)
 
        sdev->use_10_for_rw = 1;
 
+       if (sbp2_exclusive_login)
+               sdev->manage_start_stop = 1;
        if (sdev->type == TYPE_ROM)
                sdev->use_10_for_ms = 1;
        if (sdev->type == TYPE_DISK &&
@@ -2002,6 +2044,8 @@ static int sbp2scsi_slave_configure(struct scsi_device *sdev)
                sdev->skip_ms_page_8 = 1;
        if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
                sdev->fix_capacity = 1;
+       if (lu->workarounds & SBP2_WORKAROUND_POWER_CONDITION)
+               sdev->start_stop_pwr_cond = 1;
        if (lu->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
                blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512);
        return 0;
@@ -2020,7 +2064,6 @@ static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
 static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
 {
        struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
-       struct sbp2_fwhost_info *hi = lu->hi;
        struct sbp2_command_info *cmd;
        unsigned long flags;
 
@@ -2034,14 +2077,6 @@ static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
                spin_lock_irqsave(&lu->cmd_orb_lock, flags);
                cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
                if (cmd) {
-                       dma_sync_single_for_cpu(hi->host->device.parent,
-                                       cmd->command_orb_dma,
-                                       sizeof(struct sbp2_command_orb),
-                                       DMA_TO_DEVICE);
-                       dma_sync_single_for_cpu(hi->host->device.parent,
-                                       cmd->sge_dma,
-                                       sizeof(cmd->scatter_gather_element),
-                                       DMA_TO_DEVICE);
                        sbp2util_mark_command_completed(lu, cmd);
                        if (cmd->Current_SCpnt) {
                                cmd->Current_SCpnt->result = DID_ABORT << 16;