]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - drivers/scsi/hisi_sas/hisi_sas_v3_hw.c
scsi: hisi_sas: Add SATA FIS check for v3 hw
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / hisi_sas / hisi_sas_v3_hw.c
index ec86201e6a0aa9fa3910d6443419c92389e770b5..7a5a028d3f2168080397760b582b4d09ea60328c 100644 (file)
@@ -51,7 +51,6 @@
 #define CFG_ABT_SET_IPTT_DONE  0xd8
 #define CFG_ABT_SET_IPTT_DONE_OFF      0
 #define HGC_IOMB_PROC1_STATUS  0x104
-#define CFG_1US_TIMER_TRSH             0xcc
 #define CHNL_INT_STATUS                        0x148
 #define HGC_AXI_FIFO_ERR_INFO  0x154
 #define AXI_ERR_INFO_OFF               0
@@ -92,6 +91,7 @@
 #define SAS_ECC_INTR                   0x1e8
 #define SAS_ECC_INTR_MSK               0x1ec
 #define HGC_ERR_STAT_EN                        0x238
+#define CQE_SEND_CNT                   0x248
 #define DLVRY_Q_0_BASE_ADDR_LO         0x260
 #define DLVRY_Q_0_BASE_ADDR_HI         0x264
 #define DLVRY_Q_0_DEPTH                        0x268
 #define AWQOS_AWCACHE_CFG      0xc84
 #define ARQOS_ARCACHE_CFG      0xc88
 #define HILINK_ERR_DFX         0xe04
+#define SAS_GPIO_CFG_0         0x1000
+#define SAS_GPIO_CFG_1         0x1004
+#define SAS_GPIO_TX_0_1        0x1040
+#define SAS_CFG_DRIVE_VLD      0x1070
 
 /* phy registers requiring init */
 #define PORT_BASE                      (0x2000)
 #define PHY_CFG_ENA_MSK                        (0x1 << PHY_CFG_ENA_OFF)
 #define PHY_CFG_DC_OPT_OFF             2
 #define PHY_CFG_DC_OPT_MSK             (0x1 << PHY_CFG_DC_OPT_OFF)
+#define PHY_CFG_PHY_RST_OFF            3
+#define PHY_CFG_PHY_RST_MSK            (0x1 << PHY_CFG_PHY_RST_OFF)
 #define PROG_PHY_LINK_RATE             (PORT_BASE + 0x8)
 #define PHY_CTRL                       (PORT_BASE + 0x14)
 #define PHY_CTRL_RESET_OFF             0
 #define SL_CONTROL_NOTIFY_EN_MSK       (0x1 << SL_CONTROL_NOTIFY_EN_OFF)
 #define SL_CTA_OFF             17
 #define SL_CTA_MSK             (0x1 << SL_CTA_OFF)
+#define RX_PRIMS_STATUS                        (PORT_BASE + 0x98)
+#define RX_BCAST_CHG_OFF               1
+#define RX_BCAST_CHG_MSK               (0x1 << RX_BCAST_CHG_OFF)
 #define TX_ID_DWORD0                   (PORT_BASE + 0x9c)
 #define TX_ID_DWORD1                   (PORT_BASE + 0xa0)
 #define TX_ID_DWORD2                   (PORT_BASE + 0xa4)
 
 #define AXI_MASTER_CFG_BASE            (0x5000)
 #define AM_CTRL_GLOBAL                 (0x0)
+#define AM_CTRL_SHUTDOWN_REQ_OFF       0
+#define AM_CTRL_SHUTDOWN_REQ_MSK       (0x1 << AM_CTRL_SHUTDOWN_REQ_OFF)
 #define AM_CURR_TRANS_RETURN   (0x150)
 
 #define AM_CFG_MAX_TRANS               (0x5010)
@@ -420,7 +431,6 @@ static void init_reg_v3_hw(struct hisi_hba *hisi_hba)
                         (u32)((1ULL << hisi_hba->queue_count) - 1));
        hisi_sas_write32(hisi_hba, CFG_MAX_TAG, 0xfff0400);
        hisi_sas_write32(hisi_hba, HGC_SAS_TXFAIL_RETRY_CTRL, 0x108);
-       hisi_sas_write32(hisi_hba, CFG_1US_TIMER_TRSH, 0xd);
        hisi_sas_write32(hisi_hba, INT_COAL_EN, 0x1);
        hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x1);
        hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 0x1);
