scsi: hisi_sas: Drop hisi_sas_slot_abort()
[linux-2.6-microblaze.git] / drivers / scsi / hisi_sas / hisi_sas_main.c
index 49c1fa6..ed671af 100644 (file)
@@ -24,6 +24,9 @@ hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
                                void *funcdata);
+static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
+                                 struct domain_device *device);
+static void hisi_sas_dev_gone(struct domain_device *device);
 
 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
 {
@@ -78,22 +81,23 @@ u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
        case ATA_CMD_STANDBYNOW1:
        case ATA_CMD_ZAC_MGMT_OUT:
                return HISI_SAS_SATA_PROTOCOL_NONDATA;
+
+       case ATA_CMD_SET_MAX:
+               switch (fis->features) {
+               case ATA_SET_MAX_PASSWD:
+               case ATA_SET_MAX_LOCK:
+                       return HISI_SAS_SATA_PROTOCOL_PIO;
+
+               case ATA_SET_MAX_PASSWD_DMA:
+               case ATA_SET_MAX_UNLOCK_DMA:
+                       return HISI_SAS_SATA_PROTOCOL_DMA;
+
+               default:
+                       return HISI_SAS_SATA_PROTOCOL_NONDATA;
+               }
+
        default:
        {
-               if (fis->command == ATA_CMD_SET_MAX) {
-                       switch (fis->features) {
-                       case ATA_SET_MAX_PASSWD:
-                       case ATA_SET_MAX_LOCK:
-                               return HISI_SAS_SATA_PROTOCOL_PIO;
-
-                       case ATA_SET_MAX_PASSWD_DMA:
-                       case ATA_SET_MAX_UNLOCK_DMA:
-                               return HISI_SAS_SATA_PROTOCOL_DMA;
-
-                       default:
-                               return HISI_SAS_SATA_PROTOCOL_NONDATA;
-                       }
-               }
                if (direction == DMA_NONE)
                        return HISI_SAS_SATA_PROTOCOL_NONDATA;
                return HISI_SAS_SATA_PROTOCOL_PIO;
@@ -134,6 +138,22 @@ int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
 }
 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
 
+/*
+ * This function assumes linkrate mask fits in 8 bits, which it
+ * does for all HW versions supported.
+ */
+u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
+{
+       u16 rate = 0;
+       int i;
+
+       max -= SAS_LINK_RATE_1_5_GBPS;
+       for (i = 0; i <= max; i++)
+               rate |= 1 << (i * 2);
+       return rate;
+}
+EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
+
 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
 {
        return device->port->ha->lldd_ha;
@@ -178,11 +198,18 @@ static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
        unsigned int index;
        void *bitmap = hisi_hba->slot_index_tags;
 
-       index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
-       if (index >= hisi_hba->slot_index_count)
-               return -SAS_QUEUE_FULL;
+       index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
+                       hisi_hba->last_slot_index + 1);
+       if (index >= hisi_hba->slot_index_count) {
+               index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
+                                          0);
+               if (index >= hisi_hba->slot_index_count)
+                       return -SAS_QUEUE_FULL;
+       }
        hisi_sas_slot_index_set(hisi_hba, index);
        *slot_idx = index;
+       hisi_hba->last_slot_index = index;
+
        return 0;
 }
 
@@ -197,6 +224,8 @@ static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
                             struct hisi_sas_slot *slot)
 {
+       struct hisi_sas_dq *dq = &hisi_hba->dq[slot->dlvry_queue];
+       unsigned long flags;
 
        if (task) {
                struct device *dev = hisi_hba->dev;
@@ -213,97 +242,63 @@ void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
                                             task->data_dir);
        }
 
-       if (slot->buf)
-               dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
 
+       spin_lock_irqsave(&dq->lock, flags);
        list_del_init(&slot->entry);
-       slot->buf = NULL;
-       slot->task = NULL;
-       slot->port = NULL;
-       hisi_sas_slot_index_free(hisi_hba, slot->idx);
+       spin_unlock_irqrestore(&dq->lock, flags);
+
+       memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
 
-       /* slot memory is fully zeroed when it is reused */
+       spin_lock_irqsave(&hisi_hba->lock, flags);
+       hisi_sas_slot_index_free(hisi_hba, slot->idx);
+       spin_unlock_irqrestore(&hisi_hba->lock, flags);
 }
 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
 
-static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
+static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
                                  struct hisi_sas_slot *slot)
 {
-       return hisi_hba->hw->prep_smp(hisi_hba, slot);
+       hisi_hba->hw->prep_smp(hisi_hba, slot);
 }
 
-static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
-                                 struct hisi_sas_slot *slot, int is_tmf,
-                                 struct hisi_sas_tmf_task *tmf)
+static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
+                                 struct hisi_sas_slot *slot)
 {
-       return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
+       hisi_hba->hw->prep_ssp(hisi_hba, slot);
 }
 
-static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
+static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
                                  struct hisi_sas_slot *slot)
 {
-       return hisi_hba->hw->prep_stp(hisi_hba, slot);
+       hisi_hba->hw->prep_stp(hisi_hba, slot);
 }
 
