]> 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 5f503cb095085d2baa7e2f24248419dbbb432e29..fd92b7e9d794e28198076d4c11838fbb4b3abb3c 100644 (file)
@@ -22,16 +22,21 @@ hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
                             struct domain_device *device,
                             int abort_flag, int tag);
 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(u8 cmd, int direction)
+u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
 {
-       switch (cmd) {
+       switch (fis->command) {
        case ATA_CMD_FPDMA_WRITE:
        case ATA_CMD_FPDMA_READ:
        case ATA_CMD_FPDMA_RECV:
        case ATA_CMD_FPDMA_SEND:
        case ATA_CMD_NCQ_NON_DATA:
-       return HISI_SAS_SATA_PROTOCOL_FPDMA;
+               return HISI_SAS_SATA_PROTOCOL_FPDMA;
 
        case ATA_CMD_DOWNLOAD_MICRO:
        case ATA_CMD_ID_ATA:
@@ -43,7 +48,7 @@ u8 hisi_sas_get_ata_protocol(u8 cmd, int direction)
        case ATA_CMD_WRITE_LOG_EXT:
        case ATA_CMD_PIO_WRITE:
        case ATA_CMD_PIO_WRITE_EXT:
-       return HISI_SAS_SATA_PROTOCOL_PIO;
+               return HISI_SAS_SATA_PROTOCOL_PIO;
 
        case ATA_CMD_DSM:
        case ATA_CMD_DOWNLOAD_MICRO_DMA:
@@ -62,7 +67,7 @@ u8 hisi_sas_get_ata_protocol(u8 cmd, int direction)
        case ATA_CMD_WRITE_LOG_DMA_EXT:
        case ATA_CMD_WRITE_STREAM_DMA_EXT:
        case ATA_CMD_ZAC_MGMT_IN:
-       return HISI_SAS_SATA_PROTOCOL_DMA;
+               return HISI_SAS_SATA_PROTOCOL_DMA;
 
        case ATA_CMD_CHK_POWER:
        case ATA_CMD_DEV_RESET:
@@ -75,12 +80,28 @@ u8 hisi_sas_get_ata_protocol(u8 cmd, int direction)
        case ATA_CMD_STANDBY:
        case ATA_CMD_STANDBYNOW1:
        case ATA_CMD_ZAC_MGMT_OUT:
-       return HISI_SAS_SATA_PROTOCOL_NONDATA;
+               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;
        }
+       }
 }
 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
 
@@ -116,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;
@@ -160,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;
 }
 
@@ -179,11 +223,11 @@ 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;
-               struct domain_device *device = task->dev;
-               struct hisi_sas_device *sas_dev = device->lldd_dev;
 
                if (!task->lldd_task)
                        return;
@@ -192,50 +236,51 @@ void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
 
                if (!sas_protocol_ata(task->task_proto))
                        if (slot->n_elem)
-                               dma_unmap_sg(dev, task->scatter, slot->n_elem,
+                               dma_unmap_sg(dev, task->scatter,
+                                            task->num_scatter,
                                             task->data_dir);
-
-               if (sas_dev)
-                       atomic64_dec(&sas_dev->running_req);
        }
 
        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);
 }
 
@@ -255,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");
@@ -269,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;
@@ -302,7 +348,7 @@ static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
                 */
                if (device->dev_type != SAS_SATA_DEV)
                        task->task_done(task);
-               return SAS_PHY_DOWN;
+               return -ECOMM;
        }
 
        if (DEV_IS_GONE(sas_dev)) {
@@ -313,9 +359,11 @@ static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
                        dev_info(dev, "task prep: device %016llx not ready\n",
                                 SAS_ADDR(device->sas_addr));
 
-               return SAS_PHY_DOWN;
+               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",
@@ -323,10 +371,12 @@ static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
                         "SATA/STP" : "SAS",
                         device->port->id);
 
-               return SAS_PHY_DOWN;
+               return -ECOMM;
        }
 
        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);
@@ -334,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;
@@ -367,100 +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;
-
-       atomic64_inc(&sas_dev->running_req);
        ++(*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, n_elem,
-                                    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;
 }
