Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-microblaze.git] / drivers / scsi / hisi_sas / hisi_sas_main.c
index eed7fc5..3c3cf89 100644 (file)
@@ -10,6 +10,7 @@
  */
 
 #include "hisi_sas.h"
+#include "../libsas/sas_internal.h"
 #define DRV_NAME "hisi_sas"
 
 #define DEV_IS_GONE(dev) \
@@ -144,7 +145,7 @@ EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
  */
 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
 {
-       u16 rate = 0;
+       u8 rate = 0;
        int i;
 
        max -= SAS_LINK_RATE_1_5_GBPS;
@@ -241,8 +242,9 @@ 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;
+       int device_id = slot->device_id;
+       struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
 
        if (task) {
                struct device *dev = hisi_hba->dev;
@@ -252,17 +254,24 @@ void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
 
                task->lldd_task = NULL;
 
-               if (!sas_protocol_ata(task->task_proto))
+               if (!sas_protocol_ata(task->task_proto)) {
+                       struct sas_ssp_task *ssp_task = &task->ssp_task;
+                       struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
+
                        if (slot->n_elem)
                                dma_unmap_sg(dev, task->scatter,
                                             task->num_scatter,
                                             task->data_dir);
+                       if (slot->n_elem_dif)
+                               dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
+                                            scsi_prot_sg_count(scsi_cmnd),
+                                            task->data_dir);
+               }
        }
 
-
-       spin_lock_irqsave(&dq->lock, flags);
+       spin_lock_irqsave(&sas_dev->lock, flags);
        list_del_init(&slot->entry);
-       spin_unlock_irqrestore(&dq->lock, flags);
+       spin_unlock_irqrestore(&sas_dev->lock, flags);
 
        memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
 
@@ -380,6 +389,59 @@ prep_out:
        return rc;
 }
 
+static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
+                                  struct sas_task *task, int n_elem_dif)
+{
+       struct device *dev = hisi_hba->dev;
+
+       if (n_elem_dif) {
+               struct sas_ssp_task *ssp_task = &task->ssp_task;
+               struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
+
+               dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
+                            scsi_prot_sg_count(scsi_cmnd),
+                            task->data_dir);
+       }
+}
+
+static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
+                               int *n_elem_dif, struct sas_task *task)
+{
+       struct device *dev = hisi_hba->dev;
+       struct sas_ssp_task *ssp_task;
+       struct scsi_cmnd *scsi_cmnd;
+       int rc;
+
+       if (task->num_scatter) {
+               ssp_task = &task->ssp_task;
+               scsi_cmnd = ssp_task->cmd;
+
+               if (scsi_prot_sg_count(scsi_cmnd)) {
+                       *n_elem_dif = dma_map_sg(dev,
+                                                scsi_prot_sglist(scsi_cmnd),
+                                                scsi_prot_sg_count(scsi_cmnd),
+                                                task->data_dir);
+
+                       if (!*n_elem_dif)
+                               return -ENOMEM;
+
+                       if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
+                               dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
+                                       *n_elem_dif);
+                               rc = -EINVAL;
+                               goto err_out_dif_dma_unmap;
+                       }
+               }
+       }
+
+       return 0;
+
+err_out_dif_dma_unmap:
+       dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
+                    scsi_prot_sg_count(scsi_cmnd), task->data_dir);
+       return rc;
+}
+
 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,