-static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
+static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
                struct hisi_sas_slot *slot,
                int device_id, int abort_flag, int tag_to_abort)
 {
-       return hisi_hba->hw->prep_abort(hisi_hba, slot,
+       hisi_hba->hw->prep_abort(hisi_hba, slot,
                        device_id, abort_flag, tag_to_abort);
 }
 
-/*
- * This function will issue an abort TMF regardless of whether the
- * task is in the sdev or not. Then it will do the task complete
- * cleanup and callbacks.
- */
-static void hisi_sas_slot_abort(struct work_struct *work)
+static int hisi_sas_task_prep(struct sas_task *task,
+                             struct hisi_sas_dq **dq_pointer,
+                             bool is_tmf, struct hisi_sas_tmf_task *tmf,
+                             int *pass)
 {
-       struct hisi_sas_slot *abort_slot =
-               container_of(work, struct hisi_sas_slot, abort_slot);
-       struct sas_task *task = abort_slot->task;
-       struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
-       struct scsi_cmnd *cmnd = task->uldd_task;
-       struct hisi_sas_tmf_task tmf_task;
-       struct scsi_lun lun;
-       struct device *dev = hisi_hba->dev;
-       int tag = abort_slot->idx;
-       unsigned long flags;
-
-       if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
-               dev_err(dev, "cannot abort slot for non-ssp task\n");
-               goto out;
-       }
-
-       int_to_scsilun(cmnd->device->lun, &lun);
-       tmf_task.tmf = TMF_ABORT_TASK;
-       tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
-
-       hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
-out:
-       /* Do cleanup for this task */
-       spin_lock_irqsave(&hisi_hba->lock, flags);
-       hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
-       spin_unlock_irqrestore(&hisi_hba->lock, flags);
-       if (task->task_done)
-               task->task_done(task);
-}
-
-static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
-               *dq, int is_tmf, struct hisi_sas_tmf_task *tmf,
-               int *pass)
-{
-       struct hisi_hba *hisi_hba = dq->hisi_hba;
        struct domain_device *device = task->dev;
+       struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
        struct hisi_sas_device *sas_dev = device->lldd_dev;
        struct hisi_sas_port *port;
        struct hisi_sas_slot *slot;
        struct hisi_sas_cmd_hdr *cmd_hdr_base;
        struct asd_sas_port *sas_port = device->port;
        struct device *dev = hisi_hba->dev;
-       int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
-       unsigned long flags;
+       int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
+       int n_elem = 0, n_elem_req = 0, n_elem_resp = 0;
+       unsigned long flags, flags_dq;
+       struct hisi_sas_dq *dq;
+       int wr_q_index;
 
        if (!sas_port) {
                struct task_status_struct *ts = &task->task_status;
@@ -330,6 +325,8 @@ static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
                return -ECOMM;
        }
 
+       *dq_pointer = dq = sas_dev->dq;
+
        port = to_hisi_sas_port(sas_port);
        if (port && !port->port_attached) {
                dev_info(dev, "task prep: %s port%d not attach device\n",
@@ -341,6 +338,8 @@ static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
        }
 
        if (!sas_protocol_ata(task->task_proto)) {
+               unsigned int req_len, resp_len;
+
                if (task->num_scatter) {
                        n_elem = dma_map_sg(dev, task->scatter,
                                            task->num_scatter, task->data_dir);
@@ -348,32 +347,66 @@ static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
                                rc = -ENOMEM;
                                goto prep_out;
                        }
+               } else if (task->task_proto & SAS_PROTOCOL_SMP) {
+                       n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
+                                               1, DMA_TO_DEVICE);
+                       if (!n_elem_req) {
+                               rc = -ENOMEM;
+                               goto prep_out;
+                       }
+                       req_len = sg_dma_len(&task->smp_task.smp_req);
+                       if (req_len & 0x3) {
+                               rc = -EINVAL;
+                               goto err_out_dma_unmap;
+                       }
+                       n_elem_resp = dma_map_sg(dev, &task->smp_task.smp_resp,
+                                                1, DMA_FROM_DEVICE);
+                       if (!n_elem_resp) {
+                               rc = -ENOMEM;
+                               goto err_out_dma_unmap;
+                       }
+                       resp_len = sg_dma_len(&task->smp_task.smp_resp);
+                       if (resp_len & 0x3) {
+                               rc = -EINVAL;
+                               goto err_out_dma_unmap;
+                       }
                }
        } else
                n_elem = task->num_scatter;
 
+       if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
+               dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
+                       n_elem);
+               rc = -EINVAL;
+               goto err_out_dma_unmap;
+       }
+
        spin_lock_irqsave(&hisi_hba->lock, flags);
        if (hisi_hba->hw->slot_index_alloc)
                rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
                                                    device);
        else
                rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
-       if (rc) {
-               spin_unlock_irqrestore(&hisi_hba->lock, flags);
-               goto err_out;
-       }
        spin_unlock_irqrestore(&hisi_hba->lock, flags);
-
-       rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
        if (rc)
+               goto err_out_dma_unmap;
+
+       slot = &hisi_hba->slot_info[slot_idx];
+
+       spin_lock_irqsave(&dq->lock, flags_dq);
+       wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
+       if (wr_q_index < 0) {
+               spin_unlock_irqrestore(&dq->lock, flags_dq);
+               rc = -EAGAIN;
                goto err_out_tag;
+       }
+
+       list_add_tail(&slot->delivery, &dq->list);
+       spin_unlock_irqrestore(&dq->lock, flags_dq);
 
        dlvry_queue = dq->id;
-       dlvry_queue_slot = dq->wr_point;
-       slot = &hisi_hba->slot_info[slot_idx];
-       memset(slot, 0, sizeof(struct hisi_sas_slot));
+       dlvry_queue_slot = wr_q_index;
 
-       slot->idx = slot_idx;
        slot->n_elem = n_elem;
        slot->dlvry_queue = dlvry_queue;
        slot->dlvry_queue_slot = dlvry_queue_slot;
@@ -381,99 +414,95 @@ static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
        slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
        slot->task = task;
        slot->port = port;
+       slot->tmf = tmf;
+       slot->is_internal = is_tmf;
        task->lldd_task = slot;
-       INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
 
-       slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
-                                  GFP_ATOMIC, &slot->buf_dma);
-       if (!slot->buf) {
-               rc = -ENOMEM;
-               goto err_out_slot_buf;
-       }
        memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
        memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
        memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
 
        switch (task->task_proto) {
        case SAS_PROTOCOL_SMP:
-               rc = hisi_sas_task_prep_smp(hisi_hba, slot);
+               hisi_sas_task_prep_smp(hisi_hba, slot);
                break;
        case SAS_PROTOCOL_SSP:
-               rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
+               hisi_sas_task_prep_ssp(hisi_hba, slot);
                break;
        case SAS_PROTOCOL_SATA:
        case SAS_PROTOCOL_STP:
        case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
-               rc = hisi_sas_task_prep_ata(hisi_hba, slot);
+               hisi_sas_task_prep_ata(hisi_hba, slot);
                break;
        default:
                dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
                        task->task_proto);
-               rc = -EINVAL;
                break;
        }
 
-       if (rc) {
-               dev_err(dev, "task prep: rc = 0x%x\n", rc);
-               goto err_out_buf;
-       }
-
-       spin_lock_irqsave(&hisi_hba->lock, flags);
+       spin_lock_irqsave(&dq->lock, flags);
        list_add_tail(&slot->entry, &sas_dev->list);
