]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/scsi/hisi_sas/hisi_sas_v2_hw.c
scsi: hisi_sas: Try wait commands before before controller reset
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / hisi_sas / hisi_sas_v2_hw.c
CommitLineData
3417ba8a
JG
1/*
2 * Copyright (c) 2016 Linaro Ltd.
3 * Copyright (c) 2016 Hisilicon Limited.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 */
11
12#include "hisi_sas.h"
13#define DRV_NAME "hisi_sas_v2_hw"
14
45c901b8
JG
15/* global registers need init*/
16#define DLVRY_QUEUE_ENABLE 0x0
17#define IOST_BASE_ADDR_LO 0x8
18#define IOST_BASE_ADDR_HI 0xc
19#define ITCT_BASE_ADDR_LO 0x10
20#define ITCT_BASE_ADDR_HI 0x14
21#define IO_BROKEN_MSG_ADDR_LO 0x18
22#define IO_BROKEN_MSG_ADDR_HI 0x1c
23#define PHY_CONTEXT 0x20
24#define PHY_STATE 0x24
25#define PHY_PORT_NUM_MA 0x28
26#define PORT_STATE 0x2c
27#define PORT_STATE_PHY8_PORT_NUM_OFF 16
28#define PORT_STATE_PHY8_PORT_NUM_MSK (0xf << PORT_STATE_PHY8_PORT_NUM_OFF)
29#define PORT_STATE_PHY8_CONN_RATE_OFF 20
30#define PORT_STATE_PHY8_CONN_RATE_MSK (0xf << PORT_STATE_PHY8_CONN_RATE_OFF)
31#define PHY_CONN_RATE 0x30
32#define HGC_TRANS_TASK_CNT_LIMIT 0x38
33#define AXI_AHB_CLK_CFG 0x3c
34#define ITCT_CLR 0x44
35#define ITCT_CLR_EN_OFF 16
36#define ITCT_CLR_EN_MSK (0x1 << ITCT_CLR_EN_OFF)
37#define ITCT_DEV_OFF 0
38#define ITCT_DEV_MSK (0x7ff << ITCT_DEV_OFF)
39#define AXI_USER1 0x48
40#define AXI_USER2 0x4c
41#define IO_SATA_BROKEN_MSG_ADDR_LO 0x58
42#define IO_SATA_BROKEN_MSG_ADDR_HI 0x5c
43#define SATA_INITI_D2H_STORE_ADDR_LO 0x60
44#define SATA_INITI_D2H_STORE_ADDR_HI 0x64
45#define HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL 0x84
46#define HGC_SAS_TXFAIL_RETRY_CTRL 0x88
47#define HGC_GET_ITV_TIME 0x90
48#define DEVICE_MSG_WORK_MODE 0x94
49#define OPENA_WT_CONTI_TIME 0x9c
50#define I_T_NEXUS_LOSS_TIME 0xa0
51#define MAX_CON_TIME_LIMIT_TIME 0xa4
52#define BUS_INACTIVE_LIMIT_TIME 0xa8
53#define REJECT_TO_OPEN_LIMIT_TIME 0xac
54#define CFG_AGING_TIME 0xbc
55#define HGC_DFX_CFG2 0xc0
56#define HGC_IOMB_PROC1_STATUS 0x104
57#define CFG_1US_TIMER_TRSH 0xcc
d3b688d3
XC
58#define HGC_LM_DFX_STATUS2 0x128
59#define HGC_LM_DFX_STATUS2_IOSTLIST_OFF 0
60#define HGC_LM_DFX_STATUS2_IOSTLIST_MSK (0xfff << \
61 HGC_LM_DFX_STATUS2_IOSTLIST_OFF)
62#define HGC_LM_DFX_STATUS2_ITCTLIST_OFF 12
63#define HGC_LM_DFX_STATUS2_ITCTLIST_MSK (0x7ff << \
64 HGC_LM_DFX_STATUS2_ITCTLIST_OFF)
65#define HGC_CQE_ECC_ADDR 0x13c
66#define HGC_CQE_ECC_1B_ADDR_OFF 0
ce41b41e 67#define HGC_CQE_ECC_1B_ADDR_MSK (0x3f << HGC_CQE_ECC_1B_ADDR_OFF)
d3b688d3 68#define HGC_CQE_ECC_MB_ADDR_OFF 8
ce41b41e 69#define HGC_CQE_ECC_MB_ADDR_MSK (0x3f << HGC_CQE_ECC_MB_ADDR_OFF)
d3b688d3
XC
70#define HGC_IOST_ECC_ADDR 0x140
71#define HGC_IOST_ECC_1B_ADDR_OFF 0
ce41b41e 72#define HGC_IOST_ECC_1B_ADDR_MSK (0x3ff << HGC_IOST_ECC_1B_ADDR_OFF)
d3b688d3 73#define HGC_IOST_ECC_MB_ADDR_OFF 16
ce41b41e 74#define HGC_IOST_ECC_MB_ADDR_MSK (0x3ff << HGC_IOST_ECC_MB_ADDR_OFF)
d3b688d3
XC
75#define HGC_DQE_ECC_ADDR 0x144
76#define HGC_DQE_ECC_1B_ADDR_OFF 0
ce41b41e 77#define HGC_DQE_ECC_1B_ADDR_MSK (0xfff << HGC_DQE_ECC_1B_ADDR_OFF)
d3b688d3 78#define HGC_DQE_ECC_MB_ADDR_OFF 16
ce41b41e 79#define HGC_DQE_ECC_MB_ADDR_MSK (0xfff << HGC_DQE_ECC_MB_ADDR_OFF)
45c901b8
JG
80#define HGC_INVLD_DQE_INFO 0x148
81#define HGC_INVLD_DQE_INFO_FB_CH0_OFF 9
82#define HGC_INVLD_DQE_INFO_FB_CH0_MSK (0x1 << HGC_INVLD_DQE_INFO_FB_CH0_OFF)
83#define HGC_INVLD_DQE_INFO_FB_CH3_OFF 18
d3b688d3
XC
84#define HGC_ITCT_ECC_ADDR 0x150
85#define HGC_ITCT_ECC_1B_ADDR_OFF 0
86#define HGC_ITCT_ECC_1B_ADDR_MSK (0x3ff << \
87 HGC_ITCT_ECC_1B_ADDR_OFF)
88#define HGC_ITCT_ECC_MB_ADDR_OFF 16
89#define HGC_ITCT_ECC_MB_ADDR_MSK (0x3ff << \
90 HGC_ITCT_ECC_MB_ADDR_OFF)
91#define HGC_AXI_FIFO_ERR_INFO 0x154
92#define AXI_ERR_INFO_OFF 0
93#define AXI_ERR_INFO_MSK (0xff << AXI_ERR_INFO_OFF)
94#define FIFO_ERR_INFO_OFF 8
95#define FIFO_ERR_INFO_MSK (0xff << FIFO_ERR_INFO_OFF)
45c901b8
JG
96#define INT_COAL_EN 0x19c
97#define OQ_INT_COAL_TIME 0x1a0
98#define OQ_INT_COAL_CNT 0x1a4
99#define ENT_INT_COAL_TIME 0x1a8
100#define ENT_INT_COAL_CNT 0x1ac
101#define OQ_INT_SRC 0x1b0
102#define OQ_INT_SRC_MSK 0x1b4
103#define ENT_INT_SRC1 0x1b8
104#define ENT_INT_SRC1_D2H_FIS_CH0_OFF 0
105#define ENT_INT_SRC1_D2H_FIS_CH0_MSK (0x1 << ENT_INT_SRC1_D2H_FIS_CH0_OFF)
106#define ENT_INT_SRC1_D2H_FIS_CH1_OFF 8
107#define ENT_INT_SRC1_D2H_FIS_CH1_MSK (0x1 << ENT_INT_SRC1_D2H_FIS_CH1_OFF)
108#define ENT_INT_SRC2 0x1bc
109#define ENT_INT_SRC3 0x1c0
d3b688d3
XC
110#define ENT_INT_SRC3_WP_DEPTH_OFF 8
111#define ENT_INT_SRC3_IPTT_SLOT_NOMATCH_OFF 9
112#define ENT_INT_SRC3_RP_DEPTH_OFF 10
113#define ENT_INT_SRC3_AXI_OFF 11
114#define ENT_INT_SRC3_FIFO_OFF 12
115#define ENT_INT_SRC3_LM_OFF 14
45c901b8
JG
116#define ENT_INT_SRC3_ITC_INT_OFF 15
117#define ENT_INT_SRC3_ITC_INT_MSK (0x1 << ENT_INT_SRC3_ITC_INT_OFF)
d3b688d3 118#define ENT_INT_SRC3_ABT_OFF 16
45c901b8
JG
119#define ENT_INT_SRC_MSK1 0x1c4
120#define ENT_INT_SRC_MSK2 0x1c8
121#define ENT_INT_SRC_MSK3 0x1cc
122#define ENT_INT_SRC_MSK3_ENT95_MSK_OFF 31
123#define ENT_INT_SRC_MSK3_ENT95_MSK_MSK (0x1 << ENT_INT_SRC_MSK3_ENT95_MSK_OFF)
d3b688d3
XC
124#define SAS_ECC_INTR 0x1e8
125#define SAS_ECC_INTR_DQE_ECC_1B_OFF 0
126#define SAS_ECC_INTR_DQE_ECC_MB_OFF 1
127#define SAS_ECC_INTR_IOST_ECC_1B_OFF 2
128#define SAS_ECC_INTR_IOST_ECC_MB_OFF 3
129#define SAS_ECC_INTR_ITCT_ECC_MB_OFF 4
130#define SAS_ECC_INTR_ITCT_ECC_1B_OFF 5
131#define SAS_ECC_INTR_IOSTLIST_ECC_MB_OFF 6
132#define SAS_ECC_INTR_IOSTLIST_ECC_1B_OFF 7
133#define SAS_ECC_INTR_ITCTLIST_ECC_1B_OFF 8
134#define SAS_ECC_INTR_ITCTLIST_ECC_MB_OFF 9
135#define SAS_ECC_INTR_CQE_ECC_1B_OFF 10
136#define SAS_ECC_INTR_CQE_ECC_MB_OFF 11
137#define SAS_ECC_INTR_NCQ_MEM0_ECC_MB_OFF 12
138#define SAS_ECC_INTR_NCQ_MEM0_ECC_1B_OFF 13
139#define SAS_ECC_INTR_NCQ_MEM1_ECC_MB_OFF 14
140#define SAS_ECC_INTR_NCQ_MEM1_ECC_1B_OFF 15
141#define SAS_ECC_INTR_NCQ_MEM2_ECC_MB_OFF 16
142#define SAS_ECC_INTR_NCQ_MEM2_ECC_1B_OFF 17
143#define SAS_ECC_INTR_NCQ_MEM3_ECC_MB_OFF 18
144#define SAS_ECC_INTR_NCQ_MEM3_ECC_1B_OFF 19
45c901b8
JG
145#define SAS_ECC_INTR_MSK 0x1ec
146#define HGC_ERR_STAT_EN 0x238
1a7068b3 147#define CQE_SEND_CNT 0x248
45c901b8
JG
148#define DLVRY_Q_0_BASE_ADDR_LO 0x260
149#define DLVRY_Q_0_BASE_ADDR_HI 0x264
150#define DLVRY_Q_0_DEPTH 0x268
151#define DLVRY_Q_0_WR_PTR 0x26c
152#define DLVRY_Q_0_RD_PTR 0x270
153#define HYPER_STREAM_ID_EN_CFG 0xc80
154#define OQ0_INT_SRC_MSK 0xc90
155#define COMPL_Q_0_BASE_ADDR_LO 0x4e0
156#define COMPL_Q_0_BASE_ADDR_HI 0x4e4
157#define COMPL_Q_0_DEPTH 0x4e8
158#define COMPL_Q_0_WR_PTR 0x4ec
159#define COMPL_Q_0_RD_PTR 0x4f0
d3b688d3
XC
160#define HGC_RXM_DFX_STATUS14 0xae8
161#define HGC_RXM_DFX_STATUS14_MEM0_OFF 0
162#define HGC_RXM_DFX_STATUS14_MEM0_MSK (0x1ff << \
163 HGC_RXM_DFX_STATUS14_MEM0_OFF)
164#define HGC_RXM_DFX_STATUS14_MEM1_OFF 9
165#define HGC_RXM_DFX_STATUS14_MEM1_MSK (0x1ff << \
166 HGC_RXM_DFX_STATUS14_MEM1_OFF)
167#define HGC_RXM_DFX_STATUS14_MEM2_OFF 18
168#define HGC_RXM_DFX_STATUS14_MEM2_MSK (0x1ff << \
169 HGC_RXM_DFX_STATUS14_MEM2_OFF)
170#define HGC_RXM_DFX_STATUS15 0xaec
171#define HGC_RXM_DFX_STATUS15_MEM3_OFF 0
172#define HGC_RXM_DFX_STATUS15_MEM3_MSK (0x1ff << \
173 HGC_RXM_DFX_STATUS15_MEM3_OFF)
45c901b8
JG
174/* phy registers need init */
175#define PORT_BASE (0x2000)
176
177#define PHY_CFG (PORT_BASE + 0x0)
178#define HARD_PHY_LINKRATE (PORT_BASE + 0x4)
179#define PHY_CFG_ENA_OFF 0
180#define PHY_CFG_ENA_MSK (0x1 << PHY_CFG_ENA_OFF)
181#define PHY_CFG_DC_OPT_OFF 2
182#define PHY_CFG_DC_OPT_MSK (0x1 << PHY_CFG_DC_OPT_OFF)
183#define PROG_PHY_LINK_RATE (PORT_BASE + 0x8)
184#define PROG_PHY_LINK_RATE_MAX_OFF 0
185#define PROG_PHY_LINK_RATE_MAX_MSK (0xff << PROG_PHY_LINK_RATE_MAX_OFF)
186#define PHY_CTRL (PORT_BASE + 0x14)
187#define PHY_CTRL_RESET_OFF 0
188#define PHY_CTRL_RESET_MSK (0x1 << PHY_CTRL_RESET_OFF)
189#define SAS_PHY_CTRL (PORT_BASE + 0x20)
190#define SL_CFG (PORT_BASE + 0x84)
191#define PHY_PCN (PORT_BASE + 0x44)
192#define SL_TOUT_CFG (PORT_BASE + 0x8c)
193#define SL_CONTROL (PORT_BASE + 0x94)
194#define SL_CONTROL_NOTIFY_EN_OFF 0
195#define SL_CONTROL_NOTIFY_EN_MSK (0x1 << SL_CONTROL_NOTIFY_EN_OFF)
9c81e2cf
JG
196#define SL_CONTROL_CTA_OFF 17
197#define SL_CONTROL_CTA_MSK (0x1 << SL_CONTROL_CTA_OFF)
0844a3ff
JG
198#define RX_PRIMS_STATUS (PORT_BASE + 0x98)
199#define RX_BCAST_CHG_OFF 1
200#define RX_BCAST_CHG_MSK (0x1 << RX_BCAST_CHG_OFF)
45c901b8
JG
201#define TX_ID_DWORD0 (PORT_BASE + 0x9c)
202#define TX_ID_DWORD1 (PORT_BASE + 0xa0)
203#define TX_ID_DWORD2 (PORT_BASE + 0xa4)
204#define TX_ID_DWORD3 (PORT_BASE + 0xa8)
205#define TX_ID_DWORD4 (PORT_BASE + 0xaC)
206#define TX_ID_DWORD5 (PORT_BASE + 0xb0)
207#define TX_ID_DWORD6 (PORT_BASE + 0xb4)
9c81e2cf
JG
208#define TXID_AUTO (PORT_BASE + 0xb8)
209#define TXID_AUTO_CT3_OFF 1
210#define TXID_AUTO_CT3_MSK (0x1 << TXID_AUTO_CT3_OFF)
819cbf18
XT
211#define TXID_AUTO_CTB_OFF 11
212#define TXID_AUTO_CTB_MSK (0x1 << TXID_AUTO_CTB_OFF)
0844a3ff
JG
213#define TX_HARDRST_OFF 2
214#define TX_HARDRST_MSK (0x1 << TX_HARDRST_OFF)
45c901b8
JG
215#define RX_IDAF_DWORD0 (PORT_BASE + 0xc4)
216#define RX_IDAF_DWORD1 (PORT_BASE + 0xc8)
217#define RX_IDAF_DWORD2 (PORT_BASE + 0xcc)
218#define RX_IDAF_DWORD3 (PORT_BASE + 0xd0)
219#define RX_IDAF_DWORD4 (PORT_BASE + 0xd4)
220#define RX_IDAF_DWORD5 (PORT_BASE + 0xd8)
221#define RX_IDAF_DWORD6 (PORT_BASE + 0xdc)
222#define RXOP_CHECK_CFG_H (PORT_BASE + 0xfc)
f2f89c32 223#define CON_CONTROL (PORT_BASE + 0x118)
c7b9d369
XT
224#define CON_CONTROL_CFG_OPEN_ACC_STP_OFF 0
225#define CON_CONTROL_CFG_OPEN_ACC_STP_MSK \
226 (0x01 << CON_CONTROL_CFG_OPEN_ACC_STP_OFF)
45c901b8
JG
227#define DONE_RECEIVED_TIME (PORT_BASE + 0x11c)
228#define CHL_INT0 (PORT_BASE + 0x1b4)
229#define CHL_INT0_HOTPLUG_TOUT_OFF 0
230#define CHL_INT0_HOTPLUG_TOUT_MSK (0x1 << CHL_INT0_HOTPLUG_TOUT_OFF)
231#define CHL_INT0_SL_RX_BCST_ACK_OFF 1
232#define CHL_INT0_SL_RX_BCST_ACK_MSK (0x1 << CHL_INT0_SL_RX_BCST_ACK_OFF)
233#define CHL_INT0_SL_PHY_ENABLE_OFF 2
234#define CHL_INT0_SL_PHY_ENABLE_MSK (0x1 << CHL_INT0_SL_PHY_ENABLE_OFF)
235#define CHL_INT0_NOT_RDY_OFF 4
236#define CHL_INT0_NOT_RDY_MSK (0x1 << CHL_INT0_NOT_RDY_OFF)
237#define CHL_INT0_PHY_RDY_OFF 5
238#define CHL_INT0_PHY_RDY_MSK (0x1 << CHL_INT0_PHY_RDY_OFF)
239#define CHL_INT1 (PORT_BASE + 0x1b8)
240#define CHL_INT1_DMAC_TX_ECC_ERR_OFF 15
241#define CHL_INT1_DMAC_TX_ECC_ERR_MSK (0x1 << CHL_INT1_DMAC_TX_ECC_ERR_OFF)
242#define CHL_INT1_DMAC_RX_ECC_ERR_OFF 17
243#define CHL_INT1_DMAC_RX_ECC_ERR_MSK (0x1 << CHL_INT1_DMAC_RX_ECC_ERR_OFF)
9dd1d620
XT
244#define CHL_INT1_DMAC_TX_AXI_WR_ERR_OFF 19
245#define CHL_INT1_DMAC_TX_AXI_RD_ERR_OFF 20
246#define CHL_INT1_DMAC_RX_AXI_WR_ERR_OFF 21
247#define CHL_INT1_DMAC_RX_AXI_RD_ERR_OFF 22
45c901b8 248#define CHL_INT2 (PORT_BASE + 0x1bc)
066312f6 249#define CHL_INT2_SL_IDAF_TOUT_CONF_OFF 0
45c901b8
JG
250#define CHL_INT0_MSK (PORT_BASE + 0x1c0)
251#define CHL_INT1_MSK (PORT_BASE + 0x1c4)
252#define CHL_INT2_MSK (PORT_BASE + 0x1c8)
253#define CHL_INT_COAL_EN (PORT_BASE + 0x1d0)
0844a3ff
JG
254#define DMA_TX_DFX0 (PORT_BASE + 0x200)
255#define DMA_TX_DFX1 (PORT_BASE + 0x204)
c7b9d369
XT
256#define DMA_TX_DFX1_IPTT_OFF 0
257#define DMA_TX_DFX1_IPTT_MSK (0xffff << DMA_TX_DFX1_IPTT_OFF)
819cbf18 258#define DMA_TX_FIFO_DFX0 (PORT_BASE + 0x240)
0844a3ff
JG
259#define PORT_DFX0 (PORT_BASE + 0x258)
260#define LINK_DFX2 (PORT_BASE + 0X264)
819cbf18
XT
261#define LINK_DFX2_RCVR_HOLD_STS_OFF 9
262#define LINK_DFX2_RCVR_HOLD_STS_MSK (0x1 << LINK_DFX2_RCVR_HOLD_STS_OFF)
263#define LINK_DFX2_SEND_HOLD_STS_OFF 10
264#define LINK_DFX2_SEND_HOLD_STS_MSK (0x1 << LINK_DFX2_SEND_HOLD_STS_OFF)
c52108c6
XT
265#define SAS_ERR_CNT4_REG (PORT_BASE + 0x290)
266#define SAS_ERR_CNT6_REG (PORT_BASE + 0x298)
45c901b8
JG
267#define PHY_CTRL_RDY_MSK (PORT_BASE + 0x2b0)
268#define PHYCTRL_NOT_RDY_MSK (PORT_BASE + 0x2b4)
269#define PHYCTRL_DWS_RESET_MSK (PORT_BASE + 0x2b8)
270#define PHYCTRL_PHY_ENA_MSK (PORT_BASE + 0x2bc)
271#define SL_RX_BCAST_CHK_MSK (PORT_BASE + 0x2c0)
272#define PHYCTRL_OOB_RESTART_MSK (PORT_BASE + 0x2c4)
273#define DMA_TX_STATUS (PORT_BASE + 0x2d0)
274#define DMA_TX_STATUS_BUSY_OFF 0
275#define DMA_TX_STATUS_BUSY_MSK (0x1 << DMA_TX_STATUS_BUSY_OFF)
276#define DMA_RX_STATUS (PORT_BASE + 0x2e8)
277#define DMA_RX_STATUS_BUSY_OFF 0
278#define DMA_RX_STATUS_BUSY_MSK (0x1 << DMA_RX_STATUS_BUSY_OFF)
279
280#define AXI_CFG (0x5100)
281#define AM_CFG_MAX_TRANS (0x5010)
282#define AM_CFG_SINGLE_PORT_MAX_TRANS (0x5014)
283
06ec0fb9
XC
284#define AXI_MASTER_CFG_BASE (0x5000)
285#define AM_CTRL_GLOBAL (0x0)
286#define AM_CURR_TRANS_RETURN (0x150)
287
45c901b8
JG
288/* HW dma structures */
289/* Delivery queue header */
290/* dw0 */
a3e665d9
JG
291#define CMD_HDR_ABORT_FLAG_OFF 0
292#define CMD_HDR_ABORT_FLAG_MSK (0x3 << CMD_HDR_ABORT_FLAG_OFF)
293#define CMD_HDR_ABORT_DEVICE_TYPE_OFF 2
294#define CMD_HDR_ABORT_DEVICE_TYPE_MSK (0x1 << CMD_HDR_ABORT_DEVICE_TYPE_OFF)
45c901b8
JG
295#define CMD_HDR_RESP_REPORT_OFF 5
296#define CMD_HDR_RESP_REPORT_MSK (0x1 << CMD_HDR_RESP_REPORT_OFF)
297#define CMD_HDR_TLR_CTRL_OFF 6
298#define CMD_HDR_TLR_CTRL_MSK (0x3 << CMD_HDR_TLR_CTRL_OFF)
299#define CMD_HDR_PORT_OFF 18
300#define CMD_HDR_PORT_MSK (0xf << CMD_HDR_PORT_OFF)
301#define CMD_HDR_PRIORITY_OFF 27
302#define CMD_HDR_PRIORITY_MSK (0x1 << CMD_HDR_PRIORITY_OFF)
303#define CMD_HDR_CMD_OFF 29
304#define CMD_HDR_CMD_MSK (0x7 << CMD_HDR_CMD_OFF)
305/* dw1 */
306#define CMD_HDR_DIR_OFF 5
307#define CMD_HDR_DIR_MSK (0x3 << CMD_HDR_DIR_OFF)
308#define CMD_HDR_RESET_OFF 7
309#define CMD_HDR_RESET_MSK (0x1 << CMD_HDR_RESET_OFF)
310#define CMD_HDR_VDTL_OFF 10
311#define CMD_HDR_VDTL_MSK (0x1 << CMD_HDR_VDTL_OFF)
312#define CMD_HDR_FRAME_TYPE_OFF 11
313#define CMD_HDR_FRAME_TYPE_MSK (0x1f << CMD_HDR_FRAME_TYPE_OFF)
314#define CMD_HDR_DEV_ID_OFF 16
315#define CMD_HDR_DEV_ID_MSK (0xffff << CMD_HDR_DEV_ID_OFF)
316/* dw2 */
317#define CMD_HDR_CFL_OFF 0
318#define CMD_HDR_CFL_MSK (0x1ff << CMD_HDR_CFL_OFF)
319#define CMD_HDR_NCQ_TAG_OFF 10
320#define CMD_HDR_NCQ_TAG_MSK (0x1f << CMD_HDR_NCQ_TAG_OFF)
321#define CMD_HDR_MRFL_OFF 15
322#define CMD_HDR_MRFL_MSK (0x1ff << CMD_HDR_MRFL_OFF)
323#define CMD_HDR_SG_MOD_OFF 24
324#define CMD_HDR_SG_MOD_MSK (0x3 << CMD_HDR_SG_MOD_OFF)
325#define CMD_HDR_FIRST_BURST_OFF 26
326#define CMD_HDR_FIRST_BURST_MSK (0x1 << CMD_HDR_SG_MOD_OFF)
327/* dw3 */
328#define CMD_HDR_IPTT_OFF 0
329#define CMD_HDR_IPTT_MSK (0xffff << CMD_HDR_IPTT_OFF)
330/* dw6 */
331#define CMD_HDR_DIF_SGL_LEN_OFF 0
332#define CMD_HDR_DIF_SGL_LEN_MSK (0xffff << CMD_HDR_DIF_SGL_LEN_OFF)
333#define CMD_HDR_DATA_SGL_LEN_OFF 16
334#define CMD_HDR_DATA_SGL_LEN_MSK (0xffff << CMD_HDR_DATA_SGL_LEN_OFF)
a3e665d9
JG
335#define CMD_HDR_ABORT_IPTT_OFF 16
336#define CMD_HDR_ABORT_IPTT_MSK (0xffff << CMD_HDR_ABORT_IPTT_OFF)
45c901b8
JG
337
338/* Completion header */
339/* dw0 */
634a9585
XC
340#define CMPLT_HDR_ERR_PHASE_OFF 2
341#define CMPLT_HDR_ERR_PHASE_MSK (0xff << CMPLT_HDR_ERR_PHASE_OFF)
45c901b8
JG
342#define CMPLT_HDR_RSPNS_XFRD_OFF 10
343#define CMPLT_HDR_RSPNS_XFRD_MSK (0x1 << CMPLT_HDR_RSPNS_XFRD_OFF)
344#define CMPLT_HDR_ERX_OFF 12
345#define CMPLT_HDR_ERX_MSK (0x1 << CMPLT_HDR_ERX_OFF)
df032d0e
JG
346#define CMPLT_HDR_ABORT_STAT_OFF 13
347#define CMPLT_HDR_ABORT_STAT_MSK (0x7 << CMPLT_HDR_ABORT_STAT_OFF)
348/* abort_stat */
349#define STAT_IO_NOT_VALID 0x1
350#define STAT_IO_NO_DEVICE 0x2
351#define STAT_IO_COMPLETE 0x3
352#define STAT_IO_ABORTED 0x4
45c901b8
JG
353/* dw1 */
354#define CMPLT_HDR_IPTT_OFF 0
355#define CMPLT_HDR_IPTT_MSK (0xffff << CMPLT_HDR_IPTT_OFF)
356#define CMPLT_HDR_DEV_ID_OFF 16
357#define CMPLT_HDR_DEV_ID_MSK (0xffff << CMPLT_HDR_DEV_ID_OFF)
358
359/* ITCT header */
360/* qw0 */
361#define ITCT_HDR_DEV_TYPE_OFF 0
362#define ITCT_HDR_DEV_TYPE_MSK (0x3 << ITCT_HDR_DEV_TYPE_OFF)
363#define ITCT_HDR_VALID_OFF 2
364#define ITCT_HDR_VALID_MSK (0x1 << ITCT_HDR_VALID_OFF)
365#define ITCT_HDR_MCR_OFF 5
366#define ITCT_HDR_MCR_MSK (0xf << ITCT_HDR_MCR_OFF)
367#define ITCT_HDR_VLN_OFF 9
368#define ITCT_HDR_VLN_MSK (0xf << ITCT_HDR_VLN_OFF)
c399acfb
XC
369#define ITCT_HDR_SMP_TIMEOUT_OFF 16
370#define ITCT_HDR_SMP_TIMEOUT_8US 1
371#define ITCT_HDR_SMP_TIMEOUT (ITCT_HDR_SMP_TIMEOUT_8US * \
372 250) /* 2ms */
373#define ITCT_HDR_AWT_CONTINUE_OFF 25
45c901b8
JG
374#define ITCT_HDR_PORT_ID_OFF 28
375#define ITCT_HDR_PORT_ID_MSK (0xf << ITCT_HDR_PORT_ID_OFF)
376/* qw2 */
377#define ITCT_HDR_INLT_OFF 0
378#define ITCT_HDR_INLT_MSK (0xffffULL << ITCT_HDR_INLT_OFF)
379#define ITCT_HDR_BITLT_OFF 16
380#define ITCT_HDR_BITLT_MSK (0xffffULL << ITCT_HDR_BITLT_OFF)
381#define ITCT_HDR_MCTLT_OFF 32
382#define ITCT_HDR_MCTLT_MSK (0xffffULL << ITCT_HDR_MCTLT_OFF)
383#define ITCT_HDR_RTOLT_OFF 48
384#define ITCT_HDR_RTOLT_MSK (0xffffULL << ITCT_HDR_RTOLT_OFF)
385
d3b688d3
XC
386#define HISI_SAS_FATAL_INT_NR 2
387
94eac9e1
JG
388struct hisi_sas_complete_v2_hdr {
389 __le32 dw0;
390 __le32 dw1;
391 __le32 act;
392 __le32 dw3;
393};
394
e8fed0e9
JG
395struct hisi_sas_err_record_v2 {
396 /* dw0 */
397 __le32 trans_tx_fail_type;
398
399 /* dw1 */
400 __le32 trans_rx_fail_type;
401
402 /* dw2 */
403 __le16 dma_tx_err_type;
404 __le16 sipc_rx_err_type;
405
406 /* dw3 */
407 __le32 dma_rx_err_type;
408};
409
ad16fe31
XT
410struct signal_attenuation_s {
411 u32 de_emphasis;
412 u32 preshoot;
413 u32 boost;
414};
415
416struct sig_atten_lu_s {
417 const struct signal_attenuation_s *att;
418 u32 sas_phy_ctrl;
419};
420
2b383351
JG
421static const struct hisi_sas_hw_error one_bit_ecc_errors[] = {
422 {
423 .irq_msk = BIT(SAS_ECC_INTR_DQE_ECC_1B_OFF),
424 .msk = HGC_DQE_ECC_1B_ADDR_MSK,
425 .shift = HGC_DQE_ECC_1B_ADDR_OFF,
729428ca 426 .msg = "hgc_dqe_acc1b_intr found: Ram address is 0x%08X\n",
2b383351
JG
427 .reg = HGC_DQE_ECC_ADDR,
428 },
429 {
430 .irq_msk = BIT(SAS_ECC_INTR_IOST_ECC_1B_OFF),
431 .msk = HGC_IOST_ECC_1B_ADDR_MSK,
432 .shift = HGC_IOST_ECC_1B_ADDR_OFF,
729428ca 433 .msg = "hgc_iost_acc1b_intr found: Ram address is 0x%08X\n",
2b383351
JG
434 .reg = HGC_IOST_ECC_ADDR,
435 },
436 {
437 .irq_msk = BIT(SAS_ECC_INTR_ITCT_ECC_1B_OFF),
438 .msk = HGC_ITCT_ECC_1B_ADDR_MSK,
439 .shift = HGC_ITCT_ECC_1B_ADDR_OFF,
729428ca 440 .msg = "hgc_itct_acc1b_intr found: am address is 0x%08X\n",
2b383351
JG
441 .reg = HGC_ITCT_ECC_ADDR,
442 },
443 {
444 .irq_msk = BIT(SAS_ECC_INTR_IOSTLIST_ECC_1B_OFF),
445 .msk = HGC_LM_DFX_STATUS2_IOSTLIST_MSK,
446 .shift = HGC_LM_DFX_STATUS2_IOSTLIST_OFF,
729428ca 447 .msg = "hgc_iostl_acc1b_intr found: memory address is 0x%08X\n",
2b383351
JG
448 .reg = HGC_LM_DFX_STATUS2,
449 },
450 {
451 .irq_msk = BIT(SAS_ECC_INTR_ITCTLIST_ECC_1B_OFF),
452 .msk = HGC_LM_DFX_STATUS2_ITCTLIST_MSK,
453 .shift = HGC_LM_DFX_STATUS2_ITCTLIST_OFF,
729428ca 454 .msg = "hgc_itctl_acc1b_intr found: memory address is 0x%08X\n",
2b383351
JG
455 .reg = HGC_LM_DFX_STATUS2,
456 },
457 {
458 .irq_msk = BIT(SAS_ECC_INTR_CQE_ECC_1B_OFF),
459 .msk = HGC_CQE_ECC_1B_ADDR_MSK,
460 .shift = HGC_CQE_ECC_1B_ADDR_OFF,
729428ca 461 .msg = "hgc_cqe_acc1b_intr found: Ram address is 0x%08X\n",
2b383351
JG
462 .reg = HGC_CQE_ECC_ADDR,
463 },
464 {
465 .irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM0_ECC_1B_OFF),
466 .msk = HGC_RXM_DFX_STATUS14_MEM0_MSK,
467 .shift = HGC_RXM_DFX_STATUS14_MEM0_OFF,
729428ca 468 .msg = "rxm_mem0_acc1b_intr found: memory address is 0x%08X\n",
2b383351
JG
469 .reg = HGC_RXM_DFX_STATUS14,
470 },
471 {
472 .irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM1_ECC_1B_OFF),
473 .msk = HGC_RXM_DFX_STATUS14_MEM1_MSK,
474 .shift = HGC_RXM_DFX_STATUS14_MEM1_OFF,
729428ca 475 .msg = "rxm_mem1_acc1b_intr found: memory address is 0x%08X\n",
2b383351
JG
476 .reg = HGC_RXM_DFX_STATUS14,
477 },
478 {
479 .irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM2_ECC_1B_OFF),
480 .msk = HGC_RXM_DFX_STATUS14_MEM2_MSK,
481 .shift = HGC_RXM_DFX_STATUS14_MEM2_OFF,
729428ca 482 .msg = "rxm_mem2_acc1b_intr found: memory address is 0x%08X\n",
2b383351
JG
483 .reg = HGC_RXM_DFX_STATUS14,
484 },
485 {
486 .irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM3_ECC_1B_OFF),
487 .msk = HGC_RXM_DFX_STATUS15_MEM3_MSK,
488 .shift = HGC_RXM_DFX_STATUS15_MEM3_OFF,
729428ca 489 .msg = "rxm_mem3_acc1b_intr found: memory address is 0x%08X\n",
2b383351
JG
490 .reg = HGC_RXM_DFX_STATUS15,
491 },
492};
493
494static const struct hisi_sas_hw_error multi_bit_ecc_errors[] = {
495 {
496 .irq_msk = BIT(SAS_ECC_INTR_DQE_ECC_MB_OFF),
497 .msk = HGC_DQE_ECC_MB_ADDR_MSK,
498 .shift = HGC_DQE_ECC_MB_ADDR_OFF,
729428ca 499 .msg = "hgc_dqe_accbad_intr (0x%x) found: Ram address is 0x%08X\n",
2b383351
JG
500 .reg = HGC_DQE_ECC_ADDR,
501 },
502 {
503 .irq_msk = BIT(SAS_ECC_INTR_IOST_ECC_MB_OFF),
504 .msk = HGC_IOST_ECC_MB_ADDR_MSK,
505 .shift = HGC_IOST_ECC_MB_ADDR_OFF,
729428ca 506 .msg = "hgc_iost_accbad_intr (0x%x) found: Ram address is 0x%08X\n",
2b383351
JG
507 .reg = HGC_IOST_ECC_ADDR,
508 },
509 {
510 .irq_msk = BIT(SAS_ECC_INTR_ITCT_ECC_MB_OFF),
511 .msk = HGC_ITCT_ECC_MB_ADDR_MSK,
512 .shift = HGC_ITCT_ECC_MB_ADDR_OFF,
729428ca 513 .msg = "hgc_itct_accbad_intr (0x%x) found: Ram address is 0x%08X\n",
2b383351
JG
514 .reg = HGC_ITCT_ECC_ADDR,
515 },
516 {
517 .irq_msk = BIT(SAS_ECC_INTR_IOSTLIST_ECC_MB_OFF),
518 .msk = HGC_LM_DFX_STATUS2_IOSTLIST_MSK,
519 .shift = HGC_LM_DFX_STATUS2_IOSTLIST_OFF,
729428ca 520 .msg = "hgc_iostl_accbad_intr (0x%x) found: memory address is 0x%08X\n",
2b383351
JG
521 .reg = HGC_LM_DFX_STATUS2,
522 },
523 {
524 .irq_msk = BIT(SAS_ECC_INTR_ITCTLIST_ECC_MB_OFF),
525 .msk = HGC_LM_DFX_STATUS2_ITCTLIST_MSK,
526 .shift = HGC_LM_DFX_STATUS2_ITCTLIST_OFF,
729428ca 527 .msg = "hgc_itctl_accbad_intr (0x%x) found: memory address is 0x%08X\n",
2b383351
JG
528 .reg = HGC_LM_DFX_STATUS2,
529 },
530 {
531 .irq_msk = BIT(SAS_ECC_INTR_CQE_ECC_MB_OFF),
532 .msk = HGC_CQE_ECC_MB_ADDR_MSK,
533 .shift = HGC_CQE_ECC_MB_ADDR_OFF,
729428ca 534 .msg = "hgc_cqe_accbad_intr (0x%x) found: Ram address is 0x%08X\n",
2b383351
JG
535 .reg = HGC_CQE_ECC_ADDR,
536 },
537 {
538 .irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM0_ECC_MB_OFF),
539 .msk = HGC_RXM_DFX_STATUS14_MEM0_MSK,
540 .shift = HGC_RXM_DFX_STATUS14_MEM0_OFF,
729428ca 541 .msg = "rxm_mem0_accbad_intr (0x%x) found: memory address is 0x%08X\n",
2b383351
JG
542 .reg = HGC_RXM_DFX_STATUS14,
543 },
544 {
545 .irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM1_ECC_MB_OFF),
546 .msk = HGC_RXM_DFX_STATUS14_MEM1_MSK,
547 .shift = HGC_RXM_DFX_STATUS14_MEM1_OFF,
729428ca 548 .msg = "rxm_mem1_accbad_intr (0x%x) found: memory address is 0x%08X\n",
2b383351
JG
549 .reg = HGC_RXM_DFX_STATUS14,
550 },
551 {
552 .irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM2_ECC_MB_OFF),
553 .msk = HGC_RXM_DFX_STATUS14_MEM2_MSK,
554 .shift = HGC_RXM_DFX_STATUS14_MEM2_OFF,
729428ca 555 .msg = "rxm_mem2_accbad_intr (0x%x) found: memory address is 0x%08X\n",
2b383351
JG
556 .reg = HGC_RXM_DFX_STATUS14,
557 },
558 {
559 .irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM3_ECC_MB_OFF),
560 .msk = HGC_RXM_DFX_STATUS15_MEM3_MSK,
561 .shift = HGC_RXM_DFX_STATUS15_MEM3_OFF,
729428ca 562 .msg = "rxm_mem3_accbad_intr (0x%x) found: memory address is 0x%08X\n",
2b383351
JG
563 .reg = HGC_RXM_DFX_STATUS15,
564 },
565};
566
7911e66f
JG
567enum {
568 HISI_SAS_PHY_PHY_UPDOWN,
d3bf3d84 569 HISI_SAS_PHY_CHNL_INT,
7911e66f
JG
570 HISI_SAS_PHY_INT_NR
571};
572
e8fed0e9
JG
573enum {
574 TRANS_TX_FAIL_BASE = 0x0, /* dw0 */
634a9585
XC
575 TRANS_RX_FAIL_BASE = 0x20, /* dw1 */
576 DMA_TX_ERR_BASE = 0x40, /* dw2 bit 15-0 */
577 SIPC_RX_ERR_BASE = 0x50, /* dw2 bit 31-16*/
578 DMA_RX_ERR_BASE = 0x60, /* dw3 */
e8fed0e9
JG
579
580 /* trans tx*/
581 TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS = TRANS_TX_FAIL_BASE, /* 0x0 */
582 TRANS_TX_ERR_PHY_NOT_ENABLE, /* 0x1 */
583 TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION, /* 0x2 */
584 TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION, /* 0x3 */
585 TRANS_TX_OPEN_CNX_ERR_BY_OTHER, /* 0x4 */
586 RESERVED0, /* 0x5 */
587 TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT, /* 0x6 */
588 TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY, /* 0x7 */
589 TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED, /* 0x8 */
590 TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED, /* 0x9 */
591 TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION, /* 0xa */
592 TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD, /* 0xb */
593 TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER, /* 0xc */
594 TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED, /* 0xd */
595 TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT, /* 0xe */
596 TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION, /* 0xf */
597 TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED, /* 0x10 */
598 TRANS_TX_ERR_FRAME_TXED, /* 0x11 */
599 TRANS_TX_ERR_WITH_BREAK_TIMEOUT, /* 0x12 */
600 TRANS_TX_ERR_WITH_BREAK_REQUEST, /* 0x13 */
601 TRANS_TX_ERR_WITH_BREAK_RECEVIED, /* 0x14 */
602 TRANS_TX_ERR_WITH_CLOSE_TIMEOUT, /* 0x15 */
603 TRANS_TX_ERR_WITH_CLOSE_NORMAL, /* 0x16 for ssp*/
604 TRANS_TX_ERR_WITH_CLOSE_PHYDISALE, /* 0x17 */
605 TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT, /* 0x18 */
606 TRANS_TX_ERR_WITH_CLOSE_COMINIT, /* 0x19 */
607 TRANS_TX_ERR_WITH_NAK_RECEVIED, /* 0x1a for ssp*/
608 TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT, /* 0x1b for ssp*/
609 /*IO_TX_ERR_WITH_R_ERR_RECEVIED, [> 0x1b for sata/stp<] */
610 TRANS_TX_ERR_WITH_CREDIT_TIMEOUT, /* 0x1c for ssp */
611 /*IO_RX_ERR_WITH_SATA_DEVICE_LOST 0x1c for sata/stp */
612 TRANS_TX_ERR_WITH_IPTT_CONFLICT, /* 0x1d for ssp/smp */
613 TRANS_TX_ERR_WITH_OPEN_BY_DES_OR_OTHERS, /* 0x1e */
614 /*IO_TX_ERR_WITH_SYNC_RXD, [> 0x1e <] for sata/stp */
615 TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT, /* 0x1f for sata/stp */
616
617 /* trans rx */
634a9585
XC
618 TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR = TRANS_RX_FAIL_BASE, /* 0x20 */
619 TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR, /* 0x21 for sata/stp */
620 TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM, /* 0x22 for ssp/smp */
621 /*IO_ERR_WITH_RXFIS_8B10B_CODE_ERR, [> 0x22 <] for sata/stp */
622 TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR, /* 0x23 for sata/stp */
623 TRANS_RX_ERR_WITH_RXFIS_CRC_ERR, /* 0x24 for sata/stp */
624 TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN, /* 0x25 for smp */
625 /*IO_ERR_WITH_RXFIS_TX SYNCP, [> 0x25 <] for sata/stp */
626 TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP, /* 0x26 for sata/stp*/
627 TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN, /* 0x27 */
628 TRANS_RX_ERR_WITH_BREAK_TIMEOUT, /* 0x28 */
629 TRANS_RX_ERR_WITH_BREAK_REQUEST, /* 0x29 */
630 TRANS_RX_ERR_WITH_BREAK_RECEVIED, /* 0x2a */
631 RESERVED1, /* 0x2b */
632 TRANS_RX_ERR_WITH_CLOSE_NORMAL, /* 0x2c */
633 TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE, /* 0x2d */
634 TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT, /* 0x2e */
635 TRANS_RX_ERR_WITH_CLOSE_COMINIT, /* 0x2f */
636 TRANS_RX_ERR_WITH_DATA_LEN0, /* 0x30 for ssp/smp */
637 TRANS_RX_ERR_WITH_BAD_HASH, /* 0x31 for ssp */
638 /*IO_RX_ERR_WITH_FIS_TOO_SHORT, [> 0x31 <] for sata/stp */
639 TRANS_RX_XRDY_WLEN_ZERO_ERR, /* 0x32 for ssp*/
640 /*IO_RX_ERR_WITH_FIS_TOO_LONG, [> 0x32 <] for sata/stp */
641 TRANS_RX_SSP_FRM_LEN_ERR, /* 0x33 for ssp */
642 /*IO_RX_ERR_WITH_SATA_DEVICE_LOST, [> 0x33 <] for sata */
643 RESERVED2, /* 0x34 */
644 RESERVED3, /* 0x35 */
645 RESERVED4, /* 0x36 */
646 RESERVED5, /* 0x37 */
647 TRANS_RX_ERR_WITH_BAD_FRM_TYPE, /* 0x38 */
648 TRANS_RX_SMP_FRM_LEN_ERR, /* 0x39 */
649 TRANS_RX_SMP_RESP_TIMEOUT_ERR, /* 0x3a */
650 RESERVED6, /* 0x3b */
651 RESERVED7, /* 0x3c */
652 RESERVED8, /* 0x3d */
653 RESERVED9, /* 0x3e */
654 TRANS_RX_R_ERR, /* 0x3f */
e8fed0e9
JG
655
656 /* dma tx */
634a9585
XC
657 DMA_TX_DIF_CRC_ERR = DMA_TX_ERR_BASE, /* 0x40 */
658 DMA_TX_DIF_APP_ERR, /* 0x41 */
659 DMA_TX_DIF_RPP_ERR, /* 0x42 */
660 DMA_TX_DATA_SGL_OVERFLOW, /* 0x43 */
661 DMA_TX_DIF_SGL_OVERFLOW, /* 0x44 */
662 DMA_TX_UNEXP_XFER_ERR, /* 0x45 */
663 DMA_TX_UNEXP_RETRANS_ERR, /* 0x46 */
664 DMA_TX_XFER_LEN_OVERFLOW, /* 0x47 */
665 DMA_TX_XFER_OFFSET_ERR, /* 0x48 */
666 DMA_TX_RAM_ECC_ERR, /* 0x49 */
667 DMA_TX_DIF_LEN_ALIGN_ERR, /* 0x4a */
668 DMA_TX_MAX_ERR_CODE,
e8fed0e9
JG
669
670 /* sipc rx */
634a9585
XC
671 SIPC_RX_FIS_STATUS_ERR_BIT_VLD = SIPC_RX_ERR_BASE, /* 0x50 */
672 SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR, /* 0x51 */
673 SIPC_RX_FIS_STATUS_BSY_BIT_ERR, /* 0x52 */
674 SIPC_RX_WRSETUP_LEN_ODD_ERR, /* 0x53 */
675 SIPC_RX_WRSETUP_LEN_ZERO_ERR, /* 0x54 */
676 SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR, /* 0x55 */
677 SIPC_RX_NCQ_WRSETUP_OFFSET_ERR, /* 0x56 */
678 SIPC_RX_NCQ_WRSETUP_AUTO_ACTIVE_ERR, /* 0x57 */
679 SIPC_RX_SATA_UNEXP_FIS_ERR, /* 0x58 */
680 SIPC_RX_WRSETUP_ESTATUS_ERR, /* 0x59 */
681 SIPC_RX_DATA_UNDERFLOW_ERR, /* 0x5a */
682 SIPC_RX_MAX_ERR_CODE,
e8fed0e9
JG
683
684 /* dma rx */
634a9585
XC
685 DMA_RX_DIF_CRC_ERR = DMA_RX_ERR_BASE, /* 0x60 */
686 DMA_RX_DIF_APP_ERR, /* 0x61 */
687 DMA_RX_DIF_RPP_ERR, /* 0x62 */
688 DMA_RX_DATA_SGL_OVERFLOW, /* 0x63 */
689 DMA_RX_DIF_SGL_OVERFLOW, /* 0x64 */
690 DMA_RX_DATA_LEN_OVERFLOW, /* 0x65 */
691 DMA_RX_DATA_LEN_UNDERFLOW, /* 0x66 */
692 DMA_RX_DATA_OFFSET_ERR, /* 0x67 */
693 RESERVED10, /* 0x68 */
694 DMA_RX_SATA_FRAME_TYPE_ERR, /* 0x69 */
695 DMA_RX_RESP_BUF_OVERFLOW, /* 0x6a */
696 DMA_RX_UNEXP_RETRANS_RESP_ERR, /* 0x6b */
697 DMA_RX_UNEXP_NORM_RESP_ERR, /* 0x6c */
698 DMA_RX_UNEXP_RDFRAME_ERR, /* 0x6d */
699 DMA_RX_PIO_DATA_LEN_ERR, /* 0x6e */
700 DMA_RX_RDSETUP_STATUS_ERR, /* 0x6f */
701 DMA_RX_RDSETUP_STATUS_DRQ_ERR, /* 0x70 */
702 DMA_RX_RDSETUP_STATUS_BSY_ERR, /* 0x71 */
703 DMA_RX_RDSETUP_LEN_ODD_ERR, /* 0x72 */
704 DMA_RX_RDSETUP_LEN_ZERO_ERR, /* 0x73 */
705 DMA_RX_RDSETUP_LEN_OVER_ERR, /* 0x74 */
706 DMA_RX_RDSETUP_OFFSET_ERR, /* 0x75 */
707 DMA_RX_RDSETUP_ACTIVE_ERR, /* 0x76 */
708 DMA_RX_RDSETUP_ESTATUS_ERR, /* 0x77 */
709 DMA_RX_RAM_ECC_ERR, /* 0x78 */
710 DMA_RX_UNKNOWN_FRM_ERR, /* 0x79 */
711 DMA_RX_MAX_ERR_CODE,
e8fed0e9
JG
712};
713
94eac9e1 714#define HISI_SAS_COMMAND_ENTRIES_V2_HW 4096
32ccba52 715#define HISI_MAX_SATA_SUPPORT_V2_HW (HISI_SAS_COMMAND_ENTRIES_V2_HW/64 - 1)
94eac9e1 716
8c36e31d
JG
717#define DIR_NO_DATA 0
718#define DIR_TO_INI 1
719#define DIR_TO_DEVICE 2
720#define DIR_RESERVED 3
721
634a9585
XC
722#define ERR_ON_TX_PHASE(err_phase) (err_phase == 0x2 || \
723 err_phase == 0x4 || err_phase == 0x8 ||\
724 err_phase == 0x6 || err_phase == 0xa)
725#define ERR_ON_RX_PHASE(err_phase) (err_phase == 0x10 || \
726 err_phase == 0x20 || err_phase == 0x40)
727
77570eed 728static void link_timeout_disable_link(struct timer_list *t);
f2f89c32 729
94eac9e1
JG
730static u32 hisi_sas_read32(struct hisi_hba *hisi_hba, u32 off)
731{
732 void __iomem *regs = hisi_hba->regs + off;
733
734 return readl(regs);
735}
736
8c36e31d
JG
737static u32 hisi_sas_read32_relaxed(struct hisi_hba *hisi_hba, u32 off)
738{
739 void __iomem *regs = hisi_hba->regs + off;
740
741 return readl_relaxed(regs);
742}
743
94eac9e1
JG
744static void hisi_sas_write32(struct hisi_hba *hisi_hba, u32 off, u32 val)
745{
746 void __iomem *regs = hisi_hba->regs + off;
747
748 writel(val, regs);
749}
750
751static void hisi_sas_phy_write32(struct hisi_hba *hisi_hba, int phy_no,
752 u32 off, u32 val)
753{
754 void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
755
756 writel(val, regs);
757}
758
759static u32 hisi_sas_phy_read32(struct hisi_hba *hisi_hba,
760 int phy_no, u32 off)
761{
762 void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
763
764 return readl(regs);
765}
766
330fa7f3
JG
767/* This function needs to be protected from pre-emption. */
768static int
769slot_index_alloc_quirk_v2_hw(struct hisi_hba *hisi_hba, int *slot_idx,
32ccba52 770 struct domain_device *device)
330fa7f3 771{
330fa7f3 772 int sata_dev = dev_is_sata(device);
32ccba52
XT
773 void *bitmap = hisi_hba->slot_index_tags;
774 struct hisi_sas_device *sas_dev = device->lldd_dev;
775 int sata_idx = sas_dev->sata_idx;
776 int start, end;
777
778 if (!sata_dev) {
779 /*
780 * STP link SoC bug workaround: index starts from 1.
781 * additionally, we can only allocate odd IPTT(1~4095)
782 * for SAS/SMP device.
783 */
784 start = 1;
785 end = hisi_hba->slot_index_count;
786 } else {
787 if (sata_idx >= HISI_MAX_SATA_SUPPORT_V2_HW)
788 return -EINVAL;
789
790 /*
791 * For SATA device: allocate even IPTT in this interval
792 * [64*(sata_idx+1), 64*(sata_idx+2)], then each SATA device
793 * own 32 IPTTs. IPTT 0 shall not be used duing to STP link
794 * SoC bug workaround. So we ignore the first 32 even IPTTs.
795 */
796 start = 64 * (sata_idx + 1);
797 end = 64 * (sata_idx + 2);
798 }
330fa7f3
JG
799
800 while (1) {
32ccba52
XT
801 start = find_next_zero_bit(bitmap,
802 hisi_hba->slot_index_count, start);
803 if (start >= end)
330fa7f3
JG
804 return -SAS_QUEUE_FULL;
805 /*
32ccba52
XT
806 * SAS IPTT bit0 should be 1, and SATA IPTT bit0 should be 0.
807 */
808 if (sata_dev ^ (start & 1))
330fa7f3 809 break;
32ccba52 810 start++;
330fa7f3
JG
811 }
812
32ccba52
XT
813 set_bit(start, bitmap);
814 *slot_idx = start;
330fa7f3
JG
815 return 0;
816}
817
32ccba52
XT
818static bool sata_index_alloc_v2_hw(struct hisi_hba *hisi_hba, int *idx)
819{
820 unsigned int index;
11b75249 821 struct device *dev = hisi_hba->dev;
32ccba52
XT
822 void *bitmap = hisi_hba->sata_dev_bitmap;
823
824 index = find_first_zero_bit(bitmap, HISI_MAX_SATA_SUPPORT_V2_HW);
825 if (index >= HISI_MAX_SATA_SUPPORT_V2_HW) {
826 dev_warn(dev, "alloc sata index failed, index=%d\n", index);
827 return false;
828 }
829
830 set_bit(index, bitmap);
831 *idx = index;
832 return true;
833}
834
835
b2bdaf2b
JG
836static struct
837hisi_sas_device *alloc_dev_quirk_v2_hw(struct domain_device *device)
838{
839 struct hisi_hba *hisi_hba = device->port->ha->lldd_ha;
840 struct hisi_sas_device *sas_dev = NULL;
841 int i, sata_dev = dev_is_sata(device);
32ccba52 842 int sata_idx = -1;
302e0901 843 unsigned long flags;
b2bdaf2b 844
302e0901 845 spin_lock_irqsave(&hisi_hba->lock, flags);
32ccba52
XT
846
847 if (sata_dev)
848 if (!sata_index_alloc_v2_hw(hisi_hba, &sata_idx))
849 goto out;
850
b2bdaf2b
JG
851 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
852 /*
853 * SATA device id bit0 should be 0
854 */
855 if (sata_dev && (i & 1))
856 continue;
857 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
b1a49412
XC
858 int queue = i % hisi_hba->queue_count;
859 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
860
b2bdaf2b
JG
861 hisi_hba->devices[i].device_id = i;
862 sas_dev = &hisi_hba->devices[i];
863 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
864 sas_dev->dev_type = device->dev_type;
865 sas_dev->hisi_hba = hisi_hba;
866 sas_dev->sas_device = device;
32ccba52 867 sas_dev->sata_idx = sata_idx;
b1a49412 868 sas_dev->dq = dq;
405314df 869 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
b2bdaf2b
JG
870 break;
871 }
872 }
32ccba52
XT
873
874out:
302e0901 875 spin_unlock_irqrestore(&hisi_hba->lock, flags);
b2bdaf2b
JG
876
877 return sas_dev;
878}
879
29a20428
JG
880static void config_phy_opt_mode_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
881{
882 u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
883
884 cfg &= ~PHY_CFG_DC_OPT_MSK;
885 cfg |= 1 << PHY_CFG_DC_OPT_OFF;
886 hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
887}
888
806bb768
JG
889static void config_id_frame_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
890{
891 struct sas_identify_frame identify_frame;
892 u32 *identify_buffer;
893
894 memset(&identify_frame, 0, sizeof(identify_frame));
895 identify_frame.dev_type = SAS_END_DEVICE;
896 identify_frame.frame_type = 0;
897 identify_frame._un1 = 1;
898 identify_frame.initiator_bits = SAS_PROTOCOL_ALL;
899 identify_frame.target_bits = SAS_PROTOCOL_NONE;
900 memcpy(&identify_frame._un4_11[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
901 memcpy(&identify_frame.sas_addr[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
902 identify_frame.phy_id = phy_no;
903 identify_buffer = (u32 *)(&identify_frame);
904
905 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD0,
906 __swab32(identify_buffer[0]));
907 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD1,
d82debec 908 __swab32(identify_buffer[1]));
806bb768 909 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD2,
d82debec 910 __swab32(identify_buffer[2]));
806bb768 911 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD3,
d82debec 912 __swab32(identify_buffer[3]));
806bb768 913 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD4,
d82debec 914 __swab32(identify_buffer[4]));
806bb768
JG
915 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD5,
916 __swab32(identify_buffer[5]));
917}
918
85b2c3c0
JG
919static void setup_itct_v2_hw(struct hisi_hba *hisi_hba,
920 struct hisi_sas_device *sas_dev)
921{
922 struct domain_device *device = sas_dev->sas_device;
11b75249 923 struct device *dev = hisi_hba->dev;
85b2c3c0
JG
924 u64 qw0, device_id = sas_dev->device_id;
925 struct hisi_sas_itct *itct = &hisi_hba->itct[device_id];
926 struct domain_device *parent_dev = device->parent;
2e244f0f
JG
927 struct asd_sas_port *sas_port = device->port;
928 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
85b2c3c0
JG
929
930 memset(itct, 0, sizeof(*itct));
931
932 /* qw0 */
933 qw0 = 0;
934 switch (sas_dev->dev_type) {
935 case SAS_END_DEVICE:
936 case SAS_EDGE_EXPANDER_DEVICE:
937 case SAS_FANOUT_EXPANDER_DEVICE:
938 qw0 = HISI_SAS_DEV_TYPE_SSP << ITCT_HDR_DEV_TYPE_OFF;
939 break;
940 case SAS_SATA_DEV:
56cc74b9 941 case SAS_SATA_PENDING:
85b2c3c0
JG
942 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type))
943 qw0 = HISI_SAS_DEV_TYPE_STP << ITCT_HDR_DEV_TYPE_OFF;
944 else
945 qw0 = HISI_SAS_DEV_TYPE_SATA << ITCT_HDR_DEV_TYPE_OFF;
946 break;
947 default:
948 dev_warn(dev, "setup itct: unsupported dev type (%d)\n",
949 sas_dev->dev_type);
950 }
951
952 qw0 |= ((1 << ITCT_HDR_VALID_OFF) |
75249268 953 (device->linkrate << ITCT_HDR_MCR_OFF) |
85b2c3c0 954 (1 << ITCT_HDR_VLN_OFF) |
c399acfb
XC
955 (ITCT_HDR_SMP_TIMEOUT << ITCT_HDR_SMP_TIMEOUT_OFF) |
956 (1 << ITCT_HDR_AWT_CONTINUE_OFF) |
85b2c3c0
JG
957 (port->id << ITCT_HDR_PORT_ID_OFF));
958 itct->qw0 = cpu_to_le64(qw0);
959
960 /* qw1 */
961 memcpy(&itct->sas_addr, device->sas_addr, SAS_ADDR_SIZE);
962 itct->sas_addr = __swab64(itct->sas_addr);
963
964 /* qw2 */
f76a0b49 965 if (!dev_is_sata(device))
c399acfb 966 itct->qw2 = cpu_to_le64((5000ULL << ITCT_HDR_INLT_OFF) |
f76a0b49
JG
967 (0x1ULL << ITCT_HDR_BITLT_OFF) |
968 (0x32ULL << ITCT_HDR_MCTLT_OFF) |
969 (0x1ULL << ITCT_HDR_RTOLT_OFF));
85b2c3c0
JG
970}
971
f39943ee 972static void clear_itct_v2_hw(struct hisi_hba *hisi_hba,
85b2c3c0
JG
973 struct hisi_sas_device *sas_dev)
974{
640acc9a 975 DECLARE_COMPLETION_ONSTACK(completion);
c399acfb 976 u64 dev_id = sas_dev->device_id;
85b2c3c0
JG
977 struct hisi_sas_itct *itct = &hisi_hba->itct[dev_id];
978 u32 reg_val = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
979 int i;
980
640acc9a
XC
981 sas_dev->completion = &completion;
982
85b2c3c0
JG
983 /* clear the itct interrupt state */
984 if (ENT_INT_SRC3_ITC_INT_MSK & reg_val)
985 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
986 ENT_INT_SRC3_ITC_INT_MSK);
987
85b2c3c0 988 for (i = 0; i < 2; i++) {
640acc9a 989 reg_val = ITCT_CLR_EN_MSK | (dev_id & ITCT_DEV_MSK);
85b2c3c0 990 hisi_sas_write32(hisi_hba, ITCT_CLR, reg_val);
640acc9a 991 wait_for_completion(sas_dev->completion);
85b2c3c0 992
640acc9a 993 memset(itct, 0, sizeof(struct hisi_sas_itct));
85b2c3c0
JG
994 }
995}
996
f39943ee
XT
997static void free_device_v2_hw(struct hisi_sas_device *sas_dev)
998{
999 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1000
1001 /* SoC bug workaround */
1002 if (dev_is_sata(sas_dev->sas_device))
1003 clear_bit(sas_dev->sata_idx, hisi_hba->sata_dev_bitmap);
1004}
1005
94eac9e1
JG
1006static int reset_hw_v2_hw(struct hisi_hba *hisi_hba)
1007{
1008 int i, reset_val;
1009 u32 val;
1010 unsigned long end_time;
11b75249 1011 struct device *dev = hisi_hba->dev;
94eac9e1
JG
1012
1013 /* The mask needs to be set depending on the number of phys */
1014 if (hisi_hba->n_phy == 9)
1015 reset_val = 0x1fffff;
1016 else
1017 reset_val = 0x7ffff;
1018
d0df8f9a 1019 hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0);
94eac9e1
JG
1020
1021 /* Disable all of the PHYs */
1022 for (i = 0; i < hisi_hba->n_phy; i++) {
1023 u32 phy_cfg = hisi_sas_phy_read32(hisi_hba, i, PHY_CFG);
1024
1025 phy_cfg &= ~PHY_CTRL_RESET_MSK;
1026 hisi_sas_phy_write32(hisi_hba, i, PHY_CFG, phy_cfg);
1027 }
1028 udelay(50);
1029
1030 /* Ensure DMA tx & rx idle */
1031 for (i = 0; i < hisi_hba->n_phy; i++) {
1032 u32 dma_tx_status, dma_rx_status;
1033
1034 end_time = jiffies + msecs_to_jiffies(1000);
1035
1036 while (1) {
1037 dma_tx_status = hisi_sas_phy_read32(hisi_hba, i,
1038 DMA_TX_STATUS);
1039 dma_rx_status = hisi_sas_phy_read32(hisi_hba, i,
1040 DMA_RX_STATUS);
1041
1042 if (!(dma_tx_status & DMA_TX_STATUS_BUSY_MSK) &&
1043 !(dma_rx_status & DMA_RX_STATUS_BUSY_MSK))
1044 break;
1045
1046 msleep(20);
1047 if (time_after(jiffies, end_time))
1048 return -EIO;
1049 }
1050 }
1051
1052 /* Ensure axi bus idle */
1053 end_time = jiffies + msecs_to_jiffies(1000);
1054 while (1) {
1055 u32 axi_status =
1056 hisi_sas_read32(hisi_hba, AXI_CFG);
1057
1058 if (axi_status == 0)
1059 break;
1060
1061 msleep(20);
1062 if (time_after(jiffies, end_time))
1063 return -EIO;
1064 }
1065
50408712
JG
1066 if (ACPI_HANDLE(dev)) {
1067 acpi_status s;
94eac9e1 1068
50408712
JG
1069 s = acpi_evaluate_object(ACPI_HANDLE(dev), "_RST", NULL, NULL);
1070 if (ACPI_FAILURE(s)) {
1071 dev_err(dev, "Reset failed\n");
1072 return -EIO;
1073 }
1074 } else if (hisi_hba->ctrl) {
1075 /* reset and disable clock*/
1076 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg,
1077 reset_val);
1078 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg + 4,
1079 reset_val);
1080 msleep(1);
1081 regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg, &val);
1082 if (reset_val != (val & reset_val)) {
1083 dev_err(dev, "SAS reset fail.\n");
1084 return -EIO;
1085 }
1086
1087 /* De-reset and enable clock*/
1088 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg + 4,
1089 reset_val);
1090 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg,
1091 reset_val);
1092 msleep(1);
1093 regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg,
1094 &val);
1095 if (val & reset_val) {
1096 dev_err(dev, "SAS de-reset fail.\n");
1097 return -EIO;
1098 }
bcbc7f1c
XC
1099 } else {
1100 dev_err(dev, "no reset method\n");
1101 return -EINVAL;
1102 }
94eac9e1
JG
1103
1104 return 0;
1105}
1106
c7b9d369
XT
1107/* This function needs to be called after resetting SAS controller. */
1108static void phys_reject_stp_links_v2_hw(struct hisi_hba *hisi_hba)
1109{
1110 u32 cfg;
1111 int phy_no;
1112
1113 hisi_hba->reject_stp_links_msk = (1 << hisi_hba->n_phy) - 1;
1114 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1115 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, CON_CONTROL);
1116 if (!(cfg & CON_CONTROL_CFG_OPEN_ACC_STP_MSK))
1117 continue;
1118
1119 cfg &= ~CON_CONTROL_CFG_OPEN_ACC_STP_MSK;
1120 hisi_sas_phy_write32(hisi_hba, phy_no, CON_CONTROL, cfg);
1121 }
1122}
1123
1124static void phys_try_accept_stp_links_v2_hw(struct hisi_hba *hisi_hba)
1125{
1126 int phy_no;
1127 u32 dma_tx_dfx1;
1128
1129 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1130 if (!(hisi_hba->reject_stp_links_msk & BIT(phy_no)))
1131 continue;
1132
1133 dma_tx_dfx1 = hisi_sas_phy_read32(hisi_hba, phy_no,
1134 DMA_TX_DFX1);
1135 if (dma_tx_dfx1 & DMA_TX_DFX1_IPTT_MSK) {
1136 u32 cfg = hisi_sas_phy_read32(hisi_hba,
1137 phy_no, CON_CONTROL);
1138
1139 cfg |= CON_CONTROL_CFG_OPEN_ACC_STP_MSK;
1140 hisi_sas_phy_write32(hisi_hba, phy_no,
1141 CON_CONTROL, cfg);
1142 clear_bit(phy_no, &hisi_hba->reject_stp_links_msk);
1143 }
1144 }
1145}
1146
ad16fe31
XT
1147static const struct signal_attenuation_s x6000 = {9200, 0, 10476};
1148static const struct sig_atten_lu_s sig_atten_lu[] = {
1149 { &x6000, 0x3016a68 },
1150};
1151
94eac9e1
JG
1152static void init_reg_v2_hw(struct hisi_hba *hisi_hba)
1153{
11b75249 1154 struct device *dev = hisi_hba->dev;
ad16fe31
XT
1155 u32 sas_phy_ctrl = 0x30b9908;
1156 u32 signal[3];
94eac9e1
JG
1157 int i;
1158
1159 /* Global registers init */
1160
1161 /* Deal with am-max-transmissions quirk */
50408712 1162 if (device_property_present(dev, "hip06-sas-v2-quirk-amt")) {
94eac9e1
JG
1163 hisi_sas_write32(hisi_hba, AM_CFG_MAX_TRANS, 0x2020);
1164 hisi_sas_write32(hisi_hba, AM_CFG_SINGLE_PORT_MAX_TRANS,
1165 0x2020);
1166 } /* Else, use defaults -> do nothing */
1167
1168 hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE,
1169 (u32)((1ULL << hisi_hba->queue_count) - 1));
1170 hisi_sas_write32(hisi_hba, AXI_USER1, 0xc0000000);
1171 hisi_sas_write32(hisi_hba, AXI_USER2, 0x10000);
f1dc7518 1172 hisi_sas_write32(hisi_hba, HGC_SAS_TXFAIL_RETRY_CTRL, 0x0);
94eac9e1
JG
1173 hisi_sas_write32(hisi_hba, HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL, 0x7FF);
1174 hisi_sas_write32(hisi_hba, OPENA_WT_CONTI_TIME, 0x1);
1175 hisi_sas_write32(hisi_hba, I_T_NEXUS_LOSS_TIME, 0x1F4);
f76a0b49 1176 hisi_sas_write32(hisi_hba, MAX_CON_TIME_LIMIT_TIME, 0x32);
94eac9e1
JG
1177 hisi_sas_write32(hisi_hba, BUS_INACTIVE_LIMIT_TIME, 0x1);
1178 hisi_sas_write32(hisi_hba, CFG_AGING_TIME, 0x1);
1179 hisi_sas_write32(hisi_hba, HGC_ERR_STAT_EN, 0x1);
1180 hisi_sas_write32(hisi_hba, HGC_GET_ITV_TIME, 0x1);
f1dc7518
JG
1181 hisi_sas_write32(hisi_hba, INT_COAL_EN, 0xc);
1182 hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x60);
1183 hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 0x3);
94eac9e1
JG
1184 hisi_sas_write32(hisi_hba, ENT_INT_COAL_TIME, 0x1);
1185 hisi_sas_write32(hisi_hba, ENT_INT_COAL_CNT, 0x1);
1186 hisi_sas_write32(hisi_hba, OQ_INT_SRC, 0x0);
1187 hisi_sas_write32(hisi_hba, ENT_INT_SRC1, 0xffffffff);
1188 hisi_sas_write32(hisi_hba, ENT_INT_SRC2, 0xffffffff);
1189 hisi_sas_write32(hisi_hba, ENT_INT_SRC3, 0xffffffff);
1190 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0x7efefefe);
1191 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0x7efefefe);
640acc9a 1192 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0x7ffe20fe);
d3b688d3 1193 hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xfff00c30);
94eac9e1
JG
1194 for (i = 0; i < hisi_hba->queue_count; i++)
1195 hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK+0x4*i, 0);
1196
1197 hisi_sas_write32(hisi_hba, AXI_AHB_CLK_CFG, 1);
1198 hisi_sas_write32(hisi_hba, HYPER_STREAM_ID_EN_CFG, 1);
1199
ad16fe31
XT
1200 /* Get sas_phy_ctrl value to deal with TX FFE issue. */
1201 if (!device_property_read_u32_array(dev, "hisilicon,signal-attenuation",
1202 signal, ARRAY_SIZE(signal))) {
1203 for (i = 0; i < ARRAY_SIZE(sig_atten_lu); i++) {
1204 const struct sig_atten_lu_s *lookup = &sig_atten_lu[i];
1205 const struct signal_attenuation_s *att = lookup->att;
1206
1207 if ((signal[0] == att->de_emphasis) &&
1208 (signal[1] == att->preshoot) &&
1209 (signal[2] == att->boost)) {
1210 sas_phy_ctrl = lookup->sas_phy_ctrl;
1211 break;
1212 }
1213 }
1214
1215 if (i == ARRAY_SIZE(sig_atten_lu))
1216 dev_warn(dev, "unknown signal attenuation values, using default PHY ctrl config\n");
1217 }
1218
94eac9e1 1219 for (i = 0; i < hisi_hba->n_phy; i++) {
f385b4ff
XC
1220 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
1221 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1222 u32 prog_phy_link_rate = 0x800;
1223
1224 if (!sas_phy->phy || (sas_phy->phy->maximum_linkrate <
1225 SAS_LINK_RATE_1_5_GBPS)) {
1226 prog_phy_link_rate = 0x855;
1227 } else {
1228 enum sas_linkrate max = sas_phy->phy->maximum_linkrate;
1229
1230 prog_phy_link_rate =
1231 hisi_sas_get_prog_phy_linkrate_mask(max) |
1232 0x800;
1233 }
1234 hisi_sas_phy_write32(hisi_hba, i, PROG_PHY_LINK_RATE,
1235 prog_phy_link_rate);
ad16fe31 1236 hisi_sas_phy_write32(hisi_hba, i, SAS_PHY_CTRL, sas_phy_ctrl);
94eac9e1 1237 hisi_sas_phy_write32(hisi_hba, i, SL_TOUT_CFG, 0x7d7d7d7d);
9c81e2cf
JG
1238 hisi_sas_phy_write32(hisi_hba, i, SL_CONTROL, 0x0);
1239 hisi_sas_phy_write32(hisi_hba, i, TXID_AUTO, 0x2);
f1dc7518 1240 hisi_sas_phy_write32(hisi_hba, i, DONE_RECEIVED_TIME, 0x8);
94eac9e1
JG
1241 hisi_sas_phy_write32(hisi_hba, i, CHL_INT0, 0xffffffff);
1242 hisi_sas_phy_write32(hisi_hba, i, CHL_INT1, 0xffffffff);
d3b688d3 1243 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2, 0xfff87fff);
94eac9e1 1244 hisi_sas_phy_write32(hisi_hba, i, RXOP_CHECK_CFG_H, 0x1000);
9dd1d620 1245 hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xff857fff);
066312f6 1246 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0x8ffffbfe);
f1dc7518 1247 hisi_sas_phy_write32(hisi_hba, i, SL_CFG, 0x13f801fc);
94eac9e1
JG
1248 hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL_RDY_MSK, 0x0);
1249 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_NOT_RDY_MSK, 0x0);
1250 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_DWS_RESET_MSK, 0x0);
1251 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_PHY_ENA_MSK, 0x0);
1252 hisi_sas_phy_write32(hisi_hba, i, SL_RX_BCAST_CHK_MSK, 0x0);
1253 hisi_sas_phy_write32(hisi_hba, i, CHL_INT_COAL_EN, 0x0);
1254 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_OOB_RESTART_MSK, 0x0);
3bc45af8
JG
1255 if (hisi_hba->refclk_frequency_mhz == 66)
1256 hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL, 0x199B694);
1257 /* else, do nothing -> leave it how you found it */
94eac9e1
JG
1258 }
1259
1260 for (i = 0; i < hisi_hba->queue_count; i++) {
1261 /* Delivery queue */
1262 hisi_sas_write32(hisi_hba,
1263 DLVRY_Q_0_BASE_ADDR_HI + (i * 0x14),
1264 upper_32_bits(hisi_hba->cmd_hdr_dma[i]));
1265
1266 hisi_sas_write32(hisi_hba, DLVRY_Q_0_BASE_ADDR_LO + (i * 0x14),
1267 lower_32_bits(hisi_hba->cmd_hdr_dma[i]));
1268
1269 hisi_sas_write32(hisi_hba, DLVRY_Q_0_DEPTH + (i * 0x14),
1270 HISI_SAS_QUEUE_SLOTS);
1271
1272 /* Completion queue */
1273 hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_HI + (i * 0x14),
1274 upper_32_bits(hisi_hba->complete_hdr_dma[i]));
1275
1276 hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_LO + (i * 0x14),
1277 lower_32_bits(hisi_hba->complete_hdr_dma[i]));
1278
1279 hisi_sas_write32(hisi_hba, COMPL_Q_0_DEPTH + (i * 0x14),
1280 HISI_SAS_QUEUE_SLOTS);
1281 }
1282
1283 /* itct */
1284 hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_LO,
1285 lower_32_bits(hisi_hba->itct_dma));
1286
1287 hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_HI,
1288 upper_32_bits(hisi_hba->itct_dma));
1289
1290 /* iost */
1291 hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_LO,
1292 lower_32_bits(hisi_hba->iost_dma));
1293
1294 hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_HI,
1295 upper_32_bits(hisi_hba->iost_dma));
1296
1297 /* breakpoint */
1298 hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_LO,
1299 lower_32_bits(hisi_hba->breakpoint_dma));
1300
1301 hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_HI,
1302 upper_32_bits(hisi_hba->breakpoint_dma));
1303
1304 /* SATA broken msg */
1305 hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_LO,
1306 lower_32_bits(hisi_hba->sata_breakpoint_dma));
1307
1308 hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_HI,
1309 upper_32_bits(hisi_hba->sata_breakpoint_dma));
1310
1311 /* SATA initial fis */
1312 hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_LO,
1313 lower_32_bits(hisi_hba->initial_fis_dma));
1314
1315 hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_HI,
1316 upper_32_bits(hisi_hba->initial_fis_dma));
1317}
1318
77570eed 1319static void link_timeout_enable_link(struct timer_list *t)
f2f89c32 1320{
77570eed 1321 struct hisi_hba *hisi_hba = from_timer(hisi_hba, t, timer);
f2f89c32
XC
1322 int i, reg_val;
1323
1324 for (i = 0; i < hisi_hba->n_phy; i++) {
c7b9d369
XT
1325 if (hisi_hba->reject_stp_links_msk & BIT(i))
1326 continue;
1327
f2f89c32
XC
1328 reg_val = hisi_sas_phy_read32(hisi_hba, i, CON_CONTROL);
1329 if (!(reg_val & BIT(0))) {
1330 hisi_sas_phy_write32(hisi_hba, i,
1331 CON_CONTROL, 0x7);
1332 break;
1333 }
1334 }
1335
841b86f3 1336 hisi_hba->timer.function = link_timeout_disable_link;
f2f89c32
XC
1337 mod_timer(&hisi_hba->timer, jiffies + msecs_to_jiffies(900));
1338}
1339
77570eed 1340static void link_timeout_disable_link(struct timer_list *t)
f2f89c32 1341{
77570eed 1342 struct hisi_hba *hisi_hba = from_timer(hisi_hba, t, timer);
f2f89c32
XC
1343 int i, reg_val;
1344
1345 reg_val = hisi_sas_read32(hisi_hba, PHY_STATE);
1346 for (i = 0; i < hisi_hba->n_phy && reg_val; i++) {
c7b9d369
XT
1347 if (hisi_hba->reject_stp_links_msk & BIT(i))
1348 continue;
1349
f2f89c32
XC
1350 if (reg_val & BIT(i)) {
1351 hisi_sas_phy_write32(hisi_hba, i,
1352 CON_CONTROL, 0x6);
1353 break;
1354 }
1355 }
1356
841b86f3 1357 hisi_hba->timer.function = link_timeout_enable_link;
f2f89c32
XC
1358 mod_timer(&hisi_hba->timer, jiffies + msecs_to_jiffies(100));
1359}
1360
1361static void set_link_timer_quirk(struct hisi_hba *hisi_hba)
1362{
841b86f3 1363 hisi_hba->timer.function = link_timeout_disable_link;
f2f89c32
XC
1364 hisi_hba->timer.expires = jiffies + msecs_to_jiffies(1000);
1365 add_timer(&hisi_hba->timer);
1366}
1367
94eac9e1
JG
1368static int hw_init_v2_hw(struct hisi_hba *hisi_hba)
1369{
11b75249 1370 struct device *dev = hisi_hba->dev;
94eac9e1
JG
1371 int rc;
1372
1373 rc = reset_hw_v2_hw(hisi_hba);
1374 if (rc) {
1375 dev_err(dev, "hisi_sas_reset_hw failed, rc=%d", rc);
1376 return rc;
1377 }
1378
1379 msleep(100);
1380 init_reg_v2_hw(hisi_hba);
806bb768 1381
94eac9e1
JG
1382 return 0;
1383}
1384
29a20428
JG
1385static void enable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1386{
1387 u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
1388
1389 cfg |= PHY_CFG_ENA_MSK;
1390 hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
1391}
1392
4935933e
XT
1393static bool is_sata_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1394{
1395 u32 context;
1396
1397 context = hisi_sas_read32(hisi_hba, PHY_CONTEXT);
1398 if (context & (1 << phy_no))
1399 return true;
1400
1401 return false;
1402}
1403
819cbf18
XT
1404static bool tx_fifo_is_empty_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1405{
1406 u32 dfx_val;
1407
1408 dfx_val = hisi_sas_phy_read32(hisi_hba, phy_no, DMA_TX_DFX1);
1409
1410 if (dfx_val & BIT(16))
1411 return false;
1412
1413 return true;
1414}
1415
1416static bool axi_bus_is_idle_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1417{
1418 int i, max_loop = 1000;
11b75249 1419 struct device *dev = hisi_hba->dev;
819cbf18
XT
1420 u32 status, axi_status, dfx_val, dfx_tx_val;
1421
1422 for (i = 0; i < max_loop; i++) {
1423 status = hisi_sas_read32_relaxed(hisi_hba,
1424 AXI_MASTER_CFG_BASE + AM_CURR_TRANS_RETURN);
1425
1426 axi_status = hisi_sas_read32(hisi_hba, AXI_CFG);
1427 dfx_val = hisi_sas_phy_read32(hisi_hba, phy_no, DMA_TX_DFX1);
1428 dfx_tx_val = hisi_sas_phy_read32(hisi_hba,
1429 phy_no, DMA_TX_FIFO_DFX0);
1430
1431 if ((status == 0x3) && (axi_status == 0x0) &&
1432 (dfx_val & BIT(20)) && (dfx_tx_val & BIT(10)))
1433 return true;
1434 udelay(10);
1435 }
1436 dev_err(dev, "bus is not idle phy%d, axi150:0x%x axi100:0x%x port204:0x%x port240:0x%x\n",
1437 phy_no, status, axi_status,
1438 dfx_val, dfx_tx_val);
1439 return false;
1440}
1441
1442static bool wait_io_done_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1443{
1444 int i, max_loop = 1000;
11b75249 1445 struct device *dev = hisi_hba->dev;
819cbf18
XT
1446 u32 status, tx_dfx0;
1447
1448 for (i = 0; i < max_loop; i++) {
1449 status = hisi_sas_phy_read32(hisi_hba, phy_no, LINK_DFX2);
1450 status = (status & 0x3fc0) >> 6;
1451
1452 if (status != 0x1)
1453 return true;
1454
1455 tx_dfx0 = hisi_sas_phy_read32(hisi_hba, phy_no, DMA_TX_DFX0);
1456 if ((tx_dfx0 & 0x1ff) == 0x2)
1457 return true;
1458 udelay(10);
1459 }
1460 dev_err(dev, "IO not done phy%d, port264:0x%x port200:0x%x\n",
1461 phy_no, status, tx_dfx0);
1462 return false;
1463}
1464
1465static bool allowed_disable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1466{
1467 if (tx_fifo_is_empty_v2_hw(hisi_hba, phy_no))
1468 return true;
1469
1470 if (!axi_bus_is_idle_v2_hw(hisi_hba, phy_no))
1471 return false;
1472
1473 if (!wait_io_done_v2_hw(hisi_hba, phy_no))
1474 return false;
1475
1476 return true;
1477}
1478
1479
63fb11b8
JG
1480static void disable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1481{
819cbf18 1482 u32 cfg, axi_val, dfx0_val, txid_auto;
11b75249 1483 struct device *dev = hisi_hba->dev;
819cbf18
XT
1484
1485 /* Close axi bus. */
1486 axi_val = hisi_sas_read32(hisi_hba, AXI_MASTER_CFG_BASE +
1487 AM_CTRL_GLOBAL);
1488 axi_val |= 0x1;
1489 hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE +
1490 AM_CTRL_GLOBAL, axi_val);
1491
1492 if (is_sata_phy_v2_hw(hisi_hba, phy_no)) {
1493 if (allowed_disable_phy_v2_hw(hisi_hba, phy_no))
1494 goto do_disable;
63fb11b8 1495
819cbf18
XT
1496 /* Reset host controller. */
1497 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
1498 return;
1499 }
1500
1501 dfx0_val = hisi_sas_phy_read32(hisi_hba, phy_no, PORT_DFX0);
1502 dfx0_val = (dfx0_val & 0x1fc0) >> 6;
1503 if (dfx0_val != 0x4)
1504 goto do_disable;
1505
1506 if (!tx_fifo_is_empty_v2_hw(hisi_hba, phy_no)) {
1507 dev_warn(dev, "phy%d, wait tx fifo need send break\n",
1508 phy_no);
1509 txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no,
1510 TXID_AUTO);
1511 txid_auto |= TXID_AUTO_CTB_MSK;
1512 hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
1513 txid_auto);
1514 }
1515
1516do_disable:
1517 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
63fb11b8
JG
1518 cfg &= ~PHY_CFG_ENA_MSK;
1519 hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
819cbf18
XT
1520
1521 /* Open axi bus. */
1522 axi_val &= ~0x1;
1523 hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE +
1524 AM_CTRL_GLOBAL, axi_val);
63fb11b8
JG
1525}
1526
29a20428
JG
1527static void start_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1528{
1529 config_id_frame_v2_hw(hisi_hba, phy_no);
1530 config_phy_opt_mode_v2_hw(hisi_hba, phy_no);
1531 enable_phy_v2_hw(hisi_hba, phy_no);
1532}
1533
63fb11b8
JG
1534static void phy_hard_reset_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1535{
0edef7e4
XC
1536 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1537 u32 txid_auto;
1538
a25d0d3d 1539 disable_phy_v2_hw(hisi_hba, phy_no);
0edef7e4
XC
1540 if (phy->identify.device_type == SAS_END_DEVICE) {
1541 txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
1542 hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
1543 txid_auto | TX_HARDRST_MSK);
1544 }
63fb11b8
JG
1545 msleep(100);
1546 start_phy_v2_hw(hisi_hba, phy_no);
1547}
1548
c52108c6
XT
1549static void phy_get_events_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1550{
1551 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1552 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1553 struct sas_phy *sphy = sas_phy->phy;
1554 u32 err4_reg_val, err6_reg_val;
1555
1556 /* loss dword syn, phy reset problem */
1557 err4_reg_val = hisi_sas_phy_read32(hisi_hba, phy_no, SAS_ERR_CNT4_REG);
1558
1559 /* disparity err, invalid dword */
1560 err6_reg_val = hisi_sas_phy_read32(hisi_hba, phy_no, SAS_ERR_CNT6_REG);
1561
1562 sphy->loss_of_dword_sync_count += (err4_reg_val >> 16) & 0xFFFF;
1563 sphy->phy_reset_problem_count += err4_reg_val & 0xFFFF;
1564 sphy->invalid_dword_count += (err6_reg_val & 0xFF0000) >> 16;
1565 sphy->running_disparity_error_count += err6_reg_val & 0xFF;
1566}
1567
a25d0d3d 1568static void phys_init_v2_hw(struct hisi_hba *hisi_hba)
29a20428 1569{
29a20428
JG
1570 int i;
1571
917d3bda
XT
1572 for (i = 0; i < hisi_hba->n_phy; i++) {
1573 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
1574 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1575
1576 if (!sas_phy->phy->enabled)
1577 continue;
1578
29a20428 1579 start_phy_v2_hw(hisi_hba, i);
917d3bda 1580 }
29a20428
JG
1581}
1582
7911e66f
JG
1583static void sl_notify_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1584{
1585 u32 sl_control;
1586
1587 sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1588 sl_control |= SL_CONTROL_NOTIFY_EN_MSK;
1589 hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
1590 msleep(1);
1591 sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1592 sl_control &= ~SL_CONTROL_NOTIFY_EN_MSK;
1593 hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
1594}
1595
2ae75787
XC
1596static enum sas_linkrate phy_get_max_linkrate_v2_hw(void)
1597{
1598 return SAS_LINK_RATE_12_0_GBPS;
1599}
1600
1601static void phy_set_linkrate_v2_hw(struct hisi_hba *hisi_hba, int phy_no,
1602 struct sas_phy_linkrates *r)
1603{
2ae75787
XC
1604 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1605 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2ae75787 1606 enum sas_linkrate min, max;
f385b4ff 1607 u32 prog_phy_link_rate = 0x800;
2ae75787
XC
1608
1609 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1610 max = sas_phy->phy->maximum_linkrate;
1611 min = r->minimum_linkrate;
1612 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1613 max = r->maximum_linkrate;
1614 min = sas_phy->phy->minimum_linkrate;
1615 } else
1616 return;
1617
1618 sas_phy->phy->maximum_linkrate = max;
1619 sas_phy->phy->minimum_linkrate = min;
f385b4ff 1620 prog_phy_link_rate |= hisi_sas_get_prog_phy_linkrate_mask(max);
2ae75787 1621
5dcac3a4
XT
1622 disable_phy_v2_hw(hisi_hba, phy_no);
1623 msleep(100);
2ae75787
XC
1624 hisi_sas_phy_write32(hisi_hba, phy_no, PROG_PHY_LINK_RATE,
1625 prog_phy_link_rate);
5dcac3a4 1626 start_phy_v2_hw(hisi_hba, phy_no);
2ae75787
XC
1627}
1628
5473c060
JG
1629static int get_wideport_bitmap_v2_hw(struct hisi_hba *hisi_hba, int port_id)
1630{
1631 int i, bitmap = 0;
1632 u32 phy_port_num_ma = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
1633 u32 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1634
1635 for (i = 0; i < (hisi_hba->n_phy < 9 ? hisi_hba->n_phy : 8); i++)
1636 if (phy_state & 1 << i)
1637 if (((phy_port_num_ma >> (i * 4)) & 0xf) == port_id)
1638 bitmap |= 1 << i;
1639
1640 if (hisi_hba->n_phy == 9) {
1641 u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
1642
1643 if (phy_state & 1 << 8)
1644 if (((port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
1645 PORT_STATE_PHY8_PORT_NUM_OFF) == port_id)
1646 bitmap |= 1 << 9;
1647 }
1648
1649 return bitmap;
1650}
1651
b1a49412 1652/*
8c36e31d
JG
1653 * The callpath to this function and upto writing the write
1654 * queue pointer should be safe from interruption.
1655 */
b1a49412
XC
1656static int
1657get_free_slot_v2_hw(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq)
8c36e31d 1658{
11b75249 1659 struct device *dev = hisi_hba->dev;
b1a49412 1660 int queue = dq->id;
8c36e31d 1661 u32 r, w;
c70f1fb7 1662
c70f1fb7
XC
1663 w = dq->wr_point;
1664 r = hisi_sas_read32_relaxed(hisi_hba,
1665 DLVRY_Q_0_RD_PTR + (queue * 0x14));
1666 if (r == (w+1) % HISI_SAS_QUEUE_SLOTS) {
c58ec824 1667 dev_warn(dev, "full queue=%d r=%d w=%d\n",
c70f1fb7
XC
1668 queue, r, w);
1669 return -EAGAIN;
8c36e31d 1670 }
c70f1fb7 1671
c58ec824
XC
1672 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
1673
1674 return w;
8c36e31d
JG
1675}
1676
c58ec824 1677/* DQ lock must be taken here */
b1a49412 1678static void start_delivery_v2_hw(struct hisi_sas_dq *dq)
8c36e31d 1679{
b1a49412 1680 struct hisi_hba *hisi_hba = dq->hisi_hba;
c58ec824
XC
1681 struct hisi_sas_slot *s, *s1;
1682 struct list_head *dq_list;
1683 int dlvry_queue = dq->id;
1684 int wp, count = 0;
1685
1686 dq_list = &dq->list;
1687 list_for_each_entry_safe(s, s1, &dq->list, delivery) {
1688 if (!s->ready)
1689 break;
1690 count++;
1691 wp = (s->dlvry_queue_slot + 1) % HISI_SAS_QUEUE_SLOTS;
1692 list_del(&s->delivery);
1693 }
1694
1695 if (!count)
1696 return;
8c36e31d 1697
c58ec824 1698 hisi_sas_write32(hisi_hba, DLVRY_Q_0_WR_PTR + (dlvry_queue * 0x14), wp);
8c36e31d
JG
1699}
1700
81d115ec 1701static void prep_prd_sge_v2_hw(struct hisi_hba *hisi_hba,
8c36e31d
JG
1702 struct hisi_sas_slot *slot,
1703 struct hisi_sas_cmd_hdr *hdr,
1704 struct scatterlist *scatter,
1705 int n_elem)
1706{
f557e32c 1707 struct hisi_sas_sge_page *sge_page = hisi_sas_sge_addr_mem(slot);
8c36e31d
JG
1708 struct scatterlist *sg;
1709 int i;
1710
8c36e31d 1711 for_each_sg(scatter, sg, n_elem, i) {
f557e32c 1712 struct hisi_sas_sge *entry = &sge_page->sge[i];
8c36e31d
JG
1713
1714 entry->addr = cpu_to_le64(sg_dma_address(sg));
1715 entry->page_ctrl_0 = entry->page_ctrl_1 = 0;
1716 entry->data_len = cpu_to_le32(sg_dma_len(sg));
1717 entry->data_off = 0;
1718 }
1719
f557e32c 1720 hdr->prd_table_addr = cpu_to_le64(hisi_sas_sge_addr_dma(slot));
8c36e31d
JG
1721
1722 hdr->sg_len = cpu_to_le32(n_elem << CMD_HDR_DATA_SGL_LEN_OFF);
8c36e31d
JG
1723}
1724
81d115ec 1725static void prep_smp_v2_hw(struct hisi_hba *hisi_hba,
c2d89392
JG
1726 struct hisi_sas_slot *slot)
1727{
1728 struct sas_task *task = slot->task;
1729 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1730 struct domain_device *device = task->dev;
c2d89392 1731 struct hisi_sas_port *port = slot->port;
8118ae07 1732 struct scatterlist *sg_req;
c2d89392
JG
1733 struct hisi_sas_device *sas_dev = device->lldd_dev;
1734 dma_addr_t req_dma_addr;
8118ae07 1735 unsigned int req_len;
c2d89392 1736
c2d89392
JG
1737 /* req */
1738 sg_req = &task->smp_task.smp_req;
c2d89392 1739 req_dma_addr = sg_dma_address(sg_req);
8118ae07 1740 req_len = sg_dma_len(&task->smp_task.smp_req);
c2d89392
JG
1741
1742 /* create header */
1743 /* dw0 */
1744 hdr->dw0 = cpu_to_le32((port->id << CMD_HDR_PORT_OFF) |
1745 (1 << CMD_HDR_PRIORITY_OFF) | /* high pri */
1746 (2 << CMD_HDR_CMD_OFF)); /* smp */
1747
1748 /* map itct entry */
1749 hdr->dw1 = cpu_to_le32((sas_dev->device_id << CMD_HDR_DEV_ID_OFF) |
1750 (1 << CMD_HDR_FRAME_TYPE_OFF) |
1751 (DIR_NO_DATA << CMD_HDR_DIR_OFF));
1752
1753 /* dw2 */
1754 hdr->dw2 = cpu_to_le32((((req_len - 4) / 4) << CMD_HDR_CFL_OFF) |
1755 (HISI_SAS_MAX_SMP_RESP_SZ / 4 <<
1756 CMD_HDR_MRFL_OFF));
1757
1758 hdr->transfer_tags = cpu_to_le32(slot->idx << CMD_HDR_IPTT_OFF);
1759
1760 hdr->cmd_table_addr = cpu_to_le64(req_dma_addr);
f557e32c 1761 hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
c2d89392
JG
1762}
1763
81d115ec 1764static void prep_ssp_v2_hw(struct hisi_hba *hisi_hba,
8c36e31d
JG
1765 struct hisi_sas_slot *slot, int is_tmf,
1766 struct hisi_sas_tmf_task *tmf)
1767{
1768 struct sas_task *task = slot->task;
1769 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1770 struct domain_device *device = task->dev;
1771 struct hisi_sas_device *sas_dev = device->lldd_dev;
1772 struct hisi_sas_port *port = slot->port;
1773 struct sas_ssp_task *ssp_task = &task->ssp_task;
1774 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
81d115ec 1775 int has_data = 0, priority = is_tmf;
8c36e31d
JG
1776 u8 *buf_cmd;
1777 u32 dw1 = 0, dw2 = 0;
1778
1779 hdr->dw0 = cpu_to_le32((1 << CMD_HDR_RESP_REPORT_OFF) |
1780 (2 << CMD_HDR_TLR_CTRL_OFF) |
1781 (port->id << CMD_HDR_PORT_OFF) |
1782 (priority << CMD_HDR_PRIORITY_OFF) |
1783 (1 << CMD_HDR_CMD_OFF)); /* ssp */
1784
1785 dw1 = 1 << CMD_HDR_VDTL_OFF;
1786 if (is_tmf) {
1787 dw1 |= 2 << CMD_HDR_FRAME_TYPE_OFF;
1788 dw1 |= DIR_NO_DATA << CMD_HDR_DIR_OFF;
1789 } else {
1790 dw1 |= 1 << CMD_HDR_FRAME_TYPE_OFF;
1791 switch (scsi_cmnd->sc_data_direction) {
1792 case DMA_TO_DEVICE:
1793 has_data = 1;
1794 dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
1795 break;
1796 case DMA_FROM_DEVICE:
1797 has_data = 1;
1798 dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
1799 break;
1800 default:
1801 dw1 &= ~CMD_HDR_DIR_MSK;
1802 }
1803 }
1804
1805 /* map itct entry */
1806 dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
1807 hdr->dw1 = cpu_to_le32(dw1);
1808
1809 dw2 = (((sizeof(struct ssp_command_iu) + sizeof(struct ssp_frame_hdr)
1810 + 3) / 4) << CMD_HDR_CFL_OFF) |
1811 ((HISI_SAS_MAX_SSP_RESP_SZ / 4) << CMD_HDR_MRFL_OFF) |
1812 (2 << CMD_HDR_SG_MOD_OFF);
1813 hdr->dw2 = cpu_to_le32(dw2);
1814
1815 hdr->transfer_tags = cpu_to_le32(slot->idx);
1816
81d115ec
XC
1817 if (has_data)
1818 prep_prd_sge_v2_hw(hisi_hba, slot, hdr, task->scatter,
8c36e31d 1819 slot->n_elem);
8c36e31d
JG
1820
1821 hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
f557e32c
XT
1822 hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
1823 hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
8c36e31d 1824
f557e32c
XT
1825 buf_cmd = hisi_sas_cmd_hdr_addr_mem(slot) +
1826 sizeof(struct ssp_frame_hdr);
8c36e31d
JG
1827
1828 memcpy(buf_cmd, &task->ssp_task.LUN, 8);
1829 if (!is_tmf) {
1830 buf_cmd[9] = task->ssp_task.task_attr |
1831 (task->ssp_task.task_prio << 3);
1832 memcpy(buf_cmd + 12, task->ssp_task.cmd->cmnd,
1833 task->ssp_task.cmd->cmd_len);
1834 } else {
1835 buf_cmd[10] = tmf->tmf;
1836 switch (tmf->tmf) {
1837 case TMF_ABORT_TASK:
1838 case TMF_QUERY_TASK:
1839 buf_cmd[12] =
1840 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
1841 buf_cmd[13] =
1842 tmf->tag_of_task_to_be_managed & 0xff;
1843 break;
1844 default:
1845 break;
1846 }
1847 }
8c36e31d
JG
1848}
1849
634a9585
XC
1850#define TRANS_TX_ERR 0
1851#define TRANS_RX_ERR 1
1852#define DMA_TX_ERR 2
1853#define SIPC_RX_ERR 3
1854#define DMA_RX_ERR 4
1855
1856#define DMA_TX_ERR_OFF 0
1857#define DMA_TX_ERR_MSK (0xffff << DMA_TX_ERR_OFF)
1858#define SIPC_RX_ERR_OFF 16
1859#define SIPC_RX_ERR_MSK (0xffff << SIPC_RX_ERR_OFF)
1860
1861static int parse_trans_tx_err_code_v2_hw(u32 err_msk)
1862{
89b203e9 1863 static const u8 trans_tx_err_code_prio[] = {
634a9585
XC
1864 TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS,
1865 TRANS_TX_ERR_PHY_NOT_ENABLE,
1866 TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION,
1867 TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION,
1868 TRANS_TX_OPEN_CNX_ERR_BY_OTHER,
1869 RESERVED0,
1870 TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT,
1871 TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY,
1872 TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED,
1873 TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED,
1874 TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION,
1875 TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD,
1876 TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER,
1877 TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED,
1878 TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT,
1879 TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION,
1880 TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED,
1881 TRANS_TX_ERR_WITH_CLOSE_PHYDISALE,
1882 TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT,
1883 TRANS_TX_ERR_WITH_CLOSE_COMINIT,
1884 TRANS_TX_ERR_WITH_BREAK_TIMEOUT,
1885 TRANS_TX_ERR_WITH_BREAK_REQUEST,
1886 TRANS_TX_ERR_WITH_BREAK_RECEVIED,
1887 TRANS_TX_ERR_WITH_CLOSE_TIMEOUT,
1888 TRANS_TX_ERR_WITH_CLOSE_NORMAL,
1889 TRANS_TX_ERR_WITH_NAK_RECEVIED,
1890 TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT,
1891 TRANS_TX_ERR_WITH_CREDIT_TIMEOUT,
1892 TRANS_TX_ERR_WITH_IPTT_CONFLICT,
1893 TRANS_TX_ERR_WITH_OPEN_BY_DES_OR_OTHERS,
1894 TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT,
1895 };
1896 int index, i;
1897
1898 for (i = 0; i < ARRAY_SIZE(trans_tx_err_code_prio); i++) {
1899 index = trans_tx_err_code_prio[i] - TRANS_TX_FAIL_BASE;
1900 if (err_msk & (1 << index))
1901 return trans_tx_err_code_prio[i];
1902 }
1903 return -1;
1904}
1905
1906static int parse_trans_rx_err_code_v2_hw(u32 err_msk)
1907{
89b203e9 1908 static const u8 trans_rx_err_code_prio[] = {
634a9585
XC
1909 TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR,
1910 TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR,
1911 TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM,
1912 TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR,
1913 TRANS_RX_ERR_WITH_RXFIS_CRC_ERR,
1914 TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN,
1915 TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP,
1916 TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN,
1917 TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE,
1918 TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT,
1919 TRANS_RX_ERR_WITH_CLOSE_COMINIT,
1920 TRANS_RX_ERR_WITH_BREAK_TIMEOUT,
1921 TRANS_RX_ERR_WITH_BREAK_REQUEST,
1922 TRANS_RX_ERR_WITH_BREAK_RECEVIED,
1923 RESERVED1,
1924 TRANS_RX_ERR_WITH_CLOSE_NORMAL,
1925 TRANS_RX_ERR_WITH_DATA_LEN0,
1926 TRANS_RX_ERR_WITH_BAD_HASH,
1927 TRANS_RX_XRDY_WLEN_ZERO_ERR,
1928 TRANS_RX_SSP_FRM_LEN_ERR,
1929 RESERVED2,
1930 RESERVED3,
1931 RESERVED4,
1932 RESERVED5,
1933 TRANS_RX_ERR_WITH_BAD_FRM_TYPE,
1934 TRANS_RX_SMP_FRM_LEN_ERR,
1935 TRANS_RX_SMP_RESP_TIMEOUT_ERR,
1936 RESERVED6,
1937 RESERVED7,
1938 RESERVED8,
1939 RESERVED9,
1940 TRANS_RX_R_ERR,
1941 };
1942 int index, i;
1943
1944 for (i = 0; i < ARRAY_SIZE(trans_rx_err_code_prio); i++) {
1945 index = trans_rx_err_code_prio[i] - TRANS_RX_FAIL_BASE;
1946 if (err_msk & (1 << index))
1947 return trans_rx_err_code_prio[i];
1948 }
1949 return -1;
1950}
1951
1952static int parse_dma_tx_err_code_v2_hw(u32 err_msk)
1953{
89b203e9 1954 static const u8 dma_tx_err_code_prio[] = {
634a9585
XC
1955 DMA_TX_UNEXP_XFER_ERR,
1956 DMA_TX_UNEXP_RETRANS_ERR,
1957 DMA_TX_XFER_LEN_OVERFLOW,
1958 DMA_TX_XFER_OFFSET_ERR,
1959 DMA_TX_RAM_ECC_ERR,
1960 DMA_TX_DIF_LEN_ALIGN_ERR,
1961 DMA_TX_DIF_CRC_ERR,
1962 DMA_TX_DIF_APP_ERR,
1963 DMA_TX_DIF_RPP_ERR,
1964 DMA_TX_DATA_SGL_OVERFLOW,
1965 DMA_TX_DIF_SGL_OVERFLOW,
1966 };
1967 int index, i;
1968
1969 for (i = 0; i < ARRAY_SIZE(dma_tx_err_code_prio); i++) {
1970 index = dma_tx_err_code_prio[i] - DMA_TX_ERR_BASE;
1971 err_msk = err_msk & DMA_TX_ERR_MSK;
1972 if (err_msk & (1 << index))
1973 return dma_tx_err_code_prio[i];
1974 }
1975 return -1;
1976}
1977
1978static int parse_sipc_rx_err_code_v2_hw(u32 err_msk)
1979{
89b203e9 1980 static const u8 sipc_rx_err_code_prio[] = {
634a9585
XC
1981 SIPC_RX_FIS_STATUS_ERR_BIT_VLD,
1982 SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR,
1983 SIPC_RX_FIS_STATUS_BSY_BIT_ERR,
1984 SIPC_RX_WRSETUP_LEN_ODD_ERR,
1985 SIPC_RX_WRSETUP_LEN_ZERO_ERR,
1986 SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR,
1987 SIPC_RX_NCQ_WRSETUP_OFFSET_ERR,
1988 SIPC_RX_NCQ_WRSETUP_AUTO_ACTIVE_ERR,
1989 SIPC_RX_SATA_UNEXP_FIS_ERR,
1990 SIPC_RX_WRSETUP_ESTATUS_ERR,
1991 SIPC_RX_DATA_UNDERFLOW_ERR,
1992 };
1993 int index, i;
1994
1995 for (i = 0; i < ARRAY_SIZE(sipc_rx_err_code_prio); i++) {
1996 index = sipc_rx_err_code_prio[i] - SIPC_RX_ERR_BASE;
1997 err_msk = err_msk & SIPC_RX_ERR_MSK;
1998 if (err_msk & (1 << (index + 0x10)))
1999 return sipc_rx_err_code_prio[i];
2000 }
2001 return -1;
2002}
2003
2004static int parse_dma_rx_err_code_v2_hw(u32 err_msk)
2005{
89b203e9 2006 static const u8 dma_rx_err_code_prio[] = {
634a9585
XC
2007 DMA_RX_UNKNOWN_FRM_ERR,
2008 DMA_RX_DATA_LEN_OVERFLOW,
2009 DMA_RX_DATA_LEN_UNDERFLOW,
2010 DMA_RX_DATA_OFFSET_ERR,
2011 RESERVED10,
2012 DMA_RX_SATA_FRAME_TYPE_ERR,
2013 DMA_RX_RESP_BUF_OVERFLOW,
2014 DMA_RX_UNEXP_RETRANS_RESP_ERR,
2015 DMA_RX_UNEXP_NORM_RESP_ERR,
2016 DMA_RX_UNEXP_RDFRAME_ERR,
2017 DMA_RX_PIO_DATA_LEN_ERR,
2018 DMA_RX_RDSETUP_STATUS_ERR,
2019 DMA_RX_RDSETUP_STATUS_DRQ_ERR,
2020 DMA_RX_RDSETUP_STATUS_BSY_ERR,
2021 DMA_RX_RDSETUP_LEN_ODD_ERR,
2022 DMA_RX_RDSETUP_LEN_ZERO_ERR,
2023 DMA_RX_RDSETUP_LEN_OVER_ERR,
2024 DMA_RX_RDSETUP_OFFSET_ERR,
2025 DMA_RX_RDSETUP_ACTIVE_ERR,
2026 DMA_RX_RDSETUP_ESTATUS_ERR,
2027 DMA_RX_RAM_ECC_ERR,
2028 DMA_RX_DIF_CRC_ERR,
2029 DMA_RX_DIF_APP_ERR,
2030 DMA_RX_DIF_RPP_ERR,
2031 DMA_RX_DATA_SGL_OVERFLOW,
2032 DMA_RX_DIF_SGL_OVERFLOW,
2033 };
2034 int index, i;
2035
2036 for (i = 0; i < ARRAY_SIZE(dma_rx_err_code_prio); i++) {
2037 index = dma_rx_err_code_prio[i] - DMA_RX_ERR_BASE;
2038 if (err_msk & (1 << index))
2039 return dma_rx_err_code_prio[i];
2040 }
2041 return -1;
2042}
2043
e8fed0e9
JG
2044/* by default, task resp is complete */
2045static void slot_err_v2_hw(struct hisi_hba *hisi_hba,
2046 struct sas_task *task,
634a9585
XC
2047 struct hisi_sas_slot *slot,
2048 int err_phase)
e8fed0e9
JG
2049{
2050 struct task_status_struct *ts = &task->task_status;
f557e32c
XT
2051 struct hisi_sas_err_record_v2 *err_record =
2052 hisi_sas_status_buf_addr_mem(slot);
e8fed0e9
JG
2053 u32 trans_tx_fail_type = cpu_to_le32(err_record->trans_tx_fail_type);
2054 u32 trans_rx_fail_type = cpu_to_le32(err_record->trans_rx_fail_type);
2055 u16 dma_tx_err_type = cpu_to_le16(err_record->dma_tx_err_type);
2056 u16 sipc_rx_err_type = cpu_to_le16(err_record->sipc_rx_err_type);
2057 u32 dma_rx_err_type = cpu_to_le32(err_record->dma_rx_err_type);
2058 int error = -1;
2059
634a9585
XC
2060 if (err_phase == 1) {
2061 /* error in TX phase, the priority of error is: DW2 > DW0 */
2062 error = parse_dma_tx_err_code_v2_hw(dma_tx_err_type);
2063 if (error == -1)
2064 error = parse_trans_tx_err_code_v2_hw(
2065 trans_tx_fail_type);
2066 } else if (err_phase == 2) {
2067 /* error in RX phase, the priority is: DW1 > DW3 > DW2 */
2068 error = parse_trans_rx_err_code_v2_hw(
2069 trans_rx_fail_type);
2070 if (error == -1) {
2071 error = parse_dma_rx_err_code_v2_hw(
2072 dma_rx_err_type);
2073 if (error == -1)
2074 error = parse_sipc_rx_err_code_v2_hw(
2075 sipc_rx_err_type);
2076 }
e8fed0e9
JG
2077 }
2078
2079 switch (task->task_proto) {
2080 case SAS_PROTOCOL_SSP:
2081 {
2082 switch (error) {
2083 case TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION:
2084 {
2085 ts->stat = SAS_OPEN_REJECT;
2086 ts->open_rej_reason = SAS_OREJ_NO_DEST;
a28b259b 2087 break;
e8fed0e9
JG
2088 }
2089 case TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED:
2090 {
2091 ts->stat = SAS_OPEN_REJECT;
2092 ts->open_rej_reason = SAS_OREJ_EPROTO;
2093 break;
2094 }
2095 case TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED:
2096 {
2097 ts->stat = SAS_OPEN_REJECT;
2098 ts->open_rej_reason = SAS_OREJ_CONN_RATE;
2099 break;
2100 }
2101 case TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION:
2102 {
2103 ts->stat = SAS_OPEN_REJECT;
2104 ts->open_rej_reason = SAS_OREJ_BAD_DEST;
2105 break;
2106 }
e8fed0e9
JG
2107 case TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION:
2108 {
2109 ts->stat = SAS_OPEN_REJECT;
2110 ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
2111 break;
2112 }
634a9585 2113 case DMA_RX_UNEXP_NORM_RESP_ERR:
e8fed0e9 2114 case TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION:
634a9585 2115 case DMA_RX_RESP_BUF_OVERFLOW:
e8fed0e9
JG
2116 {
2117 ts->stat = SAS_OPEN_REJECT;
2118 ts->open_rej_reason = SAS_OREJ_UNKNOWN;
2119 break;
2120 }
2121 case TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER:
2122 {
2123 /* not sure */
2124 ts->stat = SAS_DEV_NO_RESPONSE;
2125 break;
2126 }
e8fed0e9
JG
2127 case DMA_RX_DATA_LEN_OVERFLOW:
2128 {
2129 ts->stat = SAS_DATA_OVERRUN;
2130 ts->residual = 0;
2131 break;
2132 }
2133 case DMA_RX_DATA_LEN_UNDERFLOW:
e8fed0e9 2134 {
01b361fc 2135 ts->residual = trans_tx_fail_type;
e8fed0e9
JG
2136 ts->stat = SAS_DATA_UNDERRUN;
2137 break;
2138 }
2139 case TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS:
2140 case TRANS_TX_ERR_PHY_NOT_ENABLE:
2141 case TRANS_TX_OPEN_CNX_ERR_BY_OTHER:
2142 case TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT:
634a9585
XC
2143 case TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD:
2144 case TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED:
2145 case TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT:
e8fed0e9
JG
2146 case TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED:
2147 case TRANS_TX_ERR_WITH_BREAK_TIMEOUT:
2148 case TRANS_TX_ERR_WITH_BREAK_REQUEST:
2149 case TRANS_TX_ERR_WITH_BREAK_RECEVIED:
2150 case TRANS_TX_ERR_WITH_CLOSE_TIMEOUT:
2151 case TRANS_TX_ERR_WITH_CLOSE_NORMAL:
634a9585 2152 case TRANS_TX_ERR_WITH_CLOSE_PHYDISALE:
e8fed0e9
JG
2153 case TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT:
2154 case TRANS_TX_ERR_WITH_CLOSE_COMINIT:
2155 case TRANS_TX_ERR_WITH_NAK_RECEVIED:
2156 case TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT:
e8fed0e9 2157 case TRANS_TX_ERR_WITH_CREDIT_TIMEOUT:
634a9585 2158 case TRANS_TX_ERR_WITH_IPTT_CONFLICT:
e8fed0e9
JG
2159 case TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR:
2160 case TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR:
2161 case TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM:
634a9585 2162 case TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN:
e8fed0e9
JG
2163 case TRANS_RX_ERR_WITH_BREAK_TIMEOUT:
2164 case TRANS_RX_ERR_WITH_BREAK_REQUEST:
2165 case TRANS_RX_ERR_WITH_BREAK_RECEVIED:
2166 case TRANS_RX_ERR_WITH_CLOSE_NORMAL:
2167 case TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT:
2168 case TRANS_RX_ERR_WITH_CLOSE_COMINIT:
634a9585
XC
2169 case TRANS_TX_ERR_FRAME_TXED:
2170 case TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE:
e8fed0e9
JG
2171 case TRANS_RX_ERR_WITH_DATA_LEN0:
2172 case TRANS_RX_ERR_WITH_BAD_HASH:
2173 case TRANS_RX_XRDY_WLEN_ZERO_ERR:
2174 case TRANS_RX_SSP_FRM_LEN_ERR:
2175 case TRANS_RX_ERR_WITH_BAD_FRM_TYPE:
634a9585 2176 case DMA_TX_DATA_SGL_OVERFLOW:
e8fed0e9
JG
2177 case DMA_TX_UNEXP_XFER_ERR:
2178 case DMA_TX_UNEXP_RETRANS_ERR:
2179 case DMA_TX_XFER_LEN_OVERFLOW:
2180 case DMA_TX_XFER_OFFSET_ERR:
634a9585
XC
2181 case SIPC_RX_DATA_UNDERFLOW_ERR:
2182 case DMA_RX_DATA_SGL_OVERFLOW:
e8fed0e9 2183 case DMA_RX_DATA_OFFSET_ERR:
634a9585
XC
2184 case DMA_RX_RDSETUP_LEN_ODD_ERR:
2185 case DMA_RX_RDSETUP_LEN_ZERO_ERR:
2186 case DMA_RX_RDSETUP_LEN_OVER_ERR:
2187 case DMA_RX_SATA_FRAME_TYPE_ERR:
e8fed0e9
JG
2188 case DMA_RX_UNKNOWN_FRM_ERR:
2189 {
634a9585
XC
2190 /* This will request a retry */
2191 ts->stat = SAS_QUEUE_FULL;
2192 slot->abort = 1;
e8fed0e9
JG
2193 break;
2194 }
2195 default:
2196 break;
2197 }
2198 }
2199 break;
2200 case SAS_PROTOCOL_SMP:
2201 ts->stat = SAM_STAT_CHECK_CONDITION;
2202 break;
2203
2204 case SAS_PROTOCOL_SATA:
2205 case SAS_PROTOCOL_STP:
2206 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
2207 {
2208 switch (error) {
e8fed0e9 2209 case TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION:
634a9585
XC
2210 {
2211 ts->stat = SAS_OPEN_REJECT;
2212 ts->open_rej_reason = SAS_OREJ_NO_DEST;
2213 break;
2214 }
2215 case TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER:
e8fed0e9
JG
2216 {
2217 ts->resp = SAS_TASK_UNDELIVERED;
2218 ts->stat = SAS_DEV_NO_RESPONSE;
2219 break;
2220 }
2221 case TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED:
634a9585
XC
2222 {
2223 ts->stat = SAS_OPEN_REJECT;
2224 ts->open_rej_reason = SAS_OREJ_EPROTO;
2225 break;
2226 }
e8fed0e9 2227 case TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED:
634a9585
XC
2228 {
2229 ts->stat = SAS_OPEN_REJECT;
2230 ts->open_rej_reason = SAS_OREJ_CONN_RATE;
2231 break;
2232 }
e8fed0e9 2233 case TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION:
634a9585
XC
2234 {
2235 ts->stat = SAS_OPEN_REJECT;
2236 ts->open_rej_reason = SAS_OREJ_CONN_RATE;
2237 break;
2238 }
e8fed0e9 2239 case TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION:
e8fed0e9
JG
2240 {
2241 ts->stat = SAS_OPEN_REJECT;
634a9585 2242 ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
e8fed0e9
JG
2243 break;
2244 }
634a9585
XC
2245 case DMA_RX_RESP_BUF_OVERFLOW:
2246 case DMA_RX_UNEXP_NORM_RESP_ERR:
2247 case TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION:
e8fed0e9 2248 {
634a9585
XC
2249 ts->stat = SAS_OPEN_REJECT;
2250 ts->open_rej_reason = SAS_OREJ_UNKNOWN;
e8fed0e9
JG
2251 break;
2252 }
2253 case DMA_RX_DATA_LEN_OVERFLOW:
2254 {
2255 ts->stat = SAS_DATA_OVERRUN;
634a9585
XC
2256 ts->residual = 0;
2257 break;
2258 }
2259 case DMA_RX_DATA_LEN_UNDERFLOW:
2260 {
01b361fc 2261 ts->residual = trans_tx_fail_type;
634a9585 2262 ts->stat = SAS_DATA_UNDERRUN;
e8fed0e9
JG
2263 break;
2264 }
2265 case TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS:
2266 case TRANS_TX_ERR_PHY_NOT_ENABLE:
2267 case TRANS_TX_OPEN_CNX_ERR_BY_OTHER:
2268 case TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT:
634a9585
XC
2269 case TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD:
2270 case TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED:
2271 case TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT:
e8fed0e9
JG
2272 case TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED:
2273 case TRANS_TX_ERR_WITH_BREAK_TIMEOUT:
2274 case TRANS_TX_ERR_WITH_BREAK_REQUEST:
2275 case TRANS_TX_ERR_WITH_BREAK_RECEVIED:
2276 case TRANS_TX_ERR_WITH_CLOSE_TIMEOUT:
2277 case TRANS_TX_ERR_WITH_CLOSE_NORMAL:
634a9585 2278 case TRANS_TX_ERR_WITH_CLOSE_PHYDISALE:
e8fed0e9
JG
2279 case TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT:
2280 case TRANS_TX_ERR_WITH_CLOSE_COMINIT:
e8fed0e9
JG
2281 case TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT:
2282 case TRANS_TX_ERR_WITH_CREDIT_TIMEOUT:
634a9585 2283 case TRANS_TX_ERR_WITH_OPEN_BY_DES_OR_OTHERS:
e8fed0e9 2284 case TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT:
e8fed0e9 2285 case TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM:
634a9585 2286 case TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR:
e8fed0e9
JG
2287 case TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR:
2288 case TRANS_RX_ERR_WITH_RXFIS_CRC_ERR:
2289 case TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN:
2290 case TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP:
634a9585
XC
2291 case TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN:
2292 case TRANS_RX_ERR_WITH_BREAK_TIMEOUT:
2293 case TRANS_RX_ERR_WITH_BREAK_REQUEST:
2294 case TRANS_RX_ERR_WITH_BREAK_RECEVIED:
e8fed0e9
JG
2295 case TRANS_RX_ERR_WITH_CLOSE_NORMAL:
2296 case TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE:
2297 case TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT:
2298 case TRANS_RX_ERR_WITH_CLOSE_COMINIT:
2299 case TRANS_RX_ERR_WITH_DATA_LEN0:
2300 case TRANS_RX_ERR_WITH_BAD_HASH:
2301 case TRANS_RX_XRDY_WLEN_ZERO_ERR:
634a9585
XC
2302 case TRANS_RX_ERR_WITH_BAD_FRM_TYPE:
2303 case DMA_TX_DATA_SGL_OVERFLOW:
2304 case DMA_TX_UNEXP_XFER_ERR:
2305 case DMA_TX_UNEXP_RETRANS_ERR:
2306 case DMA_TX_XFER_LEN_OVERFLOW:
2307 case DMA_TX_XFER_OFFSET_ERR:
e8fed0e9
JG
2308 case SIPC_RX_FIS_STATUS_ERR_BIT_VLD:
2309 case SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR:
2310 case SIPC_RX_FIS_STATUS_BSY_BIT_ERR:
2311 case SIPC_RX_WRSETUP_LEN_ODD_ERR:
2312 case SIPC_RX_WRSETUP_LEN_ZERO_ERR:
2313 case SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR:
2314 case SIPC_RX_SATA_UNEXP_FIS_ERR:
634a9585
XC
2315 case DMA_RX_DATA_SGL_OVERFLOW:
2316 case DMA_RX_DATA_OFFSET_ERR:
e8fed0e9
JG
2317 case DMA_RX_SATA_FRAME_TYPE_ERR:
2318 case DMA_RX_UNEXP_RDFRAME_ERR:
2319 case DMA_RX_PIO_DATA_LEN_ERR:
2320 case DMA_RX_RDSETUP_STATUS_ERR:
2321 case DMA_RX_RDSETUP_STATUS_DRQ_ERR:
2322 case DMA_RX_RDSETUP_STATUS_BSY_ERR:
2323 case DMA_RX_RDSETUP_LEN_ODD_ERR:
2324 case DMA_RX_RDSETUP_LEN_ZERO_ERR:
2325 case DMA_RX_RDSETUP_LEN_OVER_ERR:
2326 case DMA_RX_RDSETUP_OFFSET_ERR:
2327 case DMA_RX_RDSETUP_ACTIVE_ERR:
2328 case DMA_RX_RDSETUP_ESTATUS_ERR:
2329 case DMA_RX_UNKNOWN_FRM_ERR:
634a9585
XC
2330 case TRANS_RX_SSP_FRM_LEN_ERR:
2331 case TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY:
e8fed0e9 2332 {
634a9585
XC
2333 slot->abort = 1;
2334 ts->stat = SAS_PHY_DOWN;
e8fed0e9
JG
2335 break;
2336 }
2337 default:
2338 {
2339 ts->stat = SAS_PROTO_RESPONSE;
2340 break;
2341 }
2342 }
75904077 2343 hisi_sas_sata_done(task, slot);
e8fed0e9
JG
2344 }
2345 break;
2346 default:
2347 break;
2348 }
2349}
2350
31a9cfa6 2351static int
405314df 2352slot_complete_v2_hw(struct hisi_hba *hisi_hba, struct hisi_sas_slot *slot)
31a9cfa6
JG
2353{
2354 struct sas_task *task = slot->task;
2355 struct hisi_sas_device *sas_dev;
11b75249 2356 struct device *dev = hisi_hba->dev;
31a9cfa6
JG
2357 struct task_status_struct *ts;
2358 struct domain_device *device;
68e6bace 2359 struct sas_ha_struct *ha;
31a9cfa6
JG
2360 enum exec_status sts;
2361 struct hisi_sas_complete_v2_hdr *complete_queue =
2362 hisi_hba->complete_hdr[slot->cmplt_queue];
2363 struct hisi_sas_complete_v2_hdr *complete_hdr =
2364 &complete_queue[slot->cmplt_queue_slot];
54c9dd2d 2365 unsigned long flags;
68e6bace 2366 bool is_internal = slot->is_internal;
31a9cfa6
JG
2367
2368 if (unlikely(!task || !task->lldd_task || !task->dev))
2369 return -EINVAL;
2370
2371 ts = &task->task_status;
2372 device = task->dev;
68e6bace 2373 ha = device->port->ha;
31a9cfa6
JG
2374 sas_dev = device->lldd_dev;
2375
54c9dd2d 2376 spin_lock_irqsave(&task->task_state_lock, flags);
31a9cfa6
JG
2377 task->task_state_flags &=
2378 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
54c9dd2d 2379 spin_unlock_irqrestore(&task->task_state_lock, flags);
31a9cfa6
JG
2380
2381 memset(ts, 0, sizeof(*ts));
2382 ts->resp = SAS_TASK_COMPLETE;
2383
405314df
JG
2384 if (unlikely(!sas_dev)) {
2385 dev_dbg(dev, "slot complete: port has no device\n");
31a9cfa6
JG
2386 ts->stat = SAS_PHY_DOWN;
2387 goto out;
2388 }
2389
df032d0e
JG
2390 /* Use SAS+TMF status codes */
2391 switch ((complete_hdr->dw0 & CMPLT_HDR_ABORT_STAT_MSK)
2392 >> CMPLT_HDR_ABORT_STAT_OFF) {
2393 case STAT_IO_ABORTED:
2394 /* this io has been aborted by abort command */
2395 ts->stat = SAS_ABORTED_TASK;
2396 goto out;
2397 case STAT_IO_COMPLETE:
2398 /* internal abort command complete */
c35279f2 2399 ts->stat = TMF_RESP_FUNC_SUCC;
0844a3ff 2400 del_timer(&slot->internal_abort_timer);
df032d0e
JG
2401 goto out;
2402 case STAT_IO_NO_DEVICE:
2403 ts->stat = TMF_RESP_FUNC_COMPLETE;
0844a3ff 2404 del_timer(&slot->internal_abort_timer);
df032d0e
JG
2405 goto out;
2406 case STAT_IO_NOT_VALID:
2407 /* abort single io, controller don't find
2408 * the io need to abort
2409 */
2410 ts->stat = TMF_RESP_FUNC_FAILED;
0844a3ff 2411 del_timer(&slot->internal_abort_timer);
df032d0e
JG
2412 goto out;
2413 default:
2414 break;
2415 }
2416
31a9cfa6
JG
2417 if ((complete_hdr->dw0 & CMPLT_HDR_ERX_MSK) &&
2418 (!(complete_hdr->dw0 & CMPLT_HDR_RSPNS_XFRD_MSK))) {
634a9585
XC
2419 u32 err_phase = (complete_hdr->dw0 & CMPLT_HDR_ERR_PHASE_MSK)
2420 >> CMPLT_HDR_ERR_PHASE_OFF;
081a1608 2421 u32 *error_info = hisi_sas_status_buf_addr_mem(slot);
634a9585
XC
2422
2423 /* Analyse error happens on which phase TX or RX */
2424 if (ERR_ON_TX_PHASE(err_phase))
2425 slot_err_v2_hw(hisi_hba, task, slot, 1);
2426 else if (ERR_ON_RX_PHASE(err_phase))
2427 slot_err_v2_hw(hisi_hba, task, slot, 2);
fc866951 2428
081a1608 2429 if (ts->stat != SAS_DATA_UNDERRUN)
ab2d8bd6 2430 dev_info(dev, "erroneous completion iptt=%d task=%p dev id=%d "
081a1608
XC
2431 "CQ hdr: 0x%x 0x%x 0x%x 0x%x "
2432 "Error info: 0x%x 0x%x 0x%x 0x%x\n",
ab2d8bd6 2433 slot->idx, task, sas_dev->device_id,
081a1608
XC
2434 complete_hdr->dw0, complete_hdr->dw1,
2435 complete_hdr->act, complete_hdr->dw3,
2436 error_info[0], error_info[1],
2437 error_info[2], error_info[3]);
2438
fc866951 2439 if (unlikely(slot->abort))
9c8ee657 2440 return ts->stat;
31a9cfa6
JG
2441 goto out;
2442 }
2443
2444 switch (task->task_proto) {
2445 case SAS_PROTOCOL_SSP:
2446 {
f557e32c
XT
2447 struct hisi_sas_status_buffer *status_buffer =
2448 hisi_sas_status_buf_addr_mem(slot);
2449 struct ssp_response_iu *iu = (struct ssp_response_iu *)
2450 &status_buffer->iu[0];
31a9cfa6
JG
2451
2452 sas_ssp_task_response(dev, task, iu);
2453 break;
2454 }
2455 case SAS_PROTOCOL_SMP:
2456 {
2457 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
2458 void *to;
2459
2460 ts->stat = SAM_STAT_GOOD;
2461 to = kmap_atomic(sg_page(sg_resp));
2462
2463 dma_unmap_sg(dev, &task->smp_task.smp_resp, 1,
2464 DMA_FROM_DEVICE);
2465 dma_unmap_sg(dev, &task->smp_task.smp_req, 1,
2466 DMA_TO_DEVICE);
2467 memcpy(to + sg_resp->offset,
f557e32c 2468 hisi_sas_status_buf_addr_mem(slot) +
31a9cfa6
JG
2469 sizeof(struct hisi_sas_err_record),
2470 sg_dma_len(sg_resp));
2471 kunmap_atomic(to);
2472 break;
2473 }
2474 case SAS_PROTOCOL_SATA:
2475 case SAS_PROTOCOL_STP:
2476 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
6f2ff1a1
JG
2477 {
2478 ts->stat = SAM_STAT_GOOD;
75904077 2479 hisi_sas_sata_done(task, slot);
6f2ff1a1
JG
2480 break;
2481 }
31a9cfa6
JG
2482 default:
2483 ts->stat = SAM_STAT_CHECK_CONDITION;
2484 break;
2485 }
2486
2487 if (!slot->port->port_attached) {
081a1608 2488 dev_warn(dev, "slot complete: port %d has removed\n",
31a9cfa6
JG
2489 slot->port->sas_port.id);
2490 ts->stat = SAS_PHY_DOWN;
2491 }
2492
2493out:
52ed2bba
XC
2494 hisi_sas_slot_task_free(hisi_hba, task, slot);
2495 sts = ts->stat;
54c9dd2d 2496 spin_lock_irqsave(&task->task_state_lock, flags);
52ed2bba
XC
2497 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
2498 spin_unlock_irqrestore(&task->task_state_lock, flags);
2499 dev_info(dev, "slot complete: task(%p) aborted\n", task);
2500 return SAS_ABORTED_TASK;
2501 }
fc866951 2502 task->task_state_flags |= SAS_TASK_STATE_DONE;
54c9dd2d 2503 spin_unlock_irqrestore(&task->task_state_lock, flags);
31a9cfa6 2504
68e6bace
XC
2505 if (!is_internal && (task->task_proto != SAS_PROTOCOL_SMP)) {
2506 spin_lock_irqsave(&device->done_lock, flags);
2507 if (test_bit(SAS_HA_FROZEN, &ha->state)) {
2508 spin_unlock_irqrestore(&device->done_lock, flags);
2509 dev_info(dev, "slot complete: task(%p) ignored\n ",
2510 task);
2511 return sts;
2512 }
2513 spin_unlock_irqrestore(&device->done_lock, flags);
2514 }
2515
31a9cfa6
JG
2516 if (task->task_done)
2517 task->task_done(task);
2518
2519 return sts;
2520}
2521
81d115ec 2522static void prep_ata_v2_hw(struct hisi_hba *hisi_hba,
6f2ff1a1
JG
2523 struct hisi_sas_slot *slot)
2524{
2525 struct sas_task *task = slot->task;
2526 struct domain_device *device = task->dev;
2527 struct domain_device *parent_dev = device->parent;
2528 struct hisi_sas_device *sas_dev = device->lldd_dev;
2529 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
2e244f0f
JG
2530 struct asd_sas_port *sas_port = device->port;
2531 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
6f2ff1a1 2532 u8 *buf_cmd;
81d115ec 2533 int has_data = 0, hdr_tag = 0;
6f2ff1a1
JG
2534 u32 dw1 = 0, dw2 = 0;
2535
2536 /* create header */
2537 /* dw0 */
2538 hdr->dw0 = cpu_to_le32(port->id << CMD_HDR_PORT_OFF);
2539 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type))
2540 hdr->dw0 |= cpu_to_le32(3 << CMD_HDR_CMD_OFF);
2541 else
2542 hdr->dw0 |= cpu_to_le32(4 << CMD_HDR_CMD_OFF);
2543
2544 /* dw1 */
2545 switch (task->data_dir) {
2546 case DMA_TO_DEVICE:
2547 has_data = 1;
2548 dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
2549 break;
2550 case DMA_FROM_DEVICE:
2551 has_data = 1;
2552 dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
2553 break;
2554 default:
2555 dw1 &= ~CMD_HDR_DIR_MSK;
2556 }
2557
7c594f04
XC
2558 if ((task->ata_task.fis.command == ATA_CMD_DEV_RESET) &&
2559 (task->ata_task.fis.control & ATA_SRST))
6f2ff1a1
JG
2560 dw1 |= 1 << CMD_HDR_RESET_OFF;
2561
6c7bb8a1 2562 dw1 |= (hisi_sas_get_ata_protocol(
ba0bb2be 2563 &task->ata_task.fis, task->data_dir))
6f2ff1a1
JG
2564 << CMD_HDR_FRAME_TYPE_OFF;
2565 dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
2566 hdr->dw1 = cpu_to_le32(dw1);
2567
2568 /* dw2 */
318913c6 2569 if (task->ata_task.use_ncq && hisi_sas_get_ncq_tag(task, &hdr_tag)) {
6f2ff1a1
JG
2570 task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
2571 dw2 |= hdr_tag << CMD_HDR_NCQ_TAG_OFF;
2572 }
2573
2574 dw2 |= (HISI_SAS_MAX_STP_RESP_SZ / 4) << CMD_HDR_CFL_OFF |
2575 2 << CMD_HDR_SG_MOD_OFF;
2576 hdr->dw2 = cpu_to_le32(dw2);
2577
2578 /* dw3 */
2579 hdr->transfer_tags = cpu_to_le32(slot->idx);
2580
81d115ec
XC
2581 if (has_data)
2582 prep_prd_sge_v2_hw(hisi_hba, slot, hdr, task->scatter,
6f2ff1a1 2583 slot->n_elem);
6f2ff1a1 2584
6f2ff1a1 2585 hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
f557e32c
XT
2586 hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
2587 hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
6f2ff1a1 2588
f557e32c 2589 buf_cmd = hisi_sas_cmd_hdr_addr_mem(slot);
6f2ff1a1
JG
2590
2591 if (likely(!task->ata_task.device_control_reg_update))
2592 task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
2593 /* fill in command FIS */
2594 memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
6f2ff1a1
JG
2595}
2596
77570eed 2597static void hisi_sas_internal_abort_quirk_timeout(struct timer_list *t)
0844a3ff 2598{
77570eed 2599 struct hisi_sas_slot *slot = from_timer(slot, t, internal_abort_timer);
0844a3ff
JG
2600 struct hisi_sas_port *port = slot->port;
2601 struct asd_sas_port *asd_sas_port;
2602 struct asd_sas_phy *sas_phy;
2603
2604 if (!port)
2605 return;
2606
2607 asd_sas_port = &port->sas_port;
2608
2609 /* Kick the hardware - send break command */
2610 list_for_each_entry(sas_phy, &asd_sas_port->phy_list, port_phy_el) {
2611 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
2612 struct hisi_hba *hisi_hba = phy->hisi_hba;
2613 int phy_no = sas_phy->id;
2614 u32 link_dfx2;
2615
2616 link_dfx2 = hisi_sas_phy_read32(hisi_hba, phy_no, LINK_DFX2);
2617 if ((link_dfx2 == LINK_DFX2_RCVR_HOLD_STS_MSK) ||
2618 (link_dfx2 & LINK_DFX2_SEND_HOLD_STS_MSK)) {
2619 u32 txid_auto;
2620
2621 txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no,
2622 TXID_AUTO);
2623 txid_auto |= TXID_AUTO_CTB_MSK;
2624 hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
2625 txid_auto);
2626 return;
2627 }
2628 }
2629}
2630
81d115ec 2631static void prep_abort_v2_hw(struct hisi_hba *hisi_hba,
a3e665d9
JG
2632 struct hisi_sas_slot *slot,
2633 int device_id, int abort_flag, int tag_to_abort)
2634{
2635 struct sas_task *task = slot->task;
2636 struct domain_device *dev = task->dev;
2637 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
2638 struct hisi_sas_port *port = slot->port;
0844a3ff
JG
2639 struct timer_list *timer = &slot->internal_abort_timer;
2640
2641 /* setup the quirk timer */
77570eed 2642 timer_setup(timer, hisi_sas_internal_abort_quirk_timeout, 0);
0844a3ff
JG
2643 /* Set the timeout to 10ms less than internal abort timeout */
2644 mod_timer(timer, jiffies + msecs_to_jiffies(100));
a3e665d9
JG
2645
2646 /* dw0 */
2647 hdr->dw0 = cpu_to_le32((5 << CMD_HDR_CMD_OFF) | /*abort*/
2648 (port->id << CMD_HDR_PORT_OFF) |
bcbc7f1c 2649 (dev_is_sata(dev) <<
a3e665d9
JG
2650 CMD_HDR_ABORT_DEVICE_TYPE_OFF) |
2651 (abort_flag << CMD_HDR_ABORT_FLAG_OFF));
2652
2653 /* dw1 */
2654 hdr->dw1 = cpu_to_le32(device_id << CMD_HDR_DEV_ID_OFF);
2655
2656 /* dw7 */
2657 hdr->dw7 = cpu_to_le32(tag_to_abort << CMD_HDR_ABORT_IPTT_OFF);
2658 hdr->transfer_tags = cpu_to_le32(slot->idx);
a3e665d9
JG
2659}
2660
7911e66f
JG
2661static int phy_up_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
2662{
981843c6 2663 int i, res = IRQ_HANDLED;
c57eb4e4 2664 u32 port_id, link_rate;
7911e66f
JG
2665 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2666 struct asd_sas_phy *sas_phy = &phy->sas_phy;
11b75249 2667 struct device *dev = hisi_hba->dev;
7911e66f
JG
2668 u32 *frame_rcvd = (u32 *)sas_phy->frame_rcvd;
2669 struct sas_identify_frame *id = (struct sas_identify_frame *)frame_rcvd;
2670
2671 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 1);
2672
4935933e 2673 if (is_sata_phy_v2_hw(hisi_hba, phy_no))
7911e66f
JG
2674 goto end;
2675
2676 if (phy_no == 8) {
2677 u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
2678
2679 port_id = (port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
2680 PORT_STATE_PHY8_PORT_NUM_OFF;
2681 link_rate = (port_state & PORT_STATE_PHY8_CONN_RATE_MSK) >>
2682 PORT_STATE_PHY8_CONN_RATE_OFF;
2683 } else {
2684 port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
2685 port_id = (port_id >> (4 * phy_no)) & 0xf;
2686 link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
2687 link_rate = (link_rate >> (phy_no * 4)) & 0xf;
2688 }
2689
2690 if (port_id == 0xf) {
2691 dev_err(dev, "phyup: phy%d invalid portid\n", phy_no);
2692 res = IRQ_NONE;
2693 goto end;
2694 }
2695
2696 for (i = 0; i < 6; i++) {
2697 u32 idaf = hisi_sas_phy_read32(hisi_hba, phy_no,
2698 RX_IDAF_DWORD0 + (i * 4));
2699 frame_rcvd[i] = __swab32(idaf);
2700 }
2701
7911e66f 2702 sas_phy->linkrate = link_rate;
7911e66f
JG
2703 sas_phy->oob_mode = SAS_OOB_MODE;
2704 memcpy(sas_phy->attached_sas_addr, &id->sas_addr, SAS_ADDR_SIZE);
2705 dev_info(dev, "phyup: phy%d link_rate=%d\n", phy_no, link_rate);
2706 phy->port_id = port_id;
2707 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
2708 phy->phy_type |= PORT_TYPE_SAS;
2709 phy->phy_attached = 1;
2710 phy->identify.device_type = id->dev_type;
2711 phy->frame_rcvd_size = sizeof(struct sas_identify_frame);
2712 if (phy->identify.device_type == SAS_END_DEVICE)
2713 phy->identify.target_port_protocols =
2714 SAS_PROTOCOL_SSP;
f2f89c32 2715 else if (phy->identify.device_type != SAS_PHY_UNUSED) {
7911e66f
JG
2716 phy->identify.target_port_protocols =
2717 SAS_PROTOCOL_SMP;
f2f89c32
XC
2718 if (!timer_pending(&hisi_hba->timer))
2719 set_link_timer_quirk(hisi_hba);
2720 }
320cd6f1 2721 hisi_sas_notify_phy_event(phy, HISI_PHYE_PHY_UP);
7911e66f
JG
2722
2723end:
2724 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
2725 CHL_INT0_SL_PHY_ENABLE_MSK);
2726 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 0);
2727
2728 return res;
2729}
2730
f2f89c32
XC
2731static bool check_any_wideports_v2_hw(struct hisi_hba *hisi_hba)
2732{
2733 u32 port_state;
2734
2735 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
2736 if (port_state & 0x1ff)
2737 return true;
2738
2739 return false;
2740}
2741
5473c060
JG
2742static int phy_down_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
2743{
9c81e2cf 2744 u32 phy_state, sl_ctrl, txid_auto;
f2f89c32
XC
2745 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2746 struct hisi_sas_port *port = phy->port;
081a1608 2747 struct device *dev = hisi_hba->dev;
5473c060
JG
2748
2749 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 1);
2750
5473c060 2751 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
081a1608 2752 dev_info(dev, "phydown: phy%d phy_state=0x%x\n", phy_no, phy_state);
5473c060
JG
2753 hisi_sas_phy_down(hisi_hba, phy_no, (phy_state & 1 << phy_no) ? 1 : 0);
2754
9c81e2cf
JG
2755 sl_ctrl = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
2756 hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL,
2757 sl_ctrl & ~SL_CONTROL_CTA_MSK);
f2f89c32
XC
2758 if (port && !get_wideport_bitmap_v2_hw(hisi_hba, port->id))
2759 if (!check_any_wideports_v2_hw(hisi_hba) &&
2760 timer_pending(&hisi_hba->timer))
2761 del_timer(&hisi_hba->timer);
9c81e2cf
JG
2762
2763 txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
2764 hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
2765 txid_auto | TXID_AUTO_CT3_MSK);
2766
5473c060
JG
2767 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0, CHL_INT0_NOT_RDY_MSK);
2768 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 0);
2769
981843c6 2770 return IRQ_HANDLED;
5473c060
JG
2771}
2772
7911e66f
JG
2773static irqreturn_t int_phy_updown_v2_hw(int irq_no, void *p)
2774{
2775 struct hisi_hba *hisi_hba = p;
2776 u32 irq_msk;
2777 int phy_no = 0;
c16db736 2778 irqreturn_t res = IRQ_NONE;
7911e66f
JG
2779
2780 irq_msk = (hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO)
2781 >> HGC_INVLD_DQE_INFO_FB_CH0_OFF) & 0x1ff;
2782 while (irq_msk) {
2783 if (irq_msk & 1) {
981843c6
XT
2784 u32 reg_value = hisi_sas_phy_read32(hisi_hba, phy_no,
2785 CHL_INT0);
2786
2787 switch (reg_value & (CHL_INT0_NOT_RDY_MSK |
2788 CHL_INT0_SL_PHY_ENABLE_MSK)) {
7911e66f 2789
981843c6 2790 case CHL_INT0_SL_PHY_ENABLE_MSK:
7911e66f 2791 /* phy up */
981843c6 2792 if (phy_up_v2_hw(phy_no, hisi_hba) ==
c16db736
XC
2793 IRQ_HANDLED)
2794 res = IRQ_HANDLED;
981843c6 2795 break;
7911e66f 2796
981843c6 2797 case CHL_INT0_NOT_RDY_MSK:
5473c060 2798 /* phy down */
981843c6 2799 if (phy_down_v2_hw(phy_no, hisi_hba) ==
c16db736
XC
2800 IRQ_HANDLED)
2801 res = IRQ_HANDLED;
981843c6
XT
2802 break;
2803
2804 case (CHL_INT0_NOT_RDY_MSK |
2805 CHL_INT0_SL_PHY_ENABLE_MSK):
2806 reg_value = hisi_sas_read32(hisi_hba,
2807 PHY_STATE);
2808 if (reg_value & BIT(phy_no)) {
2809 /* phy up */
2810 if (phy_up_v2_hw(phy_no, hisi_hba) ==
c16db736
XC
2811 IRQ_HANDLED)
2812 res = IRQ_HANDLED;
981843c6
XT
2813 } else {
2814 /* phy down */
2815 if (phy_down_v2_hw(phy_no, hisi_hba) ==
c16db736
XC
2816 IRQ_HANDLED)
2817 res = IRQ_HANDLED;
5473c060 2818 }
981843c6
XT
2819 break;
2820
2821 default:
2822 break;
2823 }
2824
7911e66f
JG
2825 }
2826 irq_msk >>= 1;
2827 phy_no++;
2828 }
2829
c16db736 2830 return res;
7911e66f
JG
2831}
2832
d3bf3d84
JG
2833static void phy_bcast_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
2834{
2835 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2836 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2837 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
85080a25 2838 u32 bcast_status;
d3bf3d84
JG
2839
2840 hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 1);
85080a25
XC
2841 bcast_status = hisi_sas_phy_read32(hisi_hba, phy_no, RX_PRIMS_STATUS);
2842 if (bcast_status & RX_BCAST_CHG_MSK)
2843 sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
d3bf3d84
JG
2844 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
2845 CHL_INT0_SL_RX_BCST_ACK_MSK);
2846 hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 0);
2847}
2848
9dd1d620
XT
2849static const struct hisi_sas_hw_error port_ecc_axi_error[] = {
2850 {
2851 .irq_msk = BIT(CHL_INT1_DMAC_TX_ECC_ERR_OFF),
2852 .msg = "dmac_tx_ecc_bad_err",
2853 },
2854 {
2855 .irq_msk = BIT(CHL_INT1_DMAC_RX_ECC_ERR_OFF),
2856 .msg = "dmac_rx_ecc_bad_err",
2857 },
2858 {
2859 .irq_msk = BIT(CHL_INT1_DMAC_TX_AXI_WR_ERR_OFF),
2860 .msg = "dma_tx_axi_wr_err",
2861 },
2862 {
2863 .irq_msk = BIT(CHL_INT1_DMAC_TX_AXI_RD_ERR_OFF),
2864 .msg = "dma_tx_axi_rd_err",
2865 },
2866 {
2867 .irq_msk = BIT(CHL_INT1_DMAC_RX_AXI_WR_ERR_OFF),
2868 .msg = "dma_rx_axi_wr_err",
2869 },
2870 {
2871 .irq_msk = BIT(CHL_INT1_DMAC_RX_AXI_RD_ERR_OFF),
2872 .msg = "dma_rx_axi_rd_err",
2873 },
2874};
2875
d3bf3d84
JG
2876static irqreturn_t int_chnl_int_v2_hw(int irq_no, void *p)
2877{
2878 struct hisi_hba *hisi_hba = p;
11b75249 2879 struct device *dev = hisi_hba->dev;
d3bf3d84
JG
2880 u32 ent_msk, ent_tmp, irq_msk;
2881 int phy_no = 0;
2882
2883 ent_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK3);
2884 ent_tmp = ent_msk;
2885 ent_msk |= ENT_INT_SRC_MSK3_ENT95_MSK_MSK;
2886 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, ent_msk);
2887
2888 irq_msk = (hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO) >>
2889 HGC_INVLD_DQE_INFO_FB_CH3_OFF) & 0x1ff;
2890
2891 while (irq_msk) {
af00d159
XT
2892 u32 irq_value0 = hisi_sas_phy_read32(hisi_hba, phy_no,
2893 CHL_INT0);
2894 u32 irq_value1 = hisi_sas_phy_read32(hisi_hba, phy_no,
2895 CHL_INT1);
2896 u32 irq_value2 = hisi_sas_phy_read32(hisi_hba, phy_no,
2897 CHL_INT2);
2898
2899 if ((irq_msk & (1 << phy_no)) && irq_value1) {
9dd1d620
XT
2900 int i;
2901
2902 for (i = 0; i < ARRAY_SIZE(port_ecc_axi_error); i++) {
2903 const struct hisi_sas_hw_error *error =
2904 &port_ecc_axi_error[i];
2905
2906 if (!(irq_value1 & error->irq_msk))
2907 continue;
2908
2909 dev_warn(dev, "%s error (phy%d 0x%x) found!\n",
2910 error->msg, phy_no, irq_value1);
2911 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2912 }
d3bf3d84 2913
af00d159
XT
2914 hisi_sas_phy_write32(hisi_hba, phy_no,
2915 CHL_INT1, irq_value1);
2916 }
d3bf3d84 2917
066312f6
XT
2918 if ((irq_msk & (1 << phy_no)) && irq_value2) {
2919 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2920
2921 if (irq_value2 & BIT(CHL_INT2_SL_IDAF_TOUT_CONF_OFF)) {
2922 dev_warn(dev, "phy%d identify timeout\n",
2923 phy_no);
2924 hisi_sas_notify_phy_event(phy,
2925 HISI_PHYE_LINK_RESET);
2926 }
d3bf3d84 2927
066312f6
XT
2928 hisi_sas_phy_write32(hisi_hba, phy_no,
2929 CHL_INT2, irq_value2);
2930 }
d3bf3d84 2931
af00d159
XT
2932 if ((irq_msk & (1 << phy_no)) && irq_value0) {
2933 if (irq_value0 & CHL_INT0_SL_RX_BCST_ACK_MSK)
2934 phy_bcast_v2_hw(phy_no, hisi_hba);
2935
2936 hisi_sas_phy_write32(hisi_hba, phy_no,
2937 CHL_INT0, irq_value0
2938 & (~CHL_INT0_HOTPLUG_TOUT_MSK)
2939 & (~CHL_INT0_SL_PHY_ENABLE_MSK)
2940 & (~CHL_INT0_NOT_RDY_MSK));
d3bf3d84
JG
2941 }
2942 irq_msk &= ~(1 << phy_no);
2943 phy_no++;
2944 }
2945
2946 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, ent_tmp);
2947
2948 return IRQ_HANDLED;
2949}
2950
d3b688d3
XC
2951static void
2952one_bit_ecc_error_process_v2_hw(struct hisi_hba *hisi_hba, u32 irq_value)
2953{
11b75249 2954 struct device *dev = hisi_hba->dev;
2b383351
JG
2955 const struct hisi_sas_hw_error *ecc_error;
2956 u32 val;
2957 int i;
d3b688d3 2958
2b383351
JG
2959 for (i = 0; i < ARRAY_SIZE(one_bit_ecc_errors); i++) {
2960 ecc_error = &one_bit_ecc_errors[i];
2961 if (irq_value & ecc_error->irq_msk) {
2962 val = hisi_sas_read32(hisi_hba, ecc_error->reg);
2963 val &= ecc_error->msk;
2964 val >>= ecc_error->shift;
2965 dev_warn(dev, ecc_error->msg, val);
2966 }
d3b688d3 2967 }
d3b688d3
XC
2968}
2969
2970static void multi_bit_ecc_error_process_v2_hw(struct hisi_hba *hisi_hba,
2971 u32 irq_value)
2972{
11b75249 2973 struct device *dev = hisi_hba->dev;
2b383351
JG
2974 const struct hisi_sas_hw_error *ecc_error;
2975 u32 val;
2976 int i;
d3b688d3 2977
2b383351
JG
2978 for (i = 0; i < ARRAY_SIZE(multi_bit_ecc_errors); i++) {
2979 ecc_error = &multi_bit_ecc_errors[i];
2980 if (irq_value & ecc_error->irq_msk) {
2981 val = hisi_sas_read32(hisi_hba, ecc_error->reg);
2982 val &= ecc_error->msk;
2983 val >>= ecc_error->shift;
081a1608 2984 dev_err(dev, ecc_error->msg, irq_value, val);
2b383351
JG
2985 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2986 }
d3b688d3
XC
2987 }
2988
e281f42f 2989 return;
d3b688d3
XC
2990}
2991
2992static irqreturn_t fatal_ecc_int_v2_hw(int irq_no, void *p)
2993{
2994 struct hisi_hba *hisi_hba = p;
2995 u32 irq_value, irq_msk;
2996
2997 irq_msk = hisi_sas_read32(hisi_hba, SAS_ECC_INTR_MSK);
2998 hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, irq_msk | 0xffffffff);
2999
3000 irq_value = hisi_sas_read32(hisi_hba, SAS_ECC_INTR);
3001 if (irq_value) {
3002 one_bit_ecc_error_process_v2_hw(hisi_hba, irq_value);
3003 multi_bit_ecc_error_process_v2_hw(hisi_hba, irq_value);
3004 }
3005
3006 hisi_sas_write32(hisi_hba, SAS_ECC_INTR, irq_value);
3007 hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, irq_msk);
3008
3009 return IRQ_HANDLED;
3010}
3011
729428ca
SJ
3012static const struct hisi_sas_hw_error axi_error[] = {
3013 { .msk = BIT(0), .msg = "IOST_AXI_W_ERR" },
3014 { .msk = BIT(1), .msg = "IOST_AXI_R_ERR" },
3015 { .msk = BIT(2), .msg = "ITCT_AXI_W_ERR" },
3016 { .msk = BIT(3), .msg = "ITCT_AXI_R_ERR" },
3017 { .msk = BIT(4), .msg = "SATA_AXI_W_ERR" },
3018 { .msk = BIT(5), .msg = "SATA_AXI_R_ERR" },
3019 { .msk = BIT(6), .msg = "DQE_AXI_R_ERR" },
3020 { .msk = BIT(7), .msg = "CQE_AXI_W_ERR" },
3021 {},
d3b688d3
XC
3022};
3023
729428ca
SJ
3024static const struct hisi_sas_hw_error fifo_error[] = {
3025 { .msk = BIT(8), .msg = "CQE_WINFO_FIFO" },
3026 { .msk = BIT(9), .msg = "CQE_MSG_FIFIO" },
3027 { .msk = BIT(10), .msg = "GETDQE_FIFO" },
3028 { .msk = BIT(11), .msg = "CMDP_FIFO" },
3029 { .msk = BIT(12), .msg = "AWTCTRL_FIFO" },
3030 {},
d3b688d3
XC
3031};
3032
729428ca
SJ
3033static const struct hisi_sas_hw_error fatal_axi_errors[] = {
3034 {
3035 .irq_msk = BIT(ENT_INT_SRC3_WP_DEPTH_OFF),
3036 .msg = "write pointer and depth",
3037 },
3038 {
3039 .irq_msk = BIT(ENT_INT_SRC3_IPTT_SLOT_NOMATCH_OFF),
3040 .msg = "iptt no match slot",
3041 },
3042 {
3043 .irq_msk = BIT(ENT_INT_SRC3_RP_DEPTH_OFF),
3044 .msg = "read pointer and depth",
3045 },
3046 {
3047 .irq_msk = BIT(ENT_INT_SRC3_AXI_OFF),
3048 .reg = HGC_AXI_FIFO_ERR_INFO,
3049 .sub = axi_error,
3050 },
3051 {
3052 .irq_msk = BIT(ENT_INT_SRC3_FIFO_OFF),
3053 .reg = HGC_AXI_FIFO_ERR_INFO,
3054 .sub = fifo_error,
3055 },
3056 {
3057 .irq_msk = BIT(ENT_INT_SRC3_LM_OFF),
3058 .msg = "LM add/fetch list",
3059 },
3060 {
3061 .irq_msk = BIT(ENT_INT_SRC3_ABT_OFF),
3062 .msg = "SAS_HGC_ABT fetch LM list",
3063 },
d3b688d3
XC
3064};
3065
3066static irqreturn_t fatal_axi_int_v2_hw(int irq_no, void *p)
3067{
3068 struct hisi_hba *hisi_hba = p;
3069 u32 irq_value, irq_msk, err_value;
11b75249 3070 struct device *dev = hisi_hba->dev;
729428ca
SJ
3071 const struct hisi_sas_hw_error *axi_error;
3072 int i;
d3b688d3
XC
3073
3074 irq_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK3);
3075 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, irq_msk | 0xfffffffe);
3076
3077 irq_value = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
d3b688d3 3078
729428ca
SJ
3079 for (i = 0; i < ARRAY_SIZE(fatal_axi_errors); i++) {
3080 axi_error = &fatal_axi_errors[i];
3081 if (!(irq_value & axi_error->irq_msk))
3082 continue;
d3b688d3 3083
729428ca
SJ
3084 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
3085 1 << axi_error->shift);
3086 if (axi_error->sub) {
3087 const struct hisi_sas_hw_error *sub = axi_error->sub;
3088
3089 err_value = hisi_sas_read32(hisi_hba, axi_error->reg);
3090 for (; sub->msk || sub->msg; sub++) {
3091 if (!(err_value & sub->msk))
3092 continue;
081a1608 3093 dev_err(dev, "%s (0x%x) found!\n",
729428ca
SJ
3094 sub->msg, irq_value);
3095 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
d3b688d3 3096 }
729428ca 3097 } else {
081a1608 3098 dev_err(dev, "%s (0x%x) found!\n",
729428ca 3099 axi_error->msg, irq_value);
e281f42f 3100 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
d3b688d3 3101 }
729428ca 3102 }
640acc9a 3103
729428ca
SJ
3104 if (irq_value & BIT(ENT_INT_SRC3_ITC_INT_OFF)) {
3105 u32 reg_val = hisi_sas_read32(hisi_hba, ITCT_CLR);
3106 u32 dev_id = reg_val & ITCT_DEV_MSK;
3107 struct hisi_sas_device *sas_dev = &hisi_hba->devices[dev_id];
640acc9a 3108
729428ca
SJ
3109 hisi_sas_write32(hisi_hba, ITCT_CLR, 0);
3110 dev_dbg(dev, "clear ITCT ok\n");
3111 complete(sas_dev->completion);
d3b688d3
XC
3112 }
3113
640acc9a 3114 hisi_sas_write32(hisi_hba, ENT_INT_SRC3, irq_value);
d3b688d3
XC
3115 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, irq_msk);
3116
3117 return IRQ_HANDLED;
3118}
3119
d177c408 3120static void cq_tasklet_v2_hw(unsigned long val)
31a9cfa6 3121{
d177c408 3122 struct hisi_sas_cq *cq = (struct hisi_sas_cq *)val;
31a9cfa6
JG
3123 struct hisi_hba *hisi_hba = cq->hisi_hba;
3124 struct hisi_sas_slot *slot;
3125 struct hisi_sas_itct *itct;
3126 struct hisi_sas_complete_v2_hdr *complete_queue;
d177c408 3127 u32 rd_point = cq->rd_point, wr_point, dev_id;
31a9cfa6
JG
3128 int queue = cq->id;
3129
c7b9d369
XT
3130 if (unlikely(hisi_hba->reject_stp_links_msk))
3131 phys_try_accept_stp_links_v2_hw(hisi_hba);
3132
31a9cfa6 3133 complete_queue = hisi_hba->complete_hdr[queue];
31a9cfa6 3134
31a9cfa6
JG
3135 wr_point = hisi_sas_read32(hisi_hba, COMPL_Q_0_WR_PTR +
3136 (0x14 * queue));
3137
3138 while (rd_point != wr_point) {
3139 struct hisi_sas_complete_v2_hdr *complete_hdr;
3140 int iptt;
3141
3142 complete_hdr = &complete_queue[rd_point];
3143
3144 /* Check for NCQ completion */
3145 if (complete_hdr->act) {
3146 u32 act_tmp = complete_hdr->act;
3147 int ncq_tag_count = ffs(act_tmp);
3148
3149 dev_id = (complete_hdr->dw1 & CMPLT_HDR_DEV_ID_MSK) >>
3150 CMPLT_HDR_DEV_ID_OFF;
3151 itct = &hisi_hba->itct[dev_id];
3152
3153 /* The NCQ tags are held in the itct header */
3154 while (ncq_tag_count) {
3155 __le64 *ncq_tag = &itct->qw4_15[0];
3156
3157 ncq_tag_count -= 1;
3158 iptt = (ncq_tag[ncq_tag_count / 5]
3159 >> (ncq_tag_count % 5) * 12) & 0xfff;
3160
3161 slot = &hisi_hba->slot_info[iptt];
3162 slot->cmplt_queue_slot = rd_point;
3163 slot->cmplt_queue = queue;
405314df 3164 slot_complete_v2_hw(hisi_hba, slot);
31a9cfa6
JG
3165
3166 act_tmp &= ~(1 << ncq_tag_count);
3167 ncq_tag_count = ffs(act_tmp);
3168 }
3169 } else {
3170 iptt = (complete_hdr->dw1) & CMPLT_HDR_IPTT_MSK;
3171 slot = &hisi_hba->slot_info[iptt];
3172 slot->cmplt_queue_slot = rd_point;
3173 slot->cmplt_queue = queue;
405314df 3174 slot_complete_v2_hw(hisi_hba, slot);
31a9cfa6
JG
3175 }
3176
3177 if (++rd_point >= HISI_SAS_QUEUE_SLOTS)
3178 rd_point = 0;
3179 }
3180
3181 /* update rd_point */
e6c346f3 3182 cq->rd_point = rd_point;
31a9cfa6 3183 hisi_sas_write32(hisi_hba, COMPL_Q_0_RD_PTR + (0x14 * queue), rd_point);
d177c408
JG
3184}
3185
3186static irqreturn_t cq_interrupt_v2_hw(int irq_no, void *p)
3187{
3188 struct hisi_sas_cq *cq = p;
3189 struct hisi_hba *hisi_hba = cq->hisi_hba;
3190 int queue = cq->id;
3191
3192 hisi_sas_write32(hisi_hba, OQ_INT_SRC, 1 << queue);
3193
3194 tasklet_schedule(&cq->tasklet);
3195
31a9cfa6
JG
3196 return IRQ_HANDLED;
3197}
3198
d43f9cdb
JG
3199static irqreturn_t sata_int_v2_hw(int irq_no, void *p)
3200{
3201 struct hisi_sas_phy *phy = p;
3202 struct hisi_hba *hisi_hba = phy->hisi_hba;
3203 struct asd_sas_phy *sas_phy = &phy->sas_phy;
11b75249 3204 struct device *dev = hisi_hba->dev;
d43f9cdb
JG
3205 struct hisi_sas_initial_fis *initial_fis;
3206 struct dev_to_host_fis *fis;
3207 u32 ent_tmp, ent_msk, ent_int, port_id, link_rate, hard_phy_linkrate;
3208 irqreturn_t res = IRQ_HANDLED;
3209 u8 attached_sas_addr[SAS_ADDR_SIZE] = {0};
11826e5d 3210 int phy_no, offset;
d43f9cdb
JG
3211
3212 phy_no = sas_phy->id;
3213 initial_fis = &hisi_hba->initial_fis[phy_no];
3214 fis = &initial_fis->fis;
3215
11826e5d
JG
3216 offset = 4 * (phy_no / 4);
3217 ent_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK1 + offset);
3218 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1 + offset,
3219 ent_msk | 1 << ((phy_no % 4) * 8));
d43f9cdb 3220
11826e5d
JG
3221 ent_int = hisi_sas_read32(hisi_hba, ENT_INT_SRC1 + offset);
3222 ent_tmp = ent_int & (1 << (ENT_INT_SRC1_D2H_FIS_CH1_OFF *
3223 (phy_no % 4)));
d43f9cdb
JG
3224 ent_int >>= ENT_INT_SRC1_D2H_FIS_CH1_OFF * (phy_no % 4);
3225 if ((ent_int & ENT_INT_SRC1_D2H_FIS_CH0_MSK) == 0) {
3226 dev_warn(dev, "sata int: phy%d did not receive FIS\n", phy_no);
d43f9cdb
JG
3227 res = IRQ_NONE;
3228 goto end;
04708ff4
XC
3229 }
3230
3231 /* check ERR bit of Status Register */
3232 if (fis->status & ATA_ERR) {
3233 dev_warn(dev, "sata int: phy%d FIS status: 0x%x\n", phy_no,
3234 fis->status);
3235 disable_phy_v2_hw(hisi_hba, phy_no);
3236 enable_phy_v2_hw(hisi_hba, phy_no);
3237 res = IRQ_NONE;
3238 goto end;
d43f9cdb
JG
3239 }
3240
3241 if (unlikely(phy_no == 8)) {
3242 u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
3243
3244 port_id = (port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
3245 PORT_STATE_PHY8_PORT_NUM_OFF;
3246 link_rate = (port_state & PORT_STATE_PHY8_CONN_RATE_MSK) >>
3247 PORT_STATE_PHY8_CONN_RATE_OFF;
3248 } else {
3249 port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
3250 port_id = (port_id >> (4 * phy_no)) & 0xf;
3251 link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
3252 link_rate = (link_rate >> (phy_no * 4)) & 0xf;
3253 }
3254
3255 if (port_id == 0xf) {
3256 dev_err(dev, "sata int: phy%d invalid portid\n", phy_no);
3257 res = IRQ_NONE;
3258 goto end;
3259 }
3260
3261 sas_phy->linkrate = link_rate;
3262 hard_phy_linkrate = hisi_sas_phy_read32(hisi_hba, phy_no,
3263 HARD_PHY_LINKRATE);
3264 phy->maximum_linkrate = hard_phy_linkrate & 0xf;
3265 phy->minimum_linkrate = (hard_phy_linkrate >> 4) & 0xf;
3266
3267 sas_phy->oob_mode = SATA_OOB_MODE;
3268 /* Make up some unique SAS address */
3269 attached_sas_addr[0] = 0x50;
3270 attached_sas_addr[7] = phy_no;
3271 memcpy(sas_phy->attached_sas_addr, attached_sas_addr, SAS_ADDR_SIZE);
3272 memcpy(sas_phy->frame_rcvd, fis, sizeof(struct dev_to_host_fis));
3273 dev_info(dev, "sata int phyup: phy%d link_rate=%d\n", phy_no, link_rate);
3274 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
3275 phy->port_id = port_id;
3276 phy->phy_type |= PORT_TYPE_SATA;
3277 phy->phy_attached = 1;
3278 phy->identify.device_type = SAS_SATA_DEV;
3279 phy->frame_rcvd_size = sizeof(struct dev_to_host_fis);
3280 phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
320cd6f1 3281 hisi_sas_notify_phy_event(phy, HISI_PHYE_PHY_UP);
d43f9cdb
JG
3282
3283end:
11826e5d
JG
3284 hisi_sas_write32(hisi_hba, ENT_INT_SRC1 + offset, ent_tmp);
3285 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1 + offset, ent_msk);
d43f9cdb
JG
3286
3287 return res;
3288}
3289
7911e66f
JG
3290static irq_handler_t phy_interrupts[HISI_SAS_PHY_INT_NR] = {
3291 int_phy_updown_v2_hw,
d3bf3d84 3292 int_chnl_int_v2_hw,
7911e66f
JG
3293};
3294
d3b688d3
XC
3295static irq_handler_t fatal_interrupts[HISI_SAS_FATAL_INT_NR] = {
3296 fatal_ecc_int_v2_hw,
3297 fatal_axi_int_v2_hw
3298};
3299
7911e66f
JG
3300/**
3301 * There is a limitation in the hip06 chipset that we need
3302 * to map in all mbigen interrupts, even if they are not used.
3303 */
3304static int interrupt_init_v2_hw(struct hisi_hba *hisi_hba)
3305{
11b75249 3306 struct platform_device *pdev = hisi_hba->platform_dev;
7911e66f 3307 struct device *dev = &pdev->dev;
8a253888
XC
3308 int irq, rc, irq_map[128];
3309 int i, phy_no, fatal_no, queue_no, k;
7911e66f
JG
3310
3311 for (i = 0; i < 128; i++)
3312 irq_map[i] = platform_get_irq(pdev, i);
3313
3314 for (i = 0; i < HISI_SAS_PHY_INT_NR; i++) {
8a253888 3315 irq = irq_map[i + 1]; /* Phy up/down is irq1 */
7911e66f
JG
3316 rc = devm_request_irq(dev, irq, phy_interrupts[i], 0,
3317 DRV_NAME " phy", hisi_hba);
3318 if (rc) {
3319 dev_err(dev, "irq init: could not request "
3320 "phy interrupt %d, rc=%d\n",
3321 irq, rc);
8a253888
XC
3322 rc = -ENOENT;
3323 goto free_phy_int_irqs;
7911e66f
JG
3324 }
3325 }
3326
8a253888
XC
3327 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
3328 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
d43f9cdb 3329
8a253888 3330 irq = irq_map[phy_no + 72];
d43f9cdb
JG
3331 rc = devm_request_irq(dev, irq, sata_int_v2_hw, 0,
3332 DRV_NAME " sata", phy);
3333 if (rc) {
3334 dev_err(dev, "irq init: could not request "
3335 "sata interrupt %d, rc=%d\n",
3336 irq, rc);
8a253888
XC
3337 rc = -ENOENT;
3338 goto free_sata_int_irqs;
d43f9cdb
JG
3339 }
3340 }
31a9cfa6 3341
8a253888
XC
3342 for (fatal_no = 0; fatal_no < HISI_SAS_FATAL_INT_NR; fatal_no++) {
3343 irq = irq_map[fatal_no + 81];
3344 rc = devm_request_irq(dev, irq, fatal_interrupts[fatal_no], 0,
d3b688d3
XC
3345 DRV_NAME " fatal", hisi_hba);
3346 if (rc) {
3347 dev_err(dev,
3348 "irq init: could not request fatal interrupt %d, rc=%d\n",
3349 irq, rc);
8a253888
XC
3350 rc = -ENOENT;
3351 goto free_fatal_int_irqs;
d3b688d3
XC
3352 }
3353 }
3354
8a253888
XC
3355 for (queue_no = 0; queue_no < hisi_hba->queue_count; queue_no++) {
3356 struct hisi_sas_cq *cq = &hisi_hba->cq[queue_no];
d177c408 3357 struct tasklet_struct *t = &cq->tasklet;
31a9cfa6 3358
8a253888 3359 irq = irq_map[queue_no + 96];
31a9cfa6 3360 rc = devm_request_irq(dev, irq, cq_interrupt_v2_hw, 0,
8a253888 3361 DRV_NAME " cq", cq);
31a9cfa6
JG
3362 if (rc) {
3363 dev_err(dev,
3364 "irq init: could not request cq interrupt %d, rc=%d\n",
3365 irq, rc);
8a253888
XC
3366 rc = -ENOENT;
3367 goto free_cq_int_irqs;
31a9cfa6 3368 }
d177c408 3369 tasklet_init(t, cq_tasklet_v2_hw, (unsigned long)cq);
31a9cfa6
JG
3370 }
3371
7911e66f 3372 return 0;
8a253888
XC
3373
3374free_cq_int_irqs:
3375 for (k = 0; k < queue_no; k++) {
3376 struct hisi_sas_cq *cq = &hisi_hba->cq[k];
3377
3378 free_irq(irq_map[k + 96], cq);
3379 tasklet_kill(&cq->tasklet);
3380 }
3381free_fatal_int_irqs:
3382 for (k = 0; k < fatal_no; k++)
3383 free_irq(irq_map[k + 81], hisi_hba);
3384free_sata_int_irqs:
3385 for (k = 0; k < phy_no; k++) {
3386 struct hisi_sas_phy *phy = &hisi_hba->phy[k];
3387
3388 free_irq(irq_map[k + 72], phy);
3389 }
3390free_phy_int_irqs:
3391 for (k = 0; k < i; k++)
3392 free_irq(irq_map[k + 1], hisi_hba);
3393 return rc;
7911e66f
JG
3394}
3395
94eac9e1
JG
3396static int hisi_sas_v2_init(struct hisi_hba *hisi_hba)
3397{
3398 int rc;
3399
32ccba52
XT
3400 memset(hisi_hba->sata_dev_bitmap, 0, sizeof(hisi_hba->sata_dev_bitmap));
3401
94eac9e1
JG
3402 rc = hw_init_v2_hw(hisi_hba);
3403 if (rc)
3404 return rc;
3405
7911e66f
JG
3406 rc = interrupt_init_v2_hw(hisi_hba);
3407 if (rc)
3408 return rc;
3409
94eac9e1
JG
3410 return 0;
3411}
3412
06ec0fb9
XC
3413static void interrupt_disable_v2_hw(struct hisi_hba *hisi_hba)
3414{
11b75249 3415 struct platform_device *pdev = hisi_hba->platform_dev;
06ec0fb9
XC
3416 int i;
3417
3418 for (i = 0; i < hisi_hba->queue_count; i++)
3419 hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK + 0x4 * i, 0x1);
3420
3421 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0xffffffff);
3422 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0xffffffff);
3423 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0xffffffff);
3424 hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xffffffff);
3425
3426 for (i = 0; i < hisi_hba->n_phy; i++) {
3427 hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xffffffff);
3428 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0xffffffff);
3429 }
3430
3431 for (i = 0; i < 128; i++)
3432 synchronize_irq(platform_get_irq(pdev, i));
3433}
3434
917d3bda
XT
3435
3436static u32 get_phys_state_v2_hw(struct hisi_hba *hisi_hba)
3437{
3438 return hisi_sas_read32(hisi_hba, PHY_STATE);
3439}
3440
06ec0fb9
XC
3441static int soft_reset_v2_hw(struct hisi_hba *hisi_hba)
3442{
11b75249 3443 struct device *dev = hisi_hba->dev;
06ec0fb9 3444 int rc, cnt;
06ec0fb9
XC
3445
3446 interrupt_disable_v2_hw(hisi_hba);
3447 hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0x0);
571295f8 3448 hisi_sas_kill_tasklets(hisi_hba);
06ec0fb9 3449
a25d0d3d 3450 hisi_sas_stop_phys(hisi_hba);
06ec0fb9
XC
3451
3452 mdelay(10);
3453
3454 hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE + AM_CTRL_GLOBAL, 0x1);
3455
3456 /* wait until bus idle */
3457 cnt = 0;
3458 while (1) {
3459 u32 status = hisi_sas_read32_relaxed(hisi_hba,
3460 AXI_MASTER_CFG_BASE + AM_CURR_TRANS_RETURN);
3461
3462 if (status == 0x3)
3463 break;
3464
3465 udelay(10);
3466 if (cnt++ > 10) {
081a1608 3467 dev_err(dev, "wait axi bus state to idle timeout!\n");
06ec0fb9
XC
3468 return -1;
3469 }
3470 }
3471
3472 hisi_sas_init_mem(hisi_hba);
3473
3474 rc = hw_init_v2_hw(hisi_hba);
3475 if (rc)
3476 return rc;
3477
c7b9d369
XT
3478 phys_reject_stp_links_v2_hw(hisi_hba);
3479
06ec0fb9
XC
3480 return 0;
3481}
3482
02615ec8
XT
3483static int write_gpio_v2_hw(struct hisi_hba *hisi_hba, u8 reg_type,
3484 u8 reg_index, u8 reg_count, u8 *write_data)
3485{
3486 struct device *dev = hisi_hba->dev;
3487 int phy_no, count;
3488
3489 if (!hisi_hba->sgpio_regs)
3490 return -EOPNOTSUPP;
3491
3492 switch (reg_type) {
3493 case SAS_GPIO_REG_TX:
3494 count = reg_count * 4;
3495 count = min(count, hisi_hba->n_phy);
3496
3497 for (phy_no = 0; phy_no < count; phy_no++) {
3498 /*
3499 * GPIO_TX[n] register has the highest numbered drive
3500 * of the four in the first byte and the lowest
3501 * numbered drive in the fourth byte.
3502 * See SFF-8485 Rev. 0.7 Table 24.
3503 */
3504 void __iomem *reg_addr = hisi_hba->sgpio_regs +
3505 reg_index * 4 + phy_no;
3506 int data_idx = phy_no + 3 - (phy_no % 4) * 2;
3507
3508 writeb(write_data[data_idx], reg_addr);
3509 }
3510
3511 break;
3512 default:
3513 dev_err(dev, "write gpio: unsupported or bad reg type %d\n",
3514 reg_type);
3515 return -EINVAL;
3516 }
3517
3518 return 0;
3519}
3520
1a7068b3
XT
3521static void wait_cmds_complete_timeout_v2_hw(struct hisi_hba *hisi_hba,
3522 int delay_ms, int timeout_ms)
3523{
3524 struct device *dev = hisi_hba->dev;
3525 int entries, entries_old = 0, time;
3526
3527 for (time = 0; time < timeout_ms; time += delay_ms) {
3528 entries = hisi_sas_read32(hisi_hba, CQE_SEND_CNT);
3529 if (entries == entries_old)
3530 break;
3531
3532 entries_old = entries;
3533 msleep(delay_ms);
3534 }
3535
3536 dev_dbg(dev, "wait commands complete %dms\n", time);
3537}
b1793064
XC
3538
3539static struct scsi_host_template sht_v2_hw = {
3540 .name = DRV_NAME,
3541 .module = THIS_MODULE,
3542 .queuecommand = sas_queuecommand,
3543 .target_alloc = sas_target_alloc,
3544 .slave_configure = hisi_sas_slave_configure,
3545 .scan_finished = hisi_sas_scan_finished,
3546 .scan_start = hisi_sas_scan_start,
3547 .change_queue_depth = sas_change_queue_depth,
3548 .bios_param = sas_bios_param,
3549 .can_queue = 1,
3550 .this_id = -1,
3551 .sg_tablesize = SG_ALL,
3552 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
3553 .use_clustering = ENABLE_CLUSTERING,
3554 .eh_device_reset_handler = sas_eh_device_reset_handler,
3555 .eh_target_reset_handler = sas_eh_target_reset_handler,
3556 .target_destroy = sas_target_destroy,
3557 .ioctl = sas_ioctl,
3558 .shost_attrs = host_attrs,
3559};
3560
3417ba8a 3561static const struct hisi_sas_hw hisi_sas_v2_hw = {
94eac9e1 3562 .hw_init = hisi_sas_v2_init,
85b2c3c0 3563 .setup_itct = setup_itct_v2_hw,
330fa7f3 3564 .slot_index_alloc = slot_index_alloc_quirk_v2_hw,
b2bdaf2b 3565 .alloc_dev = alloc_dev_quirk_v2_hw,
7911e66f 3566 .sl_notify = sl_notify_v2_hw,
5473c060 3567 .get_wideport_bitmap = get_wideport_bitmap_v2_hw,
f39943ee 3568 .clear_itct = clear_itct_v2_hw,
85b2c3c0 3569 .free_device = free_device_v2_hw,
c2d89392 3570 .prep_smp = prep_smp_v2_hw,
8c36e31d 3571 .prep_ssp = prep_ssp_v2_hw,
6f2ff1a1 3572 .prep_stp = prep_ata_v2_hw,
a3e665d9 3573 .prep_abort = prep_abort_v2_hw,
8c36e31d
JG
3574 .get_free_slot = get_free_slot_v2_hw,
3575 .start_delivery = start_delivery_v2_hw,
31a9cfa6 3576 .slot_complete = slot_complete_v2_hw,
396b8044 3577 .phys_init = phys_init_v2_hw,
1eb8eeac 3578 .phy_start = start_phy_v2_hw,
63fb11b8
JG
3579 .phy_disable = disable_phy_v2_hw,
3580 .phy_hard_reset = phy_hard_reset_v2_hw,
c52108c6 3581 .get_events = phy_get_events_v2_hw,
2ae75787
XC
3582 .phy_set_linkrate = phy_set_linkrate_v2_hw,
3583 .phy_get_max_linkrate = phy_get_max_linkrate_v2_hw,
94eac9e1
JG
3584 .max_command_entries = HISI_SAS_COMMAND_ENTRIES_V2_HW,
3585 .complete_hdr_size = sizeof(struct hisi_sas_complete_v2_hdr),
06ec0fb9 3586 .soft_reset = soft_reset_v2_hw,
917d3bda 3587 .get_phys_state = get_phys_state_v2_hw,
02615ec8 3588 .write_gpio = write_gpio_v2_hw,
1a7068b3 3589 .wait_cmds_complete_timeout = wait_cmds_complete_timeout_v2_hw,
b1793064 3590 .sht = &sht_v2_hw,
3417ba8a
JG
3591};
3592
3593static int hisi_sas_v2_probe(struct platform_device *pdev)
3594{
26f3ba96
JG
3595 /*
3596 * Check if we should defer the probe before we probe the
3597 * upper layer, as it's hard to defer later on.
3598 */
3599 int ret = platform_get_irq(pdev, 0);
3600
3601 if (ret < 0) {
3602 if (ret != -EPROBE_DEFER)
3603 dev_err(&pdev->dev, "cannot obtain irq\n");
3604 return ret;
3605 }
3606
3417ba8a
JG
3607 return hisi_sas_probe(pdev, &hisi_sas_v2_hw);
3608}
3609
3610static int hisi_sas_v2_remove(struct platform_device *pdev)
3611{
f2f89c32
XC
3612 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
3613 struct hisi_hba *hisi_hba = sha->lldd_ha;
3614
571295f8 3615 hisi_sas_kill_tasklets(hisi_hba);
8a253888 3616
3417ba8a
JG
3617 return hisi_sas_remove(pdev);
3618}
3619
3620static const struct of_device_id sas_v2_of_match[] = {
3621 { .compatible = "hisilicon,hip06-sas-v2",},
039ae102 3622 { .compatible = "hisilicon,hip07-sas-v2",},
3417ba8a
JG
3623 {},
3624};
3625MODULE_DEVICE_TABLE(of, sas_v2_of_match);
3626
50408712
JG
3627static const struct acpi_device_id sas_v2_acpi_match[] = {
3628 { "HISI0162", 0 },
3629 { }
3630};
3631
3632MODULE_DEVICE_TABLE(acpi, sas_v2_acpi_match);
3633
3417ba8a
JG
3634static struct platform_driver hisi_sas_v2_driver = {
3635 .probe = hisi_sas_v2_probe,
3636 .remove = hisi_sas_v2_remove,
3637 .driver = {
3638 .name = DRV_NAME,
3639 .of_match_table = sas_v2_of_match,
50408712 3640 .acpi_match_table = ACPI_PTR(sas_v2_acpi_match),
3417ba8a
JG
3641 },
3642};
3643
3644module_platform_driver(hisi_sas_v2_driver);
3645
3646MODULE_LICENSE("GPL");
3647MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
3648MODULE_DESCRIPTION("HISILICON SAS controller v2 hw driver");
3649MODULE_ALIAS("platform:" DRV_NAME);