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,
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 	unsigned long flags;
782 
783 	if (!sata_dev) {
784 		/*
785 		 * STP link SoC bug workaround: index starts from 1.
786 		 * additionally, we can only allocate odd IPTT(1~4095)
787 		 * for SAS/SMP device.
788 		 */
789 		start = 1;
790 		end = hisi_hba->slot_index_count;
791 	} else {
792 		if (sata_idx >= HISI_MAX_SATA_SUPPORT_V2_HW)
793 			return -EINVAL;
794 
795 		/*
796 		 * For SATA device: allocate even IPTT in this interval
797 		 * [64*(sata_idx+1), 64*(sata_idx+2)], then each SATA device
798 		 * own 32 IPTTs. IPTT 0 shall not be used duing to STP link
799 		 * SoC bug workaround. So we ignore the first 32 even IPTTs.
800 		 */
801 		start = 64 * (sata_idx + 1);
802 		end = 64 * (sata_idx + 2);
803 	}
804 
805 	spin_lock_irqsave(&hisi_hba->lock, flags);
806 	while (1) {
807 		start = find_next_zero_bit(bitmap,
808 					hisi_hba->slot_index_count, start);
809 		if (start >= end) {
810 			spin_unlock_irqrestore(&hisi_hba->lock, flags);
811 			return -SAS_QUEUE_FULL;
812 		}
813 		/*
814 		  * SAS IPTT bit0 should be 1, and SATA IPTT bit0 should be 0.
815 		  */
816 		if (sata_dev ^ (start & 1))
817 			break;
818 		start++;
819 	}
820 
821 	set_bit(start, bitmap);
822 	spin_unlock_irqrestore(&hisi_hba->lock, flags);
823 	return start;
824 }
825 
826 static bool sata_index_alloc_v2_hw(struct hisi_hba *hisi_hba, int *idx)
827 {
828 	unsigned int index;
829 	struct device *dev = hisi_hba->dev;
830 	void *bitmap = hisi_hba->sata_dev_bitmap;
831 
832 	index = find_first_zero_bit(bitmap, HISI_MAX_SATA_SUPPORT_V2_HW);
833 	if (index >= HISI_MAX_SATA_SUPPORT_V2_HW) {
834 		dev_warn(dev, "alloc sata index failed, index=%d\n", index);
835 		return false;
836 	}
837 
838 	set_bit(index, bitmap);
839 	*idx = index;
840 	return true;
841 }
842 
843 
844 static struct
845 hisi_sas_device *alloc_dev_quirk_v2_hw(struct domain_device *device)
846 {
847 	struct hisi_hba *hisi_hba = device->port->ha->lldd_ha;
848 	struct hisi_sas_device *sas_dev = NULL;
849 	int i, sata_dev = dev_is_sata(device);
850 	int sata_idx = -1;
851 	unsigned long flags;
852 
853 	spin_lock_irqsave(&hisi_hba->lock, flags);
854 
855 	if (sata_dev)
856 		if (!sata_index_alloc_v2_hw(hisi_hba, &sata_idx))
857 			goto out;
858 
859 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
860 		/*
861 		 * SATA device id bit0 should be 0
862 		 */
863 		if (sata_dev && (i & 1))
864 			continue;
865 		if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
866 			int queue = i % hisi_hba->queue_count;
867 			struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
868 
869 			hisi_hba->devices[i].device_id = i;
870 			sas_dev = &hisi_hba->devices[i];
871 			sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
872 			sas_dev->dev_type = device->dev_type;
873 			sas_dev->hisi_hba = hisi_hba;
874 			sas_dev->sas_device = device;
875 			sas_dev->sata_idx = sata_idx;
876 			sas_dev->dq = dq;
877 			INIT_LIST_HEAD(&hisi_hba->devices[i].list);
878 			break;
879 		}
880 	}
881 
882 out:
883 	spin_unlock_irqrestore(&hisi_hba->lock, flags);
884 
885 	return sas_dev;
886 }
887 
888 static void config_phy_opt_mode_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
889 {
890 	u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
891 
892 	cfg &= ~PHY_CFG_DC_OPT_MSK;
893 	cfg |= 1 << PHY_CFG_DC_OPT_OFF;
894 	hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
895 }
896 
897 static void config_id_frame_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
898 {
899 	struct sas_identify_frame identify_frame;
900 	u32 *identify_buffer;
901 
902 	memset(&identify_frame, 0, sizeof(identify_frame));
903 	identify_frame.dev_type = SAS_END_DEVICE;
904 	identify_frame.frame_type = 0;
905 	identify_frame._un1 = 1;
906 	identify_frame.initiator_bits = SAS_PROTOCOL_ALL;
907 	identify_frame.target_bits = SAS_PROTOCOL_NONE;
908 	memcpy(&identify_frame._un4_11[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
909 	memcpy(&identify_frame.sas_addr[0], hisi_hba->sas_addr,	SAS_ADDR_SIZE);
910 	identify_frame.phy_id = phy_no;
911 	identify_buffer = (u32 *)(&identify_frame);
912 
913 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD0,
914 			__swab32(identify_buffer[0]));
915 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD1,
916 			__swab32(identify_buffer[1]));
917 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD2,
918 			__swab32(identify_buffer[2]));
919 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD3,
920 			__swab32(identify_buffer[3]));
921 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD4,
922 			__swab32(identify_buffer[4]));
923 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD5,
924 			__swab32(identify_buffer[5]));
925 }
926 
927 static void setup_itct_v2_hw(struct hisi_hba *hisi_hba,
928 			     struct hisi_sas_device *sas_dev)
929 {
930 	struct domain_device *device = sas_dev->sas_device;
931 	struct device *dev = hisi_hba->dev;
932 	u64 qw0, device_id = sas_dev->device_id;
933 	struct hisi_sas_itct *itct = &hisi_hba->itct[device_id];
934 	struct domain_device *parent_dev = device->parent;
935 	struct asd_sas_port *sas_port = device->port;
936 	struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
937 	u64 sas_addr;
938 
939 	memset(itct, 0, sizeof(*itct));
940 
941 	/* qw0 */
942 	qw0 = 0;
943 	switch (sas_dev->dev_type) {
944 	case SAS_END_DEVICE:
945 	case SAS_EDGE_EXPANDER_DEVICE:
946 	case SAS_FANOUT_EXPANDER_DEVICE:
947 		qw0 = HISI_SAS_DEV_TYPE_SSP << ITCT_HDR_DEV_TYPE_OFF;
948 		break;
949 	case SAS_SATA_DEV:
950 	case SAS_SATA_PENDING:
951 		if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type))
952 			qw0 = HISI_SAS_DEV_TYPE_STP << ITCT_HDR_DEV_TYPE_OFF;
953 		else
954 			qw0 = HISI_SAS_DEV_TYPE_SATA << ITCT_HDR_DEV_TYPE_OFF;
955 		break;
956 	default:
957 		dev_warn(dev, "setup itct: unsupported dev type (%d)\n",
958 			 sas_dev->dev_type);
959 	}
960 
961 	qw0 |= ((1 << ITCT_HDR_VALID_OFF) |
962 		(device->linkrate << ITCT_HDR_MCR_OFF) |
963 		(1 << ITCT_HDR_VLN_OFF) |
964 		(ITCT_HDR_SMP_TIMEOUT << ITCT_HDR_SMP_TIMEOUT_OFF) |
965 		(1 << ITCT_HDR_AWT_CONTINUE_OFF) |
966 		(port->id << ITCT_HDR_PORT_ID_OFF));
967 	itct->qw0 = cpu_to_le64(qw0);
968 
969 	/* qw1 */
970 	memcpy(&sas_addr, device->sas_addr, SAS_ADDR_SIZE);
971 	itct->sas_addr = cpu_to_le64(__swab64(sas_addr));
972 
973 	/* qw2 */
974 	if (!dev_is_sata(device))
975 		itct->qw2 = cpu_to_le64((5000ULL << ITCT_HDR_INLT_OFF) |
976 					(0x1ULL << ITCT_HDR_BITLT_OFF) |
977 					(0x32ULL << ITCT_HDR_MCTLT_OFF) |
978 					(0x1ULL << ITCT_HDR_RTOLT_OFF));
979 }
980 
981 static void clear_itct_v2_hw(struct hisi_hba *hisi_hba,
982 			      struct hisi_sas_device *sas_dev)
983 {
984 	DECLARE_COMPLETION_ONSTACK(completion);
985 	u64 dev_id = sas_dev->device_id;
986 	struct hisi_sas_itct *itct = &hisi_hba->itct[dev_id];
987 	u32 reg_val = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
988 	int i;
989 
990 	sas_dev->completion = &completion;
991 
992 	/* clear the itct interrupt state */
993 	if (ENT_INT_SRC3_ITC_INT_MSK & reg_val)
994 		hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
995 				 ENT_INT_SRC3_ITC_INT_MSK);
996 
997 	for (i = 0; i < 2; i++) {
998 		reg_val = ITCT_CLR_EN_MSK | (dev_id & ITCT_DEV_MSK);
999 		hisi_sas_write32(hisi_hba, ITCT_CLR, reg_val);
1000 		wait_for_completion(sas_dev->completion);
1001 
1002 		memset(itct, 0, sizeof(struct hisi_sas_itct));
1003 	}
1004 }
1005 
1006 static void free_device_v2_hw(struct hisi_sas_device *sas_dev)
1007 {
1008 	struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1009 
1010 	/* SoC bug workaround */
1011 	if (dev_is_sata(sas_dev->sas_device))
1012 		clear_bit(sas_dev->sata_idx, hisi_hba->sata_dev_bitmap);
1013 }
1014 
1015 static int reset_hw_v2_hw(struct hisi_hba *hisi_hba)
1016 {
1017 	int i, reset_val;
1018 	u32 val;
1019 	unsigned long end_time;
1020 	struct device *dev = hisi_hba->dev;
1021 
1022 	/* The mask needs to be set depending on the number of phys */
1023 	if (hisi_hba->n_phy == 9)
1024 		reset_val = 0x1fffff;
1025 	else
1026 		reset_val = 0x7ffff;
1027 
1028 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0);
1029 
1030 	/* Disable all of the PHYs */
1031 	for (i = 0; i < hisi_hba->n_phy; i++) {
1032 		u32 phy_cfg = hisi_sas_phy_read32(hisi_hba, i, PHY_CFG);
1033 
1034 		phy_cfg &= ~PHY_CTRL_RESET_MSK;
1035 		hisi_sas_phy_write32(hisi_hba, i, PHY_CFG, phy_cfg);
1036 	}
1037 	udelay(50);
1038 
1039 	/* Ensure DMA tx & rx idle */
1040 	for (i = 0; i < hisi_hba->n_phy; i++) {
1041 		u32 dma_tx_status, dma_rx_status;
1042 
1043 		end_time = jiffies + msecs_to_jiffies(1000);
1044 
1045 		while (1) {
1046 			dma_tx_status = hisi_sas_phy_read32(hisi_hba, i,
1047 							    DMA_TX_STATUS);
1048 			dma_rx_status = hisi_sas_phy_read32(hisi_hba, i,
1049 							    DMA_RX_STATUS);
1050 
1051 			if (!(dma_tx_status & DMA_TX_STATUS_BUSY_MSK) &&
1052 				!(dma_rx_status & DMA_RX_STATUS_BUSY_MSK))
1053 				break;
1054 
1055 			msleep(20);
1056 			if (time_after(jiffies, end_time))
1057 				return -EIO;
1058 		}
1059 	}
1060 
1061 	/* Ensure axi bus idle */
1062 	end_time = jiffies + msecs_to_jiffies(1000);
1063 	while (1) {
1064 		u32 axi_status =
1065 			hisi_sas_read32(hisi_hba, AXI_CFG);
1066 
1067 		if (axi_status == 0)
1068 			break;
1069 
1070 		msleep(20);
1071 		if (time_after(jiffies, end_time))
1072 			return -EIO;
1073 	}
1074 
1075 	if (ACPI_HANDLE(dev)) {
1076 		acpi_status s;
1077 
1078 		s = acpi_evaluate_object(ACPI_HANDLE(dev), "_RST", NULL, NULL);
1079 		if (ACPI_FAILURE(s)) {
1080 			dev_err(dev, "Reset failed\n");
1081 			return -EIO;
1082 		}
1083 	} else if (hisi_hba->ctrl) {
1084 		/* reset and disable clock*/
1085 		regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg,
1086 				reset_val);
1087 		regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg + 4,
1088 				reset_val);
1089 		msleep(1);
1090 		regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg, &val);
1091 		if (reset_val != (val & reset_val)) {
1092 			dev_err(dev, "SAS reset fail.\n");
1093 			return -EIO;
1094 		}
1095 
1096 		/* De-reset and enable clock*/
1097 		regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg + 4,
1098 				reset_val);
1099 		regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg,
1100 				reset_val);
1101 		msleep(1);
1102 		regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg,
1103 				&val);
1104 		if (val & reset_val) {
1105 			dev_err(dev, "SAS de-reset fail.\n");
1106 			return -EIO;
1107 		}
1108 	} else {
1109 		dev_err(dev, "no reset method\n");
1110 		return -EINVAL;
1111 	}
1112 
1113 	return 0;
1114 }
1115 
1116 /* This function needs to be called after resetting SAS controller. */
1117 static void phys_reject_stp_links_v2_hw(struct hisi_hba *hisi_hba)
1118 {
1119 	u32 cfg;
1120 	int phy_no;
1121 
1122 	hisi_hba->reject_stp_links_msk = (1 << hisi_hba->n_phy) - 1;
1123 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1124 		cfg = hisi_sas_phy_read32(hisi_hba, phy_no, CON_CONTROL);
1125 		if (!(cfg & CON_CONTROL_CFG_OPEN_ACC_STP_MSK))
1126 			continue;
1127 
1128 		cfg &= ~CON_CONTROL_CFG_OPEN_ACC_STP_MSK;
1129 		hisi_sas_phy_write32(hisi_hba, phy_no, CON_CONTROL, cfg);
1130 	}
1131 }
1132 
1133 static void phys_try_accept_stp_links_v2_hw(struct hisi_hba *hisi_hba)
1134 {
1135 	int phy_no;
1136 	u32 dma_tx_dfx1;
1137 
1138 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1139 		if (!(hisi_hba->reject_stp_links_msk & BIT(phy_no)))
1140 			continue;
1141 
1142 		dma_tx_dfx1 = hisi_sas_phy_read32(hisi_hba, phy_no,
1143 						DMA_TX_DFX1);
1144 		if (dma_tx_dfx1 & DMA_TX_DFX1_IPTT_MSK) {
1145 			u32 cfg = hisi_sas_phy_read32(hisi_hba,
1146 				phy_no, CON_CONTROL);
1147 
1148 			cfg |= CON_CONTROL_CFG_OPEN_ACC_STP_MSK;
1149 			hisi_sas_phy_write32(hisi_hba, phy_no,
1150 				CON_CONTROL, cfg);
1151 			clear_bit(phy_no, &hisi_hba->reject_stp_links_msk);
1152 		}
1153 	}
1154 }
1155 
1156 static const struct signal_attenuation_s x6000 = {9200, 0, 10476};
1157 static const struct sig_atten_lu_s sig_atten_lu[] = {
1158 	{ &x6000, 0x3016a68 },
1159 };
1160 
1161 static void init_reg_v2_hw(struct hisi_hba *hisi_hba)
1162 {
1163 	struct device *dev = hisi_hba->dev;
1164 	u32 sas_phy_ctrl = 0x30b9908;
1165 	u32 signal[3];
1166 	int i;
1167 
1168 	/* Global registers init */
1169 
1170 	/* Deal with am-max-transmissions quirk */
1171 	if (device_property_present(dev, "hip06-sas-v2-quirk-amt")) {
1172 		hisi_sas_write32(hisi_hba, AM_CFG_MAX_TRANS, 0x2020);
1173 		hisi_sas_write32(hisi_hba, AM_CFG_SINGLE_PORT_MAX_TRANS,
1174 				 0x2020);
1175 	} /* Else, use defaults -> do nothing */
1176 
1177 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE,
1178 			 (u32)((1ULL << hisi_hba->queue_count) - 1));
1179 	hisi_sas_write32(hisi_hba, AXI_USER1, 0xc0000000);
1180 	hisi_sas_write32(hisi_hba, AXI_USER2, 0x10000);
1181 	hisi_sas_write32(hisi_hba, HGC_SAS_TXFAIL_RETRY_CTRL, 0x0);
1182 	hisi_sas_write32(hisi_hba, HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL, 0x7FF);
1183 	hisi_sas_write32(hisi_hba, OPENA_WT_CONTI_TIME, 0x1);
1184 	hisi_sas_write32(hisi_hba, I_T_NEXUS_LOSS_TIME, 0x1F4);
1185 	hisi_sas_write32(hisi_hba, MAX_CON_TIME_LIMIT_TIME, 0x32);
1186 	hisi_sas_write32(hisi_hba, BUS_INACTIVE_LIMIT_TIME, 0x1);
1187 	hisi_sas_write32(hisi_hba, CFG_AGING_TIME, 0x1);
1188 	hisi_sas_write32(hisi_hba, HGC_ERR_STAT_EN, 0x1);
1189 	hisi_sas_write32(hisi_hba, HGC_GET_ITV_TIME, 0x1);
1190 	hisi_sas_write32(hisi_hba, INT_COAL_EN, 0xc);
1191 	hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x60);
1192 	hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 0x3);
1193 	hisi_sas_write32(hisi_hba, ENT_INT_COAL_TIME, 0x1);
1194 	hisi_sas_write32(hisi_hba, ENT_INT_COAL_CNT, 0x1);
1195 	hisi_sas_write32(hisi_hba, OQ_INT_SRC, 0x0);
1196 	hisi_sas_write32(hisi_hba, ENT_INT_SRC1, 0xffffffff);
1197 	hisi_sas_write32(hisi_hba, ENT_INT_SRC2, 0xffffffff);
1198 	hisi_sas_write32(hisi_hba, ENT_INT_SRC3, 0xffffffff);
1199 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0x7efefefe);
1200 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0x7efefefe);
1201 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0x7ffe20fe);
1202 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xfff00c30);
1203 	for (i = 0; i < hisi_hba->queue_count; i++)
1204 		hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK+0x4*i, 0);
1205 
1206 	hisi_sas_write32(hisi_hba, AXI_AHB_CLK_CFG, 1);
1207 	hisi_sas_write32(hisi_hba, HYPER_STREAM_ID_EN_CFG, 1);
1208 
1209 	/* Get sas_phy_ctrl value to deal with TX FFE issue. */
1210 	if (!device_property_read_u32_array(dev, "hisilicon,signal-attenuation",
1211 					    signal, ARRAY_SIZE(signal))) {
1212 		for (i = 0; i < ARRAY_SIZE(sig_atten_lu); i++) {
1213 			const struct sig_atten_lu_s *lookup = &sig_atten_lu[i];
1214 			const struct signal_attenuation_s *att = lookup->att;
1215 
1216 			if ((signal[0] == att->de_emphasis) &&
1217 			    (signal[1] == att->preshoot) &&
1218 			    (signal[2] == att->boost)) {
1219 				sas_phy_ctrl = lookup->sas_phy_ctrl;
1220 				break;
1221 			}
1222 		}
1223 
1224 		if (i == ARRAY_SIZE(sig_atten_lu))
1225 			dev_warn(dev, "unknown signal attenuation values, using default PHY ctrl config\n");
1226 	}
1227 
1228 	for (i = 0; i < hisi_hba->n_phy; i++) {
1229 		struct hisi_sas_phy *phy = &hisi_hba->phy[i];
1230 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
1231 		u32 prog_phy_link_rate = 0x800;
1232 
1233 		if (!sas_phy->phy || (sas_phy->phy->maximum_linkrate <
1234 				SAS_LINK_RATE_1_5_GBPS)) {
1235 			prog_phy_link_rate = 0x855;
1236 		} else {
1237 			enum sas_linkrate max = sas_phy->phy->maximum_linkrate;
1238 
1239 			prog_phy_link_rate =
1240 				hisi_sas_get_prog_phy_linkrate_mask(max) |
1241 				0x800;
1242 		}
1243 		hisi_sas_phy_write32(hisi_hba, i, PROG_PHY_LINK_RATE,
1244 			prog_phy_link_rate);
1245 		hisi_sas_phy_write32(hisi_hba, i, SAS_PHY_CTRL, sas_phy_ctrl);
1246 		hisi_sas_phy_write32(hisi_hba, i, SL_TOUT_CFG, 0x7d7d7d7d);
1247 		hisi_sas_phy_write32(hisi_hba, i, SL_CONTROL, 0x0);
1248 		hisi_sas_phy_write32(hisi_hba, i, TXID_AUTO, 0x2);
1249 		hisi_sas_phy_write32(hisi_hba, i, DONE_RECEIVED_TIME, 0x8);
1250 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT0, 0xffffffff);
1251 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT1, 0xffffffff);
1252 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT2, 0xfff87fff);
1253 		hisi_sas_phy_write32(hisi_hba, i, RXOP_CHECK_CFG_H, 0x1000);
1254 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xff857fff);
1255 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0x8ffffbfe);
1256 		hisi_sas_phy_write32(hisi_hba, i, SL_CFG, 0x13f801fc);
1257 		hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL_RDY_MSK, 0x0);
1258 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_NOT_RDY_MSK, 0x0);
1259 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_DWS_RESET_MSK, 0x0);
1260 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_PHY_ENA_MSK, 0x0);
1261 		hisi_sas_phy_write32(hisi_hba, i, SL_RX_BCAST_CHK_MSK, 0x0);
1262 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT_COAL_EN, 0x0);
1263 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_OOB_RESTART_MSK, 0x0);
1264 		if (hisi_hba->refclk_frequency_mhz == 66)
1265 			hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL, 0x199B694);
1266 		/* else, do nothing -> leave it how you found it */
1267 	}
1268 
1269 	for (i = 0; i < hisi_hba->queue_count; i++) {
1270 		/* Delivery queue */
1271 		hisi_sas_write32(hisi_hba,
1272 				 DLVRY_Q_0_BASE_ADDR_HI + (i * 0x14),
1273 				 upper_32_bits(hisi_hba->cmd_hdr_dma[i]));
1274 
1275 		hisi_sas_write32(hisi_hba, DLVRY_Q_0_BASE_ADDR_LO + (i * 0x14),
1276 				 lower_32_bits(hisi_hba->cmd_hdr_dma[i]));
1277 
1278 		hisi_sas_write32(hisi_hba, DLVRY_Q_0_DEPTH + (i * 0x14),
1279 				 HISI_SAS_QUEUE_SLOTS);
1280 
1281 		/* Completion queue */
1282 		hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_HI + (i * 0x14),
1283 				 upper_32_bits(hisi_hba->complete_hdr_dma[i]));
1284 
1285 		hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_LO + (i * 0x14),
1286 				 lower_32_bits(hisi_hba->complete_hdr_dma[i]));
1287 
1288 		hisi_sas_write32(hisi_hba, COMPL_Q_0_DEPTH + (i * 0x14),
1289 				 HISI_SAS_QUEUE_SLOTS);
1290 	}
1291 
1292 	/* itct */
1293 	hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_LO,
1294 			 lower_32_bits(hisi_hba->itct_dma));
1295 
1296 	hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_HI,
1297 			 upper_32_bits(hisi_hba->itct_dma));
1298 
1299 	/* iost */
1300 	hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_LO,
1301 			 lower_32_bits(hisi_hba->iost_dma));
1302 
1303 	hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_HI,
1304 			 upper_32_bits(hisi_hba->iost_dma));
1305 
1306 	/* breakpoint */
1307 	hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_LO,
1308 			 lower_32_bits(hisi_hba->breakpoint_dma));
1309 
1310 	hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_HI,
1311 			 upper_32_bits(hisi_hba->breakpoint_dma));
1312 
1313 	/* SATA broken msg */
1314 	hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_LO,
1315 			 lower_32_bits(hisi_hba->sata_breakpoint_dma));
1316 
1317 	hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_HI,
1318 			 upper_32_bits(hisi_hba->sata_breakpoint_dma));
1319 
1320 	/* SATA initial fis */
1321 	hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_LO,
1322 			 lower_32_bits(hisi_hba->initial_fis_dma));
1323 
1324 	hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_HI,
1325 			 upper_32_bits(hisi_hba->initial_fis_dma));
1326 }
1327 
1328 static void link_timeout_enable_link(struct timer_list *t)
1329 {
1330 	struct hisi_hba *hisi_hba = from_timer(hisi_hba, t, timer);
1331 	int i, reg_val;
1332 
1333 	for (i = 0; i < hisi_hba->n_phy; i++) {
1334 		if (hisi_hba->reject_stp_links_msk & BIT(i))
1335 			continue;
1336 
1337 		reg_val = hisi_sas_phy_read32(hisi_hba, i, CON_CONTROL);
1338 		if (!(reg_val & BIT(0))) {
1339 			hisi_sas_phy_write32(hisi_hba, i,
1340 					CON_CONTROL, 0x7);
1341 			break;
1342 		}
1343 	}
1344 
1345 	hisi_hba->timer.function = link_timeout_disable_link;
1346 	mod_timer(&hisi_hba->timer, jiffies + msecs_to_jiffies(900));
1347 }
1348 
1349 static void link_timeout_disable_link(struct timer_list *t)
1350 {
1351 	struct hisi_hba *hisi_hba = from_timer(hisi_hba, t, timer);
1352 	int i, reg_val;
1353 
1354 	reg_val = hisi_sas_read32(hisi_hba, PHY_STATE);
1355 	for (i = 0; i < hisi_hba->n_phy && reg_val; i++) {
1356 		if (hisi_hba->reject_stp_links_msk & BIT(i))
1357 			continue;
1358 
1359 		if (reg_val & BIT(i)) {
1360 			hisi_sas_phy_write32(hisi_hba, i,
1361 					CON_CONTROL, 0x6);
1362 			break;
1363 		}
1364 	}
1365 
1366 	hisi_hba->timer.function = link_timeout_enable_link;
1367 	mod_timer(&hisi_hba->timer, jiffies + msecs_to_jiffies(100));
1368 }
1369 
1370 static void set_link_timer_quirk(struct hisi_hba *hisi_hba)
1371 {
1372 	hisi_hba->timer.function = link_timeout_disable_link;
1373 	hisi_hba->timer.expires = jiffies + msecs_to_jiffies(1000);
1374 	add_timer(&hisi_hba->timer);
1375 }
1376 
1377 static int hw_init_v2_hw(struct hisi_hba *hisi_hba)
1378 {
1379 	struct device *dev = hisi_hba->dev;
1380 	int rc;
1381 
1382 	rc = reset_hw_v2_hw(hisi_hba);
1383 	if (rc) {
1384 		dev_err(dev, "hisi_sas_reset_hw failed, rc=%d", rc);
1385 		return rc;
1386 	}
1387 
1388 	msleep(100);
1389 	init_reg_v2_hw(hisi_hba);
1390 
1391 	return 0;
1392 }
1393 
1394 static void enable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1395 {
1396 	u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
1397 
1398 	cfg |= PHY_CFG_ENA_MSK;
1399 	hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
1400 }
1401 
1402 static bool is_sata_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1403 {
1404 	u32 context;
1405 
1406 	context = hisi_sas_read32(hisi_hba, PHY_CONTEXT);
1407 	if (context & (1 << phy_no))
1408 		return true;
1409 
1410 	return false;
1411 }
1412 
1413 static bool tx_fifo_is_empty_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1414 {
1415 	u32 dfx_val;
1416 
1417 	dfx_val = hisi_sas_phy_read32(hisi_hba, phy_no, DMA_TX_DFX1);
1418 
1419 	if (dfx_val & BIT(16))
1420 		return false;
1421 
1422 	return true;
1423 }
1424 
1425 static bool axi_bus_is_idle_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1426 {
1427 	int i, max_loop = 1000;
1428 	struct device *dev = hisi_hba->dev;
1429 	u32 status, axi_status, dfx_val, dfx_tx_val;
1430 
1431 	for (i = 0; i < max_loop; i++) {
1432 		status = hisi_sas_read32_relaxed(hisi_hba,
1433 			AXI_MASTER_CFG_BASE + AM_CURR_TRANS_RETURN);
1434 
1435 		axi_status = hisi_sas_read32(hisi_hba, AXI_CFG);
1436 		dfx_val = hisi_sas_phy_read32(hisi_hba, phy_no, DMA_TX_DFX1);
1437 		dfx_tx_val = hisi_sas_phy_read32(hisi_hba,
1438 			phy_no, DMA_TX_FIFO_DFX0);
1439 
1440 		if ((status == 0x3) && (axi_status == 0x0) &&
1441 		    (dfx_val & BIT(20)) && (dfx_tx_val & BIT(10)))
1442 			return true;
1443 		udelay(10);
1444 	}
1445 	dev_err(dev, "bus is not idle phy%d, axi150:0x%x axi100:0x%x port204:0x%x port240:0x%x\n",
1446 			phy_no, status, axi_status,
1447 			dfx_val, dfx_tx_val);
1448 	return false;
1449 }
1450 
1451 static bool wait_io_done_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1452 {
1453 	int i, max_loop = 1000;
1454 	struct device *dev = hisi_hba->dev;
1455 	u32 status, tx_dfx0;
1456 
1457 	for (i = 0; i < max_loop; i++) {
1458 		status = hisi_sas_phy_read32(hisi_hba, phy_no, LINK_DFX2);
1459 		status = (status & 0x3fc0) >> 6;
1460 
1461 		if (status != 0x1)
1462 			return true;
1463 
1464 		tx_dfx0 = hisi_sas_phy_read32(hisi_hba, phy_no, DMA_TX_DFX0);
1465 		if ((tx_dfx0 & 0x1ff) == 0x2)
1466 			return true;
1467 		udelay(10);
1468 	}
1469 	dev_err(dev, "IO not done phy%d, port264:0x%x port200:0x%x\n",
1470 			phy_no, status, tx_dfx0);
1471 	return false;
1472 }
1473 
1474 static bool allowed_disable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1475 {
1476 	if (tx_fifo_is_empty_v2_hw(hisi_hba, phy_no))
1477 		return true;
1478 
1479 	if (!axi_bus_is_idle_v2_hw(hisi_hba, phy_no))
1480 		return false;
1481 
1482 	if (!wait_io_done_v2_hw(hisi_hba, phy_no))
1483 		return false;
1484 
1485 	return true;
1486 }
1487 
1488 
1489 static void disable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1490 {
1491 	u32 cfg, axi_val, dfx0_val, txid_auto;
1492 	struct device *dev = hisi_hba->dev;
1493 
1494 	/* Close axi bus. */
1495 	axi_val = hisi_sas_read32(hisi_hba, AXI_MASTER_CFG_BASE +
1496 				AM_CTRL_GLOBAL);
1497 	axi_val |= 0x1;
1498 	hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE +
1499 		AM_CTRL_GLOBAL, axi_val);
1500 
1501 	if (is_sata_phy_v2_hw(hisi_hba, phy_no)) {
1502 		if (allowed_disable_phy_v2_hw(hisi_hba, phy_no))
1503 			goto do_disable;
1504 
1505 		/* Reset host controller. */
1506 		queue_work(hisi_hba->wq, &hisi_hba->rst_work);
1507 		return;
1508 	}
1509 
1510 	dfx0_val = hisi_sas_phy_read32(hisi_hba, phy_no, PORT_DFX0);
1511 	dfx0_val = (dfx0_val & 0x1fc0) >> 6;
1512 	if (dfx0_val != 0x4)
1513 		goto do_disable;
1514 
1515 	if (!tx_fifo_is_empty_v2_hw(hisi_hba, phy_no)) {
1516 		dev_warn(dev, "phy%d, wait tx fifo need send break\n",
1517 			phy_no);
1518 		txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no,
1519 					TXID_AUTO);
1520 		txid_auto |= TXID_AUTO_CTB_MSK;
1521 		hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
1522 					txid_auto);
1523 	}
1524 
1525 do_disable:
1526 	cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
1527 	cfg &= ~PHY_CFG_ENA_MSK;
1528 	hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
1529 
1530 	/* Open axi bus. */
1531 	axi_val &= ~0x1;
1532 	hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE +
1533 		AM_CTRL_GLOBAL, axi_val);
1534 }
1535 
1536 static void start_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1537 {
1538 	config_id_frame_v2_hw(hisi_hba, phy_no);
1539 	config_phy_opt_mode_v2_hw(hisi_hba, phy_no);
1540 	enable_phy_v2_hw(hisi_hba, phy_no);
1541 }
1542 
1543 static void phy_hard_reset_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1544 {
1545 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1546 	u32 txid_auto;
1547 
1548 	disable_phy_v2_hw(hisi_hba, phy_no);
1549 	if (phy->identify.device_type == SAS_END_DEVICE) {
1550 		txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
1551 		hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
1552 					txid_auto | TX_HARDRST_MSK);
1553 	}
1554 	msleep(100);
1555 	start_phy_v2_hw(hisi_hba, phy_no);
1556 }
1557 
1558 static void phy_get_events_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1559 {
1560 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1561 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1562 	struct sas_phy *sphy = sas_phy->phy;
1563 	u32 err4_reg_val, err6_reg_val;
1564 
1565 	/* loss dword syn, phy reset problem */
1566 	err4_reg_val = hisi_sas_phy_read32(hisi_hba, phy_no, SAS_ERR_CNT4_REG);
1567 
1568 	/* disparity err, invalid dword */
1569 	err6_reg_val = hisi_sas_phy_read32(hisi_hba, phy_no, SAS_ERR_CNT6_REG);
1570 
1571 	sphy->loss_of_dword_sync_count += (err4_reg_val >> 16) & 0xFFFF;
1572 	sphy->phy_reset_problem_count += err4_reg_val & 0xFFFF;
1573 	sphy->invalid_dword_count += (err6_reg_val & 0xFF0000) >> 16;
1574 	sphy->running_disparity_error_count += err6_reg_val & 0xFF;
1575 }
1576 
1577 static void phys_init_v2_hw(struct hisi_hba *hisi_hba)
1578 {
1579 	int i;
1580 
1581 	for (i = 0; i < hisi_hba->n_phy; i++) {
1582 		struct hisi_sas_phy *phy = &hisi_hba->phy[i];
1583 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
1584 
1585 		if (!sas_phy->phy->enabled)
1586 			continue;
1587 
1588 		start_phy_v2_hw(hisi_hba, i);
1589 	}
1590 }
1591 
1592 static void sl_notify_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1593 {
1594 	u32 sl_control;
1595 
1596 	sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1597 	sl_control |= SL_CONTROL_NOTIFY_EN_MSK;
1598 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
1599 	msleep(1);
1600 	sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1601 	sl_control &= ~SL_CONTROL_NOTIFY_EN_MSK;
1602 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
1603 }
1604 
1605 static enum sas_linkrate phy_get_max_linkrate_v2_hw(void)
1606 {
1607 	return SAS_LINK_RATE_12_0_GBPS;
1608 }
1609 
1610 static void phy_set_linkrate_v2_hw(struct hisi_hba *hisi_hba, int phy_no,
1611 		struct sas_phy_linkrates *r)
1612 {
1613 	enum sas_linkrate max = r->maximum_linkrate;
1614 	u32 prog_phy_link_rate = 0x800;
1615 
1616 	prog_phy_link_rate |= hisi_sas_get_prog_phy_linkrate_mask(max);
1617 	hisi_sas_phy_write32(hisi_hba, phy_no, PROG_PHY_LINK_RATE,
1618 			     prog_phy_link_rate);
1619 }
1620 
1621 static int get_wideport_bitmap_v2_hw(struct hisi_hba *hisi_hba, int port_id)
1622 {
1623 	int i, bitmap = 0;
1624 	u32 phy_port_num_ma = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
1625 	u32 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1626 
1627 	for (i = 0; i < (hisi_hba->n_phy < 9 ? hisi_hba->n_phy : 8); i++)
1628 		if (phy_state & 1 << i)
1629 			if (((phy_port_num_ma >> (i * 4)) & 0xf) == port_id)
1630 				bitmap |= 1 << i;
1631 
1632 	if (hisi_hba->n_phy == 9) {
1633 		u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
1634 
1635 		if (phy_state & 1 << 8)
1636 			if (((port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
1637 			     PORT_STATE_PHY8_PORT_NUM_OFF) == port_id)
1638 				bitmap |= 1 << 9;
1639 	}
1640 
1641 	return bitmap;
1642 }
1643 
1644 /*
1645  * The callpath to this function and upto writing the write
1646  * queue pointer should be safe from interruption.
1647  */
1648 static int
1649 get_free_slot_v2_hw(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq)
1650 {
1651 	struct device *dev = hisi_hba->dev;
1652 	int queue = dq->id;
1653 	u32 r, w;
1654 
1655 	w = dq->wr_point;
1656 	r = hisi_sas_read32_relaxed(hisi_hba,
1657 				DLVRY_Q_0_RD_PTR + (queue * 0x14));
1658 	if (r == (w+1) % HISI_SAS_QUEUE_SLOTS) {
1659 		dev_warn(dev, "full queue=%d r=%d w=%d\n",
1660 				queue, r, w);
1661 		return -EAGAIN;
1662 	}
1663 
1664 	dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
1665 
1666 	return w;
1667 }
1668 
1669 /* DQ lock must be taken here */
1670 static void start_delivery_v2_hw(struct hisi_sas_dq *dq)
1671 {
1672 	struct hisi_hba *hisi_hba = dq->hisi_hba;
1673 	struct hisi_sas_slot *s, *s1, *s2 = NULL;
1674 	int dlvry_queue = dq->id;
1675 	int wp;
1676 
1677 	list_for_each_entry_safe(s, s1, &dq->list, delivery) {
1678 		if (!s->ready)
1679 			break;
1680 		s2 = s;
1681 		list_del(&s->delivery);
1682 	}
1683 
1684 	if (!s2)
1685 		return;
1686 
1687 	/*
1688 	 * Ensure that memories for slots built on other CPUs is observed.
1689 	 */
1690 	smp_rmb();
1691 	wp = (s2->dlvry_queue_slot + 1) % HISI_SAS_QUEUE_SLOTS;
1692 
1693 	hisi_sas_write32(hisi_hba, DLVRY_Q_0_WR_PTR + (dlvry_queue * 0x14), wp);
1694 }
1695 
1696 static void prep_prd_sge_v2_hw(struct hisi_hba *hisi_hba,
1697 			      struct hisi_sas_slot *slot,
1698 			      struct hisi_sas_cmd_hdr *hdr,
1699 			      struct scatterlist *scatter,
1700 			      int n_elem)
1701 {
1702 	struct hisi_sas_sge_page *sge_page = hisi_sas_sge_addr_mem(slot);
1703 	struct scatterlist *sg;
1704 	int i;
1705 
1706 	for_each_sg(scatter, sg, n_elem, i) {
1707 		struct hisi_sas_sge *entry = &sge_page->sge[i];
1708 
1709 		entry->addr = cpu_to_le64(sg_dma_address(sg));
1710 		entry->page_ctrl_0 = entry->page_ctrl_1 = 0;
1711 		entry->data_len = cpu_to_le32(sg_dma_len(sg));
1712 		entry->data_off = 0;
1713 	}
1714 
1715 	hdr->prd_table_addr = cpu_to_le64(hisi_sas_sge_addr_dma(slot));
1716 
1717 	hdr->sg_len = cpu_to_le32(n_elem << CMD_HDR_DATA_SGL_LEN_OFF);
1718 }
1719 
1720 static void prep_smp_v2_hw(struct hisi_hba *hisi_hba,
1721 			  struct hisi_sas_slot *slot)
1722 {
1723 	struct sas_task *task = slot->task;
1724 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1725 	struct domain_device *device = task->dev;
1726 	struct hisi_sas_port *port = slot->port;
1727 	struct scatterlist *sg_req;
1728 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1729 	dma_addr_t req_dma_addr;
1730 	unsigned int req_len;
1731 
1732 	/* req */
1733 	sg_req = &task->smp_task.smp_req;
1734 	req_dma_addr = sg_dma_address(sg_req);
1735 	req_len = sg_dma_len(&task->smp_task.smp_req);
1736 
1737 	/* create header */
1738 	/* dw0 */
1739 	hdr->dw0 = cpu_to_le32((port->id << CMD_HDR_PORT_OFF) |
1740 			       (1 << CMD_HDR_PRIORITY_OFF) | /* high pri */
1741 			       (2 << CMD_HDR_CMD_OFF)); /* smp */
1742 
1743 	/* map itct entry */
1744 	hdr->dw1 = cpu_to_le32((sas_dev->device_id << CMD_HDR_DEV_ID_OFF) |
1745 			       (1 << CMD_HDR_FRAME_TYPE_OFF) |
1746 			       (DIR_NO_DATA << CMD_HDR_DIR_OFF));
1747 
1748 	/* dw2 */
1749 	hdr->dw2 = cpu_to_le32((((req_len - 4) / 4) << CMD_HDR_CFL_OFF) |
1750 			       (HISI_SAS_MAX_SMP_RESP_SZ / 4 <<
1751 			       CMD_HDR_MRFL_OFF));
1752 
1753 	hdr->transfer_tags = cpu_to_le32(slot->idx << CMD_HDR_IPTT_OFF);
1754 
1755 	hdr->cmd_table_addr = cpu_to_le64(req_dma_addr);
1756 	hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
1757 }
1758 
1759 static void prep_ssp_v2_hw(struct hisi_hba *hisi_hba,
1760 			  struct hisi_sas_slot *slot)
1761 {
1762 	struct sas_task *task = slot->task;
1763 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1764 	struct domain_device *device = task->dev;
1765 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1766 	struct hisi_sas_port *port = slot->port;
1767 	struct sas_ssp_task *ssp_task = &task->ssp_task;
1768 	struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
1769 	struct hisi_sas_tmf_task *tmf = slot->tmf;
1770 	int has_data = 0, priority = !!tmf;
1771 	u8 *buf_cmd;
1772 	u32 dw1 = 0, dw2 = 0;
1773 
1774 	hdr->dw0 = cpu_to_le32((1 << CMD_HDR_RESP_REPORT_OFF) |
1775 			       (2 << CMD_HDR_TLR_CTRL_OFF) |
1776 			       (port->id << CMD_HDR_PORT_OFF) |
1777 			       (priority << CMD_HDR_PRIORITY_OFF) |
1778 			       (1 << CMD_HDR_CMD_OFF)); /* ssp */
1779 
1780 	dw1 = 1 << CMD_HDR_VDTL_OFF;
1781 	if (tmf) {
1782 		dw1 |= 2 << CMD_HDR_FRAME_TYPE_OFF;
1783 		dw1 |= DIR_NO_DATA << CMD_HDR_DIR_OFF;
1784 	} else {
1785 		dw1 |= 1 << CMD_HDR_FRAME_TYPE_OFF;
1786 		switch (scsi_cmnd->sc_data_direction) {
1787 		case DMA_TO_DEVICE:
1788 			has_data = 1;
1789 			dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
1790 			break;
1791 		case DMA_FROM_DEVICE:
1792 			has_data = 1;
1793 			dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
1794 			break;
1795 		default:
1796 			dw1 &= ~CMD_HDR_DIR_MSK;
1797 		}
1798 	}
1799 
1800 	/* map itct entry */
1801 	dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
1802 	hdr->dw1 = cpu_to_le32(dw1);
1803 
1804 	dw2 = (((sizeof(struct ssp_command_iu) + sizeof(struct ssp_frame_hdr)
1805 	      + 3) / 4) << CMD_HDR_CFL_OFF) |
1806 	      ((HISI_SAS_MAX_SSP_RESP_SZ / 4) << CMD_HDR_MRFL_OFF) |
1807 	      (2 << CMD_HDR_SG_MOD_OFF);
1808 	hdr->dw2 = cpu_to_le32(dw2);
1809 
1810 	hdr->transfer_tags = cpu_to_le32(slot->idx);
1811 
1812 	if (has_data)
1813 		prep_prd_sge_v2_hw(hisi_hba, slot, hdr, task->scatter,
1814 					slot->n_elem);
1815 
1816 	hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
1817 	hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
1818 	hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
1819 
1820 	buf_cmd = hisi_sas_cmd_hdr_addr_mem(slot) +
1821 		sizeof(struct ssp_frame_hdr);
1822 
1823 	memcpy(buf_cmd, &task->ssp_task.LUN, 8);
1824 	if (!tmf) {
1825 		buf_cmd[9] = task->ssp_task.task_attr |
1826 				(task->ssp_task.task_prio << 3);
1827 		memcpy(buf_cmd + 12, task->ssp_task.cmd->cmnd,
1828 				task->ssp_task.cmd->cmd_len);
1829 	} else {
1830 		buf_cmd[10] = tmf->tmf;
1831 		switch (tmf->tmf) {
1832 		case TMF_ABORT_TASK:
1833 		case TMF_QUERY_TASK:
1834 			buf_cmd[12] =
1835 				(tmf->tag_of_task_to_be_managed >> 8) & 0xff;
1836 			buf_cmd[13] =
1837 				tmf->tag_of_task_to_be_managed & 0xff;
1838 			break;
1839 		default:
1840 			break;
1841 		}
1842 	}
1843 }
1844 
1845 #define TRANS_TX_ERR	0
1846 #define TRANS_RX_ERR	1
1847 #define DMA_TX_ERR		2
1848 #define SIPC_RX_ERR		3
1849 #define DMA_RX_ERR		4
1850 
1851 #define DMA_TX_ERR_OFF	0
1852 #define DMA_TX_ERR_MSK	(0xffff << DMA_TX_ERR_OFF)
1853 #define SIPC_RX_ERR_OFF	16
1854 #define SIPC_RX_ERR_MSK (0xffff << SIPC_RX_ERR_OFF)
1855 
1856 static int parse_trans_tx_err_code_v2_hw(u32 err_msk)
1857 {
1858 	static const u8 trans_tx_err_code_prio[] = {
1859 		TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS,
1860 		TRANS_TX_ERR_PHY_NOT_ENABLE,
1861 		TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION,
1862 		TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION,
1863 		TRANS_TX_OPEN_CNX_ERR_BY_OTHER,
1864 		RESERVED0,
1865 		TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT,
1866 		TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY,
1867 		TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED,
1868 		TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED,
1869 		TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION,
1870 		TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD,
1871 		TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER,
1872 		TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED,
1873 		TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT,
1874 		TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION,
1875 		TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED,
1876 		TRANS_TX_ERR_WITH_CLOSE_PHYDISALE,
1877 		TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT,
1878 		TRANS_TX_ERR_WITH_CLOSE_COMINIT,
1879 		TRANS_TX_ERR_WITH_BREAK_TIMEOUT,
1880 		TRANS_TX_ERR_WITH_BREAK_REQUEST,
1881 		TRANS_TX_ERR_WITH_BREAK_RECEVIED,
1882 		TRANS_TX_ERR_WITH_CLOSE_TIMEOUT,
1883 		TRANS_TX_ERR_WITH_CLOSE_NORMAL,
1884 		TRANS_TX_ERR_WITH_NAK_RECEVIED,
1885 		TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT,
1886 		TRANS_TX_ERR_WITH_CREDIT_TIMEOUT,
1887 		TRANS_TX_ERR_WITH_IPTT_CONFLICT,
1888 		TRANS_TX_ERR_WITH_OPEN_BY_DES_OR_OTHERS,
1889 		TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT,
1890 	};
1891 	int index, i;
1892 
1893 	for (i = 0; i < ARRAY_SIZE(trans_tx_err_code_prio); i++) {
1894 		index = trans_tx_err_code_prio[i] - TRANS_TX_FAIL_BASE;
1895 		if (err_msk & (1 << index))
1896 			return trans_tx_err_code_prio[i];
1897 	}
1898 	return -1;
1899 }
1900 
1901 static int parse_trans_rx_err_code_v2_hw(u32 err_msk)
1902 {
1903 	static const u8 trans_rx_err_code_prio[] = {
1904 		TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR,
1905 		TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR,
1906 		TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM,
1907 		TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR,
1908 		TRANS_RX_ERR_WITH_RXFIS_CRC_ERR,
1909 		TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN,
1910 		TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP,
1911 		TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN,
1912 		TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE,
1913 		TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT,
1914 		TRANS_RX_ERR_WITH_CLOSE_COMINIT,
1915 		TRANS_RX_ERR_WITH_BREAK_TIMEOUT,
1916 		TRANS_RX_ERR_WITH_BREAK_REQUEST,
1917 		TRANS_RX_ERR_WITH_BREAK_RECEVIED,
1918 		RESERVED1,
1919 		TRANS_RX_ERR_WITH_CLOSE_NORMAL,
1920 		TRANS_RX_ERR_WITH_DATA_LEN0,
1921 		TRANS_RX_ERR_WITH_BAD_HASH,
1922 		TRANS_RX_XRDY_WLEN_ZERO_ERR,
1923 		TRANS_RX_SSP_FRM_LEN_ERR,
1924 		RESERVED2,
1925 		RESERVED3,
1926 		RESERVED4,
1927 		RESERVED5,
1928 		TRANS_RX_ERR_WITH_BAD_FRM_TYPE,
1929 		TRANS_RX_SMP_FRM_LEN_ERR,
1930 		TRANS_RX_SMP_RESP_TIMEOUT_ERR,
1931 		RESERVED6,
1932 		RESERVED7,
1933 		RESERVED8,
1934 		RESERVED9,
1935 		TRANS_RX_R_ERR,
1936 	};
1937 	int index, i;
1938 
1939 	for (i = 0; i < ARRAY_SIZE(trans_rx_err_code_prio); i++) {
1940 		index = trans_rx_err_code_prio[i] - TRANS_RX_FAIL_BASE;
1941 		if (err_msk & (1 << index))
1942 			return trans_rx_err_code_prio[i];
1943 	}
1944 	return -1;
1945 }
1946 
1947 static int parse_dma_tx_err_code_v2_hw(u32 err_msk)
1948 {
1949 	static const u8 dma_tx_err_code_prio[] = {
1950 		DMA_TX_UNEXP_XFER_ERR,
1951 		DMA_TX_UNEXP_RETRANS_ERR,
1952 		DMA_TX_XFER_LEN_OVERFLOW,
1953 		DMA_TX_XFER_OFFSET_ERR,
1954 		DMA_TX_RAM_ECC_ERR,
1955 		DMA_TX_DIF_LEN_ALIGN_ERR,
1956 		DMA_TX_DIF_CRC_ERR,
1957 		DMA_TX_DIF_APP_ERR,
1958 		DMA_TX_DIF_RPP_ERR,
1959 		DMA_TX_DATA_SGL_OVERFLOW,
1960 		DMA_TX_DIF_SGL_OVERFLOW,
1961 	};
1962 	int index, i;
1963 
1964 	for (i = 0; i < ARRAY_SIZE(dma_tx_err_code_prio); i++) {
1965 		index = dma_tx_err_code_prio[i] - DMA_TX_ERR_BASE;
1966 		err_msk = err_msk & DMA_TX_ERR_MSK;
1967 		if (err_msk & (1 << index))
1968 			return dma_tx_err_code_prio[i];
1969 	}
1970 	return -1;
1971 }
1972 
1973 static int parse_sipc_rx_err_code_v2_hw(u32 err_msk)
1974 {
1975 	static const u8 sipc_rx_err_code_prio[] = {
1976 		SIPC_RX_FIS_STATUS_ERR_BIT_VLD,
1977 		SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR,
1978 		SIPC_RX_FIS_STATUS_BSY_BIT_ERR,
1979 		SIPC_RX_WRSETUP_LEN_ODD_ERR,
1980 		SIPC_RX_WRSETUP_LEN_ZERO_ERR,
1981 		SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR,
1982 		SIPC_RX_NCQ_WRSETUP_OFFSET_ERR,
1983 		SIPC_RX_NCQ_WRSETUP_AUTO_ACTIVE_ERR,
1984 		SIPC_RX_SATA_UNEXP_FIS_ERR,
1985 		SIPC_RX_WRSETUP_ESTATUS_ERR,
1986 		SIPC_RX_DATA_UNDERFLOW_ERR,
1987 	};
1988 	int index, i;
1989 
1990 	for (i = 0; i < ARRAY_SIZE(sipc_rx_err_code_prio); i++) {
1991 		index = sipc_rx_err_code_prio[i] - SIPC_RX_ERR_BASE;
1992 		err_msk = err_msk & SIPC_RX_ERR_MSK;
1993 		if (err_msk & (1 << (index + 0x10)))
1994 			return sipc_rx_err_code_prio[i];
1995 	}
1996 	return -1;
1997 }
1998 
1999 static int parse_dma_rx_err_code_v2_hw(u32 err_msk)
2000 {
2001 	static const u8 dma_rx_err_code_prio[] = {
2002 		DMA_RX_UNKNOWN_FRM_ERR,
2003 		DMA_RX_DATA_LEN_OVERFLOW,
2004 		DMA_RX_DATA_LEN_UNDERFLOW,
2005 		DMA_RX_DATA_OFFSET_ERR,
2006 		RESERVED10,
2007 		DMA_RX_SATA_FRAME_TYPE_ERR,
2008 		DMA_RX_RESP_BUF_OVERFLOW,
2009 		DMA_RX_UNEXP_RETRANS_RESP_ERR,
2010 		DMA_RX_UNEXP_NORM_RESP_ERR,
2011 		DMA_RX_UNEXP_RDFRAME_ERR,
2012 		DMA_RX_PIO_DATA_LEN_ERR,
2013 		DMA_RX_RDSETUP_STATUS_ERR,
2014 		DMA_RX_RDSETUP_STATUS_DRQ_ERR,
2015 		DMA_RX_RDSETUP_STATUS_BSY_ERR,
2016 		DMA_RX_RDSETUP_LEN_ODD_ERR,
2017 		DMA_RX_RDSETUP_LEN_ZERO_ERR,
2018 		DMA_RX_RDSETUP_LEN_OVER_ERR,
2019 		DMA_RX_RDSETUP_OFFSET_ERR,
2020 		DMA_RX_RDSETUP_ACTIVE_ERR,
2021 		DMA_RX_RDSETUP_ESTATUS_ERR,
2022 		DMA_RX_RAM_ECC_ERR,
2023 		DMA_RX_DIF_CRC_ERR,
2024 		DMA_RX_DIF_APP_ERR,
2025 		DMA_RX_DIF_RPP_ERR,
2026 		DMA_RX_DATA_SGL_OVERFLOW,
2027 		DMA_RX_DIF_SGL_OVERFLOW,
2028 	};
2029 	int index, i;
2030 
2031 	for (i = 0; i < ARRAY_SIZE(dma_rx_err_code_prio); i++) {
2032 		index = dma_rx_err_code_prio[i] - DMA_RX_ERR_BASE;
2033 		if (err_msk & (1 << index))
2034 			return dma_rx_err_code_prio[i];
2035 	}
2036 	return -1;
2037 }
2038 
2039 /* by default, task resp is complete */
2040 static void slot_err_v2_hw(struct hisi_hba *hisi_hba,
2041 			   struct sas_task *task,
2042 			   struct hisi_sas_slot *slot,
2043 			   int err_phase)
2044 {
2045 	struct task_status_struct *ts = &task->task_status;
2046 	struct hisi_sas_err_record_v2 *err_record =
2047 			hisi_sas_status_buf_addr_mem(slot);
2048 	u32 trans_tx_fail_type = le32_to_cpu(err_record->trans_tx_fail_type);
2049 	u32 trans_rx_fail_type = le32_to_cpu(err_record->trans_rx_fail_type);
2050 	u16 dma_tx_err_type = le16_to_cpu(err_record->dma_tx_err_type);
2051 	u16 sipc_rx_err_type = le16_to_cpu(err_record->sipc_rx_err_type);
2052 	u32 dma_rx_err_type = le32_to_cpu(err_record->dma_rx_err_type);
2053 	int error = -1;
2054 
2055 	if (err_phase == 1) {
2056 		/* error in TX phase, the priority of error is: DW2 > DW0 */
2057 		error = parse_dma_tx_err_code_v2_hw(dma_tx_err_type);
2058 		if (error == -1)
2059 			error = parse_trans_tx_err_code_v2_hw(
2060 					trans_tx_fail_type);
2061 	} else if (err_phase == 2) {
2062 		/* error in RX phase, the priority is: DW1 > DW3 > DW2 */
2063 		error = parse_trans_rx_err_code_v2_hw(trans_rx_fail_type);
2064 		if (error == -1) {
2065 			error = parse_dma_rx_err_code_v2_hw(
2066 					dma_rx_err_type);
2067 			if (error == -1)
2068 				error = parse_sipc_rx_err_code_v2_hw(
2069 						sipc_rx_err_type);
2070 		}
2071 	}
2072 
2073 	switch (task->task_proto) {
2074 	case SAS_PROTOCOL_SSP:
2075 	{
2076 		switch (error) {
2077 		case TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION:
2078 		{
2079 			ts->stat = SAS_OPEN_REJECT;
2080 			ts->open_rej_reason = SAS_OREJ_NO_DEST;
2081 			break;
2082 		}
2083 		case TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED:
2084 		{
2085 			ts->stat = SAS_OPEN_REJECT;
2086 			ts->open_rej_reason = SAS_OREJ_EPROTO;
2087 			break;
2088 		}
2089 		case TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED:
2090 		{
2091 			ts->stat = SAS_OPEN_REJECT;
2092 			ts->open_rej_reason = SAS_OREJ_CONN_RATE;
2093 			break;
2094 		}
2095 		case TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION:
2096 		{
2097 			ts->stat = SAS_OPEN_REJECT;
2098 			ts->open_rej_reason = SAS_OREJ_BAD_DEST;
2099 			break;
2100 		}
2101 		case TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION:
2102 		{
2103 			ts->stat = SAS_OPEN_REJECT;
2104 			ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
2105 			break;
2106 		}
2107 		case DMA_RX_UNEXP_NORM_RESP_ERR:
2108 		case TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION:
2109 		case DMA_RX_RESP_BUF_OVERFLOW:
2110 		{
2111 			ts->stat = SAS_OPEN_REJECT;
2112 			ts->open_rej_reason = SAS_OREJ_UNKNOWN;
2113 			break;
2114 		}
2115 		case TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER:
2116 		{
2117 			/* not sure */
2118 			ts->stat = SAS_DEV_NO_RESPONSE;
2119 			break;
2120 		}
2121 		case DMA_RX_DATA_LEN_OVERFLOW:
2122 		{
2123 			ts->stat = SAS_DATA_OVERRUN;
2124 			ts->residual = 0;
2125 			break;
2126 		}
2127 		case DMA_RX_DATA_LEN_UNDERFLOW:
2128 		{
2129 			ts->residual = trans_tx_fail_type;
2130 			ts->stat = SAS_DATA_UNDERRUN;
2131 			break;
2132 		}
2133 		case TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS:
2134 		case TRANS_TX_ERR_PHY_NOT_ENABLE:
2135 		case TRANS_TX_OPEN_CNX_ERR_BY_OTHER:
2136 		case TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT:
2137 		case TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD:
2138 		case TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED:
2139 		case TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT:
2140 		case TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED:
2141 		case TRANS_TX_ERR_WITH_BREAK_TIMEOUT:
2142 		case TRANS_TX_ERR_WITH_BREAK_REQUEST:
2143 		case TRANS_TX_ERR_WITH_BREAK_RECEVIED:
2144 		case TRANS_TX_ERR_WITH_CLOSE_TIMEOUT:
2145 		case TRANS_TX_ERR_WITH_CLOSE_NORMAL:
2146 		case TRANS_TX_ERR_WITH_CLOSE_PHYDISALE:
2147 		case TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT:
2148 		case TRANS_TX_ERR_WITH_CLOSE_COMINIT:
2149 		case TRANS_TX_ERR_WITH_NAK_RECEVIED:
2150 		case TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT:
2151 		case TRANS_TX_ERR_WITH_CREDIT_TIMEOUT:
2152 		case TRANS_TX_ERR_WITH_IPTT_CONFLICT:
2153 		case TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR:
2154 		case TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR:
2155 		case TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM:
2156 		case TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN:
2157 		case TRANS_RX_ERR_WITH_BREAK_TIMEOUT:
2158 		case TRANS_RX_ERR_WITH_BREAK_REQUEST:
2159 		case TRANS_RX_ERR_WITH_BREAK_RECEVIED:
2160 		case TRANS_RX_ERR_WITH_CLOSE_NORMAL:
2161 		case TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT:
2162 		case TRANS_RX_ERR_WITH_CLOSE_COMINIT:
2163 		case TRANS_TX_ERR_FRAME_TXED:
2164 		case TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE:
2165 		case TRANS_RX_ERR_WITH_DATA_LEN0:
2166 		case TRANS_RX_ERR_WITH_BAD_HASH:
2167 		case TRANS_RX_XRDY_WLEN_ZERO_ERR:
2168 		case TRANS_RX_SSP_FRM_LEN_ERR:
2169 		case TRANS_RX_ERR_WITH_BAD_FRM_TYPE:
2170 		case DMA_TX_DATA_SGL_OVERFLOW:
2171 		case DMA_TX_UNEXP_XFER_ERR:
2172 		case DMA_TX_UNEXP_RETRANS_ERR:
2173 		case DMA_TX_XFER_LEN_OVERFLOW:
2174 		case DMA_TX_XFER_OFFSET_ERR:
2175 		case SIPC_RX_DATA_UNDERFLOW_ERR:
2176 		case DMA_RX_DATA_SGL_OVERFLOW:
2177 		case DMA_RX_DATA_OFFSET_ERR:
2178 		case DMA_RX_RDSETUP_LEN_ODD_ERR:
2179 		case DMA_RX_RDSETUP_LEN_ZERO_ERR:
2180 		case DMA_RX_RDSETUP_LEN_OVER_ERR:
2181 		case DMA_RX_SATA_FRAME_TYPE_ERR:
2182 		case DMA_RX_UNKNOWN_FRM_ERR:
2183 		{
2184 			/* This will request a retry */
2185 			ts->stat = SAS_QUEUE_FULL;
2186 			slot->abort = 1;
2187 			break;
2188 		}
2189 		default:
2190 			break;
2191 		}
2192 	}
2193 		break;
2194 	case SAS_PROTOCOL_SMP:
2195 		ts->stat = SAM_STAT_CHECK_CONDITION;
2196 		break;
2197 
2198 	case SAS_PROTOCOL_SATA:
2199 	case SAS_PROTOCOL_STP:
2200 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
2201 	{
2202 		switch (error) {
2203 		case TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION:
2204 		{
2205 			ts->stat = SAS_OPEN_REJECT;
2206 			ts->open_rej_reason = SAS_OREJ_NO_DEST;
2207 			break;
2208 		}
2209 		case TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER:
2210 		{
2211 			ts->resp = SAS_TASK_UNDELIVERED;
2212 			ts->stat = SAS_DEV_NO_RESPONSE;
2213 			break;
2214 		}
2215 		case TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED:
2216 		{
2217 			ts->stat = SAS_OPEN_REJECT;
2218 			ts->open_rej_reason = SAS_OREJ_EPROTO;
2219 			break;
2220 		}
2221 		case TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED:
2222 		{
2223 			ts->stat = SAS_OPEN_REJECT;
2224 			ts->open_rej_reason = SAS_OREJ_CONN_RATE;
2225 			break;
2226 		}
2227 		case TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION:
2228 		{
2229 			ts->stat = SAS_OPEN_REJECT;
2230 			ts->open_rej_reason = SAS_OREJ_CONN_RATE;
2231 			break;
2232 		}
2233 		case TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION:
2234 		{
2235 			ts->stat = SAS_OPEN_REJECT;
2236 			ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
2237 			break;
2238 		}
2239 		case DMA_RX_RESP_BUF_OVERFLOW:
2240 		case DMA_RX_UNEXP_NORM_RESP_ERR:
2241 		case TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION:
2242 		{
2243 			ts->stat = SAS_OPEN_REJECT;
2244 			ts->open_rej_reason = SAS_OREJ_UNKNOWN;
2245 			break;
2246 		}
2247 		case DMA_RX_DATA_LEN_OVERFLOW:
2248 		{
2249 			ts->stat = SAS_DATA_OVERRUN;
2250 			ts->residual = 0;
2251 			break;
2252 		}
2253 		case DMA_RX_DATA_LEN_UNDERFLOW:
2254 		{
2255 			ts->residual = trans_tx_fail_type;
2256 			ts->stat = SAS_DATA_UNDERRUN;
2257 			break;
2258 		}
2259 		case TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS:
2260 		case TRANS_TX_ERR_PHY_NOT_ENABLE:
2261 		case TRANS_TX_OPEN_CNX_ERR_BY_OTHER:
2262 		case TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT:
2263 		case TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD:
2264 		case TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED:
2265 		case TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT:
2266 		case TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED:
2267 		case TRANS_TX_ERR_WITH_BREAK_TIMEOUT:
2268 		case TRANS_TX_ERR_WITH_BREAK_REQUEST:
2269 		case TRANS_TX_ERR_WITH_BREAK_RECEVIED:
2270 		case TRANS_TX_ERR_WITH_CLOSE_TIMEOUT:
2271 		case TRANS_TX_ERR_WITH_CLOSE_NORMAL:
2272 		case TRANS_TX_ERR_WITH_CLOSE_PHYDISALE:
2273 		case TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT:
2274 		case TRANS_TX_ERR_WITH_CLOSE_COMINIT:
2275 		case TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT:
2276 		case TRANS_TX_ERR_WITH_CREDIT_TIMEOUT:
2277 		case TRANS_TX_ERR_WITH_OPEN_BY_DES_OR_OTHERS:
2278 		case TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT:
2279 		case TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM:
2280 		case TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR:
2281 		case TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR:
2282 		case TRANS_RX_ERR_WITH_RXFIS_CRC_ERR:
2283 		case TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN:
2284 		case TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP:
2285 		case TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN:
2286 		case TRANS_RX_ERR_WITH_BREAK_TIMEOUT:
2287 		case TRANS_RX_ERR_WITH_BREAK_REQUEST:
2288 		case TRANS_RX_ERR_WITH_BREAK_RECEVIED:
2289 		case TRANS_RX_ERR_WITH_CLOSE_NORMAL:
2290 		case TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE:
2291 		case TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT:
2292 		case TRANS_RX_ERR_WITH_CLOSE_COMINIT:
2293 		case TRANS_RX_ERR_WITH_DATA_LEN0:
2294 		case TRANS_RX_ERR_WITH_BAD_HASH:
2295 		case TRANS_RX_XRDY_WLEN_ZERO_ERR:
2296 		case TRANS_RX_ERR_WITH_BAD_FRM_TYPE:
2297 		case DMA_TX_DATA_SGL_OVERFLOW:
2298 		case DMA_TX_UNEXP_XFER_ERR:
2299 		case DMA_TX_UNEXP_RETRANS_ERR:
2300 		case DMA_TX_XFER_LEN_OVERFLOW:
2301 		case DMA_TX_XFER_OFFSET_ERR:
2302 		case SIPC_RX_FIS_STATUS_ERR_BIT_VLD:
2303 		case SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR:
2304 		case SIPC_RX_FIS_STATUS_BSY_BIT_ERR:
2305 		case SIPC_RX_WRSETUP_LEN_ODD_ERR:
2306 		case SIPC_RX_WRSETUP_LEN_ZERO_ERR:
2307 		case SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR:
2308 		case SIPC_RX_SATA_UNEXP_FIS_ERR:
2309 		case DMA_RX_DATA_SGL_OVERFLOW:
2310 		case DMA_RX_DATA_OFFSET_ERR:
2311 		case DMA_RX_SATA_FRAME_TYPE_ERR:
2312 		case DMA_RX_UNEXP_RDFRAME_ERR:
2313 		case DMA_RX_PIO_DATA_LEN_ERR:
2314 		case DMA_RX_RDSETUP_STATUS_ERR:
2315 		case DMA_RX_RDSETUP_STATUS_DRQ_ERR:
2316 		case DMA_RX_RDSETUP_STATUS_BSY_ERR:
2317 		case DMA_RX_RDSETUP_LEN_ODD_ERR:
2318 		case DMA_RX_RDSETUP_LEN_ZERO_ERR:
2319 		case DMA_RX_RDSETUP_LEN_OVER_ERR:
2320 		case DMA_RX_RDSETUP_OFFSET_ERR:
2321 		case DMA_RX_RDSETUP_ACTIVE_ERR:
2322 		case DMA_RX_RDSETUP_ESTATUS_ERR:
2323 		case DMA_RX_UNKNOWN_FRM_ERR:
2324 		case TRANS_RX_SSP_FRM_LEN_ERR:
2325 		case TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY:
2326 		{
2327 			slot->abort = 1;
2328 			ts->stat = SAS_PHY_DOWN;
2329 			break;
2330 		}
2331 		default:
2332 		{
2333 			ts->stat = SAS_PROTO_RESPONSE;
2334 			break;
2335 		}
2336 		}
2337 		hisi_sas_sata_done(task, slot);
2338 	}
2339 		break;
2340 	default:
2341 		break;
2342 	}
2343 }
2344 
2345 static int
2346 slot_complete_v2_hw(struct hisi_hba *hisi_hba, struct hisi_sas_slot *slot)
2347 {
2348 	struct sas_task *task = slot->task;
2349 	struct hisi_sas_device *sas_dev;
2350 	struct device *dev = hisi_hba->dev;
2351 	struct task_status_struct *ts;
2352 	struct domain_device *device;
2353 	struct sas_ha_struct *ha;
2354 	enum exec_status sts;
2355 	struct hisi_sas_complete_v2_hdr *complete_queue =
2356 			hisi_hba->complete_hdr[slot->cmplt_queue];
2357 	struct hisi_sas_complete_v2_hdr *complete_hdr =
2358 			&complete_queue[slot->cmplt_queue_slot];
2359 	unsigned long flags;
2360 	bool is_internal = slot->is_internal;
2361 	u32 dw0;
2362 
2363 	if (unlikely(!task || !task->lldd_task || !task->dev))
2364 		return -EINVAL;
2365 
2366 	ts = &task->task_status;
2367 	device = task->dev;
2368 	ha = device->port->ha;
2369 	sas_dev = device->lldd_dev;
2370 
2371 	spin_lock_irqsave(&task->task_state_lock, flags);
2372 	task->task_state_flags &=
2373 		~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
2374 	spin_unlock_irqrestore(&task->task_state_lock, flags);
2375 
2376 	memset(ts, 0, sizeof(*ts));
2377 	ts->resp = SAS_TASK_COMPLETE;
2378 
2379 	if (unlikely(!sas_dev)) {
2380 		dev_dbg(dev, "slot complete: port has no device\n");
2381 		ts->stat = SAS_PHY_DOWN;
2382 		goto out;
2383 	}
2384 
2385 	/* Use SAS+TMF status codes */
2386 	dw0 = le32_to_cpu(complete_hdr->dw0);
2387 	switch ((dw0 & CMPLT_HDR_ABORT_STAT_MSK) >>
2388 		CMPLT_HDR_ABORT_STAT_OFF) {
2389 	case STAT_IO_ABORTED:
2390 		/* this io has been aborted by abort command */
2391 		ts->stat = SAS_ABORTED_TASK;
2392 		goto out;
2393 	case STAT_IO_COMPLETE:
2394 		/* internal abort command complete */
2395 		ts->stat = TMF_RESP_FUNC_SUCC;
2396 		del_timer(&slot->internal_abort_timer);
2397 		goto out;
2398 	case STAT_IO_NO_DEVICE:
2399 		ts->stat = TMF_RESP_FUNC_COMPLETE;
2400 		del_timer(&slot->internal_abort_timer);
2401 		goto out;
2402 	case STAT_IO_NOT_VALID:
2403 		/* abort single io, controller don't find
2404 		 * the io need to abort
2405 		 */
2406 		ts->stat = TMF_RESP_FUNC_FAILED;
2407 		del_timer(&slot->internal_abort_timer);
2408 		goto out;
2409 	default:
2410 		break;
2411 	}
2412 
2413 	if ((dw0 & CMPLT_HDR_ERX_MSK) && (!(dw0 & CMPLT_HDR_RSPNS_XFRD_MSK))) {
2414 		u32 err_phase = (dw0 & CMPLT_HDR_ERR_PHASE_MSK)
2415 				>> CMPLT_HDR_ERR_PHASE_OFF;
2416 		u32 *error_info = hisi_sas_status_buf_addr_mem(slot);
2417 
2418 		/* Analyse error happens on which phase TX or RX */
2419 		if (ERR_ON_TX_PHASE(err_phase))
2420 			slot_err_v2_hw(hisi_hba, task, slot, 1);
2421 		else if (ERR_ON_RX_PHASE(err_phase))
2422 			slot_err_v2_hw(hisi_hba, task, slot, 2);
2423 
2424 		if (ts->stat != SAS_DATA_UNDERRUN)
2425 			dev_info(dev, "erroneous completion iptt=%d task=%p dev id=%d "
2426 				"CQ hdr: 0x%x 0x%x 0x%x 0x%x "
2427 				"Error info: 0x%x 0x%x 0x%x 0x%x\n",
2428 				slot->idx, task, sas_dev->device_id,
2429 				complete_hdr->dw0, complete_hdr->dw1,
2430 				complete_hdr->act, complete_hdr->dw3,
2431 				error_info[0], error_info[1],
2432 				error_info[2], error_info[3]);
2433 
2434 		if (unlikely(slot->abort))
2435 			return ts->stat;
2436 		goto out;
2437 	}
2438 
2439 	switch (task->task_proto) {
2440 	case SAS_PROTOCOL_SSP:
2441 	{
2442 		struct hisi_sas_status_buffer *status_buffer =
2443 				hisi_sas_status_buf_addr_mem(slot);
2444 		struct ssp_response_iu *iu = (struct ssp_response_iu *)
2445 				&status_buffer->iu[0];
2446 
2447 		sas_ssp_task_response(dev, task, iu);
2448 		break;
2449 	}
2450 	case SAS_PROTOCOL_SMP:
2451 	{
2452 		struct scatterlist *sg_resp = &task->smp_task.smp_resp;
2453 		void *to;
2454 
2455 		ts->stat = SAM_STAT_GOOD;
2456 		to = kmap_atomic(sg_page(sg_resp));
2457 
2458 		dma_unmap_sg(dev, &task->smp_task.smp_resp, 1,
2459 			     DMA_FROM_DEVICE);
2460 		dma_unmap_sg(dev, &task->smp_task.smp_req, 1,
2461 			     DMA_TO_DEVICE);
2462 		memcpy(to + sg_resp->offset,
2463 		       hisi_sas_status_buf_addr_mem(slot) +
2464 		       sizeof(struct hisi_sas_err_record),
2465 		       sg_dma_len(sg_resp));
2466 		kunmap_atomic(to);
2467 		break;
2468 	}
2469 	case SAS_PROTOCOL_SATA:
2470 	case SAS_PROTOCOL_STP:
2471 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
2472 	{
2473 		ts->stat = SAM_STAT_GOOD;
2474 		hisi_sas_sata_done(task, slot);
2475 		break;
2476 	}
2477 	default:
2478 		ts->stat = SAM_STAT_CHECK_CONDITION;
2479 		break;
2480 	}
2481 
2482 	if (!slot->port->port_attached) {
2483 		dev_warn(dev, "slot complete: port %d has removed\n",
2484 			slot->port->sas_port.id);
2485 		ts->stat = SAS_PHY_DOWN;
2486 	}
2487 
2488 out:
2489 	sts = ts->stat;
2490 	spin_lock_irqsave(&task->task_state_lock, flags);
2491 	if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
2492 		spin_unlock_irqrestore(&task->task_state_lock, flags);
2493 		dev_info(dev, "slot complete: task(%p) aborted\n", task);
2494 		return SAS_ABORTED_TASK;
2495 	}
2496 	task->task_state_flags |= SAS_TASK_STATE_DONE;
2497 	spin_unlock_irqrestore(&task->task_state_lock, flags);
2498 	hisi_sas_slot_task_free(hisi_hba, task, slot);
2499 
2500 	if (!is_internal && (task->task_proto != SAS_PROTOCOL_SMP)) {
2501 		spin_lock_irqsave(&device->done_lock, flags);
2502 		if (test_bit(SAS_HA_FROZEN, &ha->state)) {
2503 			spin_unlock_irqrestore(&device->done_lock, flags);
2504 			dev_info(dev, "slot complete: task(%p) ignored\n ",
2505 				 task);
2506 			return sts;
2507 		}
2508 		spin_unlock_irqrestore(&device->done_lock, flags);
2509 	}
2510 
2511 	if (task->task_done)
2512 		task->task_done(task);
2513 
2514 	return sts;
2515 }
2516 
2517 static void prep_ata_v2_hw(struct hisi_hba *hisi_hba,
2518 			  struct hisi_sas_slot *slot)
2519 {
2520 	struct sas_task *task = slot->task;
2521 	struct domain_device *device = task->dev;
2522 	struct domain_device *parent_dev = device->parent;
2523 	struct hisi_sas_device *sas_dev = device->lldd_dev;
2524 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
2525 	struct asd_sas_port *sas_port = device->port;
2526 	struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
2527 	struct hisi_sas_tmf_task *tmf = slot->tmf;
2528 	u8 *buf_cmd;
2529 	int has_data = 0, hdr_tag = 0;
2530 	u32 dw0, dw1 = 0, dw2 = 0;
2531 
2532 	/* create header */
2533 	/* dw0 */
2534 	dw0 = port->id << CMD_HDR_PORT_OFF;
2535 	if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type))
2536 		dw0 |= 3 << CMD_HDR_CMD_OFF;
2537 	else
2538 		dw0 |= 4 << CMD_HDR_CMD_OFF;
2539 
2540 	if (tmf && tmf->force_phy) {
2541 		dw0 |= CMD_HDR_FORCE_PHY_MSK;
2542 		dw0 |= (1 << tmf->phy_id) << CMD_HDR_PHY_ID_OFF;
2543 	}
2544 
2545 	hdr->dw0 = cpu_to_le32(dw0);
2546 
2547 	/* dw1 */
2548 	switch (task->data_dir) {
2549 	case DMA_TO_DEVICE:
2550 		has_data = 1;
2551 		dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
2552 		break;
2553 	case DMA_FROM_DEVICE:
2554 		has_data = 1;
2555 		dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
2556 		break;
2557 	default:
2558 		dw1 &= ~CMD_HDR_DIR_MSK;
2559 	}
2560 
2561 	if ((task->ata_task.fis.command == ATA_CMD_DEV_RESET) &&
2562 			(task->ata_task.fis.control & ATA_SRST))
2563 		dw1 |= 1 << CMD_HDR_RESET_OFF;
2564 
2565 	dw1 |= (hisi_sas_get_ata_protocol(
2566 		&task->ata_task.fis, task->data_dir))
2567 		<< CMD_HDR_FRAME_TYPE_OFF;
2568 	dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
2569 	hdr->dw1 = cpu_to_le32(dw1);
2570 
2571 	/* dw2 */
2572 	if (task->ata_task.use_ncq && hisi_sas_get_ncq_tag(task, &hdr_tag)) {
2573 		task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
2574 		dw2 |= hdr_tag << CMD_HDR_NCQ_TAG_OFF;
2575 	}
2576 
2577 	dw2 |= (HISI_SAS_MAX_STP_RESP_SZ / 4) << CMD_HDR_CFL_OFF |
2578 			2 << CMD_HDR_SG_MOD_OFF;
2579 	hdr->dw2 = cpu_to_le32(dw2);
2580 
2581 	/* dw3 */
2582 	hdr->transfer_tags = cpu_to_le32(slot->idx);
2583 
2584 	if (has_data)
2585 		prep_prd_sge_v2_hw(hisi_hba, slot, hdr, task->scatter,
2586 					slot->n_elem);
2587 
2588 	hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
2589 	hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
2590 	hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
2591 
2592 	buf_cmd = hisi_sas_cmd_hdr_addr_mem(slot);
2593 
2594 	if (likely(!task->ata_task.device_control_reg_update))
2595 		task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
2596 	/* fill in command FIS */
2597 	memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
2598 }
2599 
2600 static void hisi_sas_internal_abort_quirk_timeout(struct timer_list *t)
2601 {
2602 	struct hisi_sas_slot *slot = from_timer(slot, t, internal_abort_timer);
2603 	struct hisi_sas_port *port = slot->port;
2604 	struct asd_sas_port *asd_sas_port;
2605 	struct asd_sas_phy *sas_phy;
2606 
2607 	if (!port)
2608 		return;
2609 
2610 	asd_sas_port = &port->sas_port;
2611 
2612 	/* Kick the hardware - send break command */
2613 	list_for_each_entry(sas_phy, &asd_sas_port->phy_list, port_phy_el) {
2614 		struct hisi_sas_phy *phy = sas_phy->lldd_phy;
2615 		struct hisi_hba *hisi_hba = phy->hisi_hba;
2616 		int phy_no = sas_phy->id;
2617 		u32 link_dfx2;
2618 
2619 		link_dfx2 = hisi_sas_phy_read32(hisi_hba, phy_no, LINK_DFX2);
2620 		if ((link_dfx2 == LINK_DFX2_RCVR_HOLD_STS_MSK) ||
2621 		    (link_dfx2 & LINK_DFX2_SEND_HOLD_STS_MSK)) {
2622 			u32 txid_auto;
2623 
2624 			txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no,
2625 							TXID_AUTO);
2626 			txid_auto |= TXID_AUTO_CTB_MSK;
2627 			hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
2628 					     txid_auto);
2629 			return;
2630 		}
2631 	}
2632 }
2633 
2634 static void prep_abort_v2_hw(struct hisi_hba *hisi_hba,
2635 		struct hisi_sas_slot *slot,
2636 		int device_id, int abort_flag, int tag_to_abort)
2637 {
2638 	struct sas_task *task = slot->task;
2639 	struct domain_device *dev = task->dev;
2640 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
2641 	struct hisi_sas_port *port = slot->port;
2642 	struct timer_list *timer = &slot->internal_abort_timer;
2643 
2644 	/* setup the quirk timer */
2645 	timer_setup(timer, hisi_sas_internal_abort_quirk_timeout, 0);
2646 	/* Set the timeout to 10ms less than internal abort timeout */
2647 	mod_timer(timer, jiffies + msecs_to_jiffies(100));
2648 
2649 	/* dw0 */
2650 	hdr->dw0 = cpu_to_le32((5 << CMD_HDR_CMD_OFF) | /*abort*/
2651 			       (port->id << CMD_HDR_PORT_OFF) |
2652 			       (dev_is_sata(dev) <<
2653 				CMD_HDR_ABORT_DEVICE_TYPE_OFF) |
2654 			       (abort_flag << CMD_HDR_ABORT_FLAG_OFF));
2655 
2656 	/* dw1 */
2657 	hdr->dw1 = cpu_to_le32(device_id << CMD_HDR_DEV_ID_OFF);
2658 
2659 	/* dw7 */
2660 	hdr->dw7 = cpu_to_le32(tag_to_abort << CMD_HDR_ABORT_IPTT_OFF);
2661 	hdr->transfer_tags = cpu_to_le32(slot->idx);
2662 }
2663 
2664 static int phy_up_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
2665 {
2666 	int i, res = IRQ_HANDLED;
2667 	u32 port_id, link_rate;
2668 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2669 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
2670 	struct device *dev = hisi_hba->dev;
2671 	u32 *frame_rcvd = (u32 *)sas_phy->frame_rcvd;
2672 	struct sas_identify_frame *id = (struct sas_identify_frame *)frame_rcvd;
2673 	unsigned long flags;
2674 
2675 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 1);
2676 
2677 	if (is_sata_phy_v2_hw(hisi_hba, phy_no))
2678 		goto end;
2679 
2680 	if (phy_no == 8) {
2681 		u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
2682 
2683 		port_id = (port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
2684 			  PORT_STATE_PHY8_PORT_NUM_OFF;
2685 		link_rate = (port_state & PORT_STATE_PHY8_CONN_RATE_MSK) >>
2686 			    PORT_STATE_PHY8_CONN_RATE_OFF;
2687 	} else {
2688 		port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
2689 		port_id = (port_id >> (4 * phy_no)) & 0xf;
2690 		link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
2691 		link_rate = (link_rate >> (phy_no * 4)) & 0xf;
2692 	}
2693 
2694 	if (port_id == 0xf) {
2695 		dev_err(dev, "phyup: phy%d invalid portid\n", phy_no);
2696 		res = IRQ_NONE;
2697 		goto end;
2698 	}
2699 
2700 	for (i = 0; i < 6; i++) {
2701 		u32 idaf = hisi_sas_phy_read32(hisi_hba, phy_no,
2702 					       RX_IDAF_DWORD0 + (i * 4));
2703 		frame_rcvd[i] = __swab32(idaf);
2704 	}
2705 
2706 	sas_phy->linkrate = link_rate;
2707 	sas_phy->oob_mode = SAS_OOB_MODE;
2708 	memcpy(sas_phy->attached_sas_addr, &id->sas_addr, SAS_ADDR_SIZE);
2709 	dev_info(dev, "phyup: phy%d link_rate=%d\n", phy_no, link_rate);
2710 	phy->port_id = port_id;
2711 	phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
2712 	phy->phy_type |= PORT_TYPE_SAS;
2713 	phy->phy_attached = 1;
2714 	phy->identify.device_type = id->dev_type;
2715 	phy->frame_rcvd_size =	sizeof(struct sas_identify_frame);
2716 	if (phy->identify.device_type == SAS_END_DEVICE)
2717 		phy->identify.target_port_protocols =
2718 			SAS_PROTOCOL_SSP;
2719 	else if (phy->identify.device_type != SAS_PHY_UNUSED) {
2720 		phy->identify.target_port_protocols =
2721 			SAS_PROTOCOL_SMP;
2722 		if (!timer_pending(&hisi_hba->timer))
2723 			set_link_timer_quirk(hisi_hba);
2724 	}
2725 	hisi_sas_notify_phy_event(phy, HISI_PHYE_PHY_UP);
2726 	spin_lock_irqsave(&phy->lock, flags);
2727 	if (phy->reset_completion) {
2728 		phy->in_reset = 0;
2729 		complete(phy->reset_completion);
2730 	}
2731 	spin_unlock_irqrestore(&phy->lock, flags);
2732 
2733 end:
2734 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
2735 			     CHL_INT0_SL_PHY_ENABLE_MSK);
2736 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 0);
2737 
2738 	return res;
2739 }
2740 
2741 static bool check_any_wideports_v2_hw(struct hisi_hba *hisi_hba)
2742 {
2743 	u32 port_state;
2744 
2745 	port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
2746 	if (port_state & 0x1ff)
2747 		return true;
2748 
2749 	return false;
2750 }
2751 
2752 static int phy_down_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
2753 {
2754 	u32 phy_state, sl_ctrl, txid_auto;
2755 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2756 	struct hisi_sas_port *port = phy->port;
2757 	struct device *dev = hisi_hba->dev;
2758 
2759 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 1);
2760 
2761 	phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
2762 	dev_info(dev, "phydown: phy%d phy_state=0x%x\n", phy_no, phy_state);
2763 	hisi_sas_phy_down(hisi_hba, phy_no, (phy_state & 1 << phy_no) ? 1 : 0);
2764 
2765 	sl_ctrl = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
2766 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL,
2767 			     sl_ctrl & ~SL_CONTROL_CTA_MSK);
2768 	if (port && !get_wideport_bitmap_v2_hw(hisi_hba, port->id))
2769 		if (!check_any_wideports_v2_hw(hisi_hba) &&
2770 				timer_pending(&hisi_hba->timer))
2771 			del_timer(&hisi_hba->timer);
2772 
2773 	txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
2774 	hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
2775 			     txid_auto | TXID_AUTO_CT3_MSK);
2776 
2777 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0, CHL_INT0_NOT_RDY_MSK);
2778 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 0);
2779 
2780 	return IRQ_HANDLED;
2781 }
2782 
2783 static irqreturn_t int_phy_updown_v2_hw(int irq_no, void *p)
2784 {
2785 	struct hisi_hba *hisi_hba = p;
2786 	u32 irq_msk;
2787 	int phy_no = 0;
2788 	irqreturn_t res = IRQ_NONE;
2789 
2790 	irq_msk = (hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO)
2791 		   >> HGC_INVLD_DQE_INFO_FB_CH0_OFF) & 0x1ff;
2792 	while (irq_msk) {
2793 		if (irq_msk  & 1) {
2794 			u32 reg_value = hisi_sas_phy_read32(hisi_hba, phy_no,
2795 					    CHL_INT0);
2796 
2797 			switch (reg_value & (CHL_INT0_NOT_RDY_MSK |
2798 					CHL_INT0_SL_PHY_ENABLE_MSK)) {
2799 
2800 			case CHL_INT0_SL_PHY_ENABLE_MSK:
2801 				/* phy up */
2802 				if (phy_up_v2_hw(phy_no, hisi_hba) ==
2803 				    IRQ_HANDLED)
2804 					res = IRQ_HANDLED;
2805 				break;
2806 
2807 			case CHL_INT0_NOT_RDY_MSK:
2808 				/* phy down */
2809 				if (phy_down_v2_hw(phy_no, hisi_hba) ==
2810 				    IRQ_HANDLED)
2811 					res = IRQ_HANDLED;
2812 				break;
2813 
2814 			case (CHL_INT0_NOT_RDY_MSK |
2815 					CHL_INT0_SL_PHY_ENABLE_MSK):
2816 				reg_value = hisi_sas_read32(hisi_hba,
2817 						PHY_STATE);
2818 				if (reg_value & BIT(phy_no)) {
2819 					/* phy up */
2820 					if (phy_up_v2_hw(phy_no, hisi_hba) ==
2821 					    IRQ_HANDLED)
2822 						res = IRQ_HANDLED;
2823 				} else {
2824 					/* phy down */
2825 					if (phy_down_v2_hw(phy_no, hisi_hba) ==
2826 					    IRQ_HANDLED)
2827 						res = IRQ_HANDLED;
2828 				}
2829 				break;
2830 
2831 			default:
2832 				break;
2833 			}
2834 
2835 		}
2836 		irq_msk >>= 1;
2837 		phy_no++;
2838 	}
2839 
2840 	return res;
2841 }
2842 
2843 static void phy_bcast_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
2844 {
2845 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2846 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
2847 	struct sas_ha_struct *sas_ha = &hisi_hba->sha;
2848 	u32 bcast_status;
2849 
2850 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 1);
2851 	bcast_status = hisi_sas_phy_read32(hisi_hba, phy_no, RX_PRIMS_STATUS);
2852 	if ((bcast_status & RX_BCAST_CHG_MSK) &&
2853 	    !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
2854 		sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
2855 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
2856 			     CHL_INT0_SL_RX_BCST_ACK_MSK);
2857 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 0);
2858 }
2859 
2860 static const struct hisi_sas_hw_error port_ecc_axi_error[] = {
2861 	{
2862 		.irq_msk = BIT(CHL_INT1_DMAC_TX_ECC_ERR_OFF),
2863 		.msg = "dmac_tx_ecc_bad_err",
2864 	},
2865 	{
2866 		.irq_msk = BIT(CHL_INT1_DMAC_RX_ECC_ERR_OFF),
2867 		.msg = "dmac_rx_ecc_bad_err",
2868 	},
2869 	{
2870 		.irq_msk = BIT(CHL_INT1_DMAC_TX_AXI_WR_ERR_OFF),
2871 		.msg = "dma_tx_axi_wr_err",
2872 	},
2873 	{
2874 		.irq_msk = BIT(CHL_INT1_DMAC_TX_AXI_RD_ERR_OFF),
2875 		.msg = "dma_tx_axi_rd_err",
2876 	},
2877 	{
2878 		.irq_msk = BIT(CHL_INT1_DMAC_RX_AXI_WR_ERR_OFF),
2879 		.msg = "dma_rx_axi_wr_err",
2880 	},
2881 	{
2882 		.irq_msk = BIT(CHL_INT1_DMAC_RX_AXI_RD_ERR_OFF),
2883 		.msg = "dma_rx_axi_rd_err",
2884 	},
2885 };
2886 
2887 static irqreturn_t int_chnl_int_v2_hw(int irq_no, void *p)
2888 {
2889 	struct hisi_hba *hisi_hba = p;
2890 	struct device *dev = hisi_hba->dev;
2891 	u32 ent_msk, ent_tmp, irq_msk;
2892 	int phy_no = 0;
2893 
2894 	ent_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK3);
2895 	ent_tmp = ent_msk;
2896 	ent_msk |= ENT_INT_SRC_MSK3_ENT95_MSK_MSK;
2897 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, ent_msk);
2898 
2899 	irq_msk = (hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO) >>
2900 			HGC_INVLD_DQE_INFO_FB_CH3_OFF) & 0x1ff;
2901 
2902 	while (irq_msk) {
2903 		u32 irq_value0 = hisi_sas_phy_read32(hisi_hba, phy_no,
2904 						     CHL_INT0);
2905 		u32 irq_value1 = hisi_sas_phy_read32(hisi_hba, phy_no,
2906 						     CHL_INT1);
2907 		u32 irq_value2 = hisi_sas_phy_read32(hisi_hba, phy_no,
2908 						     CHL_INT2);
2909 
2910 		if ((irq_msk & (1 << phy_no)) && irq_value1) {
2911 			int i;
2912 
2913 			for (i = 0; i < ARRAY_SIZE(port_ecc_axi_error); i++) {
2914 				const struct hisi_sas_hw_error *error =
2915 						&port_ecc_axi_error[i];
2916 
2917 				if (!(irq_value1 & error->irq_msk))
2918 					continue;
2919 
2920 				dev_warn(dev, "%s error (phy%d 0x%x) found!\n",
2921 					error->msg, phy_no, irq_value1);
2922 				queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2923 			}
2924 
2925 			hisi_sas_phy_write32(hisi_hba, phy_no,
2926 					     CHL_INT1, irq_value1);
2927 		}
2928 
2929 		if ((irq_msk & (1 << phy_no)) && irq_value2) {
2930 			struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2931 
2932 			if (irq_value2 & BIT(CHL_INT2_SL_IDAF_TOUT_CONF_OFF)) {
2933 				dev_warn(dev, "phy%d identify timeout\n",
2934 						phy_no);
2935 				hisi_sas_notify_phy_event(phy,
2936 						HISI_PHYE_LINK_RESET);
2937 			}
2938 
2939 			hisi_sas_phy_write32(hisi_hba, phy_no,
2940 						 CHL_INT2, irq_value2);
2941 		}
2942 
2943 		if ((irq_msk & (1 << phy_no)) && irq_value0) {
2944 			if (irq_value0 & CHL_INT0_SL_RX_BCST_ACK_MSK)
2945 				phy_bcast_v2_hw(phy_no, hisi_hba);
2946 
2947 			hisi_sas_phy_write32(hisi_hba, phy_no,
2948 					CHL_INT0, irq_value0
2949 					& (~CHL_INT0_HOTPLUG_TOUT_MSK)
2950 					& (~CHL_INT0_SL_PHY_ENABLE_MSK)
2951 					& (~CHL_INT0_NOT_RDY_MSK));
2952 		}
2953 		irq_msk &= ~(1 << phy_no);
2954 		phy_no++;
2955 	}
2956 
2957 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, ent_tmp);
2958 
2959 	return IRQ_HANDLED;
2960 }
2961 
2962 static void
2963 one_bit_ecc_error_process_v2_hw(struct hisi_hba *hisi_hba, u32 irq_value)
2964 {
2965 	struct device *dev = hisi_hba->dev;
2966 	const struct hisi_sas_hw_error *ecc_error;
2967 	u32 val;
2968 	int i;
2969 
2970 	for (i = 0; i < ARRAY_SIZE(one_bit_ecc_errors); i++) {
2971 		ecc_error = &one_bit_ecc_errors[i];
2972 		if (irq_value & ecc_error->irq_msk) {
2973 			val = hisi_sas_read32(hisi_hba, ecc_error->reg);
2974 			val &= ecc_error->msk;
2975 			val >>= ecc_error->shift;
2976 			dev_warn(dev, ecc_error->msg, val);
2977 		}
2978 	}
2979 }
2980 
2981 static void multi_bit_ecc_error_process_v2_hw(struct hisi_hba *hisi_hba,
2982 		u32 irq_value)
2983 {
2984 	struct device *dev = hisi_hba->dev;
2985 	const struct hisi_sas_hw_error *ecc_error;
2986 	u32 val;
2987 	int i;
2988 
2989 	for (i = 0; i < ARRAY_SIZE(multi_bit_ecc_errors); i++) {
2990 		ecc_error = &multi_bit_ecc_errors[i];
2991 		if (irq_value & ecc_error->irq_msk) {
2992 			val = hisi_sas_read32(hisi_hba, ecc_error->reg);
2993 			val &= ecc_error->msk;
2994 			val >>= ecc_error->shift;
2995 			dev_err(dev, ecc_error->msg, irq_value, val);
2996 			queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2997 		}
2998 	}
2999 
3000 	return;
3001 }
3002 
3003 static irqreturn_t fatal_ecc_int_v2_hw(int irq_no, void *p)
3004 {
3005 	struct hisi_hba *hisi_hba = p;
3006 	u32 irq_value, irq_msk;
3007 
3008 	irq_msk = hisi_sas_read32(hisi_hba, SAS_ECC_INTR_MSK);
3009 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, irq_msk | 0xffffffff);
3010 
3011 	irq_value = hisi_sas_read32(hisi_hba, SAS_ECC_INTR);
3012 	if (irq_value) {
3013 		one_bit_ecc_error_process_v2_hw(hisi_hba, irq_value);
3014 		multi_bit_ecc_error_process_v2_hw(hisi_hba, irq_value);
3015 	}
3016 
3017 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR, irq_value);
3018 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, irq_msk);
3019 
3020 	return IRQ_HANDLED;
3021 }
3022 
3023 static const struct hisi_sas_hw_error axi_error[] = {
3024 	{ .msk = BIT(0), .msg = "IOST_AXI_W_ERR" },
3025 	{ .msk = BIT(1), .msg = "IOST_AXI_R_ERR" },
3026 	{ .msk = BIT(2), .msg = "ITCT_AXI_W_ERR" },
3027 	{ .msk = BIT(3), .msg = "ITCT_AXI_R_ERR" },
3028 	{ .msk = BIT(4), .msg = "SATA_AXI_W_ERR" },
3029 	{ .msk = BIT(5), .msg = "SATA_AXI_R_ERR" },
3030 	{ .msk = BIT(6), .msg = "DQE_AXI_R_ERR" },
3031 	{ .msk = BIT(7), .msg = "CQE_AXI_W_ERR" },
3032 	{},
3033 };
3034 
3035 static const struct hisi_sas_hw_error fifo_error[] = {
3036 	{ .msk = BIT(8),  .msg = "CQE_WINFO_FIFO" },
3037 	{ .msk = BIT(9),  .msg = "CQE_MSG_FIFIO" },
3038 	{ .msk = BIT(10), .msg = "GETDQE_FIFO" },
3039 	{ .msk = BIT(11), .msg = "CMDP_FIFO" },
3040 	{ .msk = BIT(12), .msg = "AWTCTRL_FIFO" },
3041 	{},
3042 };
3043 
3044 static const struct hisi_sas_hw_error fatal_axi_errors[] = {
3045 	{
3046 		.irq_msk = BIT(ENT_INT_SRC3_WP_DEPTH_OFF),
3047 		.msg = "write pointer and depth",
3048 	},
3049 	{
3050 		.irq_msk = BIT(ENT_INT_SRC3_IPTT_SLOT_NOMATCH_OFF),
3051 		.msg = "iptt no match slot",
3052 	},
3053 	{
3054 		.irq_msk = BIT(ENT_INT_SRC3_RP_DEPTH_OFF),
3055 		.msg = "read pointer and depth",
3056 	},
3057 	{
3058 		.irq_msk = BIT(ENT_INT_SRC3_AXI_OFF),
3059 		.reg = HGC_AXI_FIFO_ERR_INFO,
3060 		.sub = axi_error,
3061 	},
3062 	{
3063 		.irq_msk = BIT(ENT_INT_SRC3_FIFO_OFF),
3064 		.reg = HGC_AXI_FIFO_ERR_INFO,
3065 		.sub = fifo_error,
3066 	},
3067 	{
3068 		.irq_msk = BIT(ENT_INT_SRC3_LM_OFF),
3069 		.msg = "LM add/fetch list",
3070 	},
3071 	{
3072 		.irq_msk = BIT(ENT_INT_SRC3_ABT_OFF),
3073 		.msg = "SAS_HGC_ABT fetch LM list",
3074 	},
3075 };
3076 
3077 static irqreturn_t fatal_axi_int_v2_hw(int irq_no, void *p)
3078 {
3079 	struct hisi_hba *hisi_hba = p;
3080 	u32 irq_value, irq_msk, err_value;
3081 	struct device *dev = hisi_hba->dev;
3082 	const struct hisi_sas_hw_error *axi_error;
3083 	int i;
3084 
3085 	irq_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK3);
3086 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, irq_msk | 0xfffffffe);
3087 
3088 	irq_value = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
3089 
3090 	for (i = 0; i < ARRAY_SIZE(fatal_axi_errors); i++) {
3091 		axi_error = &fatal_axi_errors[i];
3092 		if (!(irq_value & axi_error->irq_msk))
3093 			continue;
3094 
3095 		hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
3096 				 1 << axi_error->shift);
3097 		if (axi_error->sub) {
3098 			const struct hisi_sas_hw_error *sub = axi_error->sub;
3099 
3100 			err_value = hisi_sas_read32(hisi_hba, axi_error->reg);
3101 			for (; sub->msk || sub->msg; sub++) {
3102 				if (!(err_value & sub->msk))
3103 					continue;
3104 				dev_err(dev, "%s (0x%x) found!\n",
3105 					 sub->msg, irq_value);
3106 				queue_work(hisi_hba->wq, &hisi_hba->rst_work);
3107 			}
3108 		} else {
3109 			dev_err(dev, "%s (0x%x) found!\n",
3110 				 axi_error->msg, irq_value);
3111 			queue_work(hisi_hba->wq, &hisi_hba->rst_work);
3112 		}
3113 	}
3114 
3115 	if (irq_value & BIT(ENT_INT_SRC3_ITC_INT_OFF)) {
3116 		u32 reg_val = hisi_sas_read32(hisi_hba, ITCT_CLR);
3117 		u32 dev_id = reg_val & ITCT_DEV_MSK;
3118 		struct hisi_sas_device *sas_dev = &hisi_hba->devices[dev_id];
3119 
3120 		hisi_sas_write32(hisi_hba, ITCT_CLR, 0);
3121 		dev_dbg(dev, "clear ITCT ok\n");
3122 		complete(sas_dev->completion);
3123 	}
3124 
3125 	hisi_sas_write32(hisi_hba, ENT_INT_SRC3, irq_value);
3126 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, irq_msk);
3127 
3128 	return IRQ_HANDLED;
3129 }
3130 
3131 static void cq_tasklet_v2_hw(unsigned long val)
3132 {
3133 	struct hisi_sas_cq *cq = (struct hisi_sas_cq *)val;
3134 	struct hisi_hba *hisi_hba = cq->hisi_hba;
3135 	struct hisi_sas_slot *slot;
3136 	struct hisi_sas_itct *itct;
3137 	struct hisi_sas_complete_v2_hdr *complete_queue;
3138 	u32 rd_point = cq->rd_point, wr_point, dev_id;
3139 	int queue = cq->id;
3140 
3141 	if (unlikely(hisi_hba->reject_stp_links_msk))
3142 		phys_try_accept_stp_links_v2_hw(hisi_hba);
3143 
3144 	complete_queue = hisi_hba->complete_hdr[queue];
3145 
3146 	wr_point = hisi_sas_read32(hisi_hba, COMPL_Q_0_WR_PTR +
3147 				   (0x14 * queue));
3148 
3149 	while (rd_point != wr_point) {
3150 		struct hisi_sas_complete_v2_hdr *complete_hdr;
3151 		int iptt;
3152 
3153 		complete_hdr = &complete_queue[rd_point];
3154 
3155 		/* Check for NCQ completion */
3156 		if (complete_hdr->act) {
3157 			u32 act_tmp = le32_to_cpu(complete_hdr->act);
3158 			int ncq_tag_count = ffs(act_tmp);
3159 			u32 dw1 = le32_to_cpu(complete_hdr->dw1);
3160 
3161 			dev_id = (dw1 & CMPLT_HDR_DEV_ID_MSK) >>
3162 				 CMPLT_HDR_DEV_ID_OFF;
3163 			itct = &hisi_hba->itct[dev_id];
3164 
3165 			/* The NCQ tags are held in the itct header */
3166 			while (ncq_tag_count) {
3167 				__le64 *_ncq_tag = &itct->qw4_15[0], __ncq_tag;
3168 				u64 ncq_tag;
3169 
3170 				ncq_tag_count--;
3171 				__ncq_tag = _ncq_tag[ncq_tag_count / 5];
3172 				ncq_tag = le64_to_cpu(__ncq_tag);
3173 				iptt = (ncq_tag >> (ncq_tag_count % 5) * 12) &
3174 				       0xfff;
3175 
3176 				slot = &hisi_hba->slot_info[iptt];
3177 				slot->cmplt_queue_slot = rd_point;
3178 				slot->cmplt_queue = queue;
3179 				slot_complete_v2_hw(hisi_hba, slot);
3180 
3181 				act_tmp &= ~(1 << ncq_tag_count);
3182 				ncq_tag_count = ffs(act_tmp);
3183 			}
3184 		} else {
3185 			u32 dw1 = le32_to_cpu(complete_hdr->dw1);
3186 
3187 			iptt = dw1 & CMPLT_HDR_IPTT_MSK;
3188 			slot = &hisi_hba->slot_info[iptt];
3189 			slot->cmplt_queue_slot = rd_point;
3190 			slot->cmplt_queue = queue;
3191 			slot_complete_v2_hw(hisi_hba, slot);
3192 		}
3193 
3194 		if (++rd_point >= HISI_SAS_QUEUE_SLOTS)
3195 			rd_point = 0;
3196 	}
3197 
3198 	/* update rd_point */
3199 	cq->rd_point = rd_point;
3200 	hisi_sas_write32(hisi_hba, COMPL_Q_0_RD_PTR + (0x14 * queue), rd_point);
3201 }
3202 
3203 static irqreturn_t cq_interrupt_v2_hw(int irq_no, void *p)
3204 {
3205 	struct hisi_sas_cq *cq = p;
3206 	struct hisi_hba *hisi_hba = cq->hisi_hba;
3207 	int queue = cq->id;
3208 
3209 	hisi_sas_write32(hisi_hba, OQ_INT_SRC, 1 << queue);
3210 
3211 	tasklet_schedule(&cq->tasklet);
3212 
3213 	return IRQ_HANDLED;
3214 }
3215 
3216 static irqreturn_t sata_int_v2_hw(int irq_no, void *p)
3217 {
3218 	struct hisi_sas_phy *phy = p;
3219 	struct hisi_hba *hisi_hba = phy->hisi_hba;
3220 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
3221 	struct device *dev = hisi_hba->dev;
3222 	struct	hisi_sas_initial_fis *initial_fis;
3223 	struct dev_to_host_fis *fis;
3224 	u32 ent_tmp, ent_msk, ent_int, port_id, link_rate, hard_phy_linkrate;
3225 	irqreturn_t res = IRQ_HANDLED;
3226 	u8 attached_sas_addr[SAS_ADDR_SIZE] = {0};
3227 	unsigned long flags;
3228 	int phy_no, offset;
3229 
3230 	phy_no = sas_phy->id;
3231 	initial_fis = &hisi_hba->initial_fis[phy_no];
3232 	fis = &initial_fis->fis;
3233 
3234 	offset = 4 * (phy_no / 4);
3235 	ent_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK1 + offset);
3236 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1 + offset,
3237 			 ent_msk | 1 << ((phy_no % 4) * 8));
3238 
3239 	ent_int = hisi_sas_read32(hisi_hba, ENT_INT_SRC1 + offset);
3240 	ent_tmp = ent_int & (1 << (ENT_INT_SRC1_D2H_FIS_CH1_OFF *
3241 			     (phy_no % 4)));
3242 	ent_int >>= ENT_INT_SRC1_D2H_FIS_CH1_OFF * (phy_no % 4);
3243 	if ((ent_int & ENT_INT_SRC1_D2H_FIS_CH0_MSK) == 0) {
3244 		dev_warn(dev, "sata int: phy%d did not receive FIS\n", phy_no);
3245 		res = IRQ_NONE;
3246 		goto end;
3247 	}
3248 
3249 	/* check ERR bit of Status Register */
3250 	if (fis->status & ATA_ERR) {
3251 		dev_warn(dev, "sata int: phy%d FIS status: 0x%x\n", phy_no,
3252 				fis->status);
3253 		hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
3254 		res = IRQ_NONE;
3255 		goto end;
3256 	}
3257 
3258 	if (unlikely(phy_no == 8)) {
3259 		u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
3260 
3261 		port_id = (port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
3262 			  PORT_STATE_PHY8_PORT_NUM_OFF;
3263 		link_rate = (port_state & PORT_STATE_PHY8_CONN_RATE_MSK) >>
3264 			    PORT_STATE_PHY8_CONN_RATE_OFF;
3265 	} else {
3266 		port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
3267 		port_id = (port_id >> (4 * phy_no)) & 0xf;
3268 		link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
3269 		link_rate = (link_rate >> (phy_no * 4)) & 0xf;
3270 	}
3271 
3272 	if (port_id == 0xf) {
3273 		dev_err(dev, "sata int: phy%d invalid portid\n", phy_no);
3274 		res = IRQ_NONE;
3275 		goto end;
3276 	}
3277 
3278 	sas_phy->linkrate = link_rate;
3279 	hard_phy_linkrate = hisi_sas_phy_read32(hisi_hba, phy_no,
3280 						HARD_PHY_LINKRATE);
3281 	phy->maximum_linkrate = hard_phy_linkrate & 0xf;
3282 	phy->minimum_linkrate = (hard_phy_linkrate >> 4) & 0xf;
3283 
3284 	sas_phy->oob_mode = SATA_OOB_MODE;
3285 	/* Make up some unique SAS address */
3286 	attached_sas_addr[0] = 0x50;
3287 	attached_sas_addr[6] = hisi_hba->shost->host_no;
3288 	attached_sas_addr[7] = phy_no;
3289 	memcpy(sas_phy->attached_sas_addr, attached_sas_addr, SAS_ADDR_SIZE);
3290 	memcpy(sas_phy->frame_rcvd, fis, sizeof(struct dev_to_host_fis));
3291 	dev_info(dev, "sata int phyup: phy%d link_rate=%d\n", phy_no, link_rate);
3292 	phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
3293 	phy->port_id = port_id;
3294 	phy->phy_type |= PORT_TYPE_SATA;
3295 	phy->phy_attached = 1;
3296 	phy->identify.device_type = SAS_SATA_DEV;
3297 	phy->frame_rcvd_size = sizeof(struct dev_to_host_fis);
3298 	phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
3299 	hisi_sas_notify_phy_event(phy, HISI_PHYE_PHY_UP);
3300 
3301 	spin_lock_irqsave(&phy->lock, flags);
3302 	if (phy->reset_completion) {
3303 		phy->in_reset = 0;
3304 		complete(phy->reset_completion);
3305 	}
3306 	spin_unlock_irqrestore(&phy->lock, flags);
3307 end:
3308 	hisi_sas_write32(hisi_hba, ENT_INT_SRC1 + offset, ent_tmp);
3309 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1 + offset, ent_msk);
3310 
3311 	return res;
3312 }
3313 
3314 static irq_handler_t phy_interrupts[HISI_SAS_PHY_INT_NR] = {
3315 	int_phy_updown_v2_hw,
3316 	int_chnl_int_v2_hw,
3317 };
3318 
3319 static irq_handler_t fatal_interrupts[HISI_SAS_FATAL_INT_NR] = {
3320 	fatal_ecc_int_v2_hw,
3321 	fatal_axi_int_v2_hw
3322 };
3323 
3324 /**
3325  * There is a limitation in the hip06 chipset that we need
3326  * to map in all mbigen interrupts, even if they are not used.
3327  */
3328 static int interrupt_init_v2_hw(struct hisi_hba *hisi_hba)
3329 {
3330 	struct platform_device *pdev = hisi_hba->platform_dev;
3331 	struct device *dev = &pdev->dev;
3332 	int irq, rc, irq_map[128];
3333 	int i, phy_no, fatal_no, queue_no, k;
3334 
3335 	for (i = 0; i < 128; i++)
3336 		irq_map[i] = platform_get_irq(pdev, i);
3337 
3338 	for (i = 0; i < HISI_SAS_PHY_INT_NR; i++) {
3339 		irq = irq_map[i + 1]; /* Phy up/down is irq1 */
3340 		rc = devm_request_irq(dev, irq, phy_interrupts[i], 0,
3341 				      DRV_NAME " phy", hisi_hba);
3342 		if (rc) {
3343 			dev_err(dev, "irq init: could not request "
3344 				"phy interrupt %d, rc=%d\n",
3345 				irq, rc);
3346 			rc = -ENOENT;
3347 			goto free_phy_int_irqs;
3348 		}
3349 	}
3350 
3351 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
3352 		struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
3353 
3354 		irq = irq_map[phy_no + 72];
3355 		rc = devm_request_irq(dev, irq, sata_int_v2_hw, 0,
3356 				      DRV_NAME " sata", phy);
3357 		if (rc) {
3358 			dev_err(dev, "irq init: could not request "
3359 				"sata interrupt %d, rc=%d\n",
3360 				irq, rc);
3361 			rc = -ENOENT;
3362 			goto free_sata_int_irqs;
3363 		}
3364 	}
3365 
3366 	for (fatal_no = 0; fatal_no < HISI_SAS_FATAL_INT_NR; fatal_no++) {
3367 		irq = irq_map[fatal_no + 81];
3368 		rc = devm_request_irq(dev, irq, fatal_interrupts[fatal_no], 0,
3369 				      DRV_NAME " fatal", hisi_hba);
3370 		if (rc) {
3371 			dev_err(dev,
3372 				"irq init: could not request fatal interrupt %d, rc=%d\n",
3373 				irq, rc);
3374 			rc = -ENOENT;
3375 			goto free_fatal_int_irqs;
3376 		}
3377 	}
3378 
3379 	for (queue_no = 0; queue_no < hisi_hba->queue_count; queue_no++) {
3380 		struct hisi_sas_cq *cq = &hisi_hba->cq[queue_no];
3381 		struct tasklet_struct *t = &cq->tasklet;
3382 
3383 		irq = irq_map[queue_no + 96];
3384 		rc = devm_request_irq(dev, irq, cq_interrupt_v2_hw, 0,
3385 				      DRV_NAME " cq", cq);
3386 		if (rc) {
3387 			dev_err(dev,
3388 				"irq init: could not request cq interrupt %d, rc=%d\n",
3389 				irq, rc);
3390 			rc = -ENOENT;
3391 			goto free_cq_int_irqs;
3392 		}
3393 		tasklet_init(t, cq_tasklet_v2_hw, (unsigned long)cq);
3394 	}
3395 
3396 	return 0;
3397 
3398 free_cq_int_irqs:
3399 	for (k = 0; k < queue_no; k++) {
3400 		struct hisi_sas_cq *cq = &hisi_hba->cq[k];
3401 
3402 		free_irq(irq_map[k + 96], cq);
3403 		tasklet_kill(&cq->tasklet);
3404 	}
3405 free_fatal_int_irqs:
3406 	for (k = 0; k < fatal_no; k++)
3407 		free_irq(irq_map[k + 81], hisi_hba);
3408 free_sata_int_irqs:
3409 	for (k = 0; k < phy_no; k++) {
3410 		struct hisi_sas_phy *phy = &hisi_hba->phy[k];
3411 
3412 		free_irq(irq_map[k + 72], phy);
3413 	}
3414 free_phy_int_irqs:
3415 	for (k = 0; k < i; k++)
3416 		free_irq(irq_map[k + 1], hisi_hba);
3417 	return rc;
3418 }
3419 
3420 static int hisi_sas_v2_init(struct hisi_hba *hisi_hba)
3421 {
3422 	int rc;
3423 
3424 	memset(hisi_hba->sata_dev_bitmap, 0, sizeof(hisi_hba->sata_dev_bitmap));
3425 
3426 	rc = hw_init_v2_hw(hisi_hba);
3427 	if (rc)
3428 		return rc;
3429 
3430 	rc = interrupt_init_v2_hw(hisi_hba);
3431 	if (rc)
3432 		return rc;
3433 
3434 	return 0;
3435 }
3436 
3437 static void interrupt_disable_v2_hw(struct hisi_hba *hisi_hba)
3438 {
3439 	struct platform_device *pdev = hisi_hba->platform_dev;
3440 	int i;
3441 
3442 	for (i = 0; i < hisi_hba->queue_count; i++)
3443 		hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK + 0x4 * i, 0x1);
3444 
3445 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0xffffffff);
3446 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0xffffffff);
3447 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0xffffffff);
3448 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xffffffff);
3449 
3450 	for (i = 0; i < hisi_hba->n_phy; i++) {
3451 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xffffffff);
3452 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0xffffffff);
3453 	}
3454 
3455 	for (i = 0; i < 128; i++)
3456 		synchronize_irq(platform_get_irq(pdev, i));
3457 }
3458 
3459 
3460 static u32 get_phys_state_v2_hw(struct hisi_hba *hisi_hba)
3461 {
3462 	return hisi_sas_read32(hisi_hba, PHY_STATE);
3463 }
3464 
3465 static int soft_reset_v2_hw(struct hisi_hba *hisi_hba)
3466 {
3467 	struct device *dev = hisi_hba->dev;
3468 	int rc, cnt;
3469 
3470 	interrupt_disable_v2_hw(hisi_hba);
3471 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0x0);
3472 	hisi_sas_kill_tasklets(hisi_hba);
3473 
3474 	hisi_sas_stop_phys(hisi_hba);
3475 
3476 	mdelay(10);
3477 
3478 	hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE + AM_CTRL_GLOBAL, 0x1);
3479 
3480 	/* wait until bus idle */
3481 	cnt = 0;
3482 	while (1) {
3483 		u32 status = hisi_sas_read32_relaxed(hisi_hba,
3484 				AXI_MASTER_CFG_BASE + AM_CURR_TRANS_RETURN);
3485 
3486 		if (status == 0x3)
3487 			break;
3488 
3489 		udelay(10);
3490 		if (cnt++ > 10) {
3491 			dev_err(dev, "wait axi bus state to idle timeout!\n");
3492 			return -1;
3493 		}
3494 	}
3495 
3496 	hisi_sas_init_mem(hisi_hba);
3497 
3498 	rc = hw_init_v2_hw(hisi_hba);
3499 	if (rc)
3500 		return rc;
3501 
3502 	phys_reject_stp_links_v2_hw(hisi_hba);
3503 
3504 	return 0;
3505 }
3506 
3507 static int write_gpio_v2_hw(struct hisi_hba *hisi_hba, u8 reg_type,
3508 			u8 reg_index, u8 reg_count, u8 *write_data)
3509 {
3510 	struct device *dev = hisi_hba->dev;
3511 	int phy_no, count;
3512 
3513 	if (!hisi_hba->sgpio_regs)
3514 		return -EOPNOTSUPP;
3515 
3516 	switch (reg_type) {
3517 	case SAS_GPIO_REG_TX:
3518 		count = reg_count * 4;
3519 		count = min(count, hisi_hba->n_phy);
3520 
3521 		for (phy_no = 0; phy_no < count; phy_no++) {
3522 			/*
3523 			 * GPIO_TX[n] register has the highest numbered drive
3524 			 * of the four in the first byte and the lowest
3525 			 * numbered drive in the fourth byte.
3526 			 * See SFF-8485 Rev. 0.7 Table 24.
3527 			 */
3528 			void __iomem  *reg_addr = hisi_hba->sgpio_regs +
3529 					reg_index * 4 + phy_no;
3530 			int data_idx = phy_no + 3 - (phy_no % 4) * 2;
3531 
3532 			writeb(write_data[data_idx], reg_addr);
3533 		}
3534 
3535 		break;
3536 	default:
3537 		dev_err(dev, "write gpio: unsupported or bad reg type %d\n",
3538 				reg_type);
3539 		return -EINVAL;
3540 	}
3541 
3542 	return 0;
3543 }
3544 
3545 static void wait_cmds_complete_timeout_v2_hw(struct hisi_hba *hisi_hba,
3546 					     int delay_ms, int timeout_ms)
3547 {
3548 	struct device *dev = hisi_hba->dev;
3549 	int entries, entries_old = 0, time;
3550 
3551 	for (time = 0; time < timeout_ms; time += delay_ms) {
3552 		entries = hisi_sas_read32(hisi_hba, CQE_SEND_CNT);
3553 		if (entries == entries_old)
3554 			break;
3555 
3556 		entries_old = entries;
3557 		msleep(delay_ms);
3558 	}
3559 
3560 	dev_dbg(dev, "wait commands complete %dms\n", time);
3561 }
3562 
3563 static struct device_attribute *host_attrs_v2_hw[] = {
3564 	&dev_attr_phy_event_threshold,
3565 	NULL
3566 };
3567 
3568 static struct scsi_host_template sht_v2_hw = {
3569 	.name			= DRV_NAME,
3570 	.module			= THIS_MODULE,
3571 	.queuecommand		= sas_queuecommand,
3572 	.target_alloc		= sas_target_alloc,
3573 	.slave_configure	= hisi_sas_slave_configure,
3574 	.scan_finished		= hisi_sas_scan_finished,
3575 	.scan_start		= hisi_sas_scan_start,
3576 	.change_queue_depth	= sas_change_queue_depth,
3577 	.bios_param		= sas_bios_param,
3578 	.this_id		= -1,
3579 	.sg_tablesize		= HISI_SAS_SGE_PAGE_CNT,
3580 	.max_sectors		= SCSI_DEFAULT_MAX_SECTORS,
3581 	.eh_device_reset_handler = sas_eh_device_reset_handler,
3582 	.eh_target_reset_handler = sas_eh_target_reset_handler,
3583 	.target_destroy		= sas_target_destroy,
3584 	.ioctl			= sas_ioctl,
3585 	.shost_attrs		= host_attrs_v2_hw,
3586 };
3587 
3588 static const struct hisi_sas_hw hisi_sas_v2_hw = {
3589 	.hw_init = hisi_sas_v2_init,
3590 	.setup_itct = setup_itct_v2_hw,
3591 	.slot_index_alloc = slot_index_alloc_quirk_v2_hw,
3592 	.alloc_dev = alloc_dev_quirk_v2_hw,
3593 	.sl_notify = sl_notify_v2_hw,
3594 	.get_wideport_bitmap = get_wideport_bitmap_v2_hw,
3595 	.clear_itct = clear_itct_v2_hw,
3596 	.free_device = free_device_v2_hw,
3597 	.prep_smp = prep_smp_v2_hw,
3598 	.prep_ssp = prep_ssp_v2_hw,
3599 	.prep_stp = prep_ata_v2_hw,
3600 	.prep_abort = prep_abort_v2_hw,
3601 	.get_free_slot = get_free_slot_v2_hw,
3602 	.start_delivery = start_delivery_v2_hw,
3603 	.slot_complete = slot_complete_v2_hw,
3604 	.phys_init = phys_init_v2_hw,
3605 	.phy_start = start_phy_v2_hw,
3606 	.phy_disable = disable_phy_v2_hw,
3607 	.phy_hard_reset = phy_hard_reset_v2_hw,
3608 	.get_events = phy_get_events_v2_hw,
3609 	.phy_set_linkrate = phy_set_linkrate_v2_hw,
3610 	.phy_get_max_linkrate = phy_get_max_linkrate_v2_hw,
3611 	.max_command_entries = HISI_SAS_COMMAND_ENTRIES_V2_HW,
3612 	.complete_hdr_size = sizeof(struct hisi_sas_complete_v2_hdr),
3613 	.soft_reset = soft_reset_v2_hw,
3614 	.get_phys_state = get_phys_state_v2_hw,
3615 	.write_gpio = write_gpio_v2_hw,
3616 	.wait_cmds_complete_timeout = wait_cmds_complete_timeout_v2_hw,
3617 	.sht = &sht_v2_hw,
3618 };
3619 
3620 static int hisi_sas_v2_probe(struct platform_device *pdev)
3621 {
3622 	/*
3623 	 * Check if we should defer the probe before we probe the
3624 	 * upper layer, as it's hard to defer later on.
3625 	 */
3626 	int ret = platform_get_irq(pdev, 0);
3627 
3628 	if (ret < 0) {
3629 		if (ret != -EPROBE_DEFER)
3630 			dev_err(&pdev->dev, "cannot obtain irq\n");
3631 		return ret;
3632 	}
3633 
3634 	return hisi_sas_probe(pdev, &hisi_sas_v2_hw);
3635 }
3636 
3637 static int hisi_sas_v2_remove(struct platform_device *pdev)
3638 {
3639 	struct sas_ha_struct *sha = platform_get_drvdata(pdev);
3640 	struct hisi_hba *hisi_hba = sha->lldd_ha;
3641 
3642 	hisi_sas_kill_tasklets(hisi_hba);
3643 
3644 	return hisi_sas_remove(pdev);
3645 }
3646 
3647 static const struct of_device_id sas_v2_of_match[] = {
3648 	{ .compatible = "hisilicon,hip06-sas-v2",},
3649 	{ .compatible = "hisilicon,hip07-sas-v2",},
3650 	{},
3651 };
3652 MODULE_DEVICE_TABLE(of, sas_v2_of_match);
3653 
3654 static const struct acpi_device_id sas_v2_acpi_match[] = {
3655 	{ "HISI0162", 0 },
3656 	{ }
3657 };
3658 
3659 MODULE_DEVICE_TABLE(acpi, sas_v2_acpi_match);
3660 
3661 static struct platform_driver hisi_sas_v2_driver = {
3662 	.probe = hisi_sas_v2_probe,
3663 	.remove = hisi_sas_v2_remove,
3664 	.driver = {
3665 		.name = DRV_NAME,
3666 		.of_match_table = sas_v2_of_match,
3667 		.acpi_match_table = ACPI_PTR(sas_v2_acpi_match),
3668 	},
3669 };
3670 
3671 module_platform_driver(hisi_sas_v2_driver);
3672 
3673 MODULE_LICENSE("GPL");
3674 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
3675 MODULE_DESCRIPTION("HISILICON SAS controller v2 hw driver");
3676 MODULE_ALIAS("platform:" DRV_NAME);
3677