-       spin_unlock_irqrestore(&hisi_hba->lock, flags);
+       spin_unlock_irqrestore(&dq->lock, flags);
        spin_lock_irqsave(&task->task_state_lock, flags);
        task->task_state_flags |= SAS_TASK_AT_INITIATOR;
        spin_unlock_irqrestore(&task->task_state_lock, flags);
 
-       dq->slot_prep = slot;
        ++(*pass);
+       slot->ready = 1;
 
        return 0;
 
-err_out_buf:
-       dma_pool_free(hisi_hba->buffer_pool, slot->buf,
-               slot->buf_dma);
-err_out_slot_buf:
-       /* Nothing to be done */
 err_out_tag:
        spin_lock_irqsave(&hisi_hba->lock, flags);
        hisi_sas_slot_index_free(hisi_hba, slot_idx);
        spin_unlock_irqrestore(&hisi_hba->lock, flags);
-err_out:
-       dev_err(dev, "task prep: failed[%d]!\n", rc);
-       if (!sas_protocol_ata(task->task_proto))
-               if (n_elem)
-                       dma_unmap_sg(dev, task->scatter,
-                                    task->num_scatter,
-                                    task->data_dir);
+err_out_dma_unmap:
+       if (!sas_protocol_ata(task->task_proto)) {
+               if (task->num_scatter) {
+                       dma_unmap_sg(dev, task->scatter, task->num_scatter,
+                            task->data_dir);
+               } else if (task->task_proto & SAS_PROTOCOL_SMP) {
+                       if (n_elem_req)
+                               dma_unmap_sg(dev, &task->smp_task.smp_req,
+                                            1, DMA_TO_DEVICE);
+                       if (n_elem_resp)
+                               dma_unmap_sg(dev, &task->smp_task.smp_resp,
+                                            1, DMA_FROM_DEVICE);
+               }
+       }
 prep_out:
+       dev_err(dev, "task prep: failed[%d]!\n", rc);
        return rc;
 }
 
 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
-                             int is_tmf, struct hisi_sas_tmf_task *tmf)
+                             bool is_tmf, struct hisi_sas_tmf_task *tmf)
 {
        u32 rc;
        u32 pass = 0;
        unsigned long flags;
        struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
        struct device *dev = hisi_hba->dev;
-       struct domain_device *device = task->dev;
-       struct hisi_sas_device *sas_dev = device->lldd_dev;
-       struct hisi_sas_dq *dq = sas_dev->dq;
+       struct hisi_sas_dq *dq = NULL;
 
-       if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
-               return -EINVAL;
+       if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
+               if (in_softirq())
+                       return -EINVAL;
+
+               down(&hisi_hba->sem);
+               up(&hisi_hba->sem);
+       }
 
        /* protect task_prep and start_delivery sequence */
-       spin_lock_irqsave(&dq->lock, flags);
-       rc = hisi_sas_task_prep(task, dq, is_tmf, tmf, &pass);
+       rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
        if (rc)
                dev_err(dev, "task exec: failed[%d]!\n", rc);
 
-       if (likely(pass))
+       if (likely(pass)) {
+               spin_lock_irqsave(&dq->lock, flags);
                hisi_hba->hw->start_delivery(dq);
-       spin_unlock_irqrestore(&dq->lock, flags);
+               spin_unlock_irqrestore(&dq->lock, flags);
+       }
 
        return rc;
 }
@@ -524,10 +553,12 @@ static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
        struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
        struct hisi_sas_device *sas_dev = NULL;
        unsigned long flags;
+       int last = hisi_hba->last_dev_id;
+       int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
        int i;
 
        spin_lock_irqsave(&hisi_hba->lock, flags);
-       for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
+       for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
                if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
                        int queue = i % hisi_hba->queue_count;
                        struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
@@ -542,18 +573,57 @@ static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
                        INIT_LIST_HEAD(&hisi_hba->devices[i].list);
                        break;
                }
+               i++;
        }
+       hisi_hba->last_dev_id = i;
        spin_unlock_irqrestore(&hisi_hba->lock, flags);
 
        return sas_dev;
 }
 
+#define HISI_SAS_SRST_ATA_DISK_CNT 3
+static int hisi_sas_init_device(struct domain_device *device)
+{
+       int rc = TMF_RESP_FUNC_COMPLETE;
+       struct scsi_lun lun;
+       struct hisi_sas_tmf_task tmf_task;
+       int retry = HISI_SAS_SRST_ATA_DISK_CNT;
+       struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
+
+       switch (device->dev_type) {
+       case SAS_END_DEVICE:
+               int_to_scsilun(0, &lun);
+
+               tmf_task.tmf = TMF_CLEAR_TASK_SET;
+               rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
+                                                 &tmf_task);
+               if (rc == TMF_RESP_FUNC_COMPLETE)
+                       hisi_sas_release_task(hisi_hba, device);
+               break;
+       case SAS_SATA_DEV:
+       case SAS_SATA_PM:
+       case SAS_SATA_PM_PORT:
+       case SAS_SATA_PENDING:
+               while (retry-- > 0) {
+                       rc = hisi_sas_softreset_ata_disk(device);
+                       if (!rc)
+                               break;
+               }
+               break;
+       default:
+               break;
+       }
+
+       return rc;
+}
+
 static int hisi_sas_dev_found(struct domain_device *device)
 {
        struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
        struct domain_device *parent_dev = device->parent;
        struct hisi_sas_device *sas_dev;
        struct device *dev = hisi_hba->dev;
+       int rc;
 
        if (hisi_hba->hw->alloc_dev)
                sas_dev = hisi_hba->hw->alloc_dev(device);
@@ -576,10 +646,8 @@ static int hisi_sas_dev_found(struct domain_device *device)
                for (phy_no = 0; phy_no < phy_num; phy_no++) {
                        phy = &parent_dev->ex_dev.ex_phy[phy_no];
                        if (SAS_ADDR(phy->attached_sas_addr) ==
-                               SAS_ADDR(device->sas_addr)) {
-                               sas_dev->attached_phy = phy_no;
+                               SAS_ADDR(device->sas_addr))
                                break;
-                       }
                }
 
                if (phy_no == phy_num) {
@@ -587,17 +655,25 @@ static int hisi_sas_dev_found(struct domain_device *device)
                                 "dev:%016llx at ex:%016llx\n",
                                 SAS_ADDR(device->sas_addr),
                                 SAS_ADDR(parent_dev->sas_addr));