@@ -394,7 +456,7 @@ static int hisi_sas_task_prep(struct sas_task *task,
        struct asd_sas_port *sas_port = device->port;
        struct device *dev = hisi_hba->dev;
        int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
-       int n_elem = 0, n_elem_req = 0, n_elem_resp = 0;
+       int n_elem = 0, n_elem_dif = 0, n_elem_req = 0, n_elem_resp = 0;
        struct hisi_sas_dq *dq;
        unsigned long flags;
        int wr_q_index;
@@ -410,7 +472,14 @@ static int hisi_sas_task_prep(struct sas_task *task,
                return -ECOMM;
        }
 
-       *dq_pointer = dq = sas_dev->dq;
+       if (hisi_hba->reply_map) {
+               int cpu = raw_smp_processor_id();
+               unsigned int dq_index = hisi_hba->reply_map[cpu];
+
+               *dq_pointer = dq = &hisi_hba->dq[dq_index];
+       } else {
+               *dq_pointer = dq = sas_dev->dq;
+       }
 
        port = to_hisi_sas_port(sas_port);
        if (port && !port->port_attached) {
@@ -427,6 +496,12 @@ static int hisi_sas_task_prep(struct sas_task *task,
        if (rc < 0)
                goto prep_out;
 
+       if (!sas_protocol_ata(task->task_proto)) {
+               rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
+               if (rc < 0)
+                       goto err_out_dma_unmap;
+       }
+
        if (hisi_hba->hw->slot_index_alloc)
                rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
        else {
@@ -445,7 +520,7 @@ static int hisi_sas_task_prep(struct sas_task *task,
                rc  = hisi_sas_slot_index_alloc(hisi_hba, scsi_cmnd);
        }
        if (rc < 0)
-               goto err_out_dma_unmap;
+               goto err_out_dif_dma_unmap;
 
        slot_idx = rc;
        slot = &hisi_hba->slot_info[slot_idx];
@@ -459,13 +534,17 @@ static int hisi_sas_task_prep(struct sas_task *task,
        }
 
        list_add_tail(&slot->delivery, &dq->list);
-       list_add_tail(&slot->entry, &sas_dev->list);
        spin_unlock_irqrestore(&dq->lock, flags);
+       spin_lock_irqsave(&sas_dev->lock, flags);
+       list_add_tail(&slot->entry, &sas_dev->list);
+       spin_unlock_irqrestore(&sas_dev->lock, flags);
 
        dlvry_queue = dq->id;
        dlvry_queue_slot = wr_q_index;
 
+       slot->device_id = sas_dev->device_id;
        slot->n_elem = n_elem;
+       slot->n_elem_dif = n_elem_dif;
        slot->dlvry_queue = dlvry_queue;
        slot->dlvry_queue_slot = dlvry_queue_slot;
        cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
@@ -509,6 +588,9 @@ static int hisi_sas_task_prep(struct sas_task *task,
 
 err_out_tag:
        hisi_sas_slot_index_free(hisi_hba, slot_idx);
+err_out_dif_dma_unmap:
+       if (!sas_protocol_ata(task->task_proto))
+               hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
 err_out_dma_unmap:
        hisi_sas_dma_unmap(hisi_hba, task, n_elem,
                           n_elem_req, n_elem_resp);
@@ -626,11 +708,12 @@ static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
 
                        hisi_hba->devices[i].device_id = i;
                        sas_dev = &hisi_hba->devices[i];
-                       sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
+                       sas_dev->dev_status = HISI_SAS_DEV_INIT;
                        sas_dev->dev_type = device->dev_type;
                        sas_dev->hisi_hba = hisi_hba;
                        sas_dev->sas_device = device;
                        sas_dev->dq = dq;
+                       spin_lock_init(&sas_dev->lock);
                        INIT_LIST_HEAD(&hisi_hba->devices[i].list);
                        break;
                }
@@ -650,6 +733,8 @@ static int hisi_sas_init_device(struct domain_device *device)
        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);
+       struct device *dev = hisi_hba->dev;
+       struct sas_phy *local_phy;
 
        switch (device->dev_type) {
        case SAS_END_DEVICE:
@@ -665,6 +750,31 @@ static int hisi_sas_init_device(struct domain_device *device)
        case SAS_SATA_PM:
        case SAS_SATA_PM_PORT:
        case SAS_SATA_PENDING:
+               /*
+                * send HARD RESET to clear previous affiliation of
+                * STP target port
+                */
+               local_phy = sas_get_local_phy(device);
+               if (!scsi_is_sas_phy_local(local_phy)) {
+                       unsigned long deadline = ata_deadline(jiffies, 20000);
+                       struct sata_device *sata_dev = &device->sata_dev;
+                       struct ata_host *ata_host = sata_dev->ata_host;
+                       struct ata_port_operations *ops = ata_host->ops;
+                       struct ata_port *ap = sata_dev->ap;
+                       struct ata_link *link;
+                       unsigned int classes;
+
+                       ata_for_each_link(link, ap, EDGE)
+                               rc = ops->hardreset(link, &classes,
+                                                   deadline);
+               }
+               sas_put_local_phy(local_phy);
+               if (rc) {
+                       dev_warn(dev, "SATA disk hardreset fail: 0x%x\n",
+                                rc);
+                       return rc;
+               }
+
                while (retry-- > 0) {
                        rc = hisi_sas_softreset_ata_disk(device);
                        if (!rc)
@@ -727,6 +837,7 @@ static int hisi_sas_dev_found(struct domain_device *device)
        rc = hisi_sas_init_device(device);
        if (rc)
                goto err_out;
+       sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
        return 0;
 
 err_out:
@@ -778,7 +889,8 @@ static void hisi_sas_phyup_work(struct work_struct *work)
        struct asd_sas_phy *sas_phy = &phy->sas_phy;
        int phy_no = sas_phy->id;
 
-       hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
+       if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
+               hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
        hisi_sas_bytes_dmaed(hisi_hba, phy_no);
 }
 
@@ -808,6 +920,30 @@ bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
 }
 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
 
+static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
+{
+       struct hisi_sas_phy *phy = from_timer(phy, t, timer);
+       struct hisi_hba *hisi_hba = phy->hisi_hba;
+       struct device *dev = hisi_hba->dev;
+       int phy_no = phy->sas_phy.id;
+
+       dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
+       hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
+}
+
+void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
+{
+       struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
+       struct device *dev = hisi_hba->dev;
+
+       if (!timer_pending(&phy->timer)) {
+               dev_dbg(dev, "phy%d OOB ready\n", phy_no);
+               phy->timer.expires = jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ;
+               add_timer(&phy->timer);
+       }
+}
+EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
+
 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
 {
        struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
@@ -836,6 +972,8 @@ static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
                INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
 
        spin_lock_init(&phy->lock);
+
+       timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
 }
 
 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
@@ -872,7 +1010,8 @@ static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task
                spin_lock_irqsave(&task->task_state_lock, flags);
                task->task_state_flags &=
                        ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
-               task->task_state_flags |= SAS_TASK_STATE_DONE;
+               if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
+                       task->task_state_flags |= SAS_TASK_STATE_DONE;
                spin_unlock_irqrestore(&task->task_state_lock, flags);
        }
 
@@ -926,7 +1065,7 @@ static void hisi_sas_dev_gone(struct domain_device *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_INT_ABT_DEV, 0);
 
                hisi_sas_dereg_device(hisi_hba, device);
 
@@ -946,7 +1085,7 @@ static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
        return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
 }
 
-static void hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
+static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
                        struct sas_phy_linkrates *r)
 {
        struct sas_phy_linkrates _r;
@@ -955,6 +1094,9 @@ static void hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
        struct asd_sas_phy *sas_phy = &phy->sas_phy;
        enum sas_linkrate min, max;
 
+       if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
+               return -EINVAL;
+
        if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
                max = sas_phy->phy->maximum_linkrate;
                min = r->minimum_linkrate;
@@ -962,7 +1104,7 @@ static void hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
                max = r->maximum_linkrate;
                min = sas_phy->phy->minimum_linkrate;
        } else
