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