-                       return -EINVAL;
+                       rc = -EINVAL;
+                       goto err_out;
                }
        }
 
        dev_info(dev, "dev[%d:%x] found\n",
                sas_dev->device_id, sas_dev->dev_type);
 
+       rc = hisi_sas_init_device(device);
+       if (rc)
+               goto err_out;
        return 0;
+
+err_out:
+       hisi_sas_dev_gone(device);
+       return rc;
 }
 
-static int hisi_sas_slave_configure(struct scsi_device *sdev)
+int hisi_sas_slave_configure(struct scsi_device *sdev)
 {
        struct domain_device *dev = sdev_to_domain_dev(sdev);
        int ret = sas_slave_configure(sdev);
@@ -609,15 +685,17 @@ static int hisi_sas_slave_configure(struct scsi_device *sdev)
 
        return 0;
 }
+EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
 
-static void hisi_sas_scan_start(struct Scsi_Host *shost)
+void hisi_sas_scan_start(struct Scsi_Host *shost)
 {
        struct hisi_hba *hisi_hba = shost_priv(shost);
 
        hisi_hba->hw->phys_init(hisi_hba);
 }
+EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
 
-static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
+int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
 {
        struct hisi_hba *hisi_hba = shost_priv(shost);
        struct sas_ha_struct *sha = &hisi_hba->sha;
@@ -629,6 +707,7 @@ static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
        sas_drain_work(sha);
        return 1;
 }
+EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
 
 static void hisi_sas_phyup_work(struct work_struct *work)
 {
@@ -694,6 +773,8 @@ static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
 
        for (i = 0; i < HISI_PHYES_NUM; i++)
                INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
+
+       spin_lock_init(&phy->lock);
 }
 
 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
@@ -789,7 +870,9 @@ static void hisi_sas_dev_gone(struct domain_device *device)
 
                hisi_sas_dereg_device(hisi_hba, device);
 
+               down(&hisi_hba->sem);
                hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
+               up(&hisi_hba->sem);
                device->lldd_dev = NULL;
        }
 
@@ -803,6 +886,33 @@ static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
        return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
 }
 
+static void hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
+                       struct sas_phy_linkrates *r)
+{
+       struct sas_phy_linkrates _r;
+
+       struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
+       struct asd_sas_phy *sas_phy = &phy->sas_phy;
+       enum sas_linkrate min, max;
+
+       if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
+               max = sas_phy->phy->maximum_linkrate;
+               min = r->minimum_linkrate;
+       } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
+               max = r->maximum_linkrate;
+               min = sas_phy->phy->minimum_linkrate;
+       } else
+               return;
+
+       _r.maximum_linkrate = max;
+       _r.minimum_linkrate = min;
+
+       hisi_hba->hw->phy_disable(hisi_hba, phy_no);
+       msleep(100);
+       hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
+       hisi_hba->hw->phy_start(hisi_hba, phy_no);
+}
+
 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
                                void *funcdata)
 {
@@ -826,7 +936,7 @@ static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
                break;
 
        case PHY_FUNC_SET_LINK_RATE:
-               hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
+               hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
                break;
        case PHY_FUNC_GET_EVENTS:
                if (hisi_hba->hw->get_events) {
@@ -990,7 +1100,6 @@ static int hisi_sas_softreset_ata_disk(struct domain_device *device)
        struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
        struct device *dev = hisi_hba->dev;
        int s = sizeof(struct host_to_dev_fis);
-       unsigned long flags;
 
        ata_for_each_link(link, ap, EDGE) {
                int pmp = sata_srst_pmp(link);
@@ -1015,11 +1124,8 @@ static int hisi_sas_softreset_ata_disk(struct domain_device *device)
                dev_err(dev, "ata disk reset failed\n");
        }
 
-       if (rc == TMF_RESP_FUNC_COMPLETE) {
-               spin_lock_irqsave(&hisi_hba->lock, flags);
+       if (rc == TMF_RESP_FUNC_COMPLETE)
                hisi_sas_release_task(hisi_hba, device);
-               spin_unlock_irqrestore(&hisi_hba->lock, flags);
-       }
 
        return rc;
 }
@@ -1111,12 +1217,103 @@ static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
        }
 }
 