@@ -511,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];
@@ -529,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);
@@ -574,14 +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);
@@ -593,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;
@@ -613,11 +753,12 @@ 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)
 {
        struct hisi_sas_phy *phy =
-               container_of(work, struct hisi_sas_phy, phyup_ws);
+               container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
        struct hisi_hba *hisi_hba = phy->hisi_hba;
        struct asd_sas_phy *sas_phy = &phy->sas_phy;
        int phy_no = sas_phy->id;
@@ -626,13 +767,42 @@ static void hisi_sas_phyup_work(struct work_struct *work)
        hisi_sas_bytes_dmaed(hisi_hba, phy_no);
 }
 
+static void hisi_sas_linkreset_work(struct work_struct *work)
+{
+       struct hisi_sas_phy *phy =
+               container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
+       struct asd_sas_phy *sas_phy = &phy->sas_phy;
+
+       hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
+}
+
+static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
+       [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
+       [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
+};
+
+bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
+                               enum hisi_sas_phy_event event)
+{
+       struct hisi_hba *hisi_hba = phy->hisi_hba;
+
+       if (WARN_ON(event >= HISI_PHYES_NUM))
+               return false;
+
+       return queue_work(hisi_hba->wq, &phy->works[event]);
+}
+EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
+
 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
 {
        struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
        struct asd_sas_phy *sas_phy = &phy->sas_phy;
+       int i;
 
        phy->hisi_hba = hisi_hba;
        phy->port = NULL;
+       phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
+       phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
        sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
        sas_phy->class = SAS;
        sas_phy->iproto = SAS_PROTOCOL_ALL;
@@ -647,7 +817,8 @@ static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
        sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
        sas_phy->lldd_phy = phy;
 
-       INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
+       for (i = 0; i < HISI_PHYES_NUM; i++)
+               INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
 }
 
 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
@@ -702,7 +873,7 @@ static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
                hisi_sas_do_release_task(hisi_hba, slot->task, slot);
 }
 
-static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
+void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
 {
        struct hisi_sas_device *sas_dev;
        struct domain_device *device;
@@ -719,6 +890,7 @@ static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
                hisi_sas_release_task(hisi_hba, device);
        }
 }
+EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
 
 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
                                struct domain_device *device)
@@ -733,17 +905,21 @@ static void hisi_sas_dev_gone(struct domain_device *device)
        struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
        struct device *dev = hisi_hba->dev;
 
-       dev_info(dev, "found dev[%d:%x] is gone\n",
+       dev_info(dev, "dev[%d:%x] is gone\n",
                 sas_dev->device_id, sas_dev->dev_type);
 
-       hisi_sas_internal_task_abort(hisi_hba, device,
+       if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
+               hisi_sas_internal_task_abort(hisi_hba, device,
                                     HISI_SAS_INT_ABT_DEV, 0);
 
-       hisi_sas_dereg_device(hisi_hba, device);
+               hisi_sas_dereg_device(hisi_hba, device);
+
+               hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
+               device->lldd_dev = NULL;
+       }
 
-       hisi_hba->hw->free_device(hisi_hba, sas_dev);
-       device->lldd_dev = NULL;
-       memset(sas_dev, 0, sizeof(*sas_dev));
+       if (hisi_hba->hw->free_device)
+               hisi_hba->hw->free_device(sas_dev);
        sas_dev->dev_type = SAS_PHY_UNUSED;
 }
 
@@ -813,6 +989,7 @@ static void hisi_sas_tmf_timedout(struct timer_list *t)
 
 #define TASK_TIMEOUT 20
 #define TASK_RETRY 3
+#define INTERNAL_ABORT_TIMEOUT 6
 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
                                           void *parameter, u32 para_len,
                                           struct hisi_sas_tmf_task *tmf)
@@ -859,12 +1036,13 @@ static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
                        if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
                                struct hisi_sas_slot *slot = task->lldd_task;
 
-                               dev_err(dev, "abort tmf: TMF task timeout\n");
+                               dev_err(dev, "abort tmf: TMF task timeout and not done\n");
                                if (slot)
                                        slot->task = NULL;
 
                                goto ex_err;
-                       }
+                       } else
+                               dev_err(dev, "abort tmf: TMF task timeout\n");
                }
 
                if (task->task_status.resp == SAS_TASK_COMPLETE &&
@@ -937,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);
@@ -962,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;
 }