-               return;
+               return -EINVAL;
 
        _r.maximum_linkrate = max;
        _r.minimum_linkrate = min;
@@ -974,6 +1116,8 @@ static void hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
        msleep(100);
        hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
        hisi_hba->hw->phy_start(hisi_hba, phy_no);
+
+       return 0;
 }
 
 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
@@ -999,8 +1143,7 @@ static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
                break;
 
        case PHY_FUNC_SET_LINK_RATE:
-               hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
-               break;
+               return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
        case PHY_FUNC_GET_EVENTS:
                if (hisi_hba->hw->get_events) {
                        hisi_hba->hw->get_events(hisi_hba, phy_no);
@@ -1068,7 +1211,7 @@ static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
                task->task_done = hisi_sas_task_done;
 
                task->slow_task->timer.function = hisi_sas_tmf_timedout;
-               task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
+               task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ;
                add_timer(&task->slow_task->timer);
 
                res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
@@ -1429,6 +1572,9 @@ static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
        struct Scsi_Host *shost = hisi_hba->shost;
        int rc;
 
+       if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct)
+               queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
+
        if (!hisi_hba->hw->soft_reset)
                return -1;
 
@@ -1491,7 +1637,6 @@ static int hisi_sas_abort_task(struct sas_task *task)
        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) {
                struct scsi_cmnd *cmnd = task->uldd_task;
                struct hisi_sas_slot *slot = task->lldd_task;
@@ -1527,7 +1672,8 @@ static int hisi_sas_abort_task(struct sas_task *task)
                task->task_proto & SAS_PROTOCOL_STP) {
                if (task->dev->dev_type == SAS_SATA_DEV) {
                        rc = hisi_sas_internal_task_abort(hisi_hba, device,
-                                               HISI_SAS_INT_ABT_DEV, 0);
+                                                         HISI_SAS_INT_ABT_DEV,
+                                                         0);
                        if (rc < 0) {
                                dev_err(dev, "abort task: internal abort failed\n");
                                goto out;
@@ -1542,7 +1688,7 @@ static int hisi_sas_abort_task(struct sas_task *task)
                struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
 
                rc = hisi_sas_internal_task_abort(hisi_hba, device,
-                            HISI_SAS_INT_ABT_CMD, tag);
+                                                 HISI_SAS_INT_ABT_CMD, tag);
                if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
                                        task->lldd_task) {
                        /*
@@ -1568,7 +1714,7 @@ static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
        int rc = TMF_RESP_FUNC_FAILED;
 
        rc = hisi_sas_internal_task_abort(hisi_hba, device,
-                                       HISI_SAS_INT_ABT_DEV, 0);
+                                         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;
@@ -1586,8 +1732,8 @@ static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
 
 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
 {
-       int rc = TMF_RESP_FUNC_FAILED;
        struct hisi_sas_tmf_task tmf_task;
+       int rc;
 
        tmf_task.tmf = TMF_CLEAR_ACA;
        rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
@@ -1598,20 +1744,23 @@ static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
 {
        struct sas_phy *local_phy = sas_get_local_phy(device);
-       int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
-                       (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
+       struct hisi_sas_device *sas_dev = device->lldd_dev;
        struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
        struct sas_ha_struct *sas_ha = &hisi_hba->sha;
        struct asd_sas_phy *sas_phy = sas_ha->sas_phy[local_phy->number];
        struct hisi_sas_phy *phy = container_of(sas_phy,
                        struct hisi_sas_phy, sas_phy);
        DECLARE_COMPLETION_ONSTACK(phyreset);
+       int rc, reset_type;
 
        if (scsi_is_sas_phy_local(local_phy)) {
                phy->in_reset = 1;
                phy->reset_completion = &phyreset;
        }
 
+       reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
+                     !dev_is_sata(device)) ? 1 : 0;
+
        rc = sas_phy_reset(local_phy, reset_type);
        sas_put_local_phy(local_phy);
 
@@ -1627,25 +1776,25 @@ static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
                /* report PHY down if timed out */
                if (!ret)
                        hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
-       } else
+       } else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) {
+               /*
+                * If in init state, we rely on caller to wait for link to be
+                * ready; otherwise, delay.
+                */
                msleep(2000);
+       }
 
        return rc;
 }
 
 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);
        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;
+       int rc;
 
        rc = hisi_sas_internal_task_abort(hisi_hba, device,
-                                       HISI_SAS_INT_ABT_DEV, 0);
+                                         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;
@@ -1667,7 +1816,6 @@ static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
        struct device *dev = hisi_hba->dev;
        int rc = TMF_RESP_FUNC_FAILED;
 
-       sas_dev->dev_status = HISI_SAS_DEV_EH;
        if (dev_is_sata(device)) {
                struct sas_phy *phy;
 
@@ -1691,7 +1839,7 @@ static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
                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);