+static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
+{
+       struct hisi_sas_device *sas_dev;
+       struct domain_device *device;
+       int i;
+
+       for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
+               sas_dev = &hisi_hba->devices[i];
+               device = sas_dev->sas_device;
+
+               if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
+                       continue;
+
+               hisi_sas_init_device(device);
+       }
+}
+
+static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
+                                            struct asd_sas_port *sas_port,
+                                            struct domain_device *device)
+{
+       struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
+       struct ata_port *ap = device->sata_dev.ap;
+       struct device *dev = hisi_hba->dev;
+       int s = sizeof(struct host_to_dev_fis);
+       int rc = TMF_RESP_FUNC_FAILED;
+       struct asd_sas_phy *sas_phy;
+       struct ata_link *link;
+       u8 fis[20] = {0};
+       u32 state;
+
+       state = hisi_hba->hw->get_phys_state(hisi_hba);
+       list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
+               if (!(state & BIT(sas_phy->id)))
+                       continue;
+
+               ata_for_each_link(link, ap, EDGE) {
+                       int pmp = sata_srst_pmp(link);
+
+                       tmf_task.phy_id = sas_phy->id;
+                       hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
+                       rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
+                                                            &tmf_task);
+                       if (rc != TMF_RESP_FUNC_COMPLETE) {
+                               dev_err(dev, "phy%d ata reset failed rc=%d\n",
+                                       sas_phy->id, rc);
+                               break;
+                       }
+               }
+       }
+}
+
+static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
+{
+       struct device *dev = hisi_hba->dev;
+       int port_no, rc, i;
+
+       for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
+               struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
+               struct domain_device *device = sas_dev->sas_device;
+
+               if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
+                       continue;
+
+               rc = hisi_sas_internal_task_abort(hisi_hba, device,
+                                                 HISI_SAS_INT_ABT_DEV, 0);
+               if (rc < 0)
+                       dev_err(dev, "STP reject: abort dev failed %d\n", rc);
+       }
+
+       for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
+               struct hisi_sas_port *port = &hisi_hba->port[port_no];
+               struct asd_sas_port *sas_port = &port->sas_port;
+               struct domain_device *port_dev = sas_port->port_dev;
+               struct domain_device *device;
+
+               if (!port_dev || !DEV_IS_EXPANDER(port_dev->dev_type))
+                       continue;
+
+               /* Try to find a SATA device */
+               list_for_each_entry(device, &sas_port->dev_list,
+                                   dev_list_node) {
+                       if (dev_is_sata(device)) {
+                               hisi_sas_send_ata_reset_each_phy(hisi_hba,
+                                                                sas_port,
+                                                                device);
+                               break;
+                       }
+               }
+       }
+}
+
 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
 {
        struct device *dev = hisi_hba->dev;
        struct Scsi_Host *shost = hisi_hba->shost;
        u32 old_state, state;
-       unsigned long flags;
        int rc;
 
        if (!hisi_hba->hw->soft_reset)
@@ -1125,38 +1322,45 @@ static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
        if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
                return -1;
 
+       down(&hisi_hba->sem);
        dev_info(dev, "controller resetting...\n");
        old_state = hisi_hba->hw->get_phys_state(hisi_hba);
 
        scsi_block_requests(shost);
+       hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
+
+       if (timer_pending(&hisi_hba->timer))
+               del_timer_sync(&hisi_hba->timer);
+
        set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
        rc = hisi_hba->hw->soft_reset(hisi_hba);
        if (rc) {
                dev_warn(dev, "controller reset failed (%d)\n", rc);
                clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
+               up(&hisi_hba->sem);
                scsi_unblock_requests(shost);
-               goto out;
+               clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
+               return rc;
        }
-       spin_lock_irqsave(&hisi_hba->lock, flags);
-       hisi_sas_release_tasks(hisi_hba);
-       spin_unlock_irqrestore(&hisi_hba->lock, flags);
-
-       clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
 
        /* Init and wait for PHYs to come up and all libsas event finished. */
        hisi_hba->hw->phys_init(hisi_hba);
        msleep(1000);
        hisi_sas_refresh_port_id(hisi_hba);
+       clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
+       up(&hisi_hba->sem);
+
+       if (hisi_hba->reject_stp_links_msk)
+               hisi_sas_terminate_stp_reject(hisi_hba);
+       hisi_sas_reset_init_all_devices(hisi_hba);
        scsi_unblock_requests(shost);
+       clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
 
        state = hisi_hba->hw->get_phys_state(hisi_hba);
        hisi_sas_rescan_topology(hisi_hba, old_state, state);
        dev_info(dev, "controller reset complete\n");
 
-out:
-       clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
-
-       return rc;
+       return 0;
 }
 
 static int hisi_sas_abort_task(struct sas_task *task)
@@ -1165,20 +1369,25 @@ static int hisi_sas_abort_task(struct sas_task *task)
        struct hisi_sas_tmf_task tmf_task;
        struct domain_device *device = task->dev;
        struct hisi_sas_device *sas_dev = device->lldd_dev;
-       struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
-       struct device *dev = hisi_hba->dev;
+       struct hisi_hba *hisi_hba;
+       struct device *dev;
        int rc = TMF_RESP_FUNC_FAILED;
        unsigned long flags;
 
-       if (!sas_dev) {
-               dev_warn(dev, "Device has been removed\n");
+       if (!sas_dev)
                return TMF_RESP_FUNC_FAILED;
-       }
 
+       hisi_hba = dev_to_hisi_hba(task->dev);
+       dev = hisi_hba->dev;
+
+       spin_lock_irqsave(&task->task_state_lock, flags);
        if (task->task_state_flags & SAS_TASK_STATE_DONE) {
+               spin_unlock_irqrestore(&task->task_state_lock, flags);
                rc = TMF_RESP_FUNC_COMPLETE;
                goto out;
        }
+       task->task_state_flags |= SAS_TASK_STATE_ABORTED;
+       spin_unlock_irqrestore(&task->task_state_lock, flags);
 
        sas_dev->dev_status = HISI_SAS_DEV_EH;
        if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
@@ -1209,11 +1418,8 @@ static int hisi_sas_abort_task(struct sas_task *task)
                 * will have already been completed
                 */
                if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
-                       if (task->lldd_task) {
-                               spin_lock_irqsave(&hisi_hba->lock, flags);
+                       if (task->lldd_task)
                                hisi_sas_do_release_task(hisi_hba, task, slot);
-                               spin_unlock_irqrestore(&hisi_hba->lock, flags);
-                       }
                }
        } else if (task->task_proto & SAS_PROTOCOL_SATA ||
                task->task_proto & SAS_PROTOCOL_STP) {
@@ -1235,11 +1441,8 @@ static int hisi_sas_abort_task(struct sas_task *task)
                rc = hisi_sas_internal_task_abort(hisi_hba, device,
                             HISI_SAS_INT_ABT_CMD, tag);
                if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
-                                       task->lldd_task) {
-                       spin_lock_irqsave(&hisi_hba->lock, flags);
+                                       task->lldd_task)
                        hisi_sas_do_release_task(hisi_hba, task, slot);
-                       spin_unlock_irqrestore(&hisi_hba->lock, flags);
-               }
        }
 
 out:
@@ -1254,7 +1457,6 @@ static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
        struct device *dev = hisi_hba->dev;
        struct hisi_sas_tmf_task tmf_task;
        int rc = TMF_RESP_FUNC_FAILED;
-       unsigned long flags;
 
        rc = hisi_sas_internal_task_abort(hisi_hba, device,
                                        HISI_SAS_INT_ABT_DEV, 0);
@@ -1267,11 +1469,8 @@ static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
        tmf_task.tmf = TMF_ABORT_TASK_SET;
        rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
 