@@ -985,27 +1159,42 @@ static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
                                sizeof(ssp_task), tmf);
 }
 
-static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba,
-               struct asd_sas_port *sas_port, enum sas_linkrate linkrate)
+static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
 {
-       struct hisi_sas_device  *sas_dev;
-       struct domain_device *device;
+       u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
        int i;
 
        for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
-               sas_dev = &hisi_hba->devices[i];
-               device = sas_dev->sas_device;
+               struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
+               struct domain_device *device = sas_dev->sas_device;
+               struct asd_sas_port *sas_port;
+               struct hisi_sas_port *port;
+               struct hisi_sas_phy *phy = NULL;
+               struct asd_sas_phy *sas_phy;
+
                if ((sas_dev->dev_type == SAS_PHY_UNUSED)
-                               || !device || (device->port != sas_port))
+                               || !device || !device->port)
                        continue;
 
-               hisi_hba->hw->free_device(hisi_hba, sas_dev);
+               sas_port = device->port;
+               port = to_hisi_sas_port(sas_port);
+
+               list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
+                       if (state & BIT(sas_phy->id)) {
+                               phy = sas_phy->lldd_phy;
+                               break;
+                       }
+
+               if (phy) {
+                       port->id = phy->port_id;
 
-               /* Update linkrate of directly attached device. */
-               if (!device->parent)
-                       device->linkrate = linkrate;
+                       /* Update linkrate of directly attached device. */
+                       if (!device->parent)
+                               device->linkrate = phy->sas_phy.linkrate;
 
-               hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
+                       hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
+               } else
+                       port->id = 0xff;
        }
 }
 
@@ -1020,21 +1209,17 @@ static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
                struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
                struct asd_sas_phy *sas_phy = &phy->sas_phy;
                struct asd_sas_port *sas_port = sas_phy->port;
-               struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
                bool do_port_check = !!(_sas_port != sas_port);
 
                if (!sas_phy->phy->enabled)
                        continue;
 
                /* Report PHY state change to libsas */