+                                                 HISI_SAS_INT_ABT_DEV, 0);
                if (rc < 0) {
                        dev_err(dev, "lu_reset: internal abort failed\n");
                        goto out;
@@ -1777,7 +1925,7 @@ static int hisi_sas_query_task(struct sas_task *task)
 static int
 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
                                  struct sas_task *task, int abort_flag,
-                                 int task_tag)
+                                 int task_tag, struct hisi_sas_dq *dq)
 {
        struct domain_device *device = task->dev;
        struct hisi_sas_device *sas_dev = device->lldd_dev;
@@ -1786,7 +1934,6 @@ hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
        struct hisi_sas_slot *slot;
        struct asd_sas_port *sas_port = device->port;
        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 = 0;
        int wr_q_index;
@@ -1816,10 +1963,14 @@ hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
        }
        list_add_tail(&slot->delivery, &dq->list);
        spin_unlock_irqrestore(&dq->lock, flags_dq);
+       spin_lock_irqsave(&sas_dev->lock, flags);
+       list_add_tail(&slot->entry, &sas_dev->list);
+       spin_unlock_irqrestore(&sas_dev->lock, flags);
 
        dlvry_queue = dq->id;
        dlvry_queue_slot = wr_q_index;
 
+       slot->device_id = sas_dev->device_id;
        slot->n_elem = n_elem;
        slot->dlvry_queue = dlvry_queue;
        slot->dlvry_queue_slot = dlvry_queue_slot;
@@ -1843,7 +1994,6 @@ hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
        WRITE_ONCE(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);
 
@@ -1858,18 +2008,19 @@ err_out:
 }
 
 /**
- * hisi_sas_internal_task_abort -- execute an internal
+ * _hisi_sas_internal_task_abort -- execute an internal
  * abort command for single IO command or a device
  * @hisi_hba: host controller struct
  * @device: domain device
  * @abort_flag: mode of operation, device or single IO
  * @tag: tag of IO to be aborted (only relevant to single
  *       IO mode)
+ * @dq: delivery queue for this internal abort command
  */
 static int
-hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
-                            struct domain_device *device,
-                            int abort_flag, int tag)
+_hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
+                             struct domain_device *device, int abort_flag,
+                             int tag, struct hisi_sas_dq *dq)
 {
        struct sas_task *task;
        struct hisi_sas_device *sas_dev = device->lldd_dev;
@@ -1893,11 +2044,11 @@ 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 + INTERNAL_ABORT_TIMEOUT*HZ;
+       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,
-                                               task, abort_flag, tag);
+                                               task, abort_flag, tag, dq);
        if (res) {
                del_timer(&task->slow_task->timer);
                dev_err(dev, "internal task abort: executing internal task failed: %d\n",
@@ -1923,6 +2074,7 @@ hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
                                slot->task = NULL;
                        }
                        dev_err(dev, "internal task abort: timeout and not done.\n");
+
                        res = -EIO;
                        goto exit;
                } else
@@ -1953,6 +2105,46 @@ exit:
        return res;
 }
 