@@ -481,6 +491,7 @@ static void init_reg_v3_hw(struct hisi_hba *hisi_hba)
                hisi_sas_phy_write32(hisi_hba, i, SL_RX_BCAST_CHK_MSK, 0x0);
                hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_OOB_RESTART_MSK, 0x1);
                hisi_sas_phy_write32(hisi_hba, i, STP_LINK_TIMER, 0x7f7a120);
+               hisi_sas_phy_write32(hisi_hba, i, CON_CFG_DRIVER, 0x2a0a01);
 
                /* used for 12G negotiate */
                hisi_sas_phy_write32(hisi_hba, i, COARSETUNE_TIME, 0x1e);
@@ -549,6 +560,14 @@ static void init_reg_v3_hw(struct hisi_hba *hisi_hba)
        hisi_sas_write32(hisi_hba, SAS_RAS_INTR1_MASK, 0x0);
        hisi_sas_write32(hisi_hba, SAS_RAS_INTR2_MASK, 0x0);
        hisi_sas_write32(hisi_hba, CFG_SAS_RAS_INTR_MASK, 0x0);
+
+       /* LED registers init */
+       hisi_sas_write32(hisi_hba, SAS_CFG_DRIVE_VLD, 0x80000ff);
+       hisi_sas_write32(hisi_hba, SAS_GPIO_TX_0_1, 0x80808080);
+       hisi_sas_write32(hisi_hba, SAS_GPIO_TX_0_1 + 0x4, 0x80808080);
+       /* Configure blink generator rate A to 1Hz and B to 4Hz */
+       hisi_sas_write32(hisi_hba, SAS_GPIO_CFG_1, 0x121700);
+       hisi_sas_write32(hisi_hba, SAS_GPIO_CFG_0, 0x800000);
 }
 
 static void config_phy_opt_mode_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
@@ -745,15 +764,25 @@ static void enable_phy_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
        u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
 
        cfg |= PHY_CFG_ENA_MSK;
+       cfg &= ~PHY_CFG_PHY_RST_MSK;
        hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
 }
 
 static void disable_phy_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
 {
        u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
+       u32 state;
 
        cfg &= ~PHY_CFG_ENA_MSK;
        hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
+
+       mdelay(50);
+
+       state = hisi_sas_read32(hisi_hba, PHY_STATE);
+       if (state & BIT(phy_no)) {
+               cfg |= PHY_CFG_PHY_RST_MSK;
+               hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
+       }
 }
 
 static void start_phy_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
@@ -840,42 +869,54 @@ get_free_slot_v3_hw(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq)
        r = hisi_sas_read32_relaxed(hisi_hba,
                                DLVRY_Q_0_RD_PTR + (queue * 0x14));
        if (r == (w+1) % HISI_SAS_QUEUE_SLOTS) {
-               dev_warn(dev, "full queue=%d r=%d w=%d\n\n",
+               dev_warn(dev, "full queue=%d r=%d w=%d\n",
                                queue, r, w);
                return -EAGAIN;
        }
 
-       return 0;
+       dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
+
+       return w;
 }
 
 static void start_delivery_v3_hw(struct hisi_sas_dq *dq)
 {
        struct hisi_hba *hisi_hba = dq->hisi_hba;
-       int dlvry_queue = dq->slot_prep->dlvry_queue;
-       int dlvry_queue_slot = dq->slot_prep->dlvry_queue_slot;
+       struct hisi_sas_slot *s, *s1, *s2 = NULL;
+       struct list_head *dq_list;
+       int dlvry_queue = dq->id;
+       int wp;
+
+       dq_list = &dq->list;
+       list_for_each_entry_safe(s, s1, &dq->list, delivery) {
+               if (!s->ready)
+                       break;
+               s2 = s;
+               list_del(&s->delivery);
+       }
 
-       dq->wr_point = ++dlvry_queue_slot % HISI_SAS_QUEUE_SLOTS;
-       hisi_sas_write32(hisi_hba, DLVRY_Q_0_WR_PTR + (dlvry_queue * 0x14),
-                        dq->wr_point);
+       if (!s2)
+               return;
+
+       /*
+        * Ensure that memories for slots built on other CPUs is observed.
+        */
+       smp_rmb();
+       wp = (s2->dlvry_queue_slot + 1) % HISI_SAS_QUEUE_SLOTS;
+
+       hisi_sas_write32(hisi_hba, DLVRY_Q_0_WR_PTR + (dlvry_queue * 0x14), wp);
 }
 