-               if (state & (1 << phy_no)) {
-                       if (do_port_check && sas_port) {
+               if (state & BIT(phy_no)) {
+                       if (do_port_check && sas_port && sas_port->port_dev) {
                                struct domain_device *dev = sas_port->port_dev;
 
                                _sas_port = sas_port;
-                               port->id = phy->port_id;
-                               hisi_sas_refresh_port_id(hisi_hba,
-                                               sas_port, sas_phy->linkrate);
 
                                if (DEV_IS_EXPANDER(dev->dev_type))
                                        sas_ha->notify_port_event(sas_phy,
@@ -1045,8 +1230,98 @@ static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
                        hisi_sas_phy_down(hisi_hba, phy_no, 0);
 
        }
+}
+
+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;
 
-       drain_workqueue(hisi_hba->shost->work_q);
+               /* 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)
@@ -1054,7 +1329,6 @@ 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)
@@ -1063,35 +1337,41 @@ 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;
 
-       dev_dbg(dev, "controller resetting...\n");
+       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);
+               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);
 
        /* Init and wait for PHYs to come up and all libsas event finished. */
        hisi_hba->hw->phys_init(hisi_hba);
        msleep(1000);
-       drain_workqueue(hisi_hba->wq);
-       drain_workqueue(shost->work_q);
+       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);
        hisi_sas_rescan_topology(hisi_hba, old_state, state);
-       dev_dbg(dev, "controller reset complete\n");
+       dev_info(dev, "controller reset complete\n");
 
 out:
-       scsi_unblock_requests(shost);
        clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
 
        return rc;
@@ -1103,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) {
@@ -1134,6 +1419,11 @@ static int hisi_sas_abort_task(struct sas_task *task)
 
                rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
                                                   HISI_SAS_INT_ABT_CMD, tag);
+               if (rc2 < 0) {
+                       dev_err(dev, "abort task: internal abort (%d)\n", rc2);
+                       return TMF_RESP_FUNC_FAILED;
+               }
+
                /*
                 * If the TMF finds that the IO is not in the device and also
                 * the internal abort does not succeed, then it is safe to
@@ -1142,17 +1432,18 @@ 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) {
                if (task->dev->dev_type == SAS_SATA_DEV) {
-                       hisi_sas_internal_task_abort(hisi_hba, device,
-                                                    HISI_SAS_INT_ABT_DEV, 0);
+                       rc = hisi_sas_internal_task_abort(hisi_hba, device,
+                                               HISI_SAS_INT_ABT_DEV, 0);
+                       if (rc < 0) {
+                               dev_err(dev, "abort task: internal abort failed\n");
+                               goto out;
+                       }
                        hisi_sas_dereg_device(hisi_hba, device);
                        rc = hisi_sas_softreset_ata_disk(device);
                }
@@ -1163,11 +1454,9 @@ 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 == TMF_RESP_FUNC_FAILED && task->lldd_task) {
-                       spin_lock_irqsave(&hisi_hba->lock, flags);
+               if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
+                                       task->lldd_task)
                        hisi_sas_do_release_task(hisi_hba, task, slot);
-                       spin_unlock_irqrestore(&hisi_hba->lock, flags);
-               }
        }
 
 out:
@@ -1178,12 +1467,25 @@ out:
 
 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
 {
+       struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
+       struct device *dev = hisi_hba->dev;
        struct hisi_sas_tmf_task tmf_task;
        int rc = TMF_RESP_FUNC_FAILED;
 
+       rc = hisi_sas_internal_task_abort(hisi_hba, device,
+                                       HISI_SAS_INT_ABT_DEV, 0);
+       if (rc < 0) {
+               dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
+               return TMF_RESP_FUNC_FAILED;
+       }
+       hisi_sas_dereg_device(hisi_hba, device);
+
        tmf_task.tmf = TMF_ABORT_TASK_SET;
        rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
 
+       if (rc == TMF_RESP_FUNC_COMPLETE)
+               hisi_sas_release_task(hisi_hba, device);
+
        return rc;
 }
 
@@ -1213,24 +1515,26 @@ static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
 {
        struct hisi_sas_device *sas_dev = device->lldd_dev;
        struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
-       unsigned long flags;
+       struct device *dev = hisi_hba->dev;
        int rc = TMF_RESP_FUNC_FAILED;
 
        if (sas_dev->dev_status != HISI_SAS_DEV_EH)
                return TMF_RESP_FUNC_FAILED;
        sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
 
-       hisi_sas_internal_task_abort(hisi_hba, device,
+       rc = hisi_sas_internal_task_abort(hisi_hba, device,
                                        HISI_SAS_INT_ABT_DEV, 0);
+       if (rc < 0) {
+               dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
+               return TMF_RESP_FUNC_FAILED;
+       }
        hisi_sas_dereg_device(hisi_hba, device);
 
        rc = hisi_sas_debug_I_T_nexus_reset(device);
 
-       if (rc == TMF_RESP_FUNC_COMPLETE) {
-               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;
 }
 
@@ -1239,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;
@@ -1249,29 +1552,33 @@ static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
                /* Clear internal IO and then hardreset */
                rc = hisi_sas_internal_task_abort(hisi_hba, device,
                                                  HISI_SAS_INT_ABT_DEV, 0);
-               if (rc == TMF_RESP_FUNC_FAILED)
+               if (rc < 0) {
+                       dev_err(dev, "lu_reset: internal abort failed\n");
                        goto out;
+               }
                hisi_sas_dereg_device(hisi_hba, device);
 
                phy = sas_get_local_phy(device);
 
                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 };
 
+               rc = hisi_sas_internal_task_abort(hisi_hba, device,
+                                               HISI_SAS_INT_ABT_DEV, 0);
+               if (rc < 0) {
+                       dev_err(dev, "lu_reset: internal abort failed\n");
+                       goto out;
+               }
+               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)
@@ -1283,8 +1590,14 @@ out:
 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
 {
        struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
+       HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
+
+       queue_work(hisi_hba->wq, &r.work);
+       wait_for_completion(r.completion);
+       if (r.done)
+               return TMF_RESP_FUNC_COMPLETE;
 
-       return hisi_sas_controller_reset(hisi_hba);
+       return TMF_RESP_FUNC_FAILED;
 }
 
 static int hisi_sas_query_task(struct sas_task *task)
