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