return result;
 }
 
+static struct ibmvfc_cmd *ibmvfc_init_vfc_cmd(struct ibmvfc_event *evt, struct scsi_device *sdev)
+{
+       struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
+       struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
+       size_t offset = offsetof(struct ibmvfc_cmd, rsp);
+
+       memset(vfc_cmd, 0, sizeof(*vfc_cmd));
+       vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offset);
+       vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
+       vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
+       vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
+       vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
+       vfc_cmd->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
+       vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
+       int_to_scsilun(sdev->lun, &vfc_cmd->iu.lun);
+
+       return vfc_cmd;
+}
+
 /**
  * ibmvfc_queuecommand - The queuecommand function of the scsi template
  * @cmnd:      struct scsi_cmnd to be executed
        ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
        evt->cmnd = cmnd;
        cmnd->scsi_done = done;
-       vfc_cmd = &evt->iu.cmd;
-       memset(vfc_cmd, 0, sizeof(*vfc_cmd));
-       vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
-       vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
-       vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
-       vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
-       vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
-       vfc_cmd->cancel_key = cpu_to_be32((unsigned long)cmnd->device->hostdata);
-       vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
+
+       vfc_cmd = ibmvfc_init_vfc_cmd(evt, cmnd->device);
+
        vfc_cmd->iu.xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
-       int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
        memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
 
        if (cmnd->flags & SCMD_TAGGED) {
 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
 {
        struct ibmvfc_host *vhost = shost_priv(sdev->host);
-       struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
        struct ibmvfc_cmd *tmf;
        struct ibmvfc_event *evt = NULL;
        union ibmvfc_iu rsp_iu;
        if (vhost->state == IBMVFC_ACTIVE) {
                evt = ibmvfc_get_event(vhost);
                ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
+               tmf = ibmvfc_init_vfc_cmd(evt, sdev);
 
-               tmf = &evt->iu.cmd;
-               memset(tmf, 0, sizeof(*tmf));
-               tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
-               tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
-               tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
-               tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
-               tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
-               tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
-               tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
-               int_to_scsilun(sdev->lun, &tmf->iu.lun);
                tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
                tmf->iu.tmf_flags = type;
                evt->sync_iu = &rsp_iu;
 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
 {
        struct ibmvfc_host *vhost = shost_priv(sdev->host);
-       struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
        struct ibmvfc_cmd *tmf;
        struct ibmvfc_event *evt, *found_evt;
        union ibmvfc_iu rsp_iu;
        if (vhost->state == IBMVFC_ACTIVE) {
                evt = ibmvfc_get_event(vhost);
                ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
+               tmf = ibmvfc_init_vfc_cmd(evt, sdev);
 
-               tmf = &evt->iu.cmd;
-               memset(tmf, 0, sizeof(*tmf));
-               tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
-               tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
-               tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
-               tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
-               tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
-               tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
-               tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
-               int_to_scsilun(sdev->lun, &tmf->iu.lun);
                tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
                tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
                evt->sync_iu = &rsp_iu;