@@ -1335,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;
@@ -1354,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;
@@ -1373,49 +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;
-
-       atomic64_inc(&sas_dev->running_req);
-
+       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);
 
@@ -1441,8 +1753,14 @@ hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
        struct device *dev = hisi_hba->dev;
        int res;
 
+       /*
+        * The interface is not realized means this HW don't support internal
+        * abort, or don't need to do internal abort. Then here, we return
+        * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
+        * the internal abort has been executed and returned CQ.
+        */
        if (!hisi_hba->hw->prep_abort)
-               return -EOPNOTSUPP;
+               return TMF_RESP_FUNC_FAILED;
 
        task = sas_alloc_slow_task(GFP_KERNEL);
        if (!task)
@@ -1452,7 +1770,7 @@ hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
        task->task_proto = device->tproto;
        task->task_done = hisi_sas_task_done;
        task->slow_task->timer.function = hisi_sas_tmf_timedout;
-       task->slow_task->timer.expires = jiffies + msecs_to_jiffies(110);
+       task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
        add_timer(&task->slow_task->timer);
 
        res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
@@ -1473,9 +1791,11 @@ hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
 
                        if (slot)
                                slot->task = NULL;
-                       dev_err(dev, "internal task abort: timeout.\n");
+                       dev_err(dev, "internal task abort: timeout and not done.\n");
+                       res = -EIO;
                        goto exit;
-               }
+               } else
+                       dev_err(dev, "internal task abort: timeout.\n");
        }
 
        if (task->task_status.resp == SAS_TASK_COMPLETE &&
@@ -1507,6 +1827,22 @@ static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
        hisi_sas_port_notify_formed(sas_phy);
 }
 
+static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
+{
+}
+
+static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
+                       u8 reg_index, u8 reg_count, u8 *write_data)
+{
+       struct hisi_hba *hisi_hba = sha->lldd_ha;
+
+       if (!hisi_hba->hw->write_gpio)
+               return -EOPNOTSUPP;
+
+       return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
+                               reg_index, reg_count, write_data);
+}
+
 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
 {
        phy->phy_attached = 0;
@@ -1561,28 +1897,11 @@ EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
 struct scsi_transport_template *hisi_sas_stt;
 EXPORT_SYMBOL_GPL(hisi_sas_stt);
 
-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,
+struct device_attribute *host_attrs[] = {
+       &dev_attr_phy_event_threshold,
+       NULL,
 };
-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,
@@ -1597,6 +1916,8 @@ static struct sas_domain_function_template hisi_sas_transport_ops = {
        .lldd_query_task        = hisi_sas_query_task,
        .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
        .lldd_port_formed       = hisi_sas_port_formed,
+       .lldd_port_deformed = hisi_sas_port_deformed,
+       .lldd_write_gpio = hisi_sas_write_gpio,
 };
 
 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
@@ -1657,6 +1978,8 @@ int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
                cq->hisi_hba = hisi_hba;
 
                /* Delivery queue structure */
+               spin_lock_init(&dq->lock);
+               INIT_LIST_HEAD(&dq->list);
                dq->id = i;
                dq->hisi_hba = hisi_hba;
 
@@ -1803,6 +2126,17 @@ void hisi_sas_rst_work_handler(struct work_struct *work)
 }
 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
 
+void hisi_sas_sync_rst_work_handler(struct work_struct *work)
+{
+       struct hisi_sas_rst *rst =
+               container_of(work, struct hisi_sas_rst, work);
+
+       if (!hisi_sas_controller_reset(rst->hisi_hba))
+               rst->done = true;
+       complete(rst->completion);
+}
+EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
+
 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
 {
        struct device *dev = hisi_hba->dev;
@@ -1879,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;
@@ -1909,6 +2243,13 @@ static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
        if (IS_ERR(hisi_hba->regs))
                goto err_out;
 
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+       if (res) {
+               hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
+               if (IS_ERR(hisi_hba->sgpio_regs))
+                       goto err_out;
+       }
+
        if (hisi_sas_alloc(hisi_hba, shost)) {
                hisi_sas_free(hisi_hba);
                goto err_out;
@@ -1933,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;
@@ -2018,6 +2359,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);