-static int prep_prd_sge_v3_hw(struct hisi_hba *hisi_hba,
+static void prep_prd_sge_v3_hw(struct hisi_hba *hisi_hba,
                              struct hisi_sas_slot *slot,
                              struct hisi_sas_cmd_hdr *hdr,
                              struct scatterlist *scatter,
                              int n_elem)
 {
        struct hisi_sas_sge_page *sge_page = hisi_sas_sge_addr_mem(slot);
-       struct device *dev = hisi_hba->dev;
        struct scatterlist *sg;
        int i;
 
-       if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
-               dev_err(dev, "prd err: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
-                       n_elem);
-               return -EINVAL;
-       }
-
        for_each_sg(scatter, sg, n_elem, i) {
                struct hisi_sas_sge *entry = &sge_page->sge[i];
 
@@ -888,13 +929,10 @@ static int prep_prd_sge_v3_hw(struct hisi_hba *hisi_hba,
        hdr->prd_table_addr = cpu_to_le64(hisi_sas_sge_addr_dma(slot));
 
        hdr->sg_len = cpu_to_le32(n_elem << CMD_HDR_DATA_SGL_LEN_OFF);
-
-       return 0;
 }
 
-static int prep_ssp_v3_hw(struct hisi_hba *hisi_hba,
-                         struct hisi_sas_slot *slot, int is_tmf,
-                         struct hisi_sas_tmf_task *tmf)
+static void prep_ssp_v3_hw(struct hisi_hba *hisi_hba,
+                         struct hisi_sas_slot *slot)
 {
        struct sas_task *task = slot->task;
        struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
@@ -903,7 +941,8 @@ static int prep_ssp_v3_hw(struct hisi_hba *hisi_hba,
        struct hisi_sas_port *port = slot->port;
        struct sas_ssp_task *ssp_task = &task->ssp_task;
        struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
-       int has_data = 0, rc, priority = is_tmf;
+       struct hisi_sas_tmf_task *tmf = slot->tmf;
+       int has_data = 0, priority = !!tmf;
        u8 *buf_cmd;
        u32 dw1 = 0, dw2 = 0;
 
@@ -914,7 +953,7 @@ static int prep_ssp_v3_hw(struct hisi_hba *hisi_hba,
                               (1 << CMD_HDR_CMD_OFF)); /* ssp */
 
        dw1 = 1 << CMD_HDR_VDTL_OFF;
-       if (is_tmf) {
+       if (tmf) {
                dw1 |= 2 << CMD_HDR_FRAME_TYPE_OFF;
                dw1 |= DIR_NO_DATA << CMD_HDR_DIR_OFF;
        } else {
@@ -944,12 +983,9 @@ static int prep_ssp_v3_hw(struct hisi_hba *hisi_hba,
        hdr->dw2 = cpu_to_le32(dw2);
        hdr->transfer_tags = cpu_to_le32(slot->idx);
 
-       if (has_data) {
-               rc = prep_prd_sge_v3_hw(hisi_hba, slot, hdr, task->scatter,
+       if (has_data)
+               prep_prd_sge_v3_hw(hisi_hba, slot, hdr, task->scatter,
                                        slot->n_elem);
-               if (rc)
-                       return rc;
-       }
 
        hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
        hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
@@ -959,7 +995,7 @@ static int prep_ssp_v3_hw(struct hisi_hba *hisi_hba,
                sizeof(struct ssp_frame_hdr);
 
        memcpy(buf_cmd, &task->ssp_task.LUN, 8);
-       if (!is_tmf) {
+       if (!tmf) {
                buf_cmd[9] = ssp_task->task_attr | (ssp_task->task_prio << 3);
                memcpy(buf_cmd + 12, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
        } else {
@@ -976,11 +1012,9 @@ static int prep_ssp_v3_hw(struct hisi_hba *hisi_hba,
                        break;
                }
        }
-
-       return 0;
 }
 
-static int prep_smp_v3_hw(struct hisi_hba *hisi_hba,
+static void prep_smp_v3_hw(struct hisi_hba *hisi_hba,
                          struct hisi_sas_slot *slot)
 {
        struct sas_task *task = slot->task;
@@ -1018,10 +1052,9 @@ static int prep_smp_v3_hw(struct hisi_hba *hisi_hba,
        hdr->cmd_table_addr = cpu_to_le64(req_dma_addr);
        hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
 
-       return 0;
 }
 
-static int prep_ata_v3_hw(struct hisi_hba *hisi_hba,
+static void prep_ata_v3_hw(struct hisi_hba *hisi_hba,
                          struct hisi_sas_slot *slot)
 {
        struct sas_task *task = slot->task;
@@ -1032,7 +1065,7 @@ static int prep_ata_v3_hw(struct hisi_hba *hisi_hba,
        struct asd_sas_port *sas_port = device->port;
        struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
        u8 *buf_cmd;
-       int has_data = 0, rc = 0, hdr_tag = 0;
+       int has_data = 0, hdr_tag = 0;
        u32 dw1 = 0, dw2 = 0;
 
        hdr->dw0 = cpu_to_le32(port->id << CMD_HDR_PORT_OFF);
@@ -1081,12 +1114,9 @@ static int prep_ata_v3_hw(struct hisi_hba *hisi_hba,
        /* dw3 */
        hdr->transfer_tags = cpu_to_le32(slot->idx);
 
-       if (has_data) {
-               rc = prep_prd_sge_v3_hw(hisi_hba, slot, hdr, task->scatter,
+       if (has_data)
+               prep_prd_sge_v3_hw(hisi_hba, slot, hdr, task->scatter,
                                        slot->n_elem);
-               if (rc)
-                       return rc;
-       }
 
        hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
        hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
@@ -1098,11 +1128,9 @@ static int prep_ata_v3_hw(struct hisi_hba *hisi_hba,
                task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
        /* fill in command FIS */
        memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
-
-       return 0;
 }
 
-static int prep_abort_v3_hw(struct hisi_hba *hisi_hba,
+static void prep_abort_v3_hw(struct hisi_hba *hisi_hba,
                struct hisi_sas_slot *slot,
                int device_id, int abort_flag, int tag_to_abort)
 {
@@ -1127,7 +1155,6 @@ static int prep_abort_v3_hw(struct hisi_hba *hisi_hba,
        hdr->dw7 = cpu_to_le32(tag_to_abort << CMD_HDR_ABORT_IPTT_OFF);
        hdr->transfer_tags = cpu_to_le32(slot->idx);
 
-       return 0;
 }
 
 static irqreturn_t phy_up_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
@@ -1137,6 +1164,7 @@ static irqreturn_t phy_up_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
        struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
        struct asd_sas_phy *sas_phy = &phy->sas_phy;
        struct device *dev = hisi_hba->dev;
+       unsigned long flags;
 
        hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 1);
 
@@ -1163,6 +1191,16 @@ static irqreturn_t phy_up_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
                dev_info(dev, "phyup: phy%d link_rate=%d(sata)\n", phy_no, link_rate);
                initial_fis = &hisi_hba->initial_fis[phy_no];
                fis = &initial_fis->fis;
+
+               /* check ERR bit of Status Register */
+               if (fis->status & ATA_ERR) {
+                       dev_warn(dev, "sata int: phy%d FIS status: 0x%x\n",
+                                phy_no, fis->status);
+                       hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
+                       res = IRQ_NONE;
+                       goto end;
+               }
+
                sas_phy->oob_mode = SATA_OOB_MODE;
                attached_sas_addr[0] = 0x50;
                attached_sas_addr[7] = phy_no;
@@ -1205,6 +1243,12 @@ static irqreturn_t phy_up_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
        phy->phy_attached = 1;
        hisi_sas_notify_phy_event(phy, HISI_PHYE_PHY_UP);
        res = IRQ_HANDLED;
+       spin_lock_irqsave(&phy->lock, flags);
+       if (phy->reset_completion) {
+               phy->in_reset = 0;
+               complete(phy->reset_completion);
+       }
+       spin_unlock_irqrestore(&phy->lock, flags);
 end:
        hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
                             CHL_INT0_SL_PHY_ENABLE_MSK);
@@ -1243,9 +1287,13 @@ static irqreturn_t phy_bcast_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
        struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
        struct asd_sas_phy *sas_phy = &phy->sas_phy;
        struct sas_ha_struct *sas_ha = &hisi_hba->sha;
+       u32 bcast_status;
 
        hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 1);
-       sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
+       bcast_status = hisi_sas_phy_read32(hisi_hba, phy_no, RX_PRIMS_STATUS);
+       if ((bcast_status & RX_BCAST_CHG_MSK) &&
+           !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
+               sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
        hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
                             CHL_INT0_SL_RX_BCST_ACK_MSK);
        hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 0);
@@ -1314,11 +1362,77 @@ static const struct hisi_sas_hw_error port_axi_error[] = {
        },
 };
 
-static irqreturn_t int_chnl_int_v3_hw(int irq_no, void *p)
+static void handle_chl_int1_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
 {
-       struct hisi_hba *hisi_hba = p;
+       u32 irq_value = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT1);
+       u32 irq_msk = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT1_MSK);
        struct device *dev = hisi_hba->dev;
+       int i;
+
+       irq_value &= ~irq_msk;
+       if (!irq_value)
+               return;
+
+       for (i = 0; i < ARRAY_SIZE(port_axi_error); i++) {
+               const struct hisi_sas_hw_error *error = &port_axi_error[i];
+
+               if (!(irq_value & error->irq_msk))
+                       continue;
+
+               dev_err(dev, "%s error (phy%d 0x%x) found!\n",
+                       error->msg, phy_no, irq_value);
+               queue_work(hisi_hba->wq, &hisi_hba->rst_work);
+       }
+
+       hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT1, irq_value);
+}
+
+static void handle_chl_int2_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
+{
+       u32 irq_msk = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT2_MSK);
+       u32 irq_value = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT2);
+       struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
        struct pci_dev *pci_dev = hisi_hba->pci_dev;
+       struct device *dev = hisi_hba->dev;
+
+       irq_value &= ~irq_msk;
+       if (!irq_value)
+               return;
+
+       if (irq_value & BIT(CHL_INT2_SL_IDAF_TOUT_CONF_OFF)) {
+               dev_warn(dev, "phy%d identify timeout\n", phy_no);
+               hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
+       }
+
+       if (irq_value & BIT(CHL_INT2_STP_LINK_TIMEOUT_OFF)) {
+               u32 reg_value = hisi_sas_phy_read32(hisi_hba, phy_no,
+                               STP_LINK_TIMEOUT_STATE);
+
+               dev_warn(dev, "phy%d stp link timeout (0x%x)\n",
+                        phy_no, reg_value);
+               if (reg_value & BIT(4))
+                       hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
+       }
+
+       if ((irq_value & BIT(CHL_INT2_RX_INVLD_DW_OFF)) &&
+           (pci_dev->revision == 0x20)) {
+               u32 reg_value;
+               int rc;
+
+               rc = hisi_sas_read32_poll_timeout_atomic(
+                               HILINK_ERR_DFX, reg_value,
+                               !((reg_value >> 8) & BIT(phy_no)),
+                               1000, 10000);
+               if (rc)
+                       hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
+       }
+
+       hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2, irq_value);
+}
+
+static irqreturn_t int_chnl_int_v3_hw(int irq_no, void *p)
+{
+       struct hisi_hba *hisi_hba = p;
        u32 irq_msk;
        int phy_no = 0;
 
@@ -1328,84 +1442,12 @@ static irqreturn_t int_chnl_int_v3_hw(int irq_no, void *p)
        while (irq_msk) {
                u32 irq_value0 = hisi_sas_phy_read32(hisi_hba, phy_no,
                                                     CHL_INT0);
-               u32 irq_value1 = hisi_sas_phy_read32(hisi_hba, phy_no,
-                                                    CHL_INT1);
-               u32 irq_value2 = hisi_sas_phy_read32(hisi_hba, phy_no,
-                                                    CHL_INT2);
-               u32 irq_msk1 = hisi_sas_phy_read32(hisi_hba, phy_no,
-                                                       CHL_INT1_MSK);
-               u32 irq_msk2 = hisi_sas_phy_read32(hisi_hba, phy_no,
-                                                       CHL_INT2_MSK);
-
-               irq_value1 &= ~irq_msk1;
-               irq_value2 &= ~irq_msk2;
-
-               if ((irq_msk & (4 << (phy_no * 4))) &&
-                                               irq_value1) {
-                       int i;
-
-                       for (i = 0; i < ARRAY_SIZE(port_axi_error); i++) {
-                               const struct hisi_sas_hw_error *error =
-                                               &port_axi_error[i];
-
-                               if (!(irq_value1 & error->irq_msk))
-                                       continue;
 
-                               dev_err(dev, "%s error (phy%d 0x%x) found!\n",
-                                       error->msg, phy_no, irq_value1);
-                               queue_work(hisi_hba->wq, &hisi_hba->rst_work);
-                       }
-
-                       hisi_sas_phy_write32(hisi_hba, phy_no,
-                                            CHL_INT1, irq_value1);
-               }
-
-               if (irq_msk & (8 << (phy_no * 4)) && irq_value2) {
-                       struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
-
-                       if (irq_value2 & BIT(CHL_INT2_SL_IDAF_TOUT_CONF_OFF)) {
-                               dev_warn(dev, "phy%d identify timeout\n",
-                                                       phy_no);
-                               hisi_sas_notify_phy_event(phy,
-                                       HISI_PHYE_LINK_RESET);
-
-                       }
-
-                       if (irq_value2 & BIT(CHL_INT2_STP_LINK_TIMEOUT_OFF)) {
-                               u32 reg_value = hisi_sas_phy_read32(hisi_hba,
-                                               phy_no, STP_LINK_TIMEOUT_STATE);
-
-                               dev_warn(dev, "phy%d stp link timeout (0x%x)\n",
-                                                       phy_no, reg_value);
-                               if (reg_value & BIT(4))
-                                       hisi_sas_notify_phy_event(phy,
-                                               HISI_PHYE_LINK_RESET);
-                       }
+               if (irq_msk & (4 << (phy_no * 4)))
+                       handle_chl_int1_v3_hw(hisi_hba, phy_no);
 
-                       hisi_sas_phy_write32(hisi_hba, phy_no,
-                                            CHL_INT2, irq_value2);
-
-                       if ((irq_value2 & BIT(CHL_INT2_RX_INVLD_DW_OFF)) &&
-                           (pci_dev->revision == 0x20)) {
-                               u32 reg_value;
-                               int rc;
-
-                               rc = hisi_sas_read32_poll_timeout_atomic(
-                                       HILINK_ERR_DFX, reg_value,
-                                       !((reg_value >> 8) & BIT(phy_no)),
-                                       1000, 10000);
-                               if (rc) {
-                                       disable_phy_v3_hw(hisi_hba, phy_no);
-                                       hisi_sas_phy_write32(hisi_hba, phy_no,
-                                               CHL_INT2,
-                                               BIT(CHL_INT2_RX_INVLD_DW_OFF));
-                                       hisi_sas_phy_read32(hisi_hba, phy_no,
-                                               ERR_CNT_INVLD_DW);
-                                       mdelay(1);
-                                       enable_phy_v3_hw(hisi_hba, phy_no);
-                               }
-                       }
-               }
+               if (irq_msk & (8 << (phy_no * 4)))
+                       handle_chl_int2_v3_hw(hisi_hba, phy_no);
 
                if (irq_msk & (2 << (phy_no * 4)) && irq_value0) {
                        hisi_sas_phy_write32(hisi_hba, phy_no,
@@ -1968,11 +2010,11 @@ static void phy_get_events_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
 
 }
 
-static int soft_reset_v3_hw(struct hisi_hba *hisi_hba)
+static int disable_host_v3_hw(struct hisi_hba *hisi_hba)
 {
        struct device *dev = hisi_hba->dev;
+       u32 status, reg_val;
        int rc;
-       u32 status;
 
        interrupt_disable_v3_hw(hisi_hba);
        hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0x0);
@@ -1982,14 +2024,32 @@ static int soft_reset_v3_hw(struct hisi_hba *hisi_hba)
 
        mdelay(10);
 
-       hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE + AM_CTRL_GLOBAL, 0x1);
+       reg_val = hisi_sas_read32(hisi_hba, AXI_MASTER_CFG_BASE +
+                                 AM_CTRL_GLOBAL);
+       reg_val |= AM_CTRL_SHUTDOWN_REQ_MSK;
+       hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE +
+                        AM_CTRL_GLOBAL, reg_val);
 
        /* wait until bus idle */
        rc = hisi_sas_read32_poll_timeout(AXI_MASTER_CFG_BASE +
                                          AM_CURR_TRANS_RETURN, status,
                                          status == 0x3, 10, 100);
        if (rc) {
-               dev_err(dev, "axi bus is not idle, rc = %d\n", rc);
+               dev_err(dev, "axi bus is not idle, rc=%d\n", rc);
+               return rc;
+       }
+
+       return 0;
+}
+
+static int soft_reset_v3_hw(struct hisi_hba *hisi_hba)
+{
+       struct device *dev = hisi_hba->dev;
+       int rc;
+
+       rc = disable_host_v3_hw(hisi_hba);
+       if (rc) {
+               dev_err(dev, "soft reset: disable host failed rc=%d\n", rc);
                return rc;
        }
 
@@ -1998,6 +2058,75 @@ static int soft_reset_v3_hw(struct hisi_hba *hisi_hba)
        return hw_init_v3_hw(hisi_hba);
 }
 
+static int write_gpio_v3_hw(struct hisi_hba *hisi_hba, u8 reg_type,
+                       u8 reg_index, u8 reg_count, u8 *write_data)
+{
+       struct device *dev = hisi_hba->dev;
+       u32 *data = (u32 *)write_data;
+       int i;
+
+       switch (reg_type) {
+       case SAS_GPIO_REG_TX:
+               if ((reg_index + reg_count) > ((hisi_hba->n_phy + 3) / 4)) {
+                       dev_err(dev, "write gpio: invalid reg range[%d, %d]\n",
+                               reg_index, reg_index + reg_count - 1);
+                       return -EINVAL;
+               }
+
+               for (i = 0; i < reg_count; i++)
+                       hisi_sas_write32(hisi_hba,
+                                        SAS_GPIO_TX_0_1 + (reg_index + i) * 4,
+                                        data[i]);
+               break;
+       default:
+               dev_err(dev, "write gpio: unsupported or bad reg type %d\n",
+                               reg_type);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static void wait_cmds_complete_timeout_v3_hw(struct hisi_hba *hisi_hba,
+                                            int delay_ms, int timeout_ms)
+{
+       struct device *dev = hisi_hba->dev;
+       int entries, entries_old = 0, time;
+
+       for (time = 0; time < timeout_ms; time += delay_ms) {
+               entries = hisi_sas_read32(hisi_hba, CQE_SEND_CNT);
+               if (entries == entries_old)
+                       break;
+
+               entries_old = entries;
+               msleep(delay_ms);
+       }
+
+       dev_dbg(dev, "wait commands complete %dms\n", time);
+}
+
+static struct scsi_host_template sht_v3_hw = {
+       .name                   = DRV_NAME,
+       .module                 = THIS_MODULE,
+       .queuecommand           = sas_queuecommand,
+       .target_alloc           = sas_target_alloc,
+       .slave_configure        = hisi_sas_slave_configure,
+       .scan_finished          = hisi_sas_scan_finished,
+       .scan_start             = hisi_sas_scan_start,
+       .change_queue_depth     = sas_change_queue_depth,
+       .bios_param             = sas_bios_param,
+       .can_queue              = 1,
+       .this_id                = -1,
+       .sg_tablesize           = SG_ALL,
+       .max_sectors            = SCSI_DEFAULT_MAX_SECTORS,
+       .use_clustering         = ENABLE_CLUSTERING,
+       .eh_device_reset_handler = sas_eh_device_reset_handler,
+       .eh_target_reset_handler = sas_eh_target_reset_handler,
+       .target_destroy         = sas_target_destroy,
+       .ioctl                  = sas_ioctl,
+       .shost_attrs            = host_attrs,
+};
+
 static const struct hisi_sas_hw hisi_sas_v3_hw = {
        .hw_init = hisi_sas_v3_init,
        .setup_itct = setup_itct_v3_hw,
@@ -2023,6 +2152,8 @@ static const struct hisi_sas_hw hisi_sas_v3_hw = {
        .soft_reset = soft_reset_v3_hw,
        .get_phys_state = get_phys_state_v3_hw,
        .get_events = phy_get_events_v3_hw,
+       .write_gpio = write_gpio_v3_hw,
+       .wait_cmds_complete_timeout = wait_cmds_complete_timeout_v3_hw,
 };
 
 static struct Scsi_Host *
@@ -2032,7 +2163,7 @@ hisi_sas_shost_alloc_pci(struct pci_dev *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(&sht_v3_hw, sizeof(*hisi_hba));
        if (!shost) {
                dev_err(dev, "shost alloc failed\n");
                return NULL;
@@ -2368,6 +2499,41 @@ static pci_ers_result_t hisi_sas_slot_reset_v3_hw(struct pci_dev *pdev)
        return PCI_ERS_RESULT_DISCONNECT;
 }
 
+static void hisi_sas_reset_prepare_v3_hw(struct pci_dev *pdev)
+{
+       struct sas_ha_struct *sha = pci_get_drvdata(pdev);
+       struct hisi_hba *hisi_hba = sha->lldd_ha;
+       struct device *dev = hisi_hba->dev;
+       int rc;
+
+       dev_info(dev, "FLR prepare\n");
+       set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
+       hisi_sas_controller_reset_prepare(hisi_hba);
+
+       rc = disable_host_v3_hw(hisi_hba);
+       if (rc)
+               dev_err(dev, "FLR: disable host failed rc=%d\n", rc);
+}
+
+static void hisi_sas_reset_done_v3_hw(struct pci_dev *pdev)
+{
+       struct sas_ha_struct *sha = pci_get_drvdata(pdev);
+       struct hisi_hba *hisi_hba = sha->lldd_ha;
+       struct device *dev = hisi_hba->dev;
+       int rc;
+
+       hisi_sas_init_mem(hisi_hba);
+
+       rc = hw_init_v3_hw(hisi_hba);
+       if (rc) {
+               dev_err(dev, "FLR: hw init failed rc=%d\n", rc);
+               return;
+       }
+
+       hisi_sas_controller_reset_done(hisi_hba);
+       dev_info(dev, "FLR done\n");
+}
+
 enum {
        /* instances of the controller */
        hip08,
@@ -2379,39 +2545,24 @@ static int hisi_sas_v3_suspend(struct pci_dev *pdev, pm_message_t state)
        struct hisi_hba *hisi_hba = sha->lldd_ha;
        struct device *dev = hisi_hba->dev;
        struct Scsi_Host *shost = hisi_hba->shost;
-       u32 device_state, status;
+       u32 device_state;
        int rc;
-       u32 reg_val;
-       unsigned long flags;
 
        if (!pdev->pm_cap) {
                dev_err(dev, "PCI PM not supported\n");
                return -ENODEV;
        }
 
-       set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
+       if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
+               return -1;
+
        scsi_block_requests(shost);
        set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
        flush_workqueue(hisi_hba->wq);
-       /* disable DQ/PHY/bus */
-       interrupt_disable_v3_hw(hisi_hba);
-       hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0x0);
-       hisi_sas_kill_tasklets(hisi_hba);
-
-       hisi_sas_stop_phys(hisi_hba);
-
-       reg_val = hisi_sas_read32(hisi_hba, AXI_MASTER_CFG_BASE +
-               AM_CTRL_GLOBAL);
-       reg_val |= 0x1;
-       hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE +
-               AM_CTRL_GLOBAL, reg_val);
 
-       /* wait until bus idle */
-       rc = hisi_sas_read32_poll_timeout(AXI_MASTER_CFG_BASE +
-                                         AM_CURR_TRANS_RETURN, status,
-                                         status == 0x3, 10, 100);
+       rc = disable_host_v3_hw(hisi_hba);
        if (rc) {
-               dev_err(dev, "axi bus is not idle, rc = %d\n", rc);
+               dev_err(dev, "PM suspend: disable host failed rc=%d\n", rc);
                clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
                clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
                scsi_unblock_requests(shost);
@@ -2427,9 +2578,7 @@ static int hisi_sas_v3_suspend(struct pci_dev *pdev, pm_message_t state)
        pci_disable_device(pdev);
        pci_set_power_state(pdev, device_state);
 
-       spin_lock_irqsave(&hisi_hba->lock, flags);
        hisi_sas_release_tasks(hisi_hba);
-       spin_unlock_irqrestore(&hisi_hba->lock, flags);
 
        sas_suspend_ha(sha);
        return 0;
@@ -2476,6 +2625,8 @@ static const struct pci_error_handlers hisi_sas_err_handler = {
        .error_detected = hisi_sas_error_detected_v3_hw,
        .mmio_enabled   = hisi_sas_mmio_enabled_v3_hw,
        .slot_reset     = hisi_sas_slot_reset_v3_hw,
+       .reset_prepare  = hisi_sas_reset_prepare_v3_hw,
+       .reset_done     = hisi_sas_reset_done_v3_hw,
 };
 
 static struct pci_driver sas_v3_pci_driver = {