-       if (rc == TMF_RESP_FUNC_COMPLETE) {
-               spin_lock_irqsave(&hisi_hba->lock, flags);
+       if (rc == TMF_RESP_FUNC_COMPLETE)
                hisi_sas_release_task(hisi_hba, device);
-               spin_unlock_irqrestore(&hisi_hba->lock, flags);
-       }
 
        return rc;
 }
@@ -1289,12 +1488,39 @@ static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
 
 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
 {
-       struct sas_phy *phy = sas_get_local_phy(device);
+       struct sas_phy *local_phy = sas_get_local_phy(device);
        int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
                        (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
-       rc = sas_phy_reset(phy, reset_type);
-       sas_put_local_phy(phy);
-       msleep(2000);
+       struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
+       struct sas_ha_struct *sas_ha = &hisi_hba->sha;
+       struct asd_sas_phy *sas_phy = sas_ha->sas_phy[local_phy->number];
+       struct hisi_sas_phy *phy = container_of(sas_phy,
+                       struct hisi_sas_phy, sas_phy);
+       DECLARE_COMPLETION_ONSTACK(phyreset);
+
+       if (scsi_is_sas_phy_local(local_phy)) {
+               phy->in_reset = 1;
+               phy->reset_completion = &phyreset;
+       }
+
+       rc = sas_phy_reset(local_phy, reset_type);
+       sas_put_local_phy(local_phy);
+
+       if (scsi_is_sas_phy_local(local_phy)) {
+               int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
+               unsigned long flags;
+
+               spin_lock_irqsave(&phy->lock, flags);
+               phy->reset_completion = NULL;
+               phy->in_reset = 0;
+               spin_unlock_irqrestore(&phy->lock, flags);
+
+               /* report PHY down if timed out */
+               if (!ret)
+                       hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
+       } else
+               msleep(2000);
+
        return rc;
 }
 
@@ -1304,7 +1530,6 @@ static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
        struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
        struct device *dev = hisi_hba->dev;
        int rc = TMF_RESP_FUNC_FAILED;
-       unsigned long flags;
 
        if (sas_dev->dev_status != HISI_SAS_DEV_EH)
                return TMF_RESP_FUNC_FAILED;
@@ -1320,11 +1545,9 @@ static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
 
        rc = hisi_sas_debug_I_T_nexus_reset(device);
 
-       if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV)) {
-               spin_lock_irqsave(&hisi_hba->lock, flags);
+       if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
                hisi_sas_release_task(hisi_hba, device);
-               spin_unlock_irqrestore(&hisi_hba->lock, flags);
-       }
+
        return rc;
 }
 
@@ -1333,7 +1556,6 @@ static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
        struct hisi_sas_device *sas_dev = device->lldd_dev;
        struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
        struct device *dev = hisi_hba->dev;
-       unsigned long flags;
        int rc = TMF_RESP_FUNC_FAILED;
 
        sas_dev->dev_status = HISI_SAS_DEV_EH;
@@ -1353,11 +1575,8 @@ static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
 
                rc = sas_phy_reset(phy, 1);
 
-               if (rc == 0) {
-                       spin_lock_irqsave(&hisi_hba->lock, flags);
+               if (rc == 0)
                        hisi_sas_release_task(hisi_hba, device);
-                       spin_unlock_irqrestore(&hisi_hba->lock, flags);
-               }
                sas_put_local_phy(phy);
        } else {
                struct hisi_sas_tmf_task tmf_task = { .tmf =  TMF_LU_RESET };
@@ -1371,11 +1590,8 @@ static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
                hisi_sas_dereg_device(hisi_hba, device);
 
                rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
-               if (rc == TMF_RESP_FUNC_COMPLETE) {
-                       spin_lock_irqsave(&hisi_hba->lock, flags);
+               if (rc == TMF_RESP_FUNC_COMPLETE)
                        hisi_sas_release_task(hisi_hba, device);
-                       spin_unlock_irqrestore(&hisi_hba->lock, flags);
-               }
        }
 out:
        if (rc != TMF_RESP_FUNC_COMPLETE)
@@ -1387,14 +1603,32 @@ out:
 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
 {
        struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
+       struct device *dev = hisi_hba->dev;
        HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
+       int rc, i;
 
        queue_work(hisi_hba->wq, &r.work);
        wait_for_completion(r.completion);
-       if (r.done)
-               return TMF_RESP_FUNC_COMPLETE;
+       if (!r.done)
+               return TMF_RESP_FUNC_FAILED;
 
-       return TMF_RESP_FUNC_FAILED;
+       for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
+               struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
+               struct domain_device *device = sas_dev->sas_device;
+
+               if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
+                   DEV_IS_EXPANDER(device->dev_type))
+                       continue;
+
+               rc = hisi_sas_debug_I_T_nexus_reset(device);
+               if (rc != TMF_RESP_FUNC_COMPLETE)
+                       dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
+                                sas_dev->device_id, rc);
+       }
+
+       hisi_sas_release_tasks(hisi_hba);
+
+       return TMF_RESP_FUNC_COMPLETE;
 }
 
 static int hisi_sas_query_task(struct sas_task *task)
@@ -1445,7 +1679,8 @@ hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
        struct hisi_sas_cmd_hdr *cmd_hdr_base;
        struct hisi_sas_dq *dq = sas_dev->dq;
        int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
-       unsigned long flags, flags_dq;
+       unsigned long flags, flags_dq = 0;
+       int wr_q_index;
 
        if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
                return -EINVAL;
@@ -1464,18 +1699,21 @@ hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
        }
        spin_unlock_irqrestore(&hisi_hba->lock, flags);
 
+       slot = &hisi_hba->slot_info[slot_idx];
+
        spin_lock_irqsave(&dq->lock, flags_dq);
-       rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
-       if (rc)
+       wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
+       if (wr_q_index < 0) {
+               spin_unlock_irqrestore(&dq->lock, flags_dq);
+               rc = -EAGAIN;
                goto err_out_tag;
+       }
+       list_add_tail(&slot->delivery, &dq->list);
+       spin_unlock_irqrestore(&dq->lock, flags_dq);
 
        dlvry_queue = dq->id;
-       dlvry_queue_slot = dq->wr_point;
-
-       slot = &hisi_hba->slot_info[slot_idx];
-       memset(slot, 0, sizeof(struct hisi_sas_slot));
+       dlvry_queue_slot = wr_q_index;
 
