]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - drivers/scsi/hisi_sas/hisi_sas_main.c
scsi: hisi_sas: Terminate STP reject quickly for v2 hw
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / hisi_sas / hisi_sas_main.c
index a216795741428d00535144bb6ecdac01dc7fa7e1..fd92b7e9d794e28198076d4c11838fbb4b3abb3c 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)
 {
@@ -134,6 +137,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 +197,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 +223,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;
@@ -216,40 +244,43 @@ void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
        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);
+       spin_unlock_irqrestore(&dq->lock, flags);
        slot->buf = NULL;
        slot->task = NULL;
        slot->port = NULL;
+       spin_lock_irqsave(&hisi_hba->lock, flags);
        hisi_sas_slot_index_free(hisi_hba, slot->idx);
+       spin_unlock_irqrestore(&hisi_hba->lock, flags);
 
        /* slot memory is fully zeroed when it is reused */
 }
 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);
 }
 
@@ -269,7 +300,6 @@ static void hisi_sas_slot_abort(struct work_struct *work)
        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");
@@ -283,27 +313,29 @@ static void hisi_sas_slot_abort(struct work_struct *work)
        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)
+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_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 +362,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 +375,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,31 +384,73 @@ 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_tag;
+               goto err_out_dma_unmap;
 
-       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));
 
+       slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
+                                  GFP_ATOMIC, &slot->buf_dma);
+       if (!slot->buf) {
+               rc = -ENOMEM;
+               goto err_out_tag;
+       }
+
+       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);
+               goto err_out_buf;
+       }
+
+       list_add_tail(&slot->delivery, &dq->list);
+       spin_unlock_irqrestore(&dq->lock, flags_dq);
+
+       dlvry_queue = dq->id;
+       dlvry_queue_slot = wr_q_index;
+
        slot->idx = slot_idx;
        slot->n_elem = n_elem;
        slot->dlvry_queue = dlvry_queue;
@@ -381,99 +459,94 @@ 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 */
+                     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);
-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;
 
        /* 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 +597,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 +617,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);
@@ -587,17 +701,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 +731,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 +753,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)
 {
@@ -990,7 +1115,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 +1139,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 +1232,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)
@@ -1129,6 +1341,11 @@ static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
        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) {
@@ -1137,9 +1354,6 @@ static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
                scsi_unblock_requests(shost);
                goto out;
        }
-       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);
 
@@ -1147,6 +1361,10 @@ static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
        hisi_hba->hw->phys_init(hisi_hba);
        msleep(1000);
        hisi_sas_refresh_port_id(hisi_hba);
+
+       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);
 
        state = hisi_hba->hw->get_phys_state(hisi_hba);
@@ -1165,20 +1383,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 +1432,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 +1455,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 +1471,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 +1483,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;
 }
@@ -1304,7 +1517,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 +1532,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 +1543,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 +1562,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 +1577,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)
@@ -1445,7 +1648,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,16 +1668,27 @@ hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
        }
        spin_unlock_irqrestore(&hisi_hba->lock, flags);
 
-       spin_lock_irqsave(&dq->lock, flags_dq);
-       rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
-       if (rc)
+       slot = &hisi_hba->slot_info[slot_idx];
+       memset(slot, 0, sizeof(struct hisi_sas_slot));
+
+       slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
+                       GFP_ATOMIC, &slot->buf_dma);
+       if (!slot->buf) {
+               rc = -ENOMEM;
                goto err_out_tag;
+       }
 
-       dlvry_queue = dq->id;
-       dlvry_queue_slot = dq->wr_point;
+       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);
+               goto err_out_buf;
+       }
+       list_add_tail(&slot->delivery, &dq->list);
+       spin_unlock_irqrestore(&dq->lock, flags_dq);
 
-       slot = &hisi_hba->slot_info[slot_idx];
-       memset(slot, 0, sizeof(struct hisi_sas_slot));
+       dlvry_queue = dq->id;
+       dlvry_queue_slot = wr_q_index;
 
        slot->idx = slot_idx;
        slot->n_elem = n_elem;
@@ -1483,47 +1698,36 @@ 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);
+                     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);
 
@@ -1693,34 +1897,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,
@@ -1798,6 +1979,7 @@ 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;
 
@@ -2031,7 +2213,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;
@@ -2092,7 +2274,7 @@ void hisi_sas_init_add(struct hisi_hba *hisi_hba)
 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;