+static int
+hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
+                            struct domain_device *device,
+                            int abort_flag, int tag)
+{
+       struct hisi_sas_slot *slot;
+       struct device *dev = hisi_hba->dev;
+       struct hisi_sas_dq *dq;
+       int i, rc;
+
+       switch (abort_flag) {
+       case HISI_SAS_INT_ABT_CMD:
+               slot = &hisi_hba->slot_info[tag];
+               dq = &hisi_hba->dq[slot->dlvry_queue];
+               return _hisi_sas_internal_task_abort(hisi_hba, device,
+                                                    abort_flag, tag, dq);
+       case HISI_SAS_INT_ABT_DEV:
+               for (i = 0; i < hisi_hba->cq_nvecs; i++) {
+                       struct hisi_sas_cq *cq = &hisi_hba->cq[i];
+                       const struct cpumask *mask = cq->pci_irq_mask;
+
+                       if (mask && !cpumask_intersects(cpu_online_mask, mask))
+                               continue;
+                       dq = &hisi_hba->dq[i];
+                       rc = _hisi_sas_internal_task_abort(hisi_hba, device,
+                                                          abort_flag, tag,
+                                                          dq);
+                       if (rc)
+                               return rc;
+               }
+               break;
+       default:
+               dev_err(dev, "Unrecognised internal abort flag (%d)\n",
+                       abort_flag);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
 {
        hisi_sas_port_notify_formed(sas_phy);
@@ -1972,9 +2164,18 @@ static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
 
 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
 {
+       struct asd_sas_phy *sas_phy = &phy->sas_phy;
+       struct sas_phy *sphy = sas_phy->phy;
+       struct sas_phy_data *d = sphy->hostdata;
+
        phy->phy_attached = 0;
        phy->phy_type = 0;
        phy->port = NULL;
+
+       if (d->enable)
+               sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
+       else
+               sphy->negotiated_linkrate = SAS_PHY_DISABLED;
 }
 
 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
@@ -2019,7 +2220,7 @@ void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
 {
        int i;
 
-       for (i = 0; i < hisi_hba->queue_count; i++) {
+       for (i = 0; i < hisi_hba->cq_nvecs; i++) {
                struct hisi_sas_cq *cq = &hisi_hba->cq[i];
 
                tasklet_kill(&cq->tasklet);
@@ -2048,14 +2249,18 @@ static struct sas_domain_function_template hisi_sas_transport_ops = {
 
 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
 {
-       int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
+       int i, s, j, max_command_entries = hisi_hba->hw->max_command_entries;
+       struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
 
        for (i = 0; i < hisi_hba->queue_count; i++) {
                struct hisi_sas_cq *cq = &hisi_hba->cq[i];
                struct hisi_sas_dq *dq = &hisi_hba->dq[i];
+               struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
+
+               s = sizeof(struct hisi_sas_cmd_hdr);
+               for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
+                       memset(&cmd_hdr[j], 0, s);
 
-               s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
-               memset(hisi_hba->cmd_hdr[i], 0, s);
                dq->wr_point = 0;
 
                s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
@@ -2072,12 +2277,13 @@ void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
        s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
        memset(hisi_hba->breakpoint, 0, s);
 
-       s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
-       memset(hisi_hba->sata_breakpoint, 0, s);
+       s = sizeof(struct hisi_sas_sata_breakpoint);
+       for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
+               memset(&sata_breakpoint[j], 0, s);
 }
 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
 
-int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
+int hisi_sas_alloc(struct hisi_hba *hisi_hba)
 {
        struct device *dev = hisi_hba->dev;
        int i, j, s, max_command_entries = hisi_hba->hw->max_command_entries;
@@ -2095,7 +2301,7 @@ int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
        for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
                hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
                hisi_hba->devices[i].device_id = i;
-               hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
+               hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
        }
 
        for (i = 0; i < hisi_hba->queue_count; i++) {
@@ -2131,10 +2337,9 @@ int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
 
        s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
        hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
-                                            GFP_KERNEL);
+                                            GFP_KERNEL | __GFP_ZERO);
        if (!hisi_hba->itct)
                goto err_out;
-       memset(hisi_hba->itct, 0, s);
 
        hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
                                           sizeof(struct hisi_sas_slot),
@@ -2144,19 +2349,24 @@ int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
 
        /* roundup to avoid overly large block size */
        max_command_entries_ru = roundup(max_command_entries, 64);
-       sz_slot_buf_ru = roundup(sizeof(struct hisi_sas_slot_buf_table), 64);
+       if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
+               sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
+       else
+               sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
+       sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
        s = lcm(max_command_entries_ru, sz_slot_buf_ru);
        blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
        slots_per_blk = s / sz_slot_buf_ru;
+
        for (i = 0; i < blk_cnt; i++) {
-               struct hisi_sas_slot_buf_table *buf;
-               dma_addr_t buf_dma;
                int slot_index = i * slots_per_blk;
+               dma_addr_t buf_dma;
+               void *buf;
 
-               buf = dmam_alloc_coherent(dev, s, &buf_dma, GFP_KERNEL);
+               buf = dmam_alloc_coherent(dev, s, &buf_dma,
+                                         GFP_KERNEL | __GFP_ZERO);
                if (!buf)
                        goto err_out;
-               memset(buf, 0, s);
 
                for (j = 0; j < slots_per_blk; j++, slot_index++) {
                        struct hisi_sas_slot *slot;
@@ -2166,8 +2376,8 @@ int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
                        slot->buf_dma = buf_dma;
                        slot->idx = slot_index;
 
-                       buf++;
-                       buf_dma += sizeof(*buf);
+                       buf += sz_slot_buf_ru;
+                       buf_dma += sz_slot_buf_ru;
                }
        }
 
@@ -2323,6 +2533,7 @@ static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
        struct Scsi_Host *shost;
        struct hisi_hba *hisi_hba;
        struct device *dev = &pdev->dev;
+       int error;
 
        shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
        if (!shost) {
@@ -2343,8 +2554,11 @@ static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
        if (hisi_sas_get_fw_info(hisi_hba) < 0)
                goto err_out;
 
-       if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
-           dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
+       error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
+       if (error)
+               error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
+
+       if (error) {
                dev_err(dev, "No usable DMA addressing method\n");
                goto err_out;
        }
@@ -2361,7 +2575,7 @@ static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
                        goto err_out;
        }
 
-       if (hisi_sas_alloc(hisi_hba, shost)) {
+       if (hisi_sas_alloc(hisi_hba)) {
                hisi_sas_free(hisi_hba);
                goto err_out;
        }
@@ -2457,6 +2671,555 @@ err_out_ha:
 }
 EXPORT_SYMBOL_GPL(hisi_sas_probe);
 
+struct dentry *hisi_sas_debugfs_dir;
+
+static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba)
+{
+       int queue_entry_size = hisi_hba->hw->complete_hdr_size;
+       int i;
+
+       for (i = 0; i < hisi_hba->queue_count; i++)
+               memcpy(hisi_hba->debugfs_complete_hdr[i],
+                      hisi_hba->complete_hdr[i],
+                      HISI_SAS_QUEUE_SLOTS * queue_entry_size);
+}
+
+static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba)
+{
+       int queue_entry_size = sizeof(struct hisi_sas_cmd_hdr);
+       int i;
+
+       for (i = 0; i < hisi_hba->queue_count; i++) {
+               struct hisi_sas_cmd_hdr *debugfs_cmd_hdr, *cmd_hdr;
+               int j;
+
+               debugfs_cmd_hdr = hisi_hba->debugfs_cmd_hdr[i];
+               cmd_hdr = hisi_hba->cmd_hdr[i];
+
+               for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
+                       memcpy(&debugfs_cmd_hdr[j], &cmd_hdr[j],
+                              queue_entry_size);
+       }
+}
+
+static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba)
+{
+       const struct hisi_sas_debugfs_reg *port =
+               hisi_hba->hw->debugfs_reg_port;
+       int i, phy_cnt;
+       u32 offset;
+       u32 *databuf;
+
+       for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
+               databuf = (u32 *)hisi_hba->debugfs_port_reg[phy_cnt];
+               for (i = 0; i < port->count; i++, databuf++) {
+                       offset = port->base_off + 4 * i;
+                       *databuf = port->read_port_reg(hisi_hba, phy_cnt,
+                                                      offset);
+               }
+       }
+}
+
+static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba)
+{
+       u32 *databuf = (u32 *)hisi_hba->debugfs_global_reg;
+       const struct hisi_sas_debugfs_reg *global =
+               hisi_hba->hw->debugfs_reg_global;
+       int i;
+
+       for (i = 0; i < global->count; i++, databuf++)
+               *databuf = global->read_global_reg(hisi_hba, 4 * i);
+}
+
+static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba)
+{
+       void *databuf = hisi_hba->debugfs_itct;
+       struct hisi_sas_itct *itct;
+       int i;
+
+       itct = hisi_hba->itct;
+
+       for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
+               memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
+               databuf += sizeof(struct hisi_sas_itct);
+       }
+}
+
+static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba)
+{
+       int max_command_entries = hisi_hba->hw->max_command_entries;
+       void *databuf = hisi_hba->debugfs_iost;
+       struct hisi_sas_iost *iost;
+       int i;
+
+       iost = hisi_hba->iost;
+
+       for (i = 0; i < max_command_entries; i++, iost++) {
+               memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
+               databuf += sizeof(struct hisi_sas_iost);
+       }
+}
+
+static const char *
+hisi_sas_debugfs_to_reg_name(int off, int base_off,
+                            const struct hisi_sas_debugfs_reg_lu *lu)
+{
+       for (; lu->name; lu++) {
+               if (off == lu->off - base_off)
+                       return lu->name;
+       }
+
+       return NULL;
+}
+
+static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr,
+                                      struct seq_file *s)
+{
+       const struct hisi_sas_debugfs_reg *reg = ptr;
+       int i;
+
+       for (i = 0; i < reg->count; i++) {
+               int off = i * 4;
+               const char *name;
+
+               name = hisi_sas_debugfs_to_reg_name(off, reg->base_off,
+                                                   reg->lu);
+
+               if (name)
+                       seq_printf(s, "0x%08x 0x%08x %s\n", off,
+                                  regs_val[i], name);
+               else
+                       seq_printf(s, "0x%08x 0x%08x\n", off,
+                                  regs_val[i]);
+       }
+}
+
+static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p)
+{
+       struct hisi_hba *hisi_hba = s->private;
+       const struct hisi_sas_hw *hw = hisi_hba->hw;
+       const struct hisi_sas_debugfs_reg *reg_global = hw->debugfs_reg_global;
+
+       hisi_sas_debugfs_print_reg(hisi_hba->debugfs_global_reg,
+                                  reg_global, s);
+
+       return 0;
+}
+
+static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp)
+{
+       return single_open(filp, hisi_sas_debugfs_global_show,
+                          inode->i_private);
+}
+
+static const struct file_operations hisi_sas_debugfs_global_fops = {
+       .open = hisi_sas_debugfs_global_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = single_release,
+       .owner = THIS_MODULE,
+};
+
+static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p)
+{
+       struct hisi_sas_phy *phy = s->private;
+       struct hisi_hba *hisi_hba = phy->hisi_hba;
+       const struct hisi_sas_hw *hw = hisi_hba->hw;
+       const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port;
+       u32 *databuf = hisi_hba->debugfs_port_reg[phy->sas_phy.id];
+
+       hisi_sas_debugfs_print_reg(databuf, reg_port, s);
+
+       return 0;
+}
+
+static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp)
+{
+       return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private);
+}
+
+static const struct file_operations hisi_sas_debugfs_port_fops = {
+       .open = hisi_sas_debugfs_port_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = single_release,
+       .owner = THIS_MODULE,
+};
+
+static int hisi_sas_show_row_64(struct seq_file *s, int index,
+                               int sz, __le64 *ptr)
+{
+       int i;
+
+       /* completion header size not fixed per HW version */
+       seq_printf(s, "index %04d:\n\t", index);
+       for (i = 1; i <= sz / 8; i++, ptr++) {
+               seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
+               if (!(i % 2))
+                       seq_puts(s, "\n\t");
+       }
+
+       seq_puts(s, "\n");
+
+       return 0;
+}
+
+static int hisi_sas_show_row_32(struct seq_file *s, int index,
+                               int sz, __le32 *ptr)
+{
+       int i;
+
+       /* completion header size not fixed per HW version */
+       seq_printf(s, "index %04d:\n\t", index);
+       for (i = 1; i <= sz / 4; i++, ptr++) {
+               seq_printf(s, " 0x%08x", le32_to_cpu(*ptr));
+               if (!(i % 4))
+                       seq_puts(s, "\n\t");
+       }
+       seq_puts(s, "\n");
+
+       return 0;
+}
+
+static int hisi_sas_cq_show_slot(struct seq_file *s, int slot, void *cq_ptr)
+{
+       struct hisi_sas_cq *cq = cq_ptr;
+       struct hisi_hba *hisi_hba = cq->hisi_hba;
+       void *complete_queue = hisi_hba->debugfs_complete_hdr[cq->id];
+       __le32 *complete_hdr = complete_queue +
+                       (hisi_hba->hw->complete_hdr_size * slot);
+
+       return hisi_sas_show_row_32(s, slot,
+                               hisi_hba->hw->complete_hdr_size,
+                               complete_hdr);
+}
+
+static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p)
+{
+       struct hisi_sas_cq *cq = s->private;
+       int slot, ret;
+
+       for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
+               ret = hisi_sas_cq_show_slot(s, slot, cq);
+               if (ret)
+                       return ret;
+       }
+       return 0;
+}
+
+static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp)
+{
+       return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private);
+}
+
+static const struct file_operations hisi_sas_debugfs_cq_fops = {
+       .open = hisi_sas_debugfs_cq_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = single_release,
+       .owner = THIS_MODULE,
+};
+
+static int hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr)
+{
+       struct hisi_sas_dq *dq = dq_ptr;
+       struct hisi_hba *hisi_hba = dq->hisi_hba;
+       void *cmd_queue = hisi_hba->debugfs_cmd_hdr[dq->id];
+       __le32 *cmd_hdr = cmd_queue +
+               sizeof(struct hisi_sas_cmd_hdr) * slot;
+
+       return hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr),
+                                   cmd_hdr);
+}
+
+static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p)
+{
+       int slot, ret;
+
+       for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
+               ret = hisi_sas_dq_show_slot(s, slot, s->private);
+               if (ret)
+                       return ret;
+       }
+       return 0;
+}
+
+static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp)
+{
+       return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private);
+}
+
+static const struct file_operations hisi_sas_debugfs_dq_fops = {
+       .open = hisi_sas_debugfs_dq_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = single_release,
+       .owner = THIS_MODULE,
+};
+
+static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p)
+{
+       struct hisi_hba *hisi_hba = s->private;
+       struct hisi_sas_iost *debugfs_iost = hisi_hba->debugfs_iost;
+       int i, ret, max_command_entries = hisi_hba->hw->max_command_entries;
+       __le64 *iost = &debugfs_iost->qw0;
+
+       for (i = 0; i < max_command_entries; i++, debugfs_iost++) {
+               ret = hisi_sas_show_row_64(s, i, sizeof(*debugfs_iost),
+                                          iost);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
+static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp)
+{
+       return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private);
+}
+
+static const struct file_operations hisi_sas_debugfs_iost_fops = {
+       .open = hisi_sas_debugfs_iost_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = single_release,
+       .owner = THIS_MODULE,
+};
+
+static int hisi_sas_debugfs_itct_show(struct seq_file *s, void *p)
+{
+       int i, ret;
+       struct hisi_hba *hisi_hba = s->private;
+       struct hisi_sas_itct *debugfs_itct = hisi_hba->debugfs_itct;
+       __le64 *itct = &debugfs_itct->qw0;
+
+       for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, debugfs_itct++) {
+               ret = hisi_sas_show_row_64(s, i, sizeof(*debugfs_itct),
+                                          itct);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
+static int hisi_sas_debugfs_itct_open(struct inode *inode, struct file *filp)
+{
+       return single_open(filp, hisi_sas_debugfs_itct_show, inode->i_private);
+}
+
+static const struct file_operations hisi_sas_debugfs_itct_fops = {
+       .open = hisi_sas_debugfs_itct_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = single_release,
+       .owner = THIS_MODULE,
+};
+
+static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba)
+{
+       struct dentry *dump_dentry;
+       struct dentry *dentry;
+       char name[256];
+       int p;
+       int c;
+       int d;
+
+       /* Create dump dir inside device dir */
+       dump_dentry = debugfs_create_dir("dump", hisi_hba->debugfs_dir);
+       hisi_hba->debugfs_dump_dentry = dump_dentry;
+
+       debugfs_create_file("global", 0400, dump_dentry, hisi_hba,
+                           &hisi_sas_debugfs_global_fops);
+
+       /* Create port dir and files */
+       dentry = debugfs_create_dir("port", dump_dentry);
+       for (p = 0; p < hisi_hba->n_phy; p++) {
+               snprintf(name, 256, "%d", p);
+
+               debugfs_create_file(name, 0400, dentry, &hisi_hba->phy[p],
+                                   &hisi_sas_debugfs_port_fops);
+       }
+
+       /* Create CQ dir and files */
+       dentry = debugfs_create_dir("cq", dump_dentry);
+       for (c = 0; c < hisi_hba->queue_count; c++) {
+               snprintf(name, 256, "%d", c);
+
+               debugfs_create_file(name, 0400, dentry, &hisi_hba->cq[c],
+                                   &hisi_sas_debugfs_cq_fops);
+       }
+
+       /* Create DQ dir and files */
+       dentry = debugfs_create_dir("dq", dump_dentry);
+       for (d = 0; d < hisi_hba->queue_count; d++) {
+               snprintf(name, 256, "%d", d);
+
+               debugfs_create_file(name, 0400, dentry, &hisi_hba->dq[d],
+                                   &hisi_sas_debugfs_dq_fops);
+       }
+
+       debugfs_create_file("iost", 0400, dump_dentry, hisi_hba,
+                           &hisi_sas_debugfs_iost_fops);
+
+       debugfs_create_file("itct", 0400, dump_dentry, hisi_hba,
+                           &hisi_sas_debugfs_itct_fops);
+
+       return;
+}
+
+static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba)
+{
+       hisi_hba->hw->snapshot_prepare(hisi_hba);
+
+       hisi_sas_debugfs_snapshot_global_reg(hisi_hba);
+       hisi_sas_debugfs_snapshot_port_reg(hisi_hba);
+       hisi_sas_debugfs_snapshot_cq_reg(hisi_hba);
+       hisi_sas_debugfs_snapshot_dq_reg(hisi_hba);
+       hisi_sas_debugfs_snapshot_itct_reg(hisi_hba);
+       hisi_sas_debugfs_snapshot_iost_reg(hisi_hba);
+
+       hisi_sas_debugfs_create_files(hisi_hba);
+
+       hisi_hba->hw->snapshot_restore(hisi_hba);
+}
+
+static ssize_t hisi_sas_debugfs_trigger_dump_write(struct file *file,
+                                                  const char __user *user_buf,
+                                                  size_t count, loff_t *ppos)
+{
+       struct hisi_hba *hisi_hba = file->f_inode->i_private;
+       char buf[8];
+
+       /* A bit racy, but don't care too much since it's only debugfs */
+       if (hisi_hba->debugfs_snapshot)
+               return -EFAULT;
+
+       if (count > 8)
+               return -EFAULT;
+
+       if (copy_from_user(buf, user_buf, count))
+               return -EFAULT;
+
+       if (buf[0] != '1')
+               return -EFAULT;
+
+       queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
+
+       return count;
+}
+
+static const struct file_operations hisi_sas_debugfs_trigger_dump_fops = {
+       .write = &hisi_sas_debugfs_trigger_dump_write,
+       .owner = THIS_MODULE,
+};
+
+void hisi_sas_debugfs_work_handler(struct work_struct *work)
+{
+       struct hisi_hba *hisi_hba =
+               container_of(work, struct hisi_hba, debugfs_work);
+
+       if (hisi_hba->debugfs_snapshot)
+               return;
+       hisi_hba->debugfs_snapshot = true;
+
+       hisi_sas_debugfs_snapshot_regs(hisi_hba);
+}
+EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler);
+
+void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba)
+{
+       int max_command_entries = hisi_hba->hw->max_command_entries;
+       struct device *dev = hisi_hba->dev;
+       int p, i, c, d;
+       size_t sz;
+
+       hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
+                                                  hisi_sas_debugfs_dir);
+       debugfs_create_file("trigger_dump", 0600,
+                           hisi_hba->debugfs_dir,
+                           hisi_hba,
+                           &hisi_sas_debugfs_trigger_dump_fops);
+
+       /* Alloc buffer for global */
+       sz = hisi_hba->hw->debugfs_reg_global->count * 4;
+       hisi_hba->debugfs_global_reg =
+               devm_kmalloc(dev, sz, GFP_KERNEL);
+
+       if (!hisi_hba->debugfs_global_reg)
+               goto fail_global;
+
+       /* Alloc buffer for port */
+       sz = hisi_hba->hw->debugfs_reg_port->count * 4;
+       for (p = 0; p < hisi_hba->n_phy; p++) {
+               hisi_hba->debugfs_port_reg[p] =
+                       devm_kmalloc(dev, sz, GFP_KERNEL);
+
+               if (!hisi_hba->debugfs_port_reg[p])
+                       goto fail_port;
+       }
+
+       /* Alloc buffer for cq */
+       sz = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
+       for (c = 0; c < hisi_hba->queue_count; c++) {
+               hisi_hba->debugfs_complete_hdr[c] =
+                       devm_kmalloc(dev, sz, GFP_KERNEL);
+
+               if (!hisi_hba->debugfs_complete_hdr[c])
+                       goto fail_cq;
+       }
+
+       /* Alloc buffer for dq */
+       sz = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
+       for (d = 0; d < hisi_hba->queue_count; d++) {
+               hisi_hba->debugfs_cmd_hdr[d] =
+                       devm_kmalloc(dev, sz, GFP_KERNEL);
+
+               if (!hisi_hba->debugfs_cmd_hdr[d])
+                       goto fail_iost_dq;
+       }
+
+       /* Alloc buffer for iost */
+       sz = max_command_entries * sizeof(struct hisi_sas_iost);
+
+       hisi_hba->debugfs_iost = devm_kmalloc(dev, sz, GFP_KERNEL);
+       if (!hisi_hba->debugfs_iost)
+               goto fail_iost_dq;
+
+       /* Alloc buffer for itct */
+       /* New memory allocation must be locate before itct */
+       sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
+
+       hisi_hba->debugfs_itct = devm_kmalloc(dev, sz, GFP_KERNEL);
+       if (!hisi_hba->debugfs_itct)
+               goto fail_itct;
+
+       return;
+fail_itct:
+       devm_kfree(dev, hisi_hba->debugfs_iost);
+fail_iost_dq:
+       for (i = 0; i < d; i++)
+               devm_kfree(dev, hisi_hba->debugfs_cmd_hdr[i]);
+fail_cq:
+       for (i = 0; i < c; i++)
+               devm_kfree(dev, hisi_hba->debugfs_complete_hdr[i]);
+fail_port:
+       for (i = 0; i < p; i++)
+               devm_kfree(dev, hisi_hba->debugfs_port_reg[i]);
+       devm_kfree(dev, hisi_hba->debugfs_global_reg);
+fail_global:
+       debugfs_remove_recursive(hisi_hba->debugfs_dir);
+       dev_dbg(dev, "failed to init debugfs!\n");
+}
+EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init);
+
+void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba)
+{
+       debugfs_remove_recursive(hisi_hba->debugfs_dir);
+}
+EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit);
+
 int hisi_sas_remove(struct platform_device *pdev)
 {
        struct sas_ha_struct *sha = platform_get_drvdata(pdev);
@@ -2475,18 +3238,28 @@ int hisi_sas_remove(struct platform_device *pdev)
 }
 EXPORT_SYMBOL_GPL(hisi_sas_remove);
 
+bool hisi_sas_debugfs_enable;
+EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
+module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
+MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)");
+
 static __init int hisi_sas_init(void)
 {
        hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
        if (!hisi_sas_stt)
                return -ENOMEM;
 
+       if (hisi_sas_debugfs_enable)
+               hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
+
        return 0;
 }
 
 static __exit void hisi_sas_exit(void)
 {
        sas_release_transport(hisi_sas_stt);
+
+       debugfs_remove(hisi_sas_debugfs_dir);
 }
 
 module_init(hisi_sas_init);