-       slot->idx = slot_idx;
        slot->n_elem = n_elem;
        slot->dlvry_queue = dlvry_queue;
        slot->dlvry_queue_slot = dlvry_queue_slot;
@@ -1483,47 +1721,33 @@ hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
        slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
        slot->task = task;
        slot->port = port;
+       slot->is_internal = true;
        task->lldd_task = slot;
 
-       slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
-                       GFP_ATOMIC, &slot->buf_dma);
-       if (!slot->buf) {
-               rc = -ENOMEM;
-               goto err_out_tag;
-       }
-
        memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
        memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
        memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
 
-       rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
+       hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
                                      abort_flag, task_tag);
-       if (rc)
-               goto err_out_buf;
 
-       spin_lock_irqsave(&hisi_hba->lock, flags);
-       list_add_tail(&slot->entry, &sas_dev->list);
-       spin_unlock_irqrestore(&hisi_hba->lock, flags);
        spin_lock_irqsave(&task->task_state_lock, flags);
        task->task_state_flags |= SAS_TASK_AT_INITIATOR;
        spin_unlock_irqrestore(&task->task_state_lock, flags);
 
-       dq->slot_prep = slot;
-
+       slot->ready = 1;
        /* send abort command to the chip */
+       spin_lock_irqsave(&dq->lock, flags);
+       list_add_tail(&slot->entry, &sas_dev->list);
        hisi_hba->hw->start_delivery(dq);
-       spin_unlock_irqrestore(&dq->lock, flags_dq);
+       spin_unlock_irqrestore(&dq->lock, flags);
 
        return 0;
 
-err_out_buf:
-       dma_pool_free(hisi_hba->buffer_pool, slot->buf,
-               slot->buf_dma);
 err_out_tag:
        spin_lock_irqsave(&hisi_hba->lock, flags);
        hisi_sas_slot_index_free(hisi_hba, slot_idx);
        spin_unlock_irqrestore(&hisi_hba->lock, flags);
-       spin_unlock_irqrestore(&dq->lock, flags_dq);
 err_out:
        dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
 
@@ -1651,6 +1875,7 @@ void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
        struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
        struct asd_sas_phy *sas_phy = &phy->sas_phy;
        struct sas_ha_struct *sas_ha = &hisi_hba->sha;
+       struct device *dev = hisi_hba->dev;
 
        if (rdy) {
                /* Phy down but ready */
@@ -1659,6 +1884,11 @@ void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
        } else {
                struct hisi_sas_port *port  = phy->port;
 
+               if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
+                   phy->in_reset) {
+                       dev_info(dev, "ignore flutter phy%d down\n", phy_no);
+                       return;
+               }
                /* Phy down and not ready */
                sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
                sas_phy_disconnected(sas_phy);
@@ -1693,34 +1923,11 @@ EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
 struct scsi_transport_template *hisi_sas_stt;
 EXPORT_SYMBOL_GPL(hisi_sas_stt);
 
-static struct device_attribute *host_attrs[] = {
+struct device_attribute *host_attrs[] = {
        &dev_attr_phy_event_threshold,
        NULL,
 };
-
-static struct scsi_host_template _hisi_sas_sht = {
-       .module                 = THIS_MODULE,
-       .name                   = DRV_NAME,
-       .queuecommand           = sas_queuecommand,
-       .target_alloc           = sas_target_alloc,
-       .slave_configure        = hisi_sas_slave_configure,
-       .scan_finished          = hisi_sas_scan_finished,
-       .scan_start             = hisi_sas_scan_start,
-       .change_queue_depth     = sas_change_queue_depth,
-       .bios_param             = sas_bios_param,
-       .can_queue              = 1,
-       .this_id                = -1,
-       .sg_tablesize           = SG_ALL,
-       .max_sectors            = SCSI_DEFAULT_MAX_SECTORS,
-       .use_clustering         = ENABLE_CLUSTERING,
-       .eh_device_reset_handler = sas_eh_device_reset_handler,
-       .eh_target_reset_handler = sas_eh_target_reset_handler,
-       .target_destroy         = sas_target_destroy,
-       .ioctl                  = sas_ioctl,
-       .shost_attrs            = host_attrs,
-};
-struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
-EXPORT_SYMBOL_GPL(hisi_sas_sht);
+EXPORT_SYMBOL_GPL(host_attrs);
 
 static struct sas_domain_function_template hisi_sas_transport_ops = {
        .lldd_dev_found         = hisi_sas_dev_found,
@@ -1773,8 +1980,11 @@ EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
 int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
 {
        struct device *dev = hisi_hba->dev;
-       int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
+       int i, j, s, max_command_entries = hisi_hba->hw->max_command_entries;
+       int max_command_entries_ru, sz_slot_buf_ru;
+       int blk_cnt, slots_per_blk;
 
+       sema_init(&hisi_hba->sem, 1);
        spin_lock_init(&hisi_hba->lock);
        for (i = 0; i < hisi_hba->n_phy; i++) {
                hisi_sas_phy_init(hisi_hba, i);
@@ -1798,35 +2008,32 @@ int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
 
                /* Delivery queue structure */
                spin_lock_init(&dq->lock);
+               INIT_LIST_HEAD(&dq->list);
                dq->id = i;
                dq->hisi_hba = hisi_hba;
 
                /* Delivery queue */
                s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
-               hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
-                                       &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
+               hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
+                                               &hisi_hba->cmd_hdr_dma[i],
+                                               GFP_KERNEL);
                if (!hisi_hba->cmd_hdr[i])
                        goto err_out;
 
                /* Completion queue */
                s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
-               hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
-                               &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
+               hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
+                                               &hisi_hba->complete_hdr_dma[i],
+                                               GFP_KERNEL);
                if (!hisi_hba->complete_hdr[i])
                        goto err_out;
        }
 
-       s = sizeof(struct hisi_sas_slot_buf_table);
-       hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
-       if (!hisi_hba->buffer_pool)
-               goto err_out;
-
        s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
-       hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
-                                           GFP_KERNEL);
+       hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
+                                            GFP_KERNEL);
        if (!hisi_hba->itct)
                goto err_out;
-
        memset(hisi_hba->itct, 0, s);
 
        hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
@@ -1835,15 +2042,45 @@ int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
        if (!hisi_hba->slot_info)
                goto err_out;
 
+       /* roundup to avoid overly large block size */
+       max_command_entries_ru = roundup(max_command_entries, 64);
+       sz_slot_buf_ru = roundup(sizeof(struct hisi_sas_slot_buf_table), 64);
+       s = lcm(max_command_entries_ru, sz_slot_buf_ru);
+       blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
+       slots_per_blk = s / sz_slot_buf_ru;
+       for (i = 0; i < blk_cnt; i++) {
+               struct hisi_sas_slot_buf_table *buf;
+               dma_addr_t buf_dma;
+               int slot_index = i * slots_per_blk;
+
+               buf = dmam_alloc_coherent(dev, s, &buf_dma, GFP_KERNEL);
+               if (!buf)
+                       goto err_out;
+               memset(buf, 0, s);
+
+               for (j = 0; j < slots_per_blk; j++, slot_index++) {
+                       struct hisi_sas_slot *slot;
+
+                       slot = &hisi_hba->slot_info[slot_index];
+                       slot->buf = buf;
+                       slot->buf_dma = buf_dma;
+                       slot->idx = slot_index;
+
+                       buf++;
+                       buf_dma += sizeof(*buf);
+               }
+       }
+
        s = max_command_entries * sizeof(struct hisi_sas_iost);
-       hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
-                                           GFP_KERNEL);
+       hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
+                                            GFP_KERNEL);
        if (!hisi_hba->iost)
                goto err_out;
 
        s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
-       hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
-                               &hisi_hba->breakpoint_dma, GFP_KERNEL);
+       hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
+                                                  &hisi_hba->breakpoint_dma,
+                                                  GFP_KERNEL);
        if (!hisi_hba->breakpoint)
                goto err_out;
 
@@ -1854,14 +2091,16 @@ int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
                goto err_out;
 
        s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
-       hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
-                               &hisi_hba->initial_fis_dma, GFP_KERNEL);
+       hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
+                                                   &hisi_hba->initial_fis_dma,
+                                                   GFP_KERNEL);
        if (!hisi_hba->initial_fis)
                goto err_out;
 
        s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
-       hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
-                               &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
+       hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
+                                       &hisi_hba->sata_breakpoint_dma,
+                                       GFP_KERNEL);
        if (!hisi_hba->sata_breakpoint)
                goto err_out;
        hisi_sas_init_mem(hisi_hba);
@@ -1882,54 +2121,6 @@ EXPORT_SYMBOL_GPL(hisi_sas_alloc);
 
 void hisi_sas_free(struct hisi_hba *hisi_hba)
 {
-       struct device *dev = hisi_hba->dev;
-       int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
-
-       for (i = 0; i < hisi_hba->queue_count; i++) {
-               s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
-               if (hisi_hba->cmd_hdr[i])
-                       dma_free_coherent(dev, s,
-                                         hisi_hba->cmd_hdr[i],
-                                         hisi_hba->cmd_hdr_dma[i]);
-
-               s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
-               if (hisi_hba->complete_hdr[i])
-                       dma_free_coherent(dev, s,
-                                         hisi_hba->complete_hdr[i],
-                                         hisi_hba->complete_hdr_dma[i]);
-       }
-
-       dma_pool_destroy(hisi_hba->buffer_pool);
-
-       s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
-       if (hisi_hba->itct)
-               dma_free_coherent(dev, s,
-                                 hisi_hba->itct, hisi_hba->itct_dma);
-
-       s = max_command_entries * sizeof(struct hisi_sas_iost);
-       if (hisi_hba->iost)
-               dma_free_coherent(dev, s,
-                                 hisi_hba->iost, hisi_hba->iost_dma);
-
-       s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
-       if (hisi_hba->breakpoint)
-               dma_free_coherent(dev, s,
-                                 hisi_hba->breakpoint,
-                                 hisi_hba->breakpoint_dma);
-
-
-       s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
-       if (hisi_hba->initial_fis)
-               dma_free_coherent(dev, s,
-                                 hisi_hba->initial_fis,
-                                 hisi_hba->initial_fis_dma);
-
-       s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
-       if (hisi_hba->sata_breakpoint)
-               dma_free_coherent(dev, s,
-                                 hisi_hba->sata_breakpoint,
-                                 hisi_hba->sata_breakpoint_dma);
-
        if (hisi_hba->wq)
                destroy_workqueue(hisi_hba->wq);
 }
@@ -2031,7 +2222,7 @@ static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
        struct hisi_hba *hisi_hba;
        struct device *dev = &pdev->dev;
 
-       shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
+       shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
        if (!shost) {
                dev_err(dev, "scsi host alloc failed\n");
                return NULL;
@@ -2080,19 +2271,8 @@ err_out:
        return NULL;
 }
 
-void hisi_sas_init_add(struct hisi_hba *hisi_hba)
-{
-       int i;
-
-       for (i = 0; i < hisi_hba->n_phy; i++)
-               memcpy(&hisi_hba->phy[i].dev_sas_addr,
-                      hisi_hba->sas_addr,
-                      SAS_ADDR_SIZE);
-}
-EXPORT_SYMBOL_GPL(hisi_sas_init_add);
-
 int hisi_sas_probe(struct platform_device *pdev,
-                        const struct hisi_sas_hw *hw)
+                  const struct hisi_sas_hw *hw)
 {
        struct Scsi_Host *shost;
        struct hisi_hba *hisi_hba;
@@ -2144,8 +2324,6 @@ int hisi_sas_probe(struct platform_device *pdev,
                sha->sas_port[i] = &hisi_hba->port[i].sas_port;
        }
 
-       hisi_sas_init_add(hisi_hba);
-
        rc = scsi_add_host(shost, &pdev->dev);
        if (rc)
                goto err_out_ha;
@@ -2177,6 +2355,9 @@ int hisi_sas_remove(struct platform_device *pdev)
        struct hisi_hba *hisi_hba = sha->lldd_ha;
        struct Scsi_Host *shost = sha->core.shost;
 
+       if (timer_pending(&hisi_hba->timer))
+               del_timer(&hisi_hba->timer);
+
        sas_unregister_ha(sha);
        sas_remove_host(sha->core.shost);