1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Hisilicon Limited.
4  */
5 
6 #include "hisi_sas.h"
7 #define DRV_NAME "hisi_sas_v3_hw"
8 
9 /* global registers need init */
10 #define DLVRY_QUEUE_ENABLE		0x0
11 #define IOST_BASE_ADDR_LO		0x8
12 #define IOST_BASE_ADDR_HI		0xc
13 #define ITCT_BASE_ADDR_LO		0x10
14 #define ITCT_BASE_ADDR_HI		0x14
15 #define IO_BROKEN_MSG_ADDR_LO		0x18
16 #define IO_BROKEN_MSG_ADDR_HI		0x1c
17 #define PHY_CONTEXT			0x20
18 #define PHY_STATE			0x24
19 #define PHY_PORT_NUM_MA			0x28
20 #define PHY_CONN_RATE			0x30
21 #define ITCT_CLR			0x44
22 #define ITCT_CLR_EN_OFF			16
23 #define ITCT_CLR_EN_MSK			(0x1 << ITCT_CLR_EN_OFF)
24 #define ITCT_DEV_OFF			0
25 #define ITCT_DEV_MSK			(0x7ff << ITCT_DEV_OFF)
26 #define SAS_AXI_USER3			0x50
27 #define IO_SATA_BROKEN_MSG_ADDR_LO	0x58
28 #define IO_SATA_BROKEN_MSG_ADDR_HI	0x5c
29 #define SATA_INITI_D2H_STORE_ADDR_LO	0x60
30 #define SATA_INITI_D2H_STORE_ADDR_HI	0x64
31 #define CFG_MAX_TAG			0x68
32 #define HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL	0x84
33 #define HGC_SAS_TXFAIL_RETRY_CTRL	0x88
34 #define HGC_GET_ITV_TIME		0x90
35 #define DEVICE_MSG_WORK_MODE		0x94
36 #define OPENA_WT_CONTI_TIME		0x9c
37 #define I_T_NEXUS_LOSS_TIME		0xa0
38 #define MAX_CON_TIME_LIMIT_TIME		0xa4
39 #define BUS_INACTIVE_LIMIT_TIME		0xa8
40 #define REJECT_TO_OPEN_LIMIT_TIME	0xac
41 #define CQ_INT_CONVERGE_EN		0xb0
42 #define CFG_AGING_TIME			0xbc
43 #define HGC_DFX_CFG2			0xc0
44 #define CFG_ABT_SET_QUERY_IPTT	0xd4
45 #define CFG_SET_ABORTED_IPTT_OFF	0
46 #define CFG_SET_ABORTED_IPTT_MSK	(0xfff << CFG_SET_ABORTED_IPTT_OFF)
47 #define CFG_SET_ABORTED_EN_OFF	12
48 #define CFG_ABT_SET_IPTT_DONE	0xd8
49 #define CFG_ABT_SET_IPTT_DONE_OFF	0
50 #define HGC_IOMB_PROC1_STATUS	0x104
51 #define HGC_LM_DFX_STATUS2		0x128
52 #define HGC_LM_DFX_STATUS2_IOSTLIST_OFF		0
53 #define HGC_LM_DFX_STATUS2_IOSTLIST_MSK	(0xfff << \
54 					 HGC_LM_DFX_STATUS2_IOSTLIST_OFF)
55 #define HGC_LM_DFX_STATUS2_ITCTLIST_OFF		12
56 #define HGC_LM_DFX_STATUS2_ITCTLIST_MSK	(0x7ff << \
57 					 HGC_LM_DFX_STATUS2_ITCTLIST_OFF)
58 #define HGC_CQE_ECC_ADDR		0x13c
59 #define HGC_CQE_ECC_1B_ADDR_OFF	0
60 #define HGC_CQE_ECC_1B_ADDR_MSK	(0x3f << HGC_CQE_ECC_1B_ADDR_OFF)
61 #define HGC_CQE_ECC_MB_ADDR_OFF	8
62 #define HGC_CQE_ECC_MB_ADDR_MSK (0x3f << HGC_CQE_ECC_MB_ADDR_OFF)
63 #define HGC_IOST_ECC_ADDR		0x140
64 #define HGC_IOST_ECC_1B_ADDR_OFF	0
65 #define HGC_IOST_ECC_1B_ADDR_MSK	(0x3ff << HGC_IOST_ECC_1B_ADDR_OFF)
66 #define HGC_IOST_ECC_MB_ADDR_OFF	16
67 #define HGC_IOST_ECC_MB_ADDR_MSK	(0x3ff << HGC_IOST_ECC_MB_ADDR_OFF)
68 #define HGC_DQE_ECC_ADDR		0x144
69 #define HGC_DQE_ECC_1B_ADDR_OFF	0
70 #define HGC_DQE_ECC_1B_ADDR_MSK	(0xfff << HGC_DQE_ECC_1B_ADDR_OFF)
71 #define HGC_DQE_ECC_MB_ADDR_OFF	16
72 #define HGC_DQE_ECC_MB_ADDR_MSK (0xfff << HGC_DQE_ECC_MB_ADDR_OFF)
73 #define CHNL_INT_STATUS			0x148
74 #define TAB_DFX				0x14c
75 #define HGC_ITCT_ECC_ADDR		0x150
76 #define HGC_ITCT_ECC_1B_ADDR_OFF		0
77 #define HGC_ITCT_ECC_1B_ADDR_MSK		(0x3ff << \
78 						 HGC_ITCT_ECC_1B_ADDR_OFF)
79 #define HGC_ITCT_ECC_MB_ADDR_OFF		16
80 #define HGC_ITCT_ECC_MB_ADDR_MSK		(0x3ff << \
81 						 HGC_ITCT_ECC_MB_ADDR_OFF)
82 #define HGC_AXI_FIFO_ERR_INFO  0x154
83 #define AXI_ERR_INFO_OFF               0
84 #define AXI_ERR_INFO_MSK               (0xff << AXI_ERR_INFO_OFF)
85 #define FIFO_ERR_INFO_OFF              8
86 #define FIFO_ERR_INFO_MSK              (0xff << FIFO_ERR_INFO_OFF)
87 #define TAB_RD_TYPE			0x15c
88 #define INT_COAL_EN			0x19c
89 #define OQ_INT_COAL_TIME		0x1a0
90 #define OQ_INT_COAL_CNT			0x1a4
91 #define ENT_INT_COAL_TIME		0x1a8
92 #define ENT_INT_COAL_CNT		0x1ac
93 #define OQ_INT_SRC			0x1b0
94 #define OQ_INT_SRC_MSK			0x1b4
95 #define ENT_INT_SRC1			0x1b8
96 #define ENT_INT_SRC1_D2H_FIS_CH0_OFF	0
97 #define ENT_INT_SRC1_D2H_FIS_CH0_MSK	(0x1 << ENT_INT_SRC1_D2H_FIS_CH0_OFF)
98 #define ENT_INT_SRC1_D2H_FIS_CH1_OFF	8
99 #define ENT_INT_SRC1_D2H_FIS_CH1_MSK	(0x1 << ENT_INT_SRC1_D2H_FIS_CH1_OFF)
100 #define ENT_INT_SRC2			0x1bc
101 #define ENT_INT_SRC3			0x1c0
102 #define ENT_INT_SRC3_WP_DEPTH_OFF		8
103 #define ENT_INT_SRC3_IPTT_SLOT_NOMATCH_OFF	9
104 #define ENT_INT_SRC3_RP_DEPTH_OFF		10
105 #define ENT_INT_SRC3_AXI_OFF			11
106 #define ENT_INT_SRC3_FIFO_OFF			12
107 #define ENT_INT_SRC3_LM_OFF				14
108 #define ENT_INT_SRC3_ITC_INT_OFF	15
109 #define ENT_INT_SRC3_ITC_INT_MSK	(0x1 << ENT_INT_SRC3_ITC_INT_OFF)
110 #define ENT_INT_SRC3_ABT_OFF		16
111 #define ENT_INT_SRC3_DQE_POISON_OFF	18
112 #define ENT_INT_SRC3_IOST_POISON_OFF	19
113 #define ENT_INT_SRC3_ITCT_POISON_OFF	20
114 #define ENT_INT_SRC3_ITCT_NCQ_POISON_OFF	21
115 #define ENT_INT_SRC_MSK1		0x1c4
116 #define ENT_INT_SRC_MSK2		0x1c8
117 #define ENT_INT_SRC_MSK3		0x1cc
118 #define ENT_INT_SRC_MSK3_ENT95_MSK_OFF	31
119 #define CHNL_PHYUPDOWN_INT_MSK		0x1d0
120 #define CHNL_ENT_INT_MSK			0x1d4
121 #define HGC_COM_INT_MSK				0x1d8
122 #define ENT_INT_SRC_MSK3_ENT95_MSK_MSK	(0x1 << ENT_INT_SRC_MSK3_ENT95_MSK_OFF)
123 #define SAS_ECC_INTR			0x1e8
124 #define SAS_ECC_INTR_DQE_ECC_1B_OFF		0
125 #define SAS_ECC_INTR_DQE_ECC_MB_OFF		1
126 #define SAS_ECC_INTR_IOST_ECC_1B_OFF	2
127 #define SAS_ECC_INTR_IOST_ECC_MB_OFF	3
128 #define SAS_ECC_INTR_ITCT_ECC_1B_OFF	4
129 #define SAS_ECC_INTR_ITCT_ECC_MB_OFF	5
130 #define SAS_ECC_INTR_ITCTLIST_ECC_1B_OFF	6
131 #define SAS_ECC_INTR_ITCTLIST_ECC_MB_OFF	7
132 #define SAS_ECC_INTR_IOSTLIST_ECC_1B_OFF	8
133 #define SAS_ECC_INTR_IOSTLIST_ECC_MB_OFF	9
134 #define SAS_ECC_INTR_CQE_ECC_1B_OFF		10
135 #define SAS_ECC_INTR_CQE_ECC_MB_OFF		11
136 #define SAS_ECC_INTR_NCQ_MEM0_ECC_1B_OFF	12
137 #define SAS_ECC_INTR_NCQ_MEM0_ECC_MB_OFF	13
138 #define SAS_ECC_INTR_NCQ_MEM1_ECC_1B_OFF	14
139 #define SAS_ECC_INTR_NCQ_MEM1_ECC_MB_OFF	15
140 #define SAS_ECC_INTR_NCQ_MEM2_ECC_1B_OFF	16
141 #define SAS_ECC_INTR_NCQ_MEM2_ECC_MB_OFF	17
142 #define SAS_ECC_INTR_NCQ_MEM3_ECC_1B_OFF	18
143 #define SAS_ECC_INTR_NCQ_MEM3_ECC_MB_OFF	19
144 #define SAS_ECC_INTR_OOO_RAM_ECC_1B_OFF		20
145 #define SAS_ECC_INTR_OOO_RAM_ECC_MB_OFF		21
146 #define SAS_ECC_INTR_MSK		0x1ec
147 #define HGC_ERR_STAT_EN			0x238
148 #define CQE_SEND_CNT			0x248
149 #define DLVRY_Q_0_BASE_ADDR_LO		0x260
150 #define DLVRY_Q_0_BASE_ADDR_HI		0x264
151 #define DLVRY_Q_0_DEPTH			0x268
152 #define DLVRY_Q_0_WR_PTR		0x26c
153 #define DLVRY_Q_0_RD_PTR		0x270
154 #define HYPER_STREAM_ID_EN_CFG		0xc80
155 #define OQ0_INT_SRC_MSK			0xc90
156 #define COMPL_Q_0_BASE_ADDR_LO		0x4e0
157 #define COMPL_Q_0_BASE_ADDR_HI		0x4e4
158 #define COMPL_Q_0_DEPTH			0x4e8
159 #define COMPL_Q_0_WR_PTR		0x4ec
160 #define COMPL_Q_0_RD_PTR		0x4f0
161 #define HGC_RXM_DFX_STATUS14		0xae8
162 #define HGC_RXM_DFX_STATUS14_MEM0_OFF	0
163 #define HGC_RXM_DFX_STATUS14_MEM0_MSK	(0x1ff << \
164 					 HGC_RXM_DFX_STATUS14_MEM0_OFF)
165 #define HGC_RXM_DFX_STATUS14_MEM1_OFF	9
166 #define HGC_RXM_DFX_STATUS14_MEM1_MSK	(0x1ff << \
167 					 HGC_RXM_DFX_STATUS14_MEM1_OFF)
168 #define HGC_RXM_DFX_STATUS14_MEM2_OFF	18
169 #define HGC_RXM_DFX_STATUS14_MEM2_MSK	(0x1ff << \
170 					 HGC_RXM_DFX_STATUS14_MEM2_OFF)
171 #define HGC_RXM_DFX_STATUS15		0xaec
172 #define HGC_RXM_DFX_STATUS15_MEM3_OFF	0
173 #define HGC_RXM_DFX_STATUS15_MEM3_MSK	(0x1ff << \
174 					 HGC_RXM_DFX_STATUS15_MEM3_OFF)
175 #define AWQOS_AWCACHE_CFG	0xc84
176 #define ARQOS_ARCACHE_CFG	0xc88
177 #define HILINK_ERR_DFX		0xe04
178 #define SAS_GPIO_CFG_0		0x1000
179 #define SAS_GPIO_CFG_1		0x1004
180 #define SAS_GPIO_TX_0_1	0x1040
181 #define SAS_CFG_DRIVE_VLD	0x1070
182 
183 /* phy registers requiring init */
184 #define PORT_BASE			(0x2000)
185 #define PHY_CFG				(PORT_BASE + 0x0)
186 #define HARD_PHY_LINKRATE		(PORT_BASE + 0x4)
187 #define PHY_CFG_ENA_OFF			0
188 #define PHY_CFG_ENA_MSK			(0x1 << PHY_CFG_ENA_OFF)
189 #define PHY_CFG_DC_OPT_OFF		2
190 #define PHY_CFG_DC_OPT_MSK		(0x1 << PHY_CFG_DC_OPT_OFF)
191 #define PHY_CFG_PHY_RST_OFF		3
192 #define PHY_CFG_PHY_RST_MSK		(0x1 << PHY_CFG_PHY_RST_OFF)
193 #define PROG_PHY_LINK_RATE		(PORT_BASE + 0x8)
194 #define CFG_PROG_PHY_LINK_RATE_OFF	0
195 #define CFG_PROG_PHY_LINK_RATE_MSK	(0xff << CFG_PROG_PHY_LINK_RATE_OFF)
196 #define CFG_PROG_OOB_PHY_LINK_RATE_OFF	8
197 #define CFG_PROG_OOB_PHY_LINK_RATE_MSK	(0xf << CFG_PROG_OOB_PHY_LINK_RATE_OFF)
198 #define PHY_CTRL			(PORT_BASE + 0x14)
199 #define PHY_CTRL_RESET_OFF		0
200 #define PHY_CTRL_RESET_MSK		(0x1 << PHY_CTRL_RESET_OFF)
201 #define CMD_HDR_PIR_OFF			8
202 #define CMD_HDR_PIR_MSK			(0x1 << CMD_HDR_PIR_OFF)
203 #define SERDES_CFG			(PORT_BASE + 0x1c)
204 #define CFG_ALOS_CHK_DISABLE_OFF	9
205 #define CFG_ALOS_CHK_DISABLE_MSK	(0x1 << CFG_ALOS_CHK_DISABLE_OFF)
206 #define SAS_PHY_BIST_CTRL		(PORT_BASE + 0x2c)
207 #define CFG_BIST_MODE_SEL_OFF		0
208 #define CFG_BIST_MODE_SEL_MSK		(0xf << CFG_BIST_MODE_SEL_OFF)
209 #define CFG_LOOP_TEST_MODE_OFF		14
210 #define CFG_LOOP_TEST_MODE_MSK		(0x3 << CFG_LOOP_TEST_MODE_OFF)
211 #define CFG_RX_BIST_EN_OFF		16
212 #define CFG_RX_BIST_EN_MSK		(0x1 << CFG_RX_BIST_EN_OFF)
213 #define CFG_TX_BIST_EN_OFF		17
214 #define CFG_TX_BIST_EN_MSK		(0x1 << CFG_TX_BIST_EN_OFF)
215 #define CFG_BIST_TEST_OFF		18
216 #define CFG_BIST_TEST_MSK		(0x1 << CFG_BIST_TEST_OFF)
217 #define SAS_PHY_BIST_CODE		(PORT_BASE + 0x30)
218 #define SAS_PHY_BIST_CODE1		(PORT_BASE + 0x34)
219 #define SAS_BIST_ERR_CNT		(PORT_BASE + 0x38)
220 #define SL_CFG				(PORT_BASE + 0x84)
221 #define AIP_LIMIT			(PORT_BASE + 0x90)
222 #define SL_CONTROL			(PORT_BASE + 0x94)
223 #define SL_CONTROL_NOTIFY_EN_OFF	0
224 #define SL_CONTROL_NOTIFY_EN_MSK	(0x1 << SL_CONTROL_NOTIFY_EN_OFF)
225 #define SL_CTA_OFF		17
226 #define SL_CTA_MSK		(0x1 << SL_CTA_OFF)
227 #define RX_PRIMS_STATUS			(PORT_BASE + 0x98)
228 #define RX_BCAST_CHG_OFF		1
229 #define RX_BCAST_CHG_MSK		(0x1 << RX_BCAST_CHG_OFF)
230 #define TX_ID_DWORD0			(PORT_BASE + 0x9c)
231 #define TX_ID_DWORD1			(PORT_BASE + 0xa0)
232 #define TX_ID_DWORD2			(PORT_BASE + 0xa4)
233 #define TX_ID_DWORD3			(PORT_BASE + 0xa8)
234 #define TX_ID_DWORD4			(PORT_BASE + 0xaC)
235 #define TX_ID_DWORD5			(PORT_BASE + 0xb0)
236 #define TX_ID_DWORD6			(PORT_BASE + 0xb4)
237 #define TXID_AUTO				(PORT_BASE + 0xb8)
238 #define CT3_OFF		1
239 #define CT3_MSK		(0x1 << CT3_OFF)
240 #define TX_HARDRST_OFF          2
241 #define TX_HARDRST_MSK          (0x1 << TX_HARDRST_OFF)
242 #define RX_IDAF_DWORD0			(PORT_BASE + 0xc4)
243 #define RXOP_CHECK_CFG_H		(PORT_BASE + 0xfc)
244 #define STP_LINK_TIMER			(PORT_BASE + 0x120)
245 #define STP_LINK_TIMEOUT_STATE		(PORT_BASE + 0x124)
246 #define CON_CFG_DRIVER			(PORT_BASE + 0x130)
247 #define SAS_SSP_CON_TIMER_CFG		(PORT_BASE + 0x134)
248 #define SAS_SMP_CON_TIMER_CFG		(PORT_BASE + 0x138)
249 #define SAS_STP_CON_TIMER_CFG		(PORT_BASE + 0x13c)
250 #define CHL_INT0			(PORT_BASE + 0x1b4)
251 #define CHL_INT0_HOTPLUG_TOUT_OFF	0
252 #define CHL_INT0_HOTPLUG_TOUT_MSK	(0x1 << CHL_INT0_HOTPLUG_TOUT_OFF)
253 #define CHL_INT0_SL_RX_BCST_ACK_OFF	1
254 #define CHL_INT0_SL_RX_BCST_ACK_MSK	(0x1 << CHL_INT0_SL_RX_BCST_ACK_OFF)
255 #define CHL_INT0_SL_PHY_ENABLE_OFF	2
256 #define CHL_INT0_SL_PHY_ENABLE_MSK	(0x1 << CHL_INT0_SL_PHY_ENABLE_OFF)
257 #define CHL_INT0_NOT_RDY_OFF		4
258 #define CHL_INT0_NOT_RDY_MSK		(0x1 << CHL_INT0_NOT_RDY_OFF)
259 #define CHL_INT0_PHY_RDY_OFF		5
260 #define CHL_INT0_PHY_RDY_MSK		(0x1 << CHL_INT0_PHY_RDY_OFF)
261 #define CHL_INT1			(PORT_BASE + 0x1b8)
262 #define CHL_INT1_DMAC_TX_ECC_MB_ERR_OFF	15
263 #define CHL_INT1_DMAC_TX_ECC_1B_ERR_OFF	16
264 #define CHL_INT1_DMAC_RX_ECC_MB_ERR_OFF	17
265 #define CHL_INT1_DMAC_RX_ECC_1B_ERR_OFF	18
266 #define CHL_INT1_DMAC_TX_AXI_WR_ERR_OFF	19
267 #define CHL_INT1_DMAC_TX_AXI_RD_ERR_OFF	20
268 #define CHL_INT1_DMAC_RX_AXI_WR_ERR_OFF	21
269 #define CHL_INT1_DMAC_RX_AXI_RD_ERR_OFF	22
270 #define CHL_INT1_DMAC_TX_FIFO_ERR_OFF	23
271 #define CHL_INT1_DMAC_RX_FIFO_ERR_OFF	24
272 #define CHL_INT1_DMAC_TX_AXI_RUSER_ERR_OFF	26
273 #define CHL_INT1_DMAC_RX_AXI_RUSER_ERR_OFF	27
274 #define CHL_INT2			(PORT_BASE + 0x1bc)
275 #define CHL_INT2_SL_IDAF_TOUT_CONF_OFF	0
276 #define CHL_INT2_RX_DISP_ERR_OFF	28
277 #define CHL_INT2_RX_CODE_ERR_OFF	29
278 #define CHL_INT2_RX_INVLD_DW_OFF	30
279 #define CHL_INT2_STP_LINK_TIMEOUT_OFF	31
280 #define CHL_INT0_MSK			(PORT_BASE + 0x1c0)
281 #define CHL_INT1_MSK			(PORT_BASE + 0x1c4)
282 #define CHL_INT2_MSK			(PORT_BASE + 0x1c8)
283 #define SAS_EC_INT_COAL_TIME		(PORT_BASE + 0x1cc)
284 #define CHL_INT_COAL_EN			(PORT_BASE + 0x1d0)
285 #define SAS_RX_TRAIN_TIMER		(PORT_BASE + 0x2a4)
286 #define PHY_CTRL_RDY_MSK		(PORT_BASE + 0x2b0)
287 #define PHYCTRL_NOT_RDY_MSK		(PORT_BASE + 0x2b4)
288 #define PHYCTRL_DWS_RESET_MSK		(PORT_BASE + 0x2b8)
289 #define PHYCTRL_PHY_ENA_MSK		(PORT_BASE + 0x2bc)
290 #define SL_RX_BCAST_CHK_MSK		(PORT_BASE + 0x2c0)
291 #define PHYCTRL_OOB_RESTART_MSK		(PORT_BASE + 0x2c4)
292 #define DMA_TX_STATUS			(PORT_BASE + 0x2d0)
293 #define DMA_TX_STATUS_BUSY_OFF		0
294 #define DMA_TX_STATUS_BUSY_MSK		(0x1 << DMA_TX_STATUS_BUSY_OFF)
295 #define DMA_RX_STATUS			(PORT_BASE + 0x2e8)
296 #define DMA_RX_STATUS_BUSY_OFF		0
297 #define DMA_RX_STATUS_BUSY_MSK		(0x1 << DMA_RX_STATUS_BUSY_OFF)
298 
299 #define COARSETUNE_TIME			(PORT_BASE + 0x304)
300 #define TXDEEMPH_G1			(PORT_BASE + 0x350)
301 #define ERR_CNT_DWS_LOST		(PORT_BASE + 0x380)
302 #define ERR_CNT_RESET_PROB		(PORT_BASE + 0x384)
303 #define ERR_CNT_INVLD_DW		(PORT_BASE + 0x390)
304 #define ERR_CNT_CODE_ERR		(PORT_BASE + 0x394)
305 #define ERR_CNT_DISP_ERR		(PORT_BASE + 0x398)
306 #define DFX_FIFO_CTRL			(PORT_BASE + 0x3a0)
307 #define DFX_FIFO_CTRL_TRIGGER_MODE_OFF	0
308 #define DFX_FIFO_CTRL_TRIGGER_MODE_MSK	(0x7 << DFX_FIFO_CTRL_TRIGGER_MODE_OFF)
309 #define DFX_FIFO_CTRL_DUMP_MODE_OFF	3
310 #define DFX_FIFO_CTRL_DUMP_MODE_MSK	(0x7 << DFX_FIFO_CTRL_DUMP_MODE_OFF)
311 #define DFX_FIFO_CTRL_SIGNAL_SEL_OFF	6
312 #define DFX_FIFO_CTRL_SIGNAL_SEL_MSK	(0xF << DFX_FIFO_CTRL_SIGNAL_SEL_OFF)
313 #define DFX_FIFO_CTRL_DUMP_DISABLE_OFF	10
314 #define DFX_FIFO_CTRL_DUMP_DISABLE_MSK	(0x1 << DFX_FIFO_CTRL_DUMP_DISABLE_OFF)
315 #define DFX_FIFO_TRIGGER		(PORT_BASE + 0x3a4)
316 #define DFX_FIFO_TRIGGER_MSK		(PORT_BASE + 0x3a8)
317 #define DFX_FIFO_DUMP_MSK		(PORT_BASE + 0x3aC)
318 #define DFX_FIFO_RD_DATA		(PORT_BASE + 0x3b0)
319 
320 #define DEFAULT_ITCT_HW		2048 /* reset value, not reprogrammed */
321 #if (HISI_SAS_MAX_DEVICES > DEFAULT_ITCT_HW)
322 #error Max ITCT exceeded
323 #endif
324 
325 #define AXI_MASTER_CFG_BASE		(0x5000)
326 #define AM_CTRL_GLOBAL			(0x0)
327 #define AM_CTRL_SHUTDOWN_REQ_OFF	0
328 #define AM_CTRL_SHUTDOWN_REQ_MSK	(0x1 << AM_CTRL_SHUTDOWN_REQ_OFF)
329 #define AM_CURR_TRANS_RETURN	(0x150)
330 
331 #define AM_CFG_MAX_TRANS		(0x5010)
332 #define AM_CFG_SINGLE_PORT_MAX_TRANS	(0x5014)
333 #define AXI_CFG					(0x5100)
334 #define AM_ROB_ECC_ERR_ADDR		(0x510c)
335 #define AM_ROB_ECC_ERR_ADDR_OFF	0
336 #define AM_ROB_ECC_ERR_ADDR_MSK	0xffffffff
337 
338 /* RAS registers need init */
339 #define RAS_BASE		(0x6000)
340 #define SAS_RAS_INTR0			(RAS_BASE)
341 #define SAS_RAS_INTR1			(RAS_BASE + 0x04)
342 #define SAS_RAS_INTR0_MASK		(RAS_BASE + 0x08)
343 #define SAS_RAS_INTR1_MASK		(RAS_BASE + 0x0c)
344 #define CFG_SAS_RAS_INTR_MASK		(RAS_BASE + 0x1c)
345 #define SAS_RAS_INTR2			(RAS_BASE + 0x20)
346 #define SAS_RAS_INTR2_MASK		(RAS_BASE + 0x24)
347 
348 /* HW dma structures */
349 /* Delivery queue header */
350 /* dw0 */
351 #define CMD_HDR_ABORT_FLAG_OFF		0
352 #define CMD_HDR_ABORT_FLAG_MSK		(0x3 << CMD_HDR_ABORT_FLAG_OFF)
353 #define CMD_HDR_ABORT_DEVICE_TYPE_OFF	2
354 #define CMD_HDR_ABORT_DEVICE_TYPE_MSK	(0x1 << CMD_HDR_ABORT_DEVICE_TYPE_OFF)
355 #define CMD_HDR_RESP_REPORT_OFF		5
356 #define CMD_HDR_RESP_REPORT_MSK		(0x1 << CMD_HDR_RESP_REPORT_OFF)
357 #define CMD_HDR_TLR_CTRL_OFF		6
358 #define CMD_HDR_TLR_CTRL_MSK		(0x3 << CMD_HDR_TLR_CTRL_OFF)
359 #define CMD_HDR_PORT_OFF		18
360 #define CMD_HDR_PORT_MSK		(0xf << CMD_HDR_PORT_OFF)
361 #define CMD_HDR_PRIORITY_OFF		27
362 #define CMD_HDR_PRIORITY_MSK		(0x1 << CMD_HDR_PRIORITY_OFF)
363 #define CMD_HDR_CMD_OFF			29
364 #define CMD_HDR_CMD_MSK			(0x7 << CMD_HDR_CMD_OFF)
365 /* dw1 */
366 #define CMD_HDR_UNCON_CMD_OFF	3
367 #define CMD_HDR_DIR_OFF			5
368 #define CMD_HDR_DIR_MSK			(0x3 << CMD_HDR_DIR_OFF)
369 #define CMD_HDR_RESET_OFF		7
370 #define CMD_HDR_RESET_MSK		(0x1 << CMD_HDR_RESET_OFF)
371 #define CMD_HDR_VDTL_OFF		10
372 #define CMD_HDR_VDTL_MSK		(0x1 << CMD_HDR_VDTL_OFF)
373 #define CMD_HDR_FRAME_TYPE_OFF		11
374 #define CMD_HDR_FRAME_TYPE_MSK		(0x1f << CMD_HDR_FRAME_TYPE_OFF)
375 #define CMD_HDR_DEV_ID_OFF		16
376 #define CMD_HDR_DEV_ID_MSK		(0xffff << CMD_HDR_DEV_ID_OFF)
377 /* dw2 */
378 #define CMD_HDR_CFL_OFF			0
379 #define CMD_HDR_CFL_MSK			(0x1ff << CMD_HDR_CFL_OFF)
380 #define CMD_HDR_NCQ_TAG_OFF		10
381 #define CMD_HDR_NCQ_TAG_MSK		(0x1f << CMD_HDR_NCQ_TAG_OFF)
382 #define CMD_HDR_MRFL_OFF		15
383 #define CMD_HDR_MRFL_MSK		(0x1ff << CMD_HDR_MRFL_OFF)
384 #define CMD_HDR_SG_MOD_OFF		24
385 #define CMD_HDR_SG_MOD_MSK		(0x3 << CMD_HDR_SG_MOD_OFF)
386 /* dw3 */
387 #define CMD_HDR_IPTT_OFF		0
388 #define CMD_HDR_IPTT_MSK		(0xffff << CMD_HDR_IPTT_OFF)
389 /* dw6 */
390 #define CMD_HDR_DIF_SGL_LEN_OFF		0
391 #define CMD_HDR_DIF_SGL_LEN_MSK		(0xffff << CMD_HDR_DIF_SGL_LEN_OFF)
392 #define CMD_HDR_DATA_SGL_LEN_OFF	16
393 #define CMD_HDR_DATA_SGL_LEN_MSK	(0xffff << CMD_HDR_DATA_SGL_LEN_OFF)
394 /* dw7 */
395 #define CMD_HDR_ADDR_MODE_SEL_OFF		15
396 #define CMD_HDR_ADDR_MODE_SEL_MSK		(1 << CMD_HDR_ADDR_MODE_SEL_OFF)
397 #define CMD_HDR_ABORT_IPTT_OFF		16
398 #define CMD_HDR_ABORT_IPTT_MSK		(0xffff << CMD_HDR_ABORT_IPTT_OFF)
399 
400 /* Completion header */
401 /* dw0 */
402 #define CMPLT_HDR_CMPLT_OFF		0
403 #define CMPLT_HDR_CMPLT_MSK		(0x3 << CMPLT_HDR_CMPLT_OFF)
404 #define CMPLT_HDR_ERROR_PHASE_OFF   2
405 #define CMPLT_HDR_ERROR_PHASE_MSK   (0xff << CMPLT_HDR_ERROR_PHASE_OFF)
406 #define CMPLT_HDR_RSPNS_XFRD_OFF	10
407 #define CMPLT_HDR_RSPNS_XFRD_MSK	(0x1 << CMPLT_HDR_RSPNS_XFRD_OFF)
408 #define CMPLT_HDR_ERX_OFF		12
409 #define CMPLT_HDR_ERX_MSK		(0x1 << CMPLT_HDR_ERX_OFF)
410 #define CMPLT_HDR_ABORT_STAT_OFF	13
411 #define CMPLT_HDR_ABORT_STAT_MSK	(0x7 << CMPLT_HDR_ABORT_STAT_OFF)
412 /* abort_stat */
413 #define STAT_IO_NOT_VALID		0x1
414 #define STAT_IO_NO_DEVICE		0x2
415 #define STAT_IO_COMPLETE		0x3
416 #define STAT_IO_ABORTED			0x4
417 /* dw1 */
418 #define CMPLT_HDR_IPTT_OFF		0
419 #define CMPLT_HDR_IPTT_MSK		(0xffff << CMPLT_HDR_IPTT_OFF)
420 #define CMPLT_HDR_DEV_ID_OFF		16
421 #define CMPLT_HDR_DEV_ID_MSK		(0xffff << CMPLT_HDR_DEV_ID_OFF)
422 /* dw3 */
423 #define CMPLT_HDR_IO_IN_TARGET_OFF	17
424 #define CMPLT_HDR_IO_IN_TARGET_MSK	(0x1 << CMPLT_HDR_IO_IN_TARGET_OFF)
425 
426 /* ITCT header */
427 /* qw0 */
428 #define ITCT_HDR_DEV_TYPE_OFF		0
429 #define ITCT_HDR_DEV_TYPE_MSK		(0x3 << ITCT_HDR_DEV_TYPE_OFF)
430 #define ITCT_HDR_VALID_OFF		2
431 #define ITCT_HDR_VALID_MSK		(0x1 << ITCT_HDR_VALID_OFF)
432 #define ITCT_HDR_MCR_OFF		5
433 #define ITCT_HDR_MCR_MSK		(0xf << ITCT_HDR_MCR_OFF)
434 #define ITCT_HDR_VLN_OFF		9
435 #define ITCT_HDR_VLN_MSK		(0xf << ITCT_HDR_VLN_OFF)
436 #define ITCT_HDR_SMP_TIMEOUT_OFF	16
437 #define ITCT_HDR_AWT_CONTINUE_OFF	25
438 #define ITCT_HDR_PORT_ID_OFF		28
439 #define ITCT_HDR_PORT_ID_MSK		(0xf << ITCT_HDR_PORT_ID_OFF)
440 /* qw2 */
441 #define ITCT_HDR_INLT_OFF		0
442 #define ITCT_HDR_INLT_MSK		(0xffffULL << ITCT_HDR_INLT_OFF)
443 #define ITCT_HDR_RTOLT_OFF		48
444 #define ITCT_HDR_RTOLT_MSK		(0xffffULL << ITCT_HDR_RTOLT_OFF)
445 
446 struct hisi_sas_protect_iu_v3_hw {
447 	u32 dw0;
448 	u32 lbrtcv;
449 	u32 lbrtgv;
450 	u32 dw3;
451 	u32 dw4;
452 	u32 dw5;
453 	u32 rsv;
454 };
455 
456 struct hisi_sas_complete_v3_hdr {
457 	__le32 dw0;
458 	__le32 dw1;
459 	__le32 act;
460 	__le32 dw3;
461 };
462 
463 struct hisi_sas_err_record_v3 {
464 	/* dw0 */
465 	__le32 trans_tx_fail_type;
466 
467 	/* dw1 */
468 	__le32 trans_rx_fail_type;
469 
470 	/* dw2 */
471 	__le16 dma_tx_err_type;
472 	__le16 sipc_rx_err_type;
473 
474 	/* dw3 */
475 	__le32 dma_rx_err_type;
476 };
477 
478 #define RX_DATA_LEN_UNDERFLOW_OFF	6
479 #define RX_DATA_LEN_UNDERFLOW_MSK	(1 << RX_DATA_LEN_UNDERFLOW_OFF)
480 
481 #define HISI_SAS_COMMAND_ENTRIES_V3_HW 4096
482 #define HISI_SAS_MSI_COUNT_V3_HW 32
483 
484 #define DIR_NO_DATA 0
485 #define DIR_TO_INI 1
486 #define DIR_TO_DEVICE 2
487 #define DIR_RESERVED 3
488 
489 #define FIS_CMD_IS_UNCONSTRAINED(fis) \
490 	((fis.command == ATA_CMD_READ_LOG_EXT) || \
491 	(fis.command == ATA_CMD_READ_LOG_DMA_EXT) || \
492 	((fis.command == ATA_CMD_DEV_RESET) && \
493 	((fis.control & ATA_SRST) != 0)))
494 
495 #define T10_INSRT_EN_OFF    0
496 #define T10_INSRT_EN_MSK    (1 << T10_INSRT_EN_OFF)
497 #define T10_RMV_EN_OFF	    1
498 #define T10_RMV_EN_MSK	    (1 << T10_RMV_EN_OFF)
499 #define T10_RPLC_EN_OFF	    2
500 #define T10_RPLC_EN_MSK	    (1 << T10_RPLC_EN_OFF)
501 #define T10_CHK_EN_OFF	    3
502 #define T10_CHK_EN_MSK	    (1 << T10_CHK_EN_OFF)
503 #define INCR_LBRT_OFF	    5
504 #define INCR_LBRT_MSK	    (1 << INCR_LBRT_OFF)
505 #define USR_DATA_BLOCK_SZ_OFF	20
506 #define USR_DATA_BLOCK_SZ_MSK	(0x3 << USR_DATA_BLOCK_SZ_OFF)
507 #define T10_CHK_MSK_OFF	    16
508 #define T10_CHK_REF_TAG_MSK (0xf0 << T10_CHK_MSK_OFF)
509 #define T10_CHK_APP_TAG_MSK (0xc << T10_CHK_MSK_OFF)
510 
511 #define BASE_VECTORS_V3_HW  16
512 #define MIN_AFFINE_VECTORS_V3_HW  (BASE_VECTORS_V3_HW + 1)
513 
514 #define CHNL_INT_STS_MSK	0xeeeeeeee
515 #define CHNL_INT_STS_PHY_MSK	0xe
516 #define CHNL_INT_STS_INT0_MSK BIT(1)
517 #define CHNL_INT_STS_INT1_MSK BIT(2)
518 #define CHNL_INT_STS_INT2_MSK BIT(3)
519 #define CHNL_WIDTH 4
520 
521 enum {
522 	DSM_FUNC_ERR_HANDLE_MSI = 0,
523 };
524 
525 static bool hisi_sas_intr_conv;
526 MODULE_PARM_DESC(intr_conv, "interrupt converge enable (0-1)");
527 
528 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
529 static int prot_mask;
530 module_param(prot_mask, int, 0);
531 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=0x0 ");
532 
533 static void debugfs_work_handler_v3_hw(struct work_struct *work);
534 
535 static u32 hisi_sas_read32(struct hisi_hba *hisi_hba, u32 off)
536 {
537 	void __iomem *regs = hisi_hba->regs + off;
538 
539 	return readl(regs);
540 }
541 
542 static void hisi_sas_write32(struct hisi_hba *hisi_hba, u32 off, u32 val)
543 {
544 	void __iomem *regs = hisi_hba->regs + off;
545 
546 	writel(val, regs);
547 }
548 
549 static void hisi_sas_phy_write32(struct hisi_hba *hisi_hba, int phy_no,
550 				 u32 off, u32 val)
551 {
552 	void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
553 
554 	writel(val, regs);
555 }
556 
557 static u32 hisi_sas_phy_read32(struct hisi_hba *hisi_hba,
558 				      int phy_no, u32 off)
559 {
560 	void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
561 
562 	return readl(regs);
563 }
564 
565 #define hisi_sas_read32_poll_timeout(off, val, cond, delay_us,		\
566 				     timeout_us)			\
567 ({									\
568 	void __iomem *regs = hisi_hba->regs + off;			\
569 	readl_poll_timeout(regs, val, cond, delay_us, timeout_us);	\
570 })
571 
572 #define hisi_sas_read32_poll_timeout_atomic(off, val, cond, delay_us,	\
573 					    timeout_us)			\
574 ({									\
575 	void __iomem *regs = hisi_hba->regs + off;			\
576 	readl_poll_timeout_atomic(regs, val, cond, delay_us, timeout_us);\
577 })
578 
579 static void init_reg_v3_hw(struct hisi_hba *hisi_hba)
580 {
581 	int i, j;
582 
583 	/* Global registers init */
584 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE,
585 			 (u32)((1ULL << hisi_hba->queue_count) - 1));
586 	hisi_sas_write32(hisi_hba, SAS_AXI_USER3, 0);
587 	hisi_sas_write32(hisi_hba, CFG_MAX_TAG, 0xfff0400);
588 	hisi_sas_write32(hisi_hba, HGC_SAS_TXFAIL_RETRY_CTRL, 0x108);
589 	hisi_sas_write32(hisi_hba, CFG_AGING_TIME, 0x1);
590 	hisi_sas_write32(hisi_hba, INT_COAL_EN, 0x1);
591 	hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x1);
592 	hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 0x1);
593 	hisi_sas_write32(hisi_hba, CQ_INT_CONVERGE_EN,
594 			 hisi_sas_intr_conv);
595 	hisi_sas_write32(hisi_hba, OQ_INT_SRC, 0xffff);
596 	hisi_sas_write32(hisi_hba, ENT_INT_SRC1, 0xffffffff);
597 	hisi_sas_write32(hisi_hba, ENT_INT_SRC2, 0xffffffff);
598 	hisi_sas_write32(hisi_hba, ENT_INT_SRC3, 0xffffffff);
599 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0xfefefefe);
600 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0xfefefefe);
601 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0xffc220ff);
602 	hisi_sas_write32(hisi_hba, CHNL_PHYUPDOWN_INT_MSK, 0x0);
603 	hisi_sas_write32(hisi_hba, CHNL_ENT_INT_MSK, 0x0);
604 	hisi_sas_write32(hisi_hba, HGC_COM_INT_MSK, 0x0);
605 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0x155555);
606 	hisi_sas_write32(hisi_hba, AWQOS_AWCACHE_CFG, 0xf0f0);
607 	hisi_sas_write32(hisi_hba, ARQOS_ARCACHE_CFG, 0xf0f0);
608 	for (i = 0; i < hisi_hba->queue_count; i++)
609 		hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK + 0x4 * i, 0);
610 
611 	hisi_sas_write32(hisi_hba, HYPER_STREAM_ID_EN_CFG, 1);
612 
613 	for (i = 0; i < hisi_hba->n_phy; i++) {
614 		enum sas_linkrate max;
615 		struct hisi_sas_phy *phy = &hisi_hba->phy[i];
616 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
617 		u32 prog_phy_link_rate = hisi_sas_phy_read32(hisi_hba, i,
618 							   PROG_PHY_LINK_RATE);
619 
620 		prog_phy_link_rate &= ~CFG_PROG_PHY_LINK_RATE_MSK;
621 		if (!sas_phy->phy || (sas_phy->phy->maximum_linkrate <
622 				SAS_LINK_RATE_1_5_GBPS))
623 			max = SAS_LINK_RATE_12_0_GBPS;
624 		else
625 			max = sas_phy->phy->maximum_linkrate;
626 		prog_phy_link_rate |= hisi_sas_get_prog_phy_linkrate_mask(max);
627 		hisi_sas_phy_write32(hisi_hba, i, PROG_PHY_LINK_RATE,
628 			prog_phy_link_rate);
629 		hisi_sas_phy_write32(hisi_hba, i, SERDES_CFG, 0xffc00);
630 		hisi_sas_phy_write32(hisi_hba, i, SAS_RX_TRAIN_TIMER, 0x13e80);
631 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT0, 0xffffffff);
632 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT1, 0xffffffff);
633 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT2, 0xffffffff);
634 		hisi_sas_phy_write32(hisi_hba, i, RXOP_CHECK_CFG_H, 0x1000);
635 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xf2057fff);
636 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0xffffbfe);
637 		hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL_RDY_MSK, 0x0);
638 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_NOT_RDY_MSK, 0x0);
639 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_DWS_RESET_MSK, 0x0);
640 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_PHY_ENA_MSK, 0x0);
641 		hisi_sas_phy_write32(hisi_hba, i, SL_RX_BCAST_CHK_MSK, 0x0);
642 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_OOB_RESTART_MSK, 0x1);
643 		hisi_sas_phy_write32(hisi_hba, i, STP_LINK_TIMER, 0x7f7a120);
644 		hisi_sas_phy_write32(hisi_hba, i, CON_CFG_DRIVER, 0x2a0a01);
645 		hisi_sas_phy_write32(hisi_hba, i, SAS_SSP_CON_TIMER_CFG, 0x32);
646 		hisi_sas_phy_write32(hisi_hba, i, SAS_EC_INT_COAL_TIME,
647 				     0x30f4240);
648 		/* used for 12G negotiate */
649 		hisi_sas_phy_write32(hisi_hba, i, COARSETUNE_TIME, 0x1e);
650 		hisi_sas_phy_write32(hisi_hba, i, AIP_LIMIT, 0x2ffff);
651 
652 		/* get default FFE configuration for BIST */
653 		for (j = 0; j < FFE_CFG_MAX; j++) {
654 			u32 val = hisi_sas_phy_read32(hisi_hba, i,
655 						      TXDEEMPH_G1 + (j * 0x4));
656 			hisi_hba->debugfs_bist_ffe[i][j] = val;
657 		}
658 	}
659 
660 	for (i = 0; i < hisi_hba->queue_count; i++) {
661 		/* Delivery queue */
662 		hisi_sas_write32(hisi_hba,
663 				 DLVRY_Q_0_BASE_ADDR_HI + (i * 0x14),
664 				 upper_32_bits(hisi_hba->cmd_hdr_dma[i]));
665 
666 		hisi_sas_write32(hisi_hba, DLVRY_Q_0_BASE_ADDR_LO + (i * 0x14),
667 				 lower_32_bits(hisi_hba->cmd_hdr_dma[i]));
668 
669 		hisi_sas_write32(hisi_hba, DLVRY_Q_0_DEPTH + (i * 0x14),
670 				 HISI_SAS_QUEUE_SLOTS);
671 
672 		/* Completion queue */
673 		hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_HI + (i * 0x14),
674 				 upper_32_bits(hisi_hba->complete_hdr_dma[i]));
675 
676 		hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_LO + (i * 0x14),
677 				 lower_32_bits(hisi_hba->complete_hdr_dma[i]));
678 
679 		hisi_sas_write32(hisi_hba, COMPL_Q_0_DEPTH + (i * 0x14),
680 				 HISI_SAS_QUEUE_SLOTS);
681 	}
682 
683 	/* itct */
684 	hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_LO,
685 			 lower_32_bits(hisi_hba->itct_dma));
686 
687 	hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_HI,
688 			 upper_32_bits(hisi_hba->itct_dma));
689 
690 	/* iost */
691 	hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_LO,
692 			 lower_32_bits(hisi_hba->iost_dma));
693 
694 	hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_HI,
695 			 upper_32_bits(hisi_hba->iost_dma));
696 
697 	/* breakpoint */
698 	hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_LO,
699 			 lower_32_bits(hisi_hba->breakpoint_dma));
700 
701 	hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_HI,
702 			 upper_32_bits(hisi_hba->breakpoint_dma));
703 
704 	/* SATA broken msg */
705 	hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_LO,
706 			 lower_32_bits(hisi_hba->sata_breakpoint_dma));
707 
708 	hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_HI,
709 			 upper_32_bits(hisi_hba->sata_breakpoint_dma));
710 
711 	/* SATA initial fis */
712 	hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_LO,
713 			 lower_32_bits(hisi_hba->initial_fis_dma));
714 
715 	hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_HI,
716 			 upper_32_bits(hisi_hba->initial_fis_dma));
717 
718 	/* RAS registers init */
719 	hisi_sas_write32(hisi_hba, SAS_RAS_INTR0_MASK, 0x0);
720 	hisi_sas_write32(hisi_hba, SAS_RAS_INTR1_MASK, 0x0);
721 	hisi_sas_write32(hisi_hba, SAS_RAS_INTR2_MASK, 0x0);
722 	hisi_sas_write32(hisi_hba, CFG_SAS_RAS_INTR_MASK, 0x0);
723 
724 	/* LED registers init */
725 	hisi_sas_write32(hisi_hba, SAS_CFG_DRIVE_VLD, 0x80000ff);
726 	hisi_sas_write32(hisi_hba, SAS_GPIO_TX_0_1, 0x80808080);
727 	hisi_sas_write32(hisi_hba, SAS_GPIO_TX_0_1 + 0x4, 0x80808080);
728 	/* Configure blink generator rate A to 1Hz and B to 4Hz */
729 	hisi_sas_write32(hisi_hba, SAS_GPIO_CFG_1, 0x121700);
730 	hisi_sas_write32(hisi_hba, SAS_GPIO_CFG_0, 0x800000);
731 }
732 
733 static void config_phy_opt_mode_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
734 {
735 	u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
736 
737 	cfg &= ~PHY_CFG_DC_OPT_MSK;
738 	cfg |= 1 << PHY_CFG_DC_OPT_OFF;
739 	hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
740 }
741 
742 static void config_id_frame_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
743 {
744 	struct sas_identify_frame identify_frame;
745 	u32 *identify_buffer;
746 
747 	memset(&identify_frame, 0, sizeof(identify_frame));
748 	identify_frame.dev_type = SAS_END_DEVICE;
749 	identify_frame.frame_type = 0;
750 	identify_frame._un1 = 1;
751 	identify_frame.initiator_bits = SAS_PROTOCOL_ALL;
752 	identify_frame.target_bits = SAS_PROTOCOL_NONE;
753 	memcpy(&identify_frame._un4_11[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
754 	memcpy(&identify_frame.sas_addr[0], hisi_hba->sas_addr,	SAS_ADDR_SIZE);
755 	identify_frame.phy_id = phy_no;
756 	identify_buffer = (u32 *)(&identify_frame);
757 
758 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD0,
759 			__swab32(identify_buffer[0]));
760 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD1,
761 			__swab32(identify_buffer[1]));
762 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD2,
763 			__swab32(identify_buffer[2]));
764 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD3,
765 			__swab32(identify_buffer[3]));
766 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD4,
767 			__swab32(identify_buffer[4]));
768 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD5,
769 			__swab32(identify_buffer[5]));
770 }
771 
772 static void setup_itct_v3_hw(struct hisi_hba *hisi_hba,
773 			     struct hisi_sas_device *sas_dev)
774 {
775 	struct domain_device *device = sas_dev->sas_device;
776 	struct device *dev = hisi_hba->dev;
777 	u64 qw0, device_id = sas_dev->device_id;
778 	struct hisi_sas_itct *itct = &hisi_hba->itct[device_id];
779 	struct domain_device *parent_dev = device->parent;
780 	struct asd_sas_port *sas_port = device->port;
781 	struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
782 	u64 sas_addr;
783 
784 	memset(itct, 0, sizeof(*itct));
785 
786 	/* qw0 */
787 	qw0 = 0;
788 	switch (sas_dev->dev_type) {
789 	case SAS_END_DEVICE:
790 	case SAS_EDGE_EXPANDER_DEVICE:
791 	case SAS_FANOUT_EXPANDER_DEVICE:
792 		qw0 = HISI_SAS_DEV_TYPE_SSP << ITCT_HDR_DEV_TYPE_OFF;
793 		break;
794 	case SAS_SATA_DEV:
795 	case SAS_SATA_PENDING:
796 		if (parent_dev && dev_is_expander(parent_dev->dev_type))
797 			qw0 = HISI_SAS_DEV_TYPE_STP << ITCT_HDR_DEV_TYPE_OFF;
798 		else
799 			qw0 = HISI_SAS_DEV_TYPE_SATA << ITCT_HDR_DEV_TYPE_OFF;
800 		break;
801 	default:
802 		dev_warn(dev, "setup itct: unsupported dev type (%d)\n",
803 			 sas_dev->dev_type);
804 	}
805 
806 	qw0 |= ((1 << ITCT_HDR_VALID_OFF) |
807 		(device->linkrate << ITCT_HDR_MCR_OFF) |
808 		(1 << ITCT_HDR_VLN_OFF) |
809 		(0xfa << ITCT_HDR_SMP_TIMEOUT_OFF) |
810 		(1 << ITCT_HDR_AWT_CONTINUE_OFF) |
811 		(port->id << ITCT_HDR_PORT_ID_OFF));
812 	itct->qw0 = cpu_to_le64(qw0);
813 
814 	/* qw1 */
815 	memcpy(&sas_addr, device->sas_addr, SAS_ADDR_SIZE);
816 	itct->sas_addr = cpu_to_le64(__swab64(sas_addr));
817 
818 	/* qw2 */
819 	if (!dev_is_sata(device))
820 		itct->qw2 = cpu_to_le64((5000ULL << ITCT_HDR_INLT_OFF) |
821 					(0x1ULL << ITCT_HDR_RTOLT_OFF));
822 }
823 
824 static int clear_itct_v3_hw(struct hisi_hba *hisi_hba,
825 			    struct hisi_sas_device *sas_dev)
826 {
827 	DECLARE_COMPLETION_ONSTACK(completion);
828 	u64 dev_id = sas_dev->device_id;
829 	struct hisi_sas_itct *itct = &hisi_hba->itct[dev_id];
830 	u32 reg_val = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
831 	struct device *dev = hisi_hba->dev;
832 
833 	sas_dev->completion = &completion;
834 
835 	/* clear the itct interrupt state */
836 	if (ENT_INT_SRC3_ITC_INT_MSK & reg_val)
837 		hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
838 				 ENT_INT_SRC3_ITC_INT_MSK);
839 
840 	/* clear the itct table */
841 	reg_val = ITCT_CLR_EN_MSK | (dev_id & ITCT_DEV_MSK);
842 	hisi_sas_write32(hisi_hba, ITCT_CLR, reg_val);
843 
844 	if (!wait_for_completion_timeout(sas_dev->completion,
845 					 CLEAR_ITCT_TIMEOUT * HZ)) {
846 		dev_warn(dev, "failed to clear ITCT\n");
847 		return -ETIMEDOUT;
848 	}
849 
850 	memset(itct, 0, sizeof(struct hisi_sas_itct));
851 	return 0;
852 }
853 
854 static void dereg_device_v3_hw(struct hisi_hba *hisi_hba,
855 				struct domain_device *device)
856 {
857 	struct hisi_sas_slot *slot, *slot2;
858 	struct hisi_sas_device *sas_dev = device->lldd_dev;
859 	u32 cfg_abt_set_query_iptt;
860 
861 	cfg_abt_set_query_iptt = hisi_sas_read32(hisi_hba,
862 		CFG_ABT_SET_QUERY_IPTT);
863 	list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry) {
864 		cfg_abt_set_query_iptt &= ~CFG_SET_ABORTED_IPTT_MSK;
865 		cfg_abt_set_query_iptt |= (1 << CFG_SET_ABORTED_EN_OFF) |
866 			(slot->idx << CFG_SET_ABORTED_IPTT_OFF);
867 		hisi_sas_write32(hisi_hba, CFG_ABT_SET_QUERY_IPTT,
868 			cfg_abt_set_query_iptt);
869 	}
870 	cfg_abt_set_query_iptt &= ~(1 << CFG_SET_ABORTED_EN_OFF);
871 	hisi_sas_write32(hisi_hba, CFG_ABT_SET_QUERY_IPTT,
872 		cfg_abt_set_query_iptt);
873 	hisi_sas_write32(hisi_hba, CFG_ABT_SET_IPTT_DONE,
874 					1 << CFG_ABT_SET_IPTT_DONE_OFF);
875 }
876 
877 static int reset_hw_v3_hw(struct hisi_hba *hisi_hba)
878 {
879 	struct device *dev = hisi_hba->dev;
880 	int ret;
881 	u32 val;
882 
883 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0);
884 
885 	/* Disable all of the PHYs */
886 	hisi_sas_stop_phys(hisi_hba);
887 	udelay(50);
888 
889 	/* Ensure axi bus idle */
890 	ret = hisi_sas_read32_poll_timeout(AXI_CFG, val, !val,
891 					   20000, 1000000);
892 	if (ret) {
893 		dev_err(dev, "axi bus is not idle, ret = %d!\n", ret);
894 		return -EIO;
895 	}
896 
897 	if (ACPI_HANDLE(dev)) {
898 		acpi_status s;
899 
900 		s = acpi_evaluate_object(ACPI_HANDLE(dev), "_RST", NULL, NULL);
901 		if (ACPI_FAILURE(s)) {
902 			dev_err(dev, "Reset failed\n");
903 			return -EIO;
904 		}
905 	} else {
906 		dev_err(dev, "no reset method!\n");
907 		return -EINVAL;
908 	}
909 
910 	return 0;
911 }
912 
913 static int hw_init_v3_hw(struct hisi_hba *hisi_hba)
914 {
915 	struct device *dev = hisi_hba->dev;
916 	struct acpi_device *acpi_dev;
917 	union acpi_object *obj;
918 	guid_t guid;
919 	int rc;
920 
921 	rc = reset_hw_v3_hw(hisi_hba);
922 	if (rc) {
923 		dev_err(dev, "hisi_sas_reset_hw failed, rc=%d\n", rc);
924 		return rc;
925 	}
926 
927 	msleep(100);
928 	init_reg_v3_hw(hisi_hba);
929 
930 	if (guid_parse("D5918B4B-37AE-4E10-A99F-E5E8A6EF4C1F", &guid)) {
931 		dev_err(dev, "Parse GUID failed\n");
932 		return -EINVAL;
933 	}
934 
935 	/*
936 	 * This DSM handles some hardware-related configurations:
937 	 * 1. Switch over to MSI error handling in kernel
938 	 * 2. BIOS *may* reset some register values through this method
939 	 */
940 	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &guid, 0,
941 				DSM_FUNC_ERR_HANDLE_MSI, NULL);
942 	if (!obj)
943 		dev_warn(dev, "can not find DSM method, ignore\n");
944 	else
945 		ACPI_FREE(obj);
946 
947 	acpi_dev = ACPI_COMPANION(dev);
948 	if (!acpi_device_power_manageable(acpi_dev))
949 		dev_notice(dev, "neither _PS0 nor _PR0 is defined\n");
950 	return 0;
951 }
952 
953 static void enable_phy_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
954 {
955 	u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
956 
957 	cfg |= PHY_CFG_ENA_MSK;
958 	cfg &= ~PHY_CFG_PHY_RST_MSK;
959 	hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
960 }
961 
962 static void disable_phy_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
963 {
964 	u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
965 	u32 irq_msk = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT2_MSK);
966 	static const u32 msk = BIT(CHL_INT2_RX_DISP_ERR_OFF) |
967 			       BIT(CHL_INT2_RX_CODE_ERR_OFF) |
968 			       BIT(CHL_INT2_RX_INVLD_DW_OFF);
969 	u32 state;
970 
971 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2_MSK, msk | irq_msk);
972 
973 	cfg &= ~PHY_CFG_ENA_MSK;
974 	hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
975 
976 	mdelay(50);
977 
978 	state = hisi_sas_read32(hisi_hba, PHY_STATE);
979 	if (state & BIT(phy_no)) {
980 		cfg |= PHY_CFG_PHY_RST_MSK;
981 		hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
982 	}
983 
984 	udelay(1);
985 
986 	hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_INVLD_DW);
987 	hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_DISP_ERR);
988 	hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_CODE_ERR);
989 
990 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2, msk);
991 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2_MSK, irq_msk);
992 }
993 
994 static void start_phy_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
995 {
996 	config_id_frame_v3_hw(hisi_hba, phy_no);
997 	config_phy_opt_mode_v3_hw(hisi_hba, phy_no);
998 	enable_phy_v3_hw(hisi_hba, phy_no);
999 }
1000 
1001 static void phy_hard_reset_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1002 {
1003 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1004 	u32 txid_auto;
1005 
1006 	hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1007 	if (phy->identify.device_type == SAS_END_DEVICE) {
1008 		txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
1009 		hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
1010 					txid_auto | TX_HARDRST_MSK);
1011 	}
1012 	msleep(100);
1013 	hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1014 }
1015 
1016 static enum sas_linkrate phy_get_max_linkrate_v3_hw(void)
1017 {
1018 	return SAS_LINK_RATE_12_0_GBPS;
1019 }
1020 
1021 static void phys_init_v3_hw(struct hisi_hba *hisi_hba)
1022 {
1023 	int i;
1024 
1025 	for (i = 0; i < hisi_hba->n_phy; i++) {
1026 		struct hisi_sas_phy *phy = &hisi_hba->phy[i];
1027 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
1028 
1029 		if (!sas_phy->phy->enabled)
1030 			continue;
1031 
1032 		hisi_sas_phy_enable(hisi_hba, i, 1);
1033 	}
1034 }
1035 
1036 static void sl_notify_ssp_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1037 {
1038 	u32 sl_control;
1039 
1040 	sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1041 	sl_control |= SL_CONTROL_NOTIFY_EN_MSK;
1042 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
1043 	msleep(1);
1044 	sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1045 	sl_control &= ~SL_CONTROL_NOTIFY_EN_MSK;
1046 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
1047 }
1048 
1049 static int get_wideport_bitmap_v3_hw(struct hisi_hba *hisi_hba, int port_id)
1050 {
1051 	int i, bitmap = 0;
1052 	u32 phy_port_num_ma = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
1053 	u32 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1054 
1055 	for (i = 0; i < hisi_hba->n_phy; i++)
1056 		if (phy_state & BIT(i))
1057 			if (((phy_port_num_ma >> (i * 4)) & 0xf) == port_id)
1058 				bitmap |= BIT(i);
1059 
1060 	return bitmap;
1061 }
1062 
1063 static void start_delivery_v3_hw(struct hisi_sas_dq *dq)
1064 {
1065 	struct hisi_hba *hisi_hba = dq->hisi_hba;
1066 	struct hisi_sas_slot *s, *s1, *s2 = NULL;
1067 	int dlvry_queue = dq->id;
1068 	int wp;
1069 
1070 	list_for_each_entry_safe(s, s1, &dq->list, delivery) {
1071 		if (!s->ready)
1072 			break;
1073 		s2 = s;
1074 		list_del(&s->delivery);
1075 	}
1076 
1077 	if (!s2)
1078 		return;
1079 
1080 	/*
1081 	 * Ensure that memories for slots built on other CPUs is observed.
1082 	 */
1083 	smp_rmb();
1084 	wp = (s2->dlvry_queue_slot + 1) % HISI_SAS_QUEUE_SLOTS;
1085 
1086 	hisi_sas_write32(hisi_hba, DLVRY_Q_0_WR_PTR + (dlvry_queue * 0x14), wp);
1087 }
1088 
1089 static void prep_prd_sge_v3_hw(struct hisi_hba *hisi_hba,
1090 			      struct hisi_sas_slot *slot,
1091 			      struct hisi_sas_cmd_hdr *hdr,
1092 			      struct scatterlist *scatter,
1093 			      int n_elem)
1094 {
1095 	struct hisi_sas_sge_page *sge_page = hisi_sas_sge_addr_mem(slot);
1096 	struct scatterlist *sg;
1097 	int i;
1098 
1099 	for_each_sg(scatter, sg, n_elem, i) {
1100 		struct hisi_sas_sge *entry = &sge_page->sge[i];
1101 
1102 		entry->addr = cpu_to_le64(sg_dma_address(sg));
1103 		entry->page_ctrl_0 = entry->page_ctrl_1 = 0;
1104 		entry->data_len = cpu_to_le32(sg_dma_len(sg));
1105 		entry->data_off = 0;
1106 	}
1107 
1108 	hdr->prd_table_addr = cpu_to_le64(hisi_sas_sge_addr_dma(slot));
1109 
1110 	hdr->sg_len |= cpu_to_le32(n_elem << CMD_HDR_DATA_SGL_LEN_OFF);
1111 }
1112 
1113 static void prep_prd_sge_dif_v3_hw(struct hisi_hba *hisi_hba,
1114 				   struct hisi_sas_slot *slot,
1115 				   struct hisi_sas_cmd_hdr *hdr,
1116 				   struct scatterlist *scatter,
1117 				   int n_elem)
1118 {
1119 	struct hisi_sas_sge_dif_page *sge_dif_page;
1120 	struct scatterlist *sg;
1121 	int i;
1122 
1123 	sge_dif_page = hisi_sas_sge_dif_addr_mem(slot);
1124 
1125 	for_each_sg(scatter, sg, n_elem, i) {
1126 		struct hisi_sas_sge *entry = &sge_dif_page->sge[i];
1127 
1128 		entry->addr = cpu_to_le64(sg_dma_address(sg));
1129 		entry->page_ctrl_0 = 0;
1130 		entry->page_ctrl_1 = 0;
1131 		entry->data_len = cpu_to_le32(sg_dma_len(sg));
1132 		entry->data_off = 0;
1133 	}
1134 
1135 	hdr->dif_prd_table_addr =
1136 		cpu_to_le64(hisi_sas_sge_dif_addr_dma(slot));
1137 
1138 	hdr->sg_len |= cpu_to_le32(n_elem << CMD_HDR_DIF_SGL_LEN_OFF);
1139 }
1140 
1141 static u32 get_prot_chk_msk_v3_hw(struct scsi_cmnd *scsi_cmnd)
1142 {
1143 	unsigned char prot_flags = scsi_cmnd->prot_flags;
1144 
1145 	if (prot_flags & SCSI_PROT_REF_CHECK)
1146 		return T10_CHK_APP_TAG_MSK;
1147 	return T10_CHK_REF_TAG_MSK | T10_CHK_APP_TAG_MSK;
1148 }
1149 
1150 static void fill_prot_v3_hw(struct scsi_cmnd *scsi_cmnd,
1151 			    struct hisi_sas_protect_iu_v3_hw *prot)
1152 {
1153 	unsigned char prot_op = scsi_get_prot_op(scsi_cmnd);
1154 	unsigned int interval = scsi_prot_interval(scsi_cmnd);
1155 	u32 lbrt_chk_val = t10_pi_ref_tag(scsi_cmnd->request);
1156 
1157 	switch (prot_op) {
1158 	case SCSI_PROT_READ_INSERT:
1159 		prot->dw0 |= T10_INSRT_EN_MSK;
1160 		prot->lbrtgv = lbrt_chk_val;
1161 		break;
1162 	case SCSI_PROT_READ_STRIP:
1163 		prot->dw0 |= (T10_RMV_EN_MSK | T10_CHK_EN_MSK);
1164 		prot->lbrtcv = lbrt_chk_val;
1165 		prot->dw4 |= get_prot_chk_msk_v3_hw(scsi_cmnd);
1166 		break;
1167 	case SCSI_PROT_READ_PASS:
1168 		prot->dw0 |= T10_CHK_EN_MSK;
1169 		prot->lbrtcv = lbrt_chk_val;
1170 		prot->dw4 |= get_prot_chk_msk_v3_hw(scsi_cmnd);
1171 		break;
1172 	case SCSI_PROT_WRITE_INSERT:
1173 		prot->dw0 |= T10_INSRT_EN_MSK;
1174 		prot->lbrtgv = lbrt_chk_val;
1175 		break;
1176 	case SCSI_PROT_WRITE_STRIP:
1177 		prot->dw0 |= (T10_RMV_EN_MSK | T10_CHK_EN_MSK);
1178 		prot->lbrtcv = lbrt_chk_val;
1179 		break;
1180 	case SCSI_PROT_WRITE_PASS:
1181 		prot->dw0 |= T10_CHK_EN_MSK;
1182 		prot->lbrtcv = lbrt_chk_val;
1183 		prot->dw4 |= get_prot_chk_msk_v3_hw(scsi_cmnd);
1184 		break;
1185 	default:
1186 		WARN(1, "prot_op(0x%x) is not valid\n", prot_op);
1187 		break;
1188 	}
1189 
1190 	switch (interval) {
1191 	case 512:
1192 		break;
1193 	case 4096:
1194 		prot->dw0 |= (0x1 << USR_DATA_BLOCK_SZ_OFF);
1195 		break;
1196 	case 520:
1197 		prot->dw0 |= (0x2 << USR_DATA_BLOCK_SZ_OFF);
1198 		break;
1199 	default:
1200 		WARN(1, "protection interval (0x%x) invalid\n",
1201 		     interval);
1202 		break;
1203 	}
1204 
1205 	prot->dw0 |= INCR_LBRT_MSK;
1206 }
1207 
1208 static void prep_ssp_v3_hw(struct hisi_hba *hisi_hba,
1209 			  struct hisi_sas_slot *slot)
1210 {
1211 	struct sas_task *task = slot->task;
1212 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1213 	struct domain_device *device = task->dev;
1214 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1215 	struct hisi_sas_port *port = slot->port;
1216 	struct sas_ssp_task *ssp_task = &task->ssp_task;
1217 	struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
1218 	struct hisi_sas_tmf_task *tmf = slot->tmf;
1219 	int has_data = 0, priority = !!tmf;
1220 	unsigned char prot_op;
1221 	u8 *buf_cmd;
1222 	u32 dw1 = 0, dw2 = 0, len = 0;
1223 
1224 	hdr->dw0 = cpu_to_le32((1 << CMD_HDR_RESP_REPORT_OFF) |
1225 			       (2 << CMD_HDR_TLR_CTRL_OFF) |
1226 			       (port->id << CMD_HDR_PORT_OFF) |
1227 			       (priority << CMD_HDR_PRIORITY_OFF) |
1228 			       (1 << CMD_HDR_CMD_OFF)); /* ssp */
1229 
1230 	dw1 = 1 << CMD_HDR_VDTL_OFF;
1231 	if (tmf) {
1232 		dw1 |= 2 << CMD_HDR_FRAME_TYPE_OFF;
1233 		dw1 |= DIR_NO_DATA << CMD_HDR_DIR_OFF;
1234 	} else {
1235 		prot_op = scsi_get_prot_op(scsi_cmnd);
1236 		dw1 |= 1 << CMD_HDR_FRAME_TYPE_OFF;
1237 		switch (scsi_cmnd->sc_data_direction) {
1238 		case DMA_TO_DEVICE:
1239 			has_data = 1;
1240 			dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
1241 			break;
1242 		case DMA_FROM_DEVICE:
1243 			has_data = 1;
1244 			dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
1245 			break;
1246 		default:
1247 			dw1 &= ~CMD_HDR_DIR_MSK;
1248 		}
1249 	}
1250 
1251 	/* map itct entry */
1252 	dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
1253 
1254 	dw2 = (((sizeof(struct ssp_command_iu) + sizeof(struct ssp_frame_hdr)
1255 	      + 3) / 4) << CMD_HDR_CFL_OFF) |
1256 	      ((HISI_SAS_MAX_SSP_RESP_SZ / 4) << CMD_HDR_MRFL_OFF) |
1257 	      (2 << CMD_HDR_SG_MOD_OFF);
1258 	hdr->dw2 = cpu_to_le32(dw2);
1259 	hdr->transfer_tags = cpu_to_le32(slot->idx);
1260 
1261 	if (has_data) {
1262 		prep_prd_sge_v3_hw(hisi_hba, slot, hdr, task->scatter,
1263 				   slot->n_elem);
1264 
1265 		if (scsi_prot_sg_count(scsi_cmnd))
1266 			prep_prd_sge_dif_v3_hw(hisi_hba, slot, hdr,
1267 					       scsi_prot_sglist(scsi_cmnd),
1268 					       slot->n_elem_dif);
1269 	}
1270 
1271 	hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
1272 	hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
1273 
1274 	buf_cmd = hisi_sas_cmd_hdr_addr_mem(slot) +
1275 		sizeof(struct ssp_frame_hdr);
1276 
1277 	memcpy(buf_cmd, &task->ssp_task.LUN, 8);
1278 	if (!tmf) {
1279 		buf_cmd[9] = ssp_task->task_attr | (ssp_task->task_prio << 3);
1280 		memcpy(buf_cmd + 12, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
1281 	} else {
1282 		buf_cmd[10] = tmf->tmf;
1283 		switch (tmf->tmf) {
1284 		case TMF_ABORT_TASK:
1285 		case TMF_QUERY_TASK:
1286 			buf_cmd[12] =
1287 				(tmf->tag_of_task_to_be_managed >> 8) & 0xff;
1288 			buf_cmd[13] =
1289 				tmf->tag_of_task_to_be_managed & 0xff;
1290 			break;
1291 		default:
1292 			break;
1293 		}
1294 	}
1295 
1296 	if (has_data && (prot_op != SCSI_PROT_NORMAL)) {
1297 		struct hisi_sas_protect_iu_v3_hw prot;
1298 		u8 *buf_cmd_prot;
1299 
1300 		hdr->dw7 |= cpu_to_le32(1 << CMD_HDR_ADDR_MODE_SEL_OFF);
1301 		dw1 |= CMD_HDR_PIR_MSK;
1302 		buf_cmd_prot = hisi_sas_cmd_hdr_addr_mem(slot) +
1303 			       sizeof(struct ssp_frame_hdr) +
1304 			       sizeof(struct ssp_command_iu);
1305 
1306 		memset(&prot, 0, sizeof(struct hisi_sas_protect_iu_v3_hw));
1307 		fill_prot_v3_hw(scsi_cmnd, &prot);
1308 		memcpy(buf_cmd_prot, &prot,
1309 		       sizeof(struct hisi_sas_protect_iu_v3_hw));
1310 		/*
1311 		 * For READ, we need length of info read to memory, while for
1312 		 * WRITE we need length of data written to the disk.
1313 		 */
1314 		if (prot_op == SCSI_PROT_WRITE_INSERT ||
1315 		    prot_op == SCSI_PROT_READ_INSERT ||
1316 		    prot_op == SCSI_PROT_WRITE_PASS ||
1317 		    prot_op == SCSI_PROT_READ_PASS) {
1318 			unsigned int interval = scsi_prot_interval(scsi_cmnd);
1319 			unsigned int ilog2_interval = ilog2(interval);
1320 
1321 			len = (task->total_xfer_len >> ilog2_interval) * 8;
1322 		}
1323 	}
1324 
1325 	hdr->dw1 = cpu_to_le32(dw1);
1326 
1327 	hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len + len);
1328 }
1329 
1330 static void prep_smp_v3_hw(struct hisi_hba *hisi_hba,
1331 			  struct hisi_sas_slot *slot)
1332 {
1333 	struct sas_task *task = slot->task;
1334 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1335 	struct domain_device *device = task->dev;
1336 	struct hisi_sas_port *port = slot->port;
1337 	struct scatterlist *sg_req;
1338 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1339 	dma_addr_t req_dma_addr;
1340 	unsigned int req_len;
1341 
1342 	/* req */
1343 	sg_req = &task->smp_task.smp_req;
1344 	req_len = sg_dma_len(sg_req);
1345 	req_dma_addr = sg_dma_address(sg_req);
1346 
1347 	/* create header */
1348 	/* dw0 */
1349 	hdr->dw0 = cpu_to_le32((port->id << CMD_HDR_PORT_OFF) |
1350 			       (1 << CMD_HDR_PRIORITY_OFF) | /* high pri */
1351 			       (2 << CMD_HDR_CMD_OFF)); /* smp */
1352 
1353 	/* map itct entry */
1354 	hdr->dw1 = cpu_to_le32((sas_dev->device_id << CMD_HDR_DEV_ID_OFF) |
1355 			       (1 << CMD_HDR_FRAME_TYPE_OFF) |
1356 			       (DIR_NO_DATA << CMD_HDR_DIR_OFF));
1357 
1358 	/* dw2 */
1359 	hdr->dw2 = cpu_to_le32((((req_len - 4) / 4) << CMD_HDR_CFL_OFF) |
1360 			       (HISI_SAS_MAX_SMP_RESP_SZ / 4 <<
1361 			       CMD_HDR_MRFL_OFF));
1362 
1363 	hdr->transfer_tags = cpu_to_le32(slot->idx << CMD_HDR_IPTT_OFF);
1364 
1365 	hdr->cmd_table_addr = cpu_to_le64(req_dma_addr);
1366 	hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
1367 }
1368 
1369 static void prep_ata_v3_hw(struct hisi_hba *hisi_hba,
1370 			  struct hisi_sas_slot *slot)
1371 {
1372 	struct sas_task *task = slot->task;
1373 	struct domain_device *device = task->dev;
1374 	struct domain_device *parent_dev = device->parent;
1375 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1376 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1377 	struct asd_sas_port *sas_port = device->port;
1378 	struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
1379 	u8 *buf_cmd;
1380 	int has_data = 0, hdr_tag = 0;
1381 	u32 dw1 = 0, dw2 = 0;
1382 
1383 	hdr->dw0 = cpu_to_le32(port->id << CMD_HDR_PORT_OFF);
1384 	if (parent_dev && dev_is_expander(parent_dev->dev_type))
1385 		hdr->dw0 |= cpu_to_le32(3 << CMD_HDR_CMD_OFF);
1386 	else
1387 		hdr->dw0 |= cpu_to_le32(4U << CMD_HDR_CMD_OFF);
1388 
1389 	switch (task->data_dir) {
1390 	case DMA_TO_DEVICE:
1391 		has_data = 1;
1392 		dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
1393 		break;
1394 	case DMA_FROM_DEVICE:
1395 		has_data = 1;
1396 		dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
1397 		break;
1398 	default:
1399 		dw1 &= ~CMD_HDR_DIR_MSK;
1400 	}
1401 
1402 	if ((task->ata_task.fis.command == ATA_CMD_DEV_RESET) &&
1403 			(task->ata_task.fis.control & ATA_SRST))
1404 		dw1 |= 1 << CMD_HDR_RESET_OFF;
1405 
1406 	dw1 |= (hisi_sas_get_ata_protocol(
1407 		&task->ata_task.fis, task->data_dir))
1408 		<< CMD_HDR_FRAME_TYPE_OFF;
1409 	dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
1410 
1411 	if (FIS_CMD_IS_UNCONSTRAINED(task->ata_task.fis))
1412 		dw1 |= 1 << CMD_HDR_UNCON_CMD_OFF;
1413 
1414 	hdr->dw1 = cpu_to_le32(dw1);
1415 
1416 	/* dw2 */
1417 	if (task->ata_task.use_ncq) {
1418 		struct ata_queued_cmd *qc = task->uldd_task;
1419 
1420 		hdr_tag = qc->tag;
1421 		task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
1422 		dw2 |= hdr_tag << CMD_HDR_NCQ_TAG_OFF;
1423 	}
1424 
1425 	dw2 |= (HISI_SAS_MAX_STP_RESP_SZ / 4) << CMD_HDR_CFL_OFF |
1426 			2 << CMD_HDR_SG_MOD_OFF;
1427 	hdr->dw2 = cpu_to_le32(dw2);
1428 
1429 	/* dw3 */
1430 	hdr->transfer_tags = cpu_to_le32(slot->idx);
1431 
1432 	if (has_data)
1433 		prep_prd_sge_v3_hw(hisi_hba, slot, hdr, task->scatter,
1434 					slot->n_elem);
1435 
1436 	hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
1437 	hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
1438 	hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
1439 
1440 	buf_cmd = hisi_sas_cmd_hdr_addr_mem(slot);
1441 
1442 	if (likely(!task->ata_task.device_control_reg_update))
1443 		task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
1444 	/* fill in command FIS */
1445 	memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
1446 }
1447 
1448 static void prep_abort_v3_hw(struct hisi_hba *hisi_hba,
1449 		struct hisi_sas_slot *slot,
1450 		int device_id, int abort_flag, int tag_to_abort)
1451 {
1452 	struct sas_task *task = slot->task;
1453 	struct domain_device *dev = task->dev;
1454 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1455 	struct hisi_sas_port *port = slot->port;
1456 
1457 	/* dw0 */
1458 	hdr->dw0 = cpu_to_le32((5U << CMD_HDR_CMD_OFF) | /*abort*/
1459 			       (port->id << CMD_HDR_PORT_OFF) |
1460 				   (dev_is_sata(dev)
1461 					<< CMD_HDR_ABORT_DEVICE_TYPE_OFF) |
1462 					(abort_flag
1463 					 << CMD_HDR_ABORT_FLAG_OFF));
1464 
1465 	/* dw1 */
1466 	hdr->dw1 = cpu_to_le32(device_id
1467 			<< CMD_HDR_DEV_ID_OFF);
1468 
1469 	/* dw7 */
1470 	hdr->dw7 = cpu_to_le32(tag_to_abort << CMD_HDR_ABORT_IPTT_OFF);
1471 	hdr->transfer_tags = cpu_to_le32(slot->idx);
1472 }
1473 
1474 static irqreturn_t phy_up_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
1475 {
1476 	int i;
1477 	irqreturn_t res;
1478 	u32 context, port_id, link_rate;
1479 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1480 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1481 	struct device *dev = hisi_hba->dev;
1482 	unsigned long flags;
1483 
1484 	del_timer(&phy->timer);
1485 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 1);
1486 
1487 	port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
1488 	port_id = (port_id >> (4 * phy_no)) & 0xf;
1489 	link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
1490 	link_rate = (link_rate >> (phy_no * 4)) & 0xf;
1491 
1492 	if (port_id == 0xf) {
1493 		dev_err(dev, "phyup: phy%d invalid portid\n", phy_no);
1494 		res = IRQ_NONE;
1495 		goto end;
1496 	}
1497 	sas_phy->linkrate = link_rate;
1498 	phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1499 
1500 	/* Check for SATA dev */
1501 	context = hisi_sas_read32(hisi_hba, PHY_CONTEXT);
1502 	if (context & (1 << phy_no)) {
1503 		struct hisi_sas_initial_fis *initial_fis;
1504 		struct dev_to_host_fis *fis;
1505 		u8 attached_sas_addr[SAS_ADDR_SIZE] = {0};
1506 		struct Scsi_Host *shost = hisi_hba->shost;
1507 
1508 		dev_info(dev, "phyup: phy%d link_rate=%d(sata)\n", phy_no, link_rate);
1509 		initial_fis = &hisi_hba->initial_fis[phy_no];
1510 		fis = &initial_fis->fis;
1511 
1512 		/* check ERR bit of Status Register */
1513 		if (fis->status & ATA_ERR) {
1514 			dev_warn(dev, "sata int: phy%d FIS status: 0x%x\n",
1515 				 phy_no, fis->status);
1516 			hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
1517 			res = IRQ_NONE;
1518 			goto end;
1519 		}
1520 
1521 		sas_phy->oob_mode = SATA_OOB_MODE;
1522 		attached_sas_addr[0] = 0x50;
1523 		attached_sas_addr[6] = shost->host_no;
1524 		attached_sas_addr[7] = phy_no;
1525 		memcpy(sas_phy->attached_sas_addr,
1526 		       attached_sas_addr,
1527 		       SAS_ADDR_SIZE);
1528 		memcpy(sas_phy->frame_rcvd, fis,
1529 		       sizeof(struct dev_to_host_fis));
1530 		phy->phy_type |= PORT_TYPE_SATA;
1531 		phy->identify.device_type = SAS_SATA_DEV;
1532 		phy->frame_rcvd_size = sizeof(struct dev_to_host_fis);
1533 		phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
1534 	} else {
1535 		u32 *frame_rcvd = (u32 *)sas_phy->frame_rcvd;
1536 		struct sas_identify_frame *id =
1537 			(struct sas_identify_frame *)frame_rcvd;
1538 
1539 		dev_info(dev, "phyup: phy%d link_rate=%d\n", phy_no, link_rate);
1540 		for (i = 0; i < 6; i++) {
1541 			u32 idaf = hisi_sas_phy_read32(hisi_hba, phy_no,
1542 					       RX_IDAF_DWORD0 + (i * 4));
1543 			frame_rcvd[i] = __swab32(idaf);
1544 		}
1545 		sas_phy->oob_mode = SAS_OOB_MODE;
1546 		memcpy(sas_phy->attached_sas_addr,
1547 		       &id->sas_addr,
1548 		       SAS_ADDR_SIZE);
1549 		phy->phy_type |= PORT_TYPE_SAS;
1550 		phy->identify.device_type = id->dev_type;
1551 		phy->frame_rcvd_size = sizeof(struct sas_identify_frame);
1552 		if (phy->identify.device_type == SAS_END_DEVICE)
1553 			phy->identify.target_port_protocols =
1554 				SAS_PROTOCOL_SSP;
1555 		else if (phy->identify.device_type != SAS_PHY_UNUSED)
1556 			phy->identify.target_port_protocols =
1557 				SAS_PROTOCOL_SMP;
1558 	}
1559 
1560 	phy->port_id = port_id;
1561 	phy->phy_attached = 1;
1562 	hisi_sas_notify_phy_event(phy, HISI_PHYE_PHY_UP);
1563 	res = IRQ_HANDLED;
1564 	spin_lock_irqsave(&phy->lock, flags);
1565 	if (phy->reset_completion) {
1566 		phy->in_reset = 0;
1567 		complete(phy->reset_completion);
1568 	}
1569 	spin_unlock_irqrestore(&phy->lock, flags);
1570 end:
1571 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
1572 			     CHL_INT0_SL_PHY_ENABLE_MSK);
1573 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 0);
1574 
1575 	return res;
1576 }
1577 
1578 static irqreturn_t phy_down_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
1579 {
1580 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1581 	u32 phy_state, sl_ctrl, txid_auto;
1582 	struct device *dev = hisi_hba->dev;
1583 
1584 	atomic_inc(&phy->down_cnt);
1585 
1586 	del_timer(&phy->timer);
1587 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 1);
1588 
1589 	phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1590 	dev_info(dev, "phydown: phy%d phy_state=0x%x\n", phy_no, phy_state);
1591 	hisi_sas_phy_down(hisi_hba, phy_no, (phy_state & 1 << phy_no) ? 1 : 0,
1592 			  GFP_ATOMIC);
1593 
1594 	sl_ctrl = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1595 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL,
1596 						sl_ctrl&(~SL_CTA_MSK));
1597 
1598 	txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
1599 	hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
1600 						txid_auto | CT3_MSK);
1601 
1602 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0, CHL_INT0_NOT_RDY_MSK);
1603 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 0);
1604 
1605 	return IRQ_HANDLED;
1606 }
1607 
1608 static irqreturn_t phy_bcast_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
1609 {
1610 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1611 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1612 	u32 bcast_status;
1613 
1614 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 1);
1615 	bcast_status = hisi_sas_phy_read32(hisi_hba, phy_no, RX_PRIMS_STATUS);
1616 	if ((bcast_status & RX_BCAST_CHG_MSK) &&
1617 	    !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1618 		sas_notify_port_event(sas_phy, PORTE_BROADCAST_RCVD,
1619 				      GFP_ATOMIC);
1620 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
1621 			     CHL_INT0_SL_RX_BCST_ACK_MSK);
1622 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 0);
1623 
1624 	return IRQ_HANDLED;
1625 }
1626 
1627 static irqreturn_t int_phy_up_down_bcast_v3_hw(int irq_no, void *p)
1628 {
1629 	struct hisi_hba *hisi_hba = p;
1630 	u32 irq_msk;
1631 	int phy_no = 0;
1632 	irqreturn_t res = IRQ_NONE;
1633 
1634 	irq_msk = hisi_sas_read32(hisi_hba, CHNL_INT_STATUS)
1635 				& 0x11111111;
1636 	while (irq_msk) {
1637 		if (irq_msk  & 1) {
1638 			u32 irq_value = hisi_sas_phy_read32(hisi_hba, phy_no,
1639 							    CHL_INT0);
1640 			u32 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1641 			int rdy = phy_state & (1 << phy_no);
1642 
1643 			if (rdy) {
1644 				if (irq_value & CHL_INT0_SL_PHY_ENABLE_MSK)
1645 					/* phy up */
1646 					if (phy_up_v3_hw(phy_no, hisi_hba)
1647 							== IRQ_HANDLED)
1648 						res = IRQ_HANDLED;
1649 				if (irq_value & CHL_INT0_SL_RX_BCST_ACK_MSK)
1650 					/* phy bcast */
1651 					if (phy_bcast_v3_hw(phy_no, hisi_hba)
1652 							== IRQ_HANDLED)
1653 						res = IRQ_HANDLED;
1654 			} else {
1655 				if (irq_value & CHL_INT0_NOT_RDY_MSK)
1656 					/* phy down */
1657 					if (phy_down_v3_hw(phy_no, hisi_hba)
1658 							== IRQ_HANDLED)
1659 						res = IRQ_HANDLED;
1660 			}
1661 		}
1662 		irq_msk >>= 4;
1663 		phy_no++;
1664 	}
1665 
1666 	return res;
1667 }
1668 
1669 static const struct hisi_sas_hw_error port_axi_error[] = {
1670 	{
1671 		.irq_msk = BIT(CHL_INT1_DMAC_TX_ECC_MB_ERR_OFF),
1672 		.msg = "dmac_tx_ecc_bad_err",
1673 	},
1674 	{
1675 		.irq_msk = BIT(CHL_INT1_DMAC_RX_ECC_MB_ERR_OFF),
1676 		.msg = "dmac_rx_ecc_bad_err",
1677 	},
1678 	{
1679 		.irq_msk = BIT(CHL_INT1_DMAC_TX_AXI_WR_ERR_OFF),
1680 		.msg = "dma_tx_axi_wr_err",
1681 	},
1682 	{
1683 		.irq_msk = BIT(CHL_INT1_DMAC_TX_AXI_RD_ERR_OFF),
1684 		.msg = "dma_tx_axi_rd_err",
1685 	},
1686 	{
1687 		.irq_msk = BIT(CHL_INT1_DMAC_RX_AXI_WR_ERR_OFF),
1688 		.msg = "dma_rx_axi_wr_err",
1689 	},
1690 	{
1691 		.irq_msk = BIT(CHL_INT1_DMAC_RX_AXI_RD_ERR_OFF),
1692 		.msg = "dma_rx_axi_rd_err",
1693 	},
1694 	{
1695 		.irq_msk = BIT(CHL_INT1_DMAC_TX_FIFO_ERR_OFF),
1696 		.msg = "dma_tx_fifo_err",
1697 	},
1698 	{
1699 		.irq_msk = BIT(CHL_INT1_DMAC_RX_FIFO_ERR_OFF),
1700 		.msg = "dma_rx_fifo_err",
1701 	},
1702 	{
1703 		.irq_msk = BIT(CHL_INT1_DMAC_TX_AXI_RUSER_ERR_OFF),
1704 		.msg = "dma_tx_axi_ruser_err",
1705 	},
1706 	{
1707 		.irq_msk = BIT(CHL_INT1_DMAC_RX_AXI_RUSER_ERR_OFF),
1708 		.msg = "dma_rx_axi_ruser_err",
1709 	},
1710 };
1711 
1712 static void handle_chl_int1_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1713 {
1714 	u32 irq_value = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT1);
1715 	u32 irq_msk = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT1_MSK);
1716 	struct device *dev = hisi_hba->dev;
1717 	int i;
1718 
1719 	irq_value &= ~irq_msk;
1720 	if (!irq_value)
1721 		return;
1722 
1723 	for (i = 0; i < ARRAY_SIZE(port_axi_error); i++) {
1724 		const struct hisi_sas_hw_error *error = &port_axi_error[i];
1725 
1726 		if (!(irq_value & error->irq_msk))
1727 			continue;
1728 
1729 		dev_err(dev, "%s error (phy%d 0x%x) found!\n",
1730 			error->msg, phy_no, irq_value);
1731 		queue_work(hisi_hba->wq, &hisi_hba->rst_work);
1732 	}
1733 
1734 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT1, irq_value);
1735 }
1736 
1737 static void phy_get_events_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1738 {
1739 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1740 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1741 	struct sas_phy *sphy = sas_phy->phy;
1742 	unsigned long flags;
1743 	u32 reg_value;
1744 
1745 	spin_lock_irqsave(&phy->lock, flags);
1746 
1747 	/* loss dword sync */
1748 	reg_value = hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_DWS_LOST);
1749 	sphy->loss_of_dword_sync_count += reg_value;
1750 
1751 	/* phy reset problem */
1752 	reg_value = hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_RESET_PROB);
1753 	sphy->phy_reset_problem_count += reg_value;
1754 
1755 	/* invalid dword */
1756 	reg_value = hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_INVLD_DW);
1757 	sphy->invalid_dword_count += reg_value;
1758 
1759 	/* disparity err */
1760 	reg_value = hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_DISP_ERR);
1761 	sphy->running_disparity_error_count += reg_value;
1762 
1763 	/* code violation error */
1764 	reg_value = hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_CODE_ERR);
1765 	phy->code_violation_err_count += reg_value;
1766 
1767 	spin_unlock_irqrestore(&phy->lock, flags);
1768 }
1769 
1770 static void handle_chl_int2_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1771 {
1772 	u32 irq_msk = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT2_MSK);
1773 	u32 irq_value = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT2);
1774 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1775 	struct pci_dev *pci_dev = hisi_hba->pci_dev;
1776 	struct device *dev = hisi_hba->dev;
1777 	static const u32 msk = BIT(CHL_INT2_RX_DISP_ERR_OFF) |
1778 			BIT(CHL_INT2_RX_CODE_ERR_OFF) |
1779 			BIT(CHL_INT2_RX_INVLD_DW_OFF);
1780 
1781 	irq_value &= ~irq_msk;
1782 	if (!irq_value)
1783 		return;
1784 
1785 	if (irq_value & BIT(CHL_INT2_SL_IDAF_TOUT_CONF_OFF)) {
1786 		dev_warn(dev, "phy%d identify timeout\n", phy_no);
1787 		hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
1788 	}
1789 
1790 	if (irq_value & BIT(CHL_INT2_STP_LINK_TIMEOUT_OFF)) {
1791 		u32 reg_value = hisi_sas_phy_read32(hisi_hba, phy_no,
1792 				STP_LINK_TIMEOUT_STATE);
1793 
1794 		dev_warn(dev, "phy%d stp link timeout (0x%x)\n",
1795 			 phy_no, reg_value);
1796 		if (reg_value & BIT(4))
1797 			hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
1798 	}
1799 
1800 	if (pci_dev->revision > 0x20 && (irq_value & msk)) {
1801 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
1802 		struct sas_phy *sphy = sas_phy->phy;
1803 
1804 		phy_get_events_v3_hw(hisi_hba, phy_no);
1805 
1806 		if (irq_value & BIT(CHL_INT2_RX_INVLD_DW_OFF))
1807 			dev_info(dev, "phy%d invalid dword cnt:   %u\n", phy_no,
1808 				 sphy->invalid_dword_count);
1809 
1810 		if (irq_value & BIT(CHL_INT2_RX_CODE_ERR_OFF))
1811 			dev_info(dev, "phy%d code violation cnt:  %u\n", phy_no,
1812 				 phy->code_violation_err_count);
1813 
1814 		if (irq_value & BIT(CHL_INT2_RX_DISP_ERR_OFF))
1815 			dev_info(dev, "phy%d disparity error cnt: %u\n", phy_no,
1816 				 sphy->running_disparity_error_count);
1817 	}
1818 
1819 	if ((irq_value & BIT(CHL_INT2_RX_INVLD_DW_OFF)) &&
1820 	    (pci_dev->revision == 0x20)) {
1821 		u32 reg_value;
1822 		int rc;
1823 
1824 		rc = hisi_sas_read32_poll_timeout_atomic(
1825 				HILINK_ERR_DFX, reg_value,
1826 				!((reg_value >> 8) & BIT(phy_no)),
1827 				1000, 10000);
1828 		if (rc)
1829 			hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
1830 	}
1831 
1832 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2, irq_value);
1833 }
1834 
1835 static void handle_chl_int0_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1836 {
1837 	u32 irq_value0 = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT0);
1838 
1839 	if (irq_value0 & CHL_INT0_PHY_RDY_MSK)
1840 		hisi_sas_phy_oob_ready(hisi_hba, phy_no);
1841 
1842 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
1843 			     irq_value0 & (~CHL_INT0_SL_RX_BCST_ACK_MSK)
1844 			     & (~CHL_INT0_SL_PHY_ENABLE_MSK)
1845 			     & (~CHL_INT0_NOT_RDY_MSK));
1846 }
1847 
1848 static irqreturn_t int_chnl_int_v3_hw(int irq_no, void *p)
1849 {
1850 	struct hisi_hba *hisi_hba = p;
1851 	u32 irq_msk;
1852 	int phy_no = 0;
1853 
1854 	irq_msk = hisi_sas_read32(hisi_hba, CHNL_INT_STATUS)
1855 		  & CHNL_INT_STS_MSK;
1856 
1857 	while (irq_msk) {
1858 		if (irq_msk & (CHNL_INT_STS_INT0_MSK << (phy_no * CHNL_WIDTH)))
1859 			handle_chl_int0_v3_hw(hisi_hba, phy_no);
1860 
1861 		if (irq_msk & (CHNL_INT_STS_INT1_MSK << (phy_no * CHNL_WIDTH)))
1862 			handle_chl_int1_v3_hw(hisi_hba, phy_no);
1863 
1864 		if (irq_msk & (CHNL_INT_STS_INT2_MSK << (phy_no * CHNL_WIDTH)))
1865 			handle_chl_int2_v3_hw(hisi_hba, phy_no);
1866 
1867 		irq_msk &= ~(CHNL_INT_STS_PHY_MSK << (phy_no * CHNL_WIDTH));
1868 		phy_no++;
1869 	}
1870 
1871 	return IRQ_HANDLED;
1872 }
1873 
1874 static const struct hisi_sas_hw_error multi_bit_ecc_errors[] = {
1875 	{
1876 		.irq_msk = BIT(SAS_ECC_INTR_DQE_ECC_MB_OFF),
1877 		.msk = HGC_DQE_ECC_MB_ADDR_MSK,
1878 		.shift = HGC_DQE_ECC_MB_ADDR_OFF,
1879 		.msg = "hgc_dqe_eccbad_intr",
1880 		.reg = HGC_DQE_ECC_ADDR,
1881 	},
1882 	{
1883 		.irq_msk = BIT(SAS_ECC_INTR_IOST_ECC_MB_OFF),
1884 		.msk = HGC_IOST_ECC_MB_ADDR_MSK,
1885 		.shift = HGC_IOST_ECC_MB_ADDR_OFF,
1886 		.msg = "hgc_iost_eccbad_intr",
1887 		.reg = HGC_IOST_ECC_ADDR,
1888 	},
1889 	{
1890 		.irq_msk = BIT(SAS_ECC_INTR_ITCT_ECC_MB_OFF),
1891 		.msk = HGC_ITCT_ECC_MB_ADDR_MSK,
1892 		.shift = HGC_ITCT_ECC_MB_ADDR_OFF,
1893 		.msg = "hgc_itct_eccbad_intr",
1894 		.reg = HGC_ITCT_ECC_ADDR,
1895 	},
1896 	{
1897 		.irq_msk = BIT(SAS_ECC_INTR_IOSTLIST_ECC_MB_OFF),
1898 		.msk = HGC_LM_DFX_STATUS2_IOSTLIST_MSK,
1899 		.shift = HGC_LM_DFX_STATUS2_IOSTLIST_OFF,
1900 		.msg = "hgc_iostl_eccbad_intr",
1901 		.reg = HGC_LM_DFX_STATUS2,
1902 	},
1903 	{
1904 		.irq_msk = BIT(SAS_ECC_INTR_ITCTLIST_ECC_MB_OFF),
1905 		.msk = HGC_LM_DFX_STATUS2_ITCTLIST_MSK,
1906 		.shift = HGC_LM_DFX_STATUS2_ITCTLIST_OFF,
1907 		.msg = "hgc_itctl_eccbad_intr",
1908 		.reg = HGC_LM_DFX_STATUS2,
1909 	},
1910 	{
1911 		.irq_msk = BIT(SAS_ECC_INTR_CQE_ECC_MB_OFF),
1912 		.msk = HGC_CQE_ECC_MB_ADDR_MSK,
1913 		.shift = HGC_CQE_ECC_MB_ADDR_OFF,
1914 		.msg = "hgc_cqe_eccbad_intr",
1915 		.reg = HGC_CQE_ECC_ADDR,
1916 	},
1917 	{
1918 		.irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM0_ECC_MB_OFF),
1919 		.msk = HGC_RXM_DFX_STATUS14_MEM0_MSK,
1920 		.shift = HGC_RXM_DFX_STATUS14_MEM0_OFF,
1921 		.msg = "rxm_mem0_eccbad_intr",
1922 		.reg = HGC_RXM_DFX_STATUS14,
1923 	},
1924 	{
1925 		.irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM1_ECC_MB_OFF),
1926 		.msk = HGC_RXM_DFX_STATUS14_MEM1_MSK,
1927 		.shift = HGC_RXM_DFX_STATUS14_MEM1_OFF,
1928 		.msg = "rxm_mem1_eccbad_intr",
1929 		.reg = HGC_RXM_DFX_STATUS14,
1930 	},
1931 	{
1932 		.irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM2_ECC_MB_OFF),
1933 		.msk = HGC_RXM_DFX_STATUS14_MEM2_MSK,
1934 		.shift = HGC_RXM_DFX_STATUS14_MEM2_OFF,
1935 		.msg = "rxm_mem2_eccbad_intr",
1936 		.reg = HGC_RXM_DFX_STATUS14,
1937 	},
1938 	{
1939 		.irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM3_ECC_MB_OFF),
1940 		.msk = HGC_RXM_DFX_STATUS15_MEM3_MSK,
1941 		.shift = HGC_RXM_DFX_STATUS15_MEM3_OFF,
1942 		.msg = "rxm_mem3_eccbad_intr",
1943 		.reg = HGC_RXM_DFX_STATUS15,
1944 	},
1945 	{
1946 		.irq_msk = BIT(SAS_ECC_INTR_OOO_RAM_ECC_MB_OFF),
1947 		.msk = AM_ROB_ECC_ERR_ADDR_MSK,
1948 		.shift = AM_ROB_ECC_ERR_ADDR_OFF,
1949 		.msg = "ooo_ram_eccbad_intr",
1950 		.reg = AM_ROB_ECC_ERR_ADDR,
1951 	},
1952 };
1953 
1954 static void multi_bit_ecc_error_process_v3_hw(struct hisi_hba *hisi_hba,
1955 					      u32 irq_value)
1956 {
1957 	struct device *dev = hisi_hba->dev;
1958 	const struct hisi_sas_hw_error *ecc_error;
1959 	u32 val;
1960 	int i;
1961 
1962 	for (i = 0; i < ARRAY_SIZE(multi_bit_ecc_errors); i++) {
1963 		ecc_error = &multi_bit_ecc_errors[i];
1964 		if (irq_value & ecc_error->irq_msk) {
1965 			val = hisi_sas_read32(hisi_hba, ecc_error->reg);
1966 			val &= ecc_error->msk;
1967 			val >>= ecc_error->shift;
1968 			dev_err(dev, "%s (0x%x) found: mem addr is 0x%08X\n",
1969 				ecc_error->msg, irq_value, val);
1970 			queue_work(hisi_hba->wq, &hisi_hba->rst_work);
1971 		}
1972 	}
1973 }
1974 
1975 static void fatal_ecc_int_v3_hw(struct hisi_hba *hisi_hba)
1976 {
1977 	u32 irq_value, irq_msk;
1978 
1979 	irq_msk = hisi_sas_read32(hisi_hba, SAS_ECC_INTR_MSK);
1980 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xffffffff);
1981 
1982 	irq_value = hisi_sas_read32(hisi_hba, SAS_ECC_INTR);
1983 	if (irq_value)
1984 		multi_bit_ecc_error_process_v3_hw(hisi_hba, irq_value);
1985 
1986 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR, irq_value);
1987 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, irq_msk);
1988 }
1989 
1990 static const struct hisi_sas_hw_error axi_error[] = {
1991 	{ .msk = BIT(0), .msg = "IOST_AXI_W_ERR" },
1992 	{ .msk = BIT(1), .msg = "IOST_AXI_R_ERR" },
1993 	{ .msk = BIT(2), .msg = "ITCT_AXI_W_ERR" },
1994 	{ .msk = BIT(3), .msg = "ITCT_AXI_R_ERR" },
1995 	{ .msk = BIT(4), .msg = "SATA_AXI_W_ERR" },
1996 	{ .msk = BIT(5), .msg = "SATA_AXI_R_ERR" },
1997 	{ .msk = BIT(6), .msg = "DQE_AXI_R_ERR" },
1998 	{ .msk = BIT(7), .msg = "CQE_AXI_W_ERR" },
1999 	{}
2000 };
2001 
2002 static const struct hisi_sas_hw_error fifo_error[] = {
2003 	{ .msk = BIT(8),  .msg = "CQE_WINFO_FIFO" },
2004 	{ .msk = BIT(9),  .msg = "CQE_MSG_FIFIO" },
2005 	{ .msk = BIT(10), .msg = "GETDQE_FIFO" },
2006 	{ .msk = BIT(11), .msg = "CMDP_FIFO" },
2007 	{ .msk = BIT(12), .msg = "AWTCTRL_FIFO" },
2008 	{}
2009 };
2010 
2011 static const struct hisi_sas_hw_error fatal_axi_error[] = {
2012 	{
2013 		.irq_msk = BIT(ENT_INT_SRC3_WP_DEPTH_OFF),
2014 		.msg = "write pointer and depth",
2015 	},
2016 	{
2017 		.irq_msk = BIT(ENT_INT_SRC3_IPTT_SLOT_NOMATCH_OFF),
2018 		.msg = "iptt no match slot",
2019 	},
2020 	{
2021 		.irq_msk = BIT(ENT_INT_SRC3_RP_DEPTH_OFF),
2022 		.msg = "read pointer and depth",
2023 	},
2024 	{
2025 		.irq_msk = BIT(ENT_INT_SRC3_AXI_OFF),
2026 		.reg = HGC_AXI_FIFO_ERR_INFO,
2027 		.sub = axi_error,
2028 	},
2029 	{
2030 		.irq_msk = BIT(ENT_INT_SRC3_FIFO_OFF),
2031 		.reg = HGC_AXI_FIFO_ERR_INFO,
2032 		.sub = fifo_error,
2033 	},
2034 	{
2035 		.irq_msk = BIT(ENT_INT_SRC3_LM_OFF),
2036 		.msg = "LM add/fetch list",
2037 	},
2038 	{
2039 		.irq_msk = BIT(ENT_INT_SRC3_ABT_OFF),
2040 		.msg = "SAS_HGC_ABT fetch LM list",
2041 	},
2042 	{
2043 		.irq_msk = BIT(ENT_INT_SRC3_DQE_POISON_OFF),
2044 		.msg = "read dqe poison",
2045 	},
2046 	{
2047 		.irq_msk = BIT(ENT_INT_SRC3_IOST_POISON_OFF),
2048 		.msg = "read iost poison",
2049 	},
2050 	{
2051 		.irq_msk = BIT(ENT_INT_SRC3_ITCT_POISON_OFF),
2052 		.msg = "read itct poison",
2053 	},
2054 	{
2055 		.irq_msk = BIT(ENT_INT_SRC3_ITCT_NCQ_POISON_OFF),
2056 		.msg = "read itct ncq poison",
2057 	},
2058 
2059 };
2060 
2061 static irqreturn_t fatal_axi_int_v3_hw(int irq_no, void *p)
2062 {
2063 	u32 irq_value, irq_msk;
2064 	struct hisi_hba *hisi_hba = p;
2065 	struct device *dev = hisi_hba->dev;
2066 	struct pci_dev *pdev = hisi_hba->pci_dev;
2067 	int i;
2068 
2069 	irq_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK3);
2070 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, irq_msk | 0x1df00);
2071 
2072 	irq_value = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
2073 	irq_value &= ~irq_msk;
2074 
2075 	for (i = 0; i < ARRAY_SIZE(fatal_axi_error); i++) {
2076 		const struct hisi_sas_hw_error *error = &fatal_axi_error[i];
2077 
2078 		if (!(irq_value & error->irq_msk))
2079 			continue;
2080 
2081 		if (error->sub) {
2082 			const struct hisi_sas_hw_error *sub = error->sub;
2083 			u32 err_value = hisi_sas_read32(hisi_hba, error->reg);
2084 
2085 			for (; sub->msk || sub->msg; sub++) {
2086 				if (!(err_value & sub->msk))
2087 					continue;
2088 
2089 				dev_err(dev, "%s error (0x%x) found!\n",
2090 					sub->msg, irq_value);
2091 				queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2092 			}
2093 		} else {
2094 			dev_err(dev, "%s error (0x%x) found!\n",
2095 				error->msg, irq_value);
2096 			queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2097 		}
2098 
2099 		if (pdev->revision < 0x21) {
2100 			u32 reg_val;
2101 
2102 			reg_val = hisi_sas_read32(hisi_hba,
2103 						  AXI_MASTER_CFG_BASE +
2104 						  AM_CTRL_GLOBAL);
2105 			reg_val |= AM_CTRL_SHUTDOWN_REQ_MSK;
2106 			hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE +
2107 					 AM_CTRL_GLOBAL, reg_val);
2108 		}
2109 	}
2110 
2111 	fatal_ecc_int_v3_hw(hisi_hba);
2112 
2113 	if (irq_value & BIT(ENT_INT_SRC3_ITC_INT_OFF)) {
2114 		u32 reg_val = hisi_sas_read32(hisi_hba, ITCT_CLR);
2115 		u32 dev_id = reg_val & ITCT_DEV_MSK;
2116 		struct hisi_sas_device *sas_dev =
2117 				&hisi_hba->devices[dev_id];
2118 
2119 		hisi_sas_write32(hisi_hba, ITCT_CLR, 0);
2120 		dev_dbg(dev, "clear ITCT ok\n");
2121 		complete(sas_dev->completion);
2122 	}
2123 
2124 	hisi_sas_write32(hisi_hba, ENT_INT_SRC3, irq_value & 0x1df00);
2125 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, irq_msk);
2126 
2127 	return IRQ_HANDLED;
2128 }
2129 
2130 static void
2131 slot_err_v3_hw(struct hisi_hba *hisi_hba, struct sas_task *task,
2132 	       struct hisi_sas_slot *slot)
2133 {
2134 	struct task_status_struct *ts = &task->task_status;
2135 	struct hisi_sas_complete_v3_hdr *complete_queue =
2136 			hisi_hba->complete_hdr[slot->cmplt_queue];
2137 	struct hisi_sas_complete_v3_hdr *complete_hdr =
2138 			&complete_queue[slot->cmplt_queue_slot];
2139 	struct hisi_sas_err_record_v3 *record =
2140 			hisi_sas_status_buf_addr_mem(slot);
2141 	u32 dma_rx_err_type = le32_to_cpu(record->dma_rx_err_type);
2142 	u32 trans_tx_fail_type = le32_to_cpu(record->trans_tx_fail_type);
2143 	u32 dw3 = le32_to_cpu(complete_hdr->dw3);
2144 
2145 	switch (task->task_proto) {
2146 	case SAS_PROTOCOL_SSP:
2147 		if (dma_rx_err_type & RX_DATA_LEN_UNDERFLOW_MSK) {
2148 			ts->residual = trans_tx_fail_type;
2149 			ts->stat = SAS_DATA_UNDERRUN;
2150 		} else if (dw3 & CMPLT_HDR_IO_IN_TARGET_MSK) {
2151 			ts->stat = SAS_QUEUE_FULL;
2152 			slot->abort = 1;
2153 		} else {
2154 			ts->stat = SAS_OPEN_REJECT;
2155 			ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
2156 		}
2157 		break;
2158 	case SAS_PROTOCOL_SATA:
2159 	case SAS_PROTOCOL_STP:
2160 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
2161 		if (dma_rx_err_type & RX_DATA_LEN_UNDERFLOW_MSK) {
2162 			ts->residual = trans_tx_fail_type;
2163 			ts->stat = SAS_DATA_UNDERRUN;
2164 		} else if (dw3 & CMPLT_HDR_IO_IN_TARGET_MSK) {
2165 			ts->stat = SAS_PHY_DOWN;
2166 			slot->abort = 1;
2167 		} else {
2168 			ts->stat = SAS_OPEN_REJECT;
2169 			ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
2170 		}
2171 		hisi_sas_sata_done(task, slot);
2172 		break;
2173 	case SAS_PROTOCOL_SMP:
2174 		ts->stat = SAM_STAT_CHECK_CONDITION;
2175 		break;
2176 	default:
2177 		break;
2178 	}
2179 }
2180 
2181 static void slot_complete_v3_hw(struct hisi_hba *hisi_hba,
2182 				struct hisi_sas_slot *slot)
2183 {
2184 	struct sas_task *task = slot->task;
2185 	struct hisi_sas_device *sas_dev;
2186 	struct device *dev = hisi_hba->dev;
2187 	struct task_status_struct *ts;
2188 	struct domain_device *device;
2189 	struct sas_ha_struct *ha;
2190 	struct hisi_sas_complete_v3_hdr *complete_queue =
2191 			hisi_hba->complete_hdr[slot->cmplt_queue];
2192 	struct hisi_sas_complete_v3_hdr *complete_hdr =
2193 			&complete_queue[slot->cmplt_queue_slot];
2194 	unsigned long flags;
2195 	bool is_internal = slot->is_internal;
2196 	u32 dw0, dw1, dw3;
2197 
2198 	if (unlikely(!task || !task->lldd_task || !task->dev))
2199 		return;
2200 
2201 	ts = &task->task_status;
2202 	device = task->dev;
2203 	ha = device->port->ha;
2204 	sas_dev = device->lldd_dev;
2205 
2206 	spin_lock_irqsave(&task->task_state_lock, flags);
2207 	task->task_state_flags &=
2208 		~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
2209 	spin_unlock_irqrestore(&task->task_state_lock, flags);
2210 
2211 	memset(ts, 0, sizeof(*ts));
2212 	ts->resp = SAS_TASK_COMPLETE;
2213 
2214 	if (unlikely(!sas_dev)) {
2215 		dev_dbg(dev, "slot complete: port has not device\n");
2216 		ts->stat = SAS_PHY_DOWN;
2217 		goto out;
2218 	}
2219 
2220 	dw0 = le32_to_cpu(complete_hdr->dw0);
2221 	dw1 = le32_to_cpu(complete_hdr->dw1);
2222 	dw3 = le32_to_cpu(complete_hdr->dw3);
2223 
2224 	/*
2225 	 * Use SAS+TMF status codes
2226 	 */
2227 	switch ((dw0 & CMPLT_HDR_ABORT_STAT_MSK) >> CMPLT_HDR_ABORT_STAT_OFF) {
2228 	case STAT_IO_ABORTED:
2229 		/* this IO has been aborted by abort command */
2230 		ts->stat = SAS_ABORTED_TASK;
2231 		goto out;
2232 	case STAT_IO_COMPLETE:
2233 		/* internal abort command complete */
2234 		ts->stat = TMF_RESP_FUNC_SUCC;
2235 		goto out;
2236 	case STAT_IO_NO_DEVICE:
2237 		ts->stat = TMF_RESP_FUNC_COMPLETE;
2238 		goto out;
2239 	case STAT_IO_NOT_VALID:
2240 		/*
2241 		 * abort single IO, the controller can't find the IO
2242 		 */
2243 		ts->stat = TMF_RESP_FUNC_FAILED;
2244 		goto out;
2245 	default:
2246 		break;
2247 	}
2248 
2249 	/* check for erroneous completion */
2250 	if ((dw0 & CMPLT_HDR_CMPLT_MSK) == 0x3) {
2251 		u32 *error_info = hisi_sas_status_buf_addr_mem(slot);
2252 
2253 		slot_err_v3_hw(hisi_hba, task, slot);
2254 		if (ts->stat != SAS_DATA_UNDERRUN)
2255 			dev_info(dev, "erroneous completion iptt=%d task=%pK dev id=%d CQ hdr: 0x%x 0x%x 0x%x 0x%x Error info: 0x%x 0x%x 0x%x 0x%x\n",
2256 				 slot->idx, task, sas_dev->device_id,
2257 				 dw0, dw1, complete_hdr->act, dw3,
2258 				 error_info[0], error_info[1],
2259 				 error_info[2], error_info[3]);
2260 		if (unlikely(slot->abort)) {
2261 			sas_task_abort(task);
2262 			return;
2263 		}
2264 		goto out;
2265 	}
2266 
2267 	switch (task->task_proto) {
2268 	case SAS_PROTOCOL_SSP: {
2269 		struct ssp_response_iu *iu =
2270 			hisi_sas_status_buf_addr_mem(slot) +
2271 			sizeof(struct hisi_sas_err_record);
2272 
2273 		sas_ssp_task_response(dev, task, iu);
2274 		break;
2275 	}
2276 	case SAS_PROTOCOL_SMP: {
2277 		struct scatterlist *sg_resp = &task->smp_task.smp_resp;
2278 		void *to = page_address(sg_page(sg_resp));
2279 
2280 		ts->stat = SAM_STAT_GOOD;
2281 
2282 		dma_unmap_sg(dev, &task->smp_task.smp_req, 1,
2283 			     DMA_TO_DEVICE);
2284 		memcpy(to + sg_resp->offset,
2285 			hisi_sas_status_buf_addr_mem(slot) +
2286 		       sizeof(struct hisi_sas_err_record),
2287 		       sg_resp->length);
2288 		break;
2289 	}
2290 	case SAS_PROTOCOL_SATA:
2291 	case SAS_PROTOCOL_STP:
2292 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
2293 		ts->stat = SAM_STAT_GOOD;
2294 		hisi_sas_sata_done(task, slot);
2295 		break;
2296 	default:
2297 		ts->stat = SAM_STAT_CHECK_CONDITION;
2298 		break;
2299 	}
2300 
2301 	if (!slot->port->port_attached) {
2302 		dev_warn(dev, "slot complete: port %d has removed\n",
2303 			slot->port->sas_port.id);
2304 		ts->stat = SAS_PHY_DOWN;
2305 	}
2306 
2307 out:
2308 	spin_lock_irqsave(&task->task_state_lock, flags);
2309 	if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
2310 		spin_unlock_irqrestore(&task->task_state_lock, flags);
2311 		dev_info(dev, "slot complete: task(%pK) aborted\n", task);
2312 		return;
2313 	}
2314 	task->task_state_flags |= SAS_TASK_STATE_DONE;
2315 	spin_unlock_irqrestore(&task->task_state_lock, flags);
2316 	hisi_sas_slot_task_free(hisi_hba, task, slot);
2317 
2318 	if (!is_internal && (task->task_proto != SAS_PROTOCOL_SMP)) {
2319 		spin_lock_irqsave(&device->done_lock, flags);
2320 		if (test_bit(SAS_HA_FROZEN, &ha->state)) {
2321 			spin_unlock_irqrestore(&device->done_lock, flags);
2322 			dev_info(dev, "slot complete: task(%pK) ignored\n ",
2323 				 task);
2324 			return;
2325 		}
2326 		spin_unlock_irqrestore(&device->done_lock, flags);
2327 	}
2328 
2329 	if (task->task_done)
2330 		task->task_done(task);
2331 }
2332 
2333 static irqreturn_t  cq_thread_v3_hw(int irq_no, void *p)
2334 {
2335 	struct hisi_sas_cq *cq = p;
2336 	struct hisi_hba *hisi_hba = cq->hisi_hba;
2337 	struct hisi_sas_slot *slot;
2338 	struct hisi_sas_complete_v3_hdr *complete_queue;
2339 	u32 rd_point = cq->rd_point, wr_point;
2340 	int queue = cq->id;
2341 
2342 	complete_queue = hisi_hba->complete_hdr[queue];
2343 
2344 	wr_point = hisi_sas_read32(hisi_hba, COMPL_Q_0_WR_PTR +
2345 				   (0x14 * queue));
2346 
2347 	while (rd_point != wr_point) {
2348 		struct hisi_sas_complete_v3_hdr *complete_hdr;
2349 		struct device *dev = hisi_hba->dev;
2350 		u32 dw1;
2351 		int iptt;
2352 
2353 		complete_hdr = &complete_queue[rd_point];
2354 		dw1 = le32_to_cpu(complete_hdr->dw1);
2355 
2356 		iptt = dw1 & CMPLT_HDR_IPTT_MSK;
2357 		if (likely(iptt < HISI_SAS_COMMAND_ENTRIES_V3_HW)) {
2358 			slot = &hisi_hba->slot_info[iptt];
2359 			slot->cmplt_queue_slot = rd_point;
2360 			slot->cmplt_queue = queue;
2361 			slot_complete_v3_hw(hisi_hba, slot);
2362 		} else
2363 			dev_err(dev, "IPTT %d is invalid, discard it.\n", iptt);
2364 
2365 		if (++rd_point >= HISI_SAS_QUEUE_SLOTS)
2366 			rd_point = 0;
2367 	}
2368 
2369 	/* update rd_point */
2370 	cq->rd_point = rd_point;
2371 	hisi_sas_write32(hisi_hba, COMPL_Q_0_RD_PTR + (0x14 * queue), rd_point);
2372 
2373 	return IRQ_HANDLED;
2374 }
2375 
2376 static irqreturn_t cq_interrupt_v3_hw(int irq_no, void *p)
2377 {
2378 	struct hisi_sas_cq *cq = p;
2379 	struct hisi_hba *hisi_hba = cq->hisi_hba;
2380 	int queue = cq->id;
2381 
2382 	hisi_sas_write32(hisi_hba, OQ_INT_SRC, 1 << queue);
2383 
2384 	return IRQ_WAKE_THREAD;
2385 }
2386 
2387 static int interrupt_preinit_v3_hw(struct hisi_hba *hisi_hba)
2388 {
2389 	int vectors;
2390 	int max_msi = HISI_SAS_MSI_COUNT_V3_HW, min_msi;
2391 	struct Scsi_Host *shost = hisi_hba->shost;
2392 	struct irq_affinity desc = {
2393 		.pre_vectors = BASE_VECTORS_V3_HW,
2394 	};
2395 
2396 	min_msi = MIN_AFFINE_VECTORS_V3_HW;
2397 	vectors = pci_alloc_irq_vectors_affinity(hisi_hba->pci_dev,
2398 						 min_msi, max_msi,
2399 						 PCI_IRQ_MSI |
2400 						 PCI_IRQ_AFFINITY,
2401 						 &desc);
2402 	if (vectors < 0)
2403 		return -ENOENT;
2404 
2405 
2406 	hisi_hba->cq_nvecs = vectors - BASE_VECTORS_V3_HW;
2407 	shost->nr_hw_queues = hisi_hba->cq_nvecs;
2408 
2409 	return 0;
2410 }
2411 
2412 static int interrupt_init_v3_hw(struct hisi_hba *hisi_hba)
2413 {
2414 	struct device *dev = hisi_hba->dev;
2415 	struct pci_dev *pdev = hisi_hba->pci_dev;
2416 	int rc, i;
2417 
2418 	rc = devm_request_irq(dev, pci_irq_vector(pdev, 1),
2419 			      int_phy_up_down_bcast_v3_hw, 0,
2420 			      DRV_NAME " phy", hisi_hba);
2421 	if (rc) {
2422 		dev_err(dev, "could not request phy interrupt, rc=%d\n", rc);
2423 		return -ENOENT;
2424 	}
2425 
2426 	rc = devm_request_irq(dev, pci_irq_vector(pdev, 2),
2427 			      int_chnl_int_v3_hw, 0,
2428 			      DRV_NAME " channel", hisi_hba);
2429 	if (rc) {
2430 		dev_err(dev, "could not request chnl interrupt, rc=%d\n", rc);
2431 		return -ENOENT;
2432 	}
2433 
2434 	rc = devm_request_irq(dev, pci_irq_vector(pdev, 11),
2435 			      fatal_axi_int_v3_hw, 0,
2436 			      DRV_NAME " fatal", hisi_hba);
2437 	if (rc) {
2438 		dev_err(dev, "could not request fatal interrupt, rc=%d\n", rc);
2439 		return -ENOENT;
2440 	}
2441 
2442 	if (hisi_sas_intr_conv)
2443 		dev_info(dev, "Enable interrupt converge\n");
2444 
2445 	for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2446 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2447 		int nr = hisi_sas_intr_conv ? 16 : 16 + i;
2448 		unsigned long irqflags = hisi_sas_intr_conv ? IRQF_SHARED :
2449 							      IRQF_ONESHOT;
2450 
2451 		cq->irq_no = pci_irq_vector(pdev, nr);
2452 		rc = devm_request_threaded_irq(dev, cq->irq_no,
2453 				      cq_interrupt_v3_hw,
2454 				      cq_thread_v3_hw,
2455 				      irqflags,
2456 				      DRV_NAME " cq", cq);
2457 		if (rc) {
2458 			dev_err(dev, "could not request cq%d interrupt, rc=%d\n",
2459 				i, rc);
2460 			return -ENOENT;
2461 		}
2462 		cq->irq_mask = pci_irq_get_affinity(pdev, i + BASE_VECTORS_V3_HW);
2463 		if (!cq->irq_mask) {
2464 			dev_err(dev, "could not get cq%d irq affinity!\n", i);
2465 			return -ENOENT;
2466 		}
2467 	}
2468 
2469 	return 0;
2470 }
2471 
2472 static int hisi_sas_v3_init(struct hisi_hba *hisi_hba)
2473 {
2474 	int rc;
2475 
2476 	rc = hw_init_v3_hw(hisi_hba);
2477 	if (rc)
2478 		return rc;
2479 
2480 	rc = interrupt_init_v3_hw(hisi_hba);
2481 	if (rc)
2482 		return rc;
2483 
2484 	return 0;
2485 }
2486 
2487 static void phy_set_linkrate_v3_hw(struct hisi_hba *hisi_hba, int phy_no,
2488 		struct sas_phy_linkrates *r)
2489 {
2490 	enum sas_linkrate max = r->maximum_linkrate;
2491 	u32 prog_phy_link_rate = hisi_sas_phy_read32(hisi_hba, phy_no,
2492 						     PROG_PHY_LINK_RATE);
2493 
2494 	prog_phy_link_rate &= ~CFG_PROG_PHY_LINK_RATE_MSK;
2495 	prog_phy_link_rate |= hisi_sas_get_prog_phy_linkrate_mask(max);
2496 	hisi_sas_phy_write32(hisi_hba, phy_no, PROG_PHY_LINK_RATE,
2497 			     prog_phy_link_rate);
2498 }
2499 
2500 static void interrupt_disable_v3_hw(struct hisi_hba *hisi_hba)
2501 {
2502 	struct pci_dev *pdev = hisi_hba->pci_dev;
2503 	int i;
2504 
2505 	synchronize_irq(pci_irq_vector(pdev, 1));
2506 	synchronize_irq(pci_irq_vector(pdev, 2));
2507 	synchronize_irq(pci_irq_vector(pdev, 11));
2508 	for (i = 0; i < hisi_hba->queue_count; i++)
2509 		hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK + 0x4 * i, 0x1);
2510 
2511 	for (i = 0; i < hisi_hba->cq_nvecs; i++)
2512 		synchronize_irq(pci_irq_vector(pdev, i + 16));
2513 
2514 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0xffffffff);
2515 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0xffffffff);
2516 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0xffffffff);
2517 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xffffffff);
2518 
2519 	for (i = 0; i < hisi_hba->n_phy; i++) {
2520 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xffffffff);
2521 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0xffffffff);
2522 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_NOT_RDY_MSK, 0x1);
2523 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_PHY_ENA_MSK, 0x1);
2524 		hisi_sas_phy_write32(hisi_hba, i, SL_RX_BCAST_CHK_MSK, 0x1);
2525 	}
2526 }
2527 
2528 static u32 get_phys_state_v3_hw(struct hisi_hba *hisi_hba)
2529 {
2530 	return hisi_sas_read32(hisi_hba, PHY_STATE);
2531 }
2532 
2533 static int disable_host_v3_hw(struct hisi_hba *hisi_hba)
2534 {
2535 	struct device *dev = hisi_hba->dev;
2536 	u32 status, reg_val;
2537 	int rc;
2538 
2539 	interrupt_disable_v3_hw(hisi_hba);
2540 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0x0);
2541 
2542 	hisi_sas_stop_phys(hisi_hba);
2543 
2544 	mdelay(10);
2545 
2546 	reg_val = hisi_sas_read32(hisi_hba, AXI_MASTER_CFG_BASE +
2547 				  AM_CTRL_GLOBAL);
2548 	reg_val |= AM_CTRL_SHUTDOWN_REQ_MSK;
2549 	hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE +
2550 			 AM_CTRL_GLOBAL, reg_val);
2551 
2552 	/* wait until bus idle */
2553 	rc = hisi_sas_read32_poll_timeout(AXI_MASTER_CFG_BASE +
2554 					  AM_CURR_TRANS_RETURN, status,
2555 					  status == 0x3, 10, 100);
2556 	if (rc) {
2557 		dev_err(dev, "axi bus is not idle, rc=%d\n", rc);
2558 		return rc;
2559 	}
2560 
2561 	return 0;
2562 }
2563 
2564 static int soft_reset_v3_hw(struct hisi_hba *hisi_hba)
2565 {
2566 	struct device *dev = hisi_hba->dev;
2567 	int rc;
2568 
2569 	rc = disable_host_v3_hw(hisi_hba);
2570 	if (rc) {
2571 		dev_err(dev, "soft reset: disable host failed rc=%d\n", rc);
2572 		return rc;
2573 	}
2574 
2575 	hisi_sas_init_mem(hisi_hba);
2576 
2577 	return hw_init_v3_hw(hisi_hba);
2578 }
2579 
2580 static int write_gpio_v3_hw(struct hisi_hba *hisi_hba, u8 reg_type,
2581 			u8 reg_index, u8 reg_count, u8 *write_data)
2582 {
2583 	struct device *dev = hisi_hba->dev;
2584 	u32 *data = (u32 *)write_data;
2585 	int i;
2586 
2587 	switch (reg_type) {
2588 	case SAS_GPIO_REG_TX:
2589 		if ((reg_index + reg_count) > ((hisi_hba->n_phy + 3) / 4)) {
2590 			dev_err(dev, "write gpio: invalid reg range[%d, %d]\n",
2591 				reg_index, reg_index + reg_count - 1);
2592 			return -EINVAL;
2593 		}
2594 
2595 		for (i = 0; i < reg_count; i++)
2596 			hisi_sas_write32(hisi_hba,
2597 					 SAS_GPIO_TX_0_1 + (reg_index + i) * 4,
2598 					 data[i]);
2599 		break;
2600 	default:
2601 		dev_err(dev, "write gpio: unsupported or bad reg type %d\n",
2602 			reg_type);
2603 		return -EINVAL;
2604 	}
2605 
2606 	return 0;
2607 }
2608 
2609 static void wait_cmds_complete_timeout_v3_hw(struct hisi_hba *hisi_hba,
2610 					     int delay_ms, int timeout_ms)
2611 {
2612 	struct device *dev = hisi_hba->dev;
2613 	int entries, entries_old = 0, time;
2614 
2615 	for (time = 0; time < timeout_ms; time += delay_ms) {
2616 		entries = hisi_sas_read32(hisi_hba, CQE_SEND_CNT);
2617 		if (entries == entries_old)
2618 			break;
2619 
2620 		entries_old = entries;
2621 		msleep(delay_ms);
2622 	}
2623 
2624 	if (time >= timeout_ms) {
2625 		dev_dbg(dev, "Wait commands complete timeout!\n");
2626 		return;
2627 	}
2628 
2629 	dev_dbg(dev, "wait commands complete %dms\n", time);
2630 }
2631 
2632 static ssize_t intr_conv_v3_hw_show(struct device *dev,
2633 				    struct device_attribute *attr, char *buf)
2634 {
2635 	return scnprintf(buf, PAGE_SIZE, "%u\n", hisi_sas_intr_conv);
2636 }
2637 static DEVICE_ATTR_RO(intr_conv_v3_hw);
2638 
2639 static void config_intr_coal_v3_hw(struct hisi_hba *hisi_hba)
2640 {
2641 	/* config those registers between enable and disable PHYs */
2642 	hisi_sas_stop_phys(hisi_hba);
2643 
2644 	if (hisi_hba->intr_coal_ticks == 0 ||
2645 	    hisi_hba->intr_coal_count == 0) {
2646 		hisi_sas_write32(hisi_hba, INT_COAL_EN, 0x1);
2647 		hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x1);
2648 		hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 0x1);
2649 	} else {
2650 		hisi_sas_write32(hisi_hba, INT_COAL_EN, 0x3);
2651 		hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME,
2652 				 hisi_hba->intr_coal_ticks);
2653 		hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT,
2654 				 hisi_hba->intr_coal_count);
2655 	}
2656 	phys_init_v3_hw(hisi_hba);
2657 }
2658 
2659 static ssize_t intr_coal_ticks_v3_hw_show(struct device *dev,
2660 					  struct device_attribute *attr,
2661 					  char *buf)
2662 {
2663 	struct Scsi_Host *shost = class_to_shost(dev);
2664 	struct hisi_hba *hisi_hba = shost_priv(shost);
2665 
2666 	return scnprintf(buf, PAGE_SIZE, "%u\n",
2667 			 hisi_hba->intr_coal_ticks);
2668 }
2669 
2670 static ssize_t intr_coal_ticks_v3_hw_store(struct device *dev,
2671 					   struct device_attribute *attr,
2672 					   const char *buf, size_t count)
2673 {
2674 	struct Scsi_Host *shost = class_to_shost(dev);
2675 	struct hisi_hba *hisi_hba = shost_priv(shost);
2676 	u32 intr_coal_ticks;
2677 	int ret;
2678 
2679 	ret = kstrtou32(buf, 10, &intr_coal_ticks);
2680 	if (ret) {
2681 		dev_err(dev, "Input data of interrupt coalesce unmatch\n");
2682 		return -EINVAL;
2683 	}
2684 
2685 	if (intr_coal_ticks >= BIT(24)) {
2686 		dev_err(dev, "intr_coal_ticks must be less than 2^24!\n");
2687 		return -EINVAL;
2688 	}
2689 
2690 	hisi_hba->intr_coal_ticks = intr_coal_ticks;
2691 
2692 	config_intr_coal_v3_hw(hisi_hba);
2693 
2694 	return count;
2695 }
2696 static DEVICE_ATTR_RW(intr_coal_ticks_v3_hw);
2697 
2698 static ssize_t intr_coal_count_v3_hw_show(struct device *dev,
2699 					  struct device_attribute
2700 					  *attr, char *buf)
2701 {
2702 	struct Scsi_Host *shost = class_to_shost(dev);
2703 	struct hisi_hba *hisi_hba = shost_priv(shost);
2704 
2705 	return scnprintf(buf, PAGE_SIZE, "%u\n",
2706 			 hisi_hba->intr_coal_count);
2707 }
2708 
2709 static ssize_t intr_coal_count_v3_hw_store(struct device *dev,
2710 		struct device_attribute
2711 		*attr, const char *buf, size_t count)
2712 {
2713 	struct Scsi_Host *shost = class_to_shost(dev);
2714 	struct hisi_hba *hisi_hba = shost_priv(shost);
2715 	u32 intr_coal_count;
2716 	int ret;
2717 
2718 	ret = kstrtou32(buf, 10, &intr_coal_count);
2719 	if (ret) {
2720 		dev_err(dev, "Input data of interrupt coalesce unmatch\n");
2721 		return -EINVAL;
2722 	}
2723 
2724 	if (intr_coal_count >= BIT(8)) {
2725 		dev_err(dev, "intr_coal_count must be less than 2^8!\n");
2726 		return -EINVAL;
2727 	}
2728 
2729 	hisi_hba->intr_coal_count = intr_coal_count;
2730 
2731 	config_intr_coal_v3_hw(hisi_hba);
2732 
2733 	return count;
2734 }
2735 static DEVICE_ATTR_RW(intr_coal_count_v3_hw);
2736 
2737 static int slave_configure_v3_hw(struct scsi_device *sdev)
2738 {
2739 	struct Scsi_Host *shost = dev_to_shost(&sdev->sdev_gendev);
2740 	struct domain_device *ddev = sdev_to_domain_dev(sdev);
2741 	struct hisi_hba *hisi_hba = shost_priv(shost);
2742 	struct device *dev = hisi_hba->dev;
2743 	int ret = sas_slave_configure(sdev);
2744 
2745 	if (ret)
2746 		return ret;
2747 	if (!dev_is_sata(ddev))
2748 		sas_change_queue_depth(sdev, 64);
2749 
2750 	if (sdev->type == TYPE_ENCLOSURE)
2751 		return 0;
2752 
2753 	if (!device_link_add(&sdev->sdev_gendev, dev,
2754 			     DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)) {
2755 		if (pm_runtime_enabled(dev)) {
2756 			dev_info(dev, "add device link failed, disable runtime PM for the host\n");
2757 			pm_runtime_disable(dev);
2758 		}
2759 	}
2760 
2761 	return 0;
2762 }
2763 
2764 static struct device_attribute *host_attrs_v3_hw[] = {
2765 	&dev_attr_phy_event_threshold,
2766 	&dev_attr_intr_conv_v3_hw,
2767 	&dev_attr_intr_coal_ticks_v3_hw,
2768 	&dev_attr_intr_coal_count_v3_hw,
2769 	NULL
2770 };
2771 
2772 #define HISI_SAS_DEBUGFS_REG(x) {#x, x}
2773 
2774 struct hisi_sas_debugfs_reg_lu {
2775 	char *name;
2776 	int off;
2777 };
2778 
2779 struct hisi_sas_debugfs_reg {
2780 	const struct hisi_sas_debugfs_reg_lu *lu;
2781 	int count;
2782 	int base_off;
2783 };
2784 
2785 static const struct hisi_sas_debugfs_reg_lu debugfs_port_reg_lu[] = {
2786 	HISI_SAS_DEBUGFS_REG(PHY_CFG),
2787 	HISI_SAS_DEBUGFS_REG(HARD_PHY_LINKRATE),
2788 	HISI_SAS_DEBUGFS_REG(PROG_PHY_LINK_RATE),
2789 	HISI_SAS_DEBUGFS_REG(PHY_CTRL),
2790 	HISI_SAS_DEBUGFS_REG(SL_CFG),
2791 	HISI_SAS_DEBUGFS_REG(AIP_LIMIT),
2792 	HISI_SAS_DEBUGFS_REG(SL_CONTROL),
2793 	HISI_SAS_DEBUGFS_REG(RX_PRIMS_STATUS),
2794 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD0),
2795 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD1),
2796 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD2),
2797 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD3),
2798 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD4),
2799 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD5),
2800 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD6),
2801 	HISI_SAS_DEBUGFS_REG(TXID_AUTO),
2802 	HISI_SAS_DEBUGFS_REG(RX_IDAF_DWORD0),
2803 	HISI_SAS_DEBUGFS_REG(RXOP_CHECK_CFG_H),
2804 	HISI_SAS_DEBUGFS_REG(STP_LINK_TIMER),
2805 	HISI_SAS_DEBUGFS_REG(STP_LINK_TIMEOUT_STATE),
2806 	HISI_SAS_DEBUGFS_REG(CON_CFG_DRIVER),
2807 	HISI_SAS_DEBUGFS_REG(SAS_SSP_CON_TIMER_CFG),
2808 	HISI_SAS_DEBUGFS_REG(SAS_SMP_CON_TIMER_CFG),
2809 	HISI_SAS_DEBUGFS_REG(SAS_STP_CON_TIMER_CFG),
2810 	HISI_SAS_DEBUGFS_REG(CHL_INT0),
2811 	HISI_SAS_DEBUGFS_REG(CHL_INT1),
2812 	HISI_SAS_DEBUGFS_REG(CHL_INT2),
2813 	HISI_SAS_DEBUGFS_REG(CHL_INT0_MSK),
2814 	HISI_SAS_DEBUGFS_REG(CHL_INT1_MSK),
2815 	HISI_SAS_DEBUGFS_REG(CHL_INT2_MSK),
2816 	HISI_SAS_DEBUGFS_REG(SAS_EC_INT_COAL_TIME),
2817 	HISI_SAS_DEBUGFS_REG(CHL_INT_COAL_EN),
2818 	HISI_SAS_DEBUGFS_REG(SAS_RX_TRAIN_TIMER),
2819 	HISI_SAS_DEBUGFS_REG(PHY_CTRL_RDY_MSK),
2820 	HISI_SAS_DEBUGFS_REG(PHYCTRL_NOT_RDY_MSK),
2821 	HISI_SAS_DEBUGFS_REG(PHYCTRL_DWS_RESET_MSK),
2822 	HISI_SAS_DEBUGFS_REG(PHYCTRL_PHY_ENA_MSK),
2823 	HISI_SAS_DEBUGFS_REG(SL_RX_BCAST_CHK_MSK),
2824 	HISI_SAS_DEBUGFS_REG(PHYCTRL_OOB_RESTART_MSK),
2825 	HISI_SAS_DEBUGFS_REG(DMA_TX_STATUS),
2826 	HISI_SAS_DEBUGFS_REG(DMA_RX_STATUS),
2827 	HISI_SAS_DEBUGFS_REG(COARSETUNE_TIME),
2828 	HISI_SAS_DEBUGFS_REG(ERR_CNT_DWS_LOST),
2829 	HISI_SAS_DEBUGFS_REG(ERR_CNT_RESET_PROB),
2830 	HISI_SAS_DEBUGFS_REG(ERR_CNT_INVLD_DW),
2831 	HISI_SAS_DEBUGFS_REG(ERR_CNT_CODE_ERR),
2832 	HISI_SAS_DEBUGFS_REG(ERR_CNT_DISP_ERR),
2833 	{}
2834 };
2835 
2836 static const struct hisi_sas_debugfs_reg debugfs_port_reg = {
2837 	.lu = debugfs_port_reg_lu,
2838 	.count = 0x100,
2839 	.base_off = PORT_BASE,
2840 };
2841 
2842 static const struct hisi_sas_debugfs_reg_lu debugfs_global_reg_lu[] = {
2843 	HISI_SAS_DEBUGFS_REG(DLVRY_QUEUE_ENABLE),
2844 	HISI_SAS_DEBUGFS_REG(PHY_CONTEXT),
2845 	HISI_SAS_DEBUGFS_REG(PHY_STATE),
2846 	HISI_SAS_DEBUGFS_REG(PHY_PORT_NUM_MA),
2847 	HISI_SAS_DEBUGFS_REG(PHY_CONN_RATE),
2848 	HISI_SAS_DEBUGFS_REG(ITCT_CLR),
2849 	HISI_SAS_DEBUGFS_REG(IO_SATA_BROKEN_MSG_ADDR_LO),
2850 	HISI_SAS_DEBUGFS_REG(IO_SATA_BROKEN_MSG_ADDR_HI),
2851 	HISI_SAS_DEBUGFS_REG(SATA_INITI_D2H_STORE_ADDR_LO),
2852 	HISI_SAS_DEBUGFS_REG(SATA_INITI_D2H_STORE_ADDR_HI),
2853 	HISI_SAS_DEBUGFS_REG(CFG_MAX_TAG),
2854 	HISI_SAS_DEBUGFS_REG(HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL),
2855 	HISI_SAS_DEBUGFS_REG(HGC_SAS_TXFAIL_RETRY_CTRL),
2856 	HISI_SAS_DEBUGFS_REG(HGC_GET_ITV_TIME),
2857 	HISI_SAS_DEBUGFS_REG(DEVICE_MSG_WORK_MODE),
2858 	HISI_SAS_DEBUGFS_REG(OPENA_WT_CONTI_TIME),
2859 	HISI_SAS_DEBUGFS_REG(I_T_NEXUS_LOSS_TIME),
2860 	HISI_SAS_DEBUGFS_REG(MAX_CON_TIME_LIMIT_TIME),
2861 	HISI_SAS_DEBUGFS_REG(BUS_INACTIVE_LIMIT_TIME),
2862 	HISI_SAS_DEBUGFS_REG(REJECT_TO_OPEN_LIMIT_TIME),
2863 	HISI_SAS_DEBUGFS_REG(CQ_INT_CONVERGE_EN),
2864 	HISI_SAS_DEBUGFS_REG(CFG_AGING_TIME),
2865 	HISI_SAS_DEBUGFS_REG(HGC_DFX_CFG2),
2866 	HISI_SAS_DEBUGFS_REG(CFG_ABT_SET_QUERY_IPTT),
2867 	HISI_SAS_DEBUGFS_REG(CFG_ABT_SET_IPTT_DONE),
2868 	HISI_SAS_DEBUGFS_REG(HGC_IOMB_PROC1_STATUS),
2869 	HISI_SAS_DEBUGFS_REG(CHNL_INT_STATUS),
2870 	HISI_SAS_DEBUGFS_REG(HGC_AXI_FIFO_ERR_INFO),
2871 	HISI_SAS_DEBUGFS_REG(INT_COAL_EN),
2872 	HISI_SAS_DEBUGFS_REG(OQ_INT_COAL_TIME),
2873 	HISI_SAS_DEBUGFS_REG(OQ_INT_COAL_CNT),
2874 	HISI_SAS_DEBUGFS_REG(ENT_INT_COAL_TIME),
2875 	HISI_SAS_DEBUGFS_REG(ENT_INT_COAL_CNT),
2876 	HISI_SAS_DEBUGFS_REG(OQ_INT_SRC),
2877 	HISI_SAS_DEBUGFS_REG(OQ_INT_SRC_MSK),
2878 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC1),
2879 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC2),
2880 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC3),
2881 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC_MSK1),
2882 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC_MSK2),
2883 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC_MSK3),
2884 	HISI_SAS_DEBUGFS_REG(CHNL_PHYUPDOWN_INT_MSK),
2885 	HISI_SAS_DEBUGFS_REG(CHNL_ENT_INT_MSK),
2886 	HISI_SAS_DEBUGFS_REG(HGC_COM_INT_MSK),
2887 	HISI_SAS_DEBUGFS_REG(SAS_ECC_INTR),
2888 	HISI_SAS_DEBUGFS_REG(SAS_ECC_INTR_MSK),
2889 	HISI_SAS_DEBUGFS_REG(HGC_ERR_STAT_EN),
2890 	HISI_SAS_DEBUGFS_REG(CQE_SEND_CNT),
2891 	HISI_SAS_DEBUGFS_REG(DLVRY_Q_0_DEPTH),
2892 	HISI_SAS_DEBUGFS_REG(DLVRY_Q_0_WR_PTR),
2893 	HISI_SAS_DEBUGFS_REG(DLVRY_Q_0_RD_PTR),
2894 	HISI_SAS_DEBUGFS_REG(HYPER_STREAM_ID_EN_CFG),
2895 	HISI_SAS_DEBUGFS_REG(OQ0_INT_SRC_MSK),
2896 	HISI_SAS_DEBUGFS_REG(COMPL_Q_0_DEPTH),
2897 	HISI_SAS_DEBUGFS_REG(COMPL_Q_0_WR_PTR),
2898 	HISI_SAS_DEBUGFS_REG(COMPL_Q_0_RD_PTR),
2899 	HISI_SAS_DEBUGFS_REG(AWQOS_AWCACHE_CFG),
2900 	HISI_SAS_DEBUGFS_REG(ARQOS_ARCACHE_CFG),
2901 	HISI_SAS_DEBUGFS_REG(HILINK_ERR_DFX),
2902 	HISI_SAS_DEBUGFS_REG(SAS_GPIO_CFG_0),
2903 	HISI_SAS_DEBUGFS_REG(SAS_GPIO_CFG_1),
2904 	HISI_SAS_DEBUGFS_REG(SAS_GPIO_TX_0_1),
2905 	HISI_SAS_DEBUGFS_REG(SAS_CFG_DRIVE_VLD),
2906 	{}
2907 };
2908 
2909 static const struct hisi_sas_debugfs_reg debugfs_global_reg = {
2910 	.lu = debugfs_global_reg_lu,
2911 	.count = 0x800,
2912 };
2913 
2914 static const struct hisi_sas_debugfs_reg_lu debugfs_axi_reg_lu[] = {
2915 	HISI_SAS_DEBUGFS_REG(AM_CFG_MAX_TRANS),
2916 	HISI_SAS_DEBUGFS_REG(AM_CFG_SINGLE_PORT_MAX_TRANS),
2917 	HISI_SAS_DEBUGFS_REG(AXI_CFG),
2918 	HISI_SAS_DEBUGFS_REG(AM_ROB_ECC_ERR_ADDR),
2919 	{}
2920 };
2921 
2922 static const struct hisi_sas_debugfs_reg debugfs_axi_reg = {
2923 	.lu = debugfs_axi_reg_lu,
2924 	.count = 0x61,
2925 	.base_off = AXI_MASTER_CFG_BASE,
2926 };
2927 
2928 static const struct hisi_sas_debugfs_reg_lu debugfs_ras_reg_lu[] = {
2929 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR0),
2930 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR1),
2931 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR0_MASK),
2932 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR1_MASK),
2933 	HISI_SAS_DEBUGFS_REG(CFG_SAS_RAS_INTR_MASK),
2934 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR2),
2935 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR2_MASK),
2936 	{}
2937 };
2938 
2939 static const struct hisi_sas_debugfs_reg debugfs_ras_reg = {
2940 	.lu = debugfs_ras_reg_lu,
2941 	.count = 0x10,
2942 	.base_off = RAS_BASE,
2943 };
2944 
2945 static void debugfs_snapshot_prepare_v3_hw(struct hisi_hba *hisi_hba)
2946 {
2947 	set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
2948 
2949 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0);
2950 
2951 	wait_cmds_complete_timeout_v3_hw(hisi_hba, 100, 5000);
2952 
2953 	hisi_sas_sync_irqs(hisi_hba);
2954 }
2955 
2956 static void debugfs_snapshot_restore_v3_hw(struct hisi_hba *hisi_hba)
2957 {
2958 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE,
2959 			 (u32)((1ULL << hisi_hba->queue_count) - 1));
2960 
2961 	clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
2962 }
2963 
2964 static void read_iost_itct_cache_v3_hw(struct hisi_hba *hisi_hba,
2965 				       enum hisi_sas_debugfs_cache_type type,
2966 				       u32 *cache)
2967 {
2968 	u32 cache_dw_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ *
2969 			    HISI_SAS_IOST_ITCT_CACHE_NUM;
2970 	struct device *dev = hisi_hba->dev;
2971 	u32 *buf = cache;
2972 	u32 i, val;
2973 
2974 	hisi_sas_write32(hisi_hba, TAB_RD_TYPE, type);
2975 
2976 	for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_DW_SZ; i++) {
2977 		val = hisi_sas_read32(hisi_hba, TAB_DFX);
2978 		if (val == 0xffffffff)
2979 			break;
2980 	}
2981 
2982 	if (val != 0xffffffff) {
2983 		dev_err(dev, "Issue occurred in reading IOST/ITCT cache!\n");
2984 		return;
2985 	}
2986 
2987 	memset(buf, 0, cache_dw_size * 4);
2988 	buf[0] = val;
2989 
2990 	for (i = 1; i < cache_dw_size; i++)
2991 		buf[i] = hisi_sas_read32(hisi_hba, TAB_DFX);
2992 }
2993 
2994 static void hisi_sas_bist_test_prep_v3_hw(struct hisi_hba *hisi_hba)
2995 {
2996 	u32 reg_val;
2997 	int phy_no = hisi_hba->debugfs_bist_phy_no;
2998 	int i;
2999 
3000 	/* disable PHY */
3001 	hisi_sas_phy_enable(hisi_hba, phy_no, 0);
3002 
3003 	/* update FFE */
3004 	for (i = 0; i < FFE_CFG_MAX; i++)
3005 		hisi_sas_phy_write32(hisi_hba, phy_no, TXDEEMPH_G1 + (i * 0x4),
3006 				     hisi_hba->debugfs_bist_ffe[phy_no][i]);
3007 
3008 	/* disable ALOS */
3009 	reg_val = hisi_sas_phy_read32(hisi_hba, phy_no, SERDES_CFG);
3010 	reg_val |= CFG_ALOS_CHK_DISABLE_MSK;
3011 	hisi_sas_phy_write32(hisi_hba, phy_no, SERDES_CFG, reg_val);
3012 }
3013 
3014 static void hisi_sas_bist_test_restore_v3_hw(struct hisi_hba *hisi_hba)
3015 {
3016 	u32 reg_val;
3017 	int phy_no = hisi_hba->debugfs_bist_phy_no;
3018 
3019 	/* disable loopback */
3020 	reg_val = hisi_sas_phy_read32(hisi_hba, phy_no, SAS_PHY_BIST_CTRL);
3021 	reg_val &= ~(CFG_RX_BIST_EN_MSK | CFG_TX_BIST_EN_MSK |
3022 		     CFG_BIST_TEST_MSK);
3023 	hisi_sas_phy_write32(hisi_hba, phy_no, SAS_PHY_BIST_CTRL, reg_val);
3024 
3025 	/* enable ALOS */
3026 	reg_val = hisi_sas_phy_read32(hisi_hba, phy_no, SERDES_CFG);
3027 	reg_val &= ~CFG_ALOS_CHK_DISABLE_MSK;
3028 	hisi_sas_phy_write32(hisi_hba, phy_no, SERDES_CFG, reg_val);
3029 
3030 	/* restore the linkrate */
3031 	reg_val = hisi_sas_phy_read32(hisi_hba, phy_no, PROG_PHY_LINK_RATE);
3032 	/* init OOB link rate as 1.5 Gbits */
3033 	reg_val &= ~CFG_PROG_OOB_PHY_LINK_RATE_MSK;
3034 	reg_val |= (0x8 << CFG_PROG_OOB_PHY_LINK_RATE_OFF);
3035 	hisi_sas_phy_write32(hisi_hba, phy_no, PROG_PHY_LINK_RATE, reg_val);
3036 
3037 	/* enable PHY */
3038 	hisi_sas_phy_enable(hisi_hba, phy_no, 1);
3039 }
3040 
3041 #define SAS_PHY_BIST_CODE_INIT	0x1
3042 #define SAS_PHY_BIST_CODE1_INIT	0X80
3043 static int debugfs_set_bist_v3_hw(struct hisi_hba *hisi_hba, bool enable)
3044 {
3045 	u32 reg_val, mode_tmp;
3046 	u32 linkrate = hisi_hba->debugfs_bist_linkrate;
3047 	u32 phy_no = hisi_hba->debugfs_bist_phy_no;
3048 	u32 *ffe = hisi_hba->debugfs_bist_ffe[phy_no];
3049 	u32 code_mode = hisi_hba->debugfs_bist_code_mode;
3050 	u32 path_mode = hisi_hba->debugfs_bist_mode;
3051 	u32 *fix_code = &hisi_hba->debugfs_bist_fixed_code[0];
3052 	struct device *dev = hisi_hba->dev;
3053 
3054 	dev_info(dev, "BIST info:phy%d link_rate=%d code_mode=%d path_mode=%d ffe={0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x} fixed_code={0x%x, 0x%x}\n",
3055 		 phy_no, linkrate, code_mode, path_mode,
3056 		 ffe[FFE_SAS_1_5_GBPS], ffe[FFE_SAS_3_0_GBPS],
3057 		 ffe[FFE_SAS_6_0_GBPS], ffe[FFE_SAS_12_0_GBPS],
3058 		 ffe[FFE_SATA_1_5_GBPS], ffe[FFE_SATA_3_0_GBPS],
3059 		 ffe[FFE_SATA_6_0_GBPS], fix_code[FIXED_CODE],
3060 		 fix_code[FIXED_CODE_1]);
3061 	mode_tmp = path_mode ? 2 : 1;
3062 	if (enable) {
3063 		/* some preparations before bist test */
3064 		hisi_sas_bist_test_prep_v3_hw(hisi_hba);
3065 
3066 		/* set linkrate of bit test*/
3067 		reg_val = hisi_sas_phy_read32(hisi_hba, phy_no,
3068 					      PROG_PHY_LINK_RATE);
3069 		reg_val &= ~CFG_PROG_OOB_PHY_LINK_RATE_MSK;
3070 		reg_val |= (linkrate << CFG_PROG_OOB_PHY_LINK_RATE_OFF);
3071 		hisi_sas_phy_write32(hisi_hba, phy_no, PROG_PHY_LINK_RATE,
3072 				     reg_val);
3073 
3074 		/* set code mode of bit test */
3075 		reg_val = hisi_sas_phy_read32(hisi_hba, phy_no,
3076 					      SAS_PHY_BIST_CTRL);
3077 		reg_val &= ~(CFG_BIST_MODE_SEL_MSK | CFG_LOOP_TEST_MODE_MSK |
3078 			     CFG_RX_BIST_EN_MSK | CFG_TX_BIST_EN_MSK |
3079 			     CFG_BIST_TEST_MSK);
3080 		reg_val |= ((code_mode << CFG_BIST_MODE_SEL_OFF) |
3081 			    (mode_tmp << CFG_LOOP_TEST_MODE_OFF) |
3082 			    CFG_BIST_TEST_MSK);
3083 		hisi_sas_phy_write32(hisi_hba, phy_no, SAS_PHY_BIST_CTRL,
3084 				     reg_val);
3085 
3086 		/* set the bist init value */
3087 		if (code_mode == HISI_SAS_BIST_CODE_MODE_FIXED_DATA) {
3088 			reg_val = hisi_hba->debugfs_bist_fixed_code[0];
3089 			hisi_sas_phy_write32(hisi_hba, phy_no,
3090 					     SAS_PHY_BIST_CODE, reg_val);
3091 
3092 			reg_val = hisi_hba->debugfs_bist_fixed_code[1];
3093 			hisi_sas_phy_write32(hisi_hba, phy_no,
3094 					     SAS_PHY_BIST_CODE1, reg_val);
3095 		} else {
3096 			hisi_sas_phy_write32(hisi_hba, phy_no,
3097 					     SAS_PHY_BIST_CODE,
3098 					     SAS_PHY_BIST_CODE_INIT);
3099 			hisi_sas_phy_write32(hisi_hba, phy_no,
3100 					     SAS_PHY_BIST_CODE1,
3101 					     SAS_PHY_BIST_CODE1_INIT);
3102 		}
3103 
3104 		mdelay(100);
3105 		reg_val |= (CFG_RX_BIST_EN_MSK | CFG_TX_BIST_EN_MSK);
3106 		hisi_sas_phy_write32(hisi_hba, phy_no, SAS_PHY_BIST_CTRL,
3107 				     reg_val);
3108 
3109 		/* clear error bit */
3110 		mdelay(100);
3111 		hisi_sas_phy_read32(hisi_hba, phy_no, SAS_BIST_ERR_CNT);
3112 	} else {
3113 		/* disable bist test and recover it */
3114 		hisi_hba->debugfs_bist_cnt += hisi_sas_phy_read32(hisi_hba,
3115 				phy_no, SAS_BIST_ERR_CNT);
3116 		hisi_sas_bist_test_restore_v3_hw(hisi_hba);
3117 	}
3118 
3119 	return 0;
3120 }
3121 
3122 static int hisi_sas_map_queues(struct Scsi_Host *shost)
3123 {
3124 	struct hisi_hba *hisi_hba = shost_priv(shost);
3125 	struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
3126 
3127 	return blk_mq_pci_map_queues(qmap, hisi_hba->pci_dev,
3128 				     BASE_VECTORS_V3_HW);
3129 }
3130 
3131 static struct scsi_host_template sht_v3_hw = {
3132 	.name			= DRV_NAME,
3133 	.proc_name		= DRV_NAME,
3134 	.module			= THIS_MODULE,
3135 	.queuecommand		= sas_queuecommand,
3136 	.dma_need_drain		= ata_scsi_dma_need_drain,
3137 	.target_alloc		= sas_target_alloc,
3138 	.slave_configure	= slave_configure_v3_hw,
3139 	.scan_finished		= hisi_sas_scan_finished,
3140 	.scan_start		= hisi_sas_scan_start,
3141 	.map_queues		= hisi_sas_map_queues,
3142 	.change_queue_depth	= sas_change_queue_depth,
3143 	.bios_param		= sas_bios_param,
3144 	.this_id		= -1,
3145 	.sg_tablesize		= HISI_SAS_SGE_PAGE_CNT,
3146 	.sg_prot_tablesize	= HISI_SAS_SGE_PAGE_CNT,
3147 	.max_sectors		= SCSI_DEFAULT_MAX_SECTORS,
3148 	.eh_device_reset_handler = sas_eh_device_reset_handler,
3149 	.eh_target_reset_handler = sas_eh_target_reset_handler,
3150 	.target_destroy		= sas_target_destroy,
3151 	.ioctl			= sas_ioctl,
3152 #ifdef CONFIG_COMPAT
3153 	.compat_ioctl		= sas_ioctl,
3154 #endif
3155 	.shost_attrs		= host_attrs_v3_hw,
3156 	.tag_alloc_policy	= BLK_TAG_ALLOC_RR,
3157 	.host_reset             = hisi_sas_host_reset,
3158 	.host_tagset		= 1,
3159 };
3160 
3161 static const struct hisi_sas_hw hisi_sas_v3_hw = {
3162 	.setup_itct = setup_itct_v3_hw,
3163 	.get_wideport_bitmap = get_wideport_bitmap_v3_hw,
3164 	.complete_hdr_size = sizeof(struct hisi_sas_complete_v3_hdr),
3165 	.clear_itct = clear_itct_v3_hw,
3166 	.sl_notify_ssp = sl_notify_ssp_v3_hw,
3167 	.prep_ssp = prep_ssp_v3_hw,
3168 	.prep_smp = prep_smp_v3_hw,
3169 	.prep_stp = prep_ata_v3_hw,
3170 	.prep_abort = prep_abort_v3_hw,
3171 	.start_delivery = start_delivery_v3_hw,
3172 	.phys_init = phys_init_v3_hw,
3173 	.phy_start = start_phy_v3_hw,
3174 	.phy_disable = disable_phy_v3_hw,
3175 	.phy_hard_reset = phy_hard_reset_v3_hw,
3176 	.phy_get_max_linkrate = phy_get_max_linkrate_v3_hw,
3177 	.phy_set_linkrate = phy_set_linkrate_v3_hw,
3178 	.dereg_device = dereg_device_v3_hw,
3179 	.soft_reset = soft_reset_v3_hw,
3180 	.get_phys_state = get_phys_state_v3_hw,
3181 	.get_events = phy_get_events_v3_hw,
3182 	.write_gpio = write_gpio_v3_hw,
3183 	.wait_cmds_complete_timeout = wait_cmds_complete_timeout_v3_hw,
3184 };
3185 
3186 static struct Scsi_Host *
3187 hisi_sas_shost_alloc_pci(struct pci_dev *pdev)
3188 {
3189 	struct Scsi_Host *shost;
3190 	struct hisi_hba *hisi_hba;
3191 	struct device *dev = &pdev->dev;
3192 
3193 	shost = scsi_host_alloc(&sht_v3_hw, sizeof(*hisi_hba));
3194 	if (!shost) {
3195 		dev_err(dev, "shost alloc failed\n");
3196 		return NULL;
3197 	}
3198 	hisi_hba = shost_priv(shost);
3199 
3200 	INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
3201 	INIT_WORK(&hisi_hba->debugfs_work, debugfs_work_handler_v3_hw);
3202 	hisi_hba->hw = &hisi_sas_v3_hw;
3203 	hisi_hba->pci_dev = pdev;
3204 	hisi_hba->dev = dev;
3205 	hisi_hba->shost = shost;
3206 	SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
3207 
3208 	if (prot_mask & ~HISI_SAS_PROT_MASK)
3209 		dev_err(dev, "unsupported protection mask 0x%x, using default (0x0)\n",
3210 			prot_mask);
3211 	else
3212 		hisi_hba->prot_mask = prot_mask;
3213 
3214 	if (hisi_sas_get_fw_info(hisi_hba) < 0)
3215 		goto err_out;
3216 
3217 	if (hisi_sas_alloc(hisi_hba)) {
3218 		hisi_sas_free(hisi_hba);
3219 		goto err_out;
3220 	}
3221 
3222 	return shost;
3223 err_out:
3224 	scsi_host_put(shost);
3225 	dev_err(dev, "shost alloc failed\n");
3226 	return NULL;
3227 }
3228 
3229 static void debugfs_snapshot_cq_reg_v3_hw(struct hisi_hba *hisi_hba)
3230 {
3231 	int queue_entry_size = hisi_hba->hw->complete_hdr_size;
3232 	int dump_index = hisi_hba->debugfs_dump_index;
3233 	int i;
3234 
3235 	for (i = 0; i < hisi_hba->queue_count; i++)
3236 		memcpy(hisi_hba->debugfs_cq[dump_index][i].complete_hdr,
3237 		       hisi_hba->complete_hdr[i],
3238 		       HISI_SAS_QUEUE_SLOTS * queue_entry_size);
3239 }
3240 
3241 static void debugfs_snapshot_dq_reg_v3_hw(struct hisi_hba *hisi_hba)
3242 {
3243 	int queue_entry_size = sizeof(struct hisi_sas_cmd_hdr);
3244 	int dump_index = hisi_hba->debugfs_dump_index;
3245 	int i;
3246 
3247 	for (i = 0; i < hisi_hba->queue_count; i++) {
3248 		struct hisi_sas_cmd_hdr *debugfs_cmd_hdr, *cmd_hdr;
3249 		int j;
3250 
3251 		debugfs_cmd_hdr = hisi_hba->debugfs_dq[dump_index][i].hdr;
3252 		cmd_hdr = hisi_hba->cmd_hdr[i];
3253 
3254 		for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
3255 			memcpy(&debugfs_cmd_hdr[j], &cmd_hdr[j],
3256 			       queue_entry_size);
3257 	}
3258 }
3259 
3260 static void debugfs_snapshot_port_reg_v3_hw(struct hisi_hba *hisi_hba)
3261 {
3262 	int dump_index = hisi_hba->debugfs_dump_index;
3263 	const struct hisi_sas_debugfs_reg *port = &debugfs_port_reg;
3264 	int i, phy_cnt;
3265 	u32 offset;
3266 	u32 *databuf;
3267 
3268 	for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
3269 		databuf = hisi_hba->debugfs_port_reg[dump_index][phy_cnt].data;
3270 		for (i = 0; i < port->count; i++, databuf++) {
3271 			offset = port->base_off + 4 * i;
3272 			*databuf = hisi_sas_phy_read32(hisi_hba, phy_cnt,
3273 						       offset);
3274 		}
3275 	}
3276 }
3277 
3278 static void debugfs_snapshot_global_reg_v3_hw(struct hisi_hba *hisi_hba)
3279 {
3280 	int dump_index = hisi_hba->debugfs_dump_index;
3281 	u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL].data;
3282 	int i;
3283 
3284 	for (i = 0; i < debugfs_axi_reg.count; i++, databuf++)
3285 		*databuf = hisi_sas_read32(hisi_hba, 4 * i);
3286 }
3287 
3288 static void debugfs_snapshot_axi_reg_v3_hw(struct hisi_hba *hisi_hba)
3289 {
3290 	int dump_index = hisi_hba->debugfs_dump_index;
3291 	u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI].data;
3292 	const struct hisi_sas_debugfs_reg *axi = &debugfs_axi_reg;
3293 	int i;
3294 
3295 	for (i = 0; i < axi->count; i++, databuf++)
3296 		*databuf = hisi_sas_read32(hisi_hba, 4 * i + axi->base_off);
3297 }
3298 
3299 static void debugfs_snapshot_ras_reg_v3_hw(struct hisi_hba *hisi_hba)
3300 {
3301 	int dump_index = hisi_hba->debugfs_dump_index;
3302 	u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS].data;
3303 	const struct hisi_sas_debugfs_reg *ras = &debugfs_ras_reg;
3304 	int i;
3305 
3306 	for (i = 0; i < ras->count; i++, databuf++)
3307 		*databuf = hisi_sas_read32(hisi_hba, 4 * i + ras->base_off);
3308 }
3309 
3310 static void debugfs_snapshot_itct_reg_v3_hw(struct hisi_hba *hisi_hba)
3311 {
3312 	int dump_index = hisi_hba->debugfs_dump_index;
3313 	void *cachebuf = hisi_hba->debugfs_itct_cache[dump_index].cache;
3314 	void *databuf = hisi_hba->debugfs_itct[dump_index].itct;
3315 	struct hisi_sas_itct *itct;
3316 	int i;
3317 
3318 	read_iost_itct_cache_v3_hw(hisi_hba, HISI_SAS_ITCT_CACHE, cachebuf);
3319 
3320 	itct = hisi_hba->itct;
3321 
3322 	for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
3323 		memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
3324 		databuf += sizeof(struct hisi_sas_itct);
3325 	}
3326 }
3327 
3328 static void debugfs_snapshot_iost_reg_v3_hw(struct hisi_hba *hisi_hba)
3329 {
3330 	int dump_index = hisi_hba->debugfs_dump_index;
3331 	int max_command_entries = HISI_SAS_MAX_COMMANDS;
3332 	void *cachebuf = hisi_hba->debugfs_iost_cache[dump_index].cache;
3333 	void *databuf = hisi_hba->debugfs_iost[dump_index].iost;
3334 	struct hisi_sas_iost *iost;
3335 	int i;
3336 
3337 	read_iost_itct_cache_v3_hw(hisi_hba, HISI_SAS_IOST_CACHE, cachebuf);
3338 
3339 	iost = hisi_hba->iost;
3340 
3341 	for (i = 0; i < max_command_entries; i++, iost++) {
3342 		memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
3343 		databuf += sizeof(struct hisi_sas_iost);
3344 	}
3345 }
3346 
3347 static const char *
3348 debugfs_to_reg_name_v3_hw(int off, int base_off,
3349 			  const struct hisi_sas_debugfs_reg_lu *lu)
3350 {
3351 	for (; lu->name; lu++) {
3352 		if (off == lu->off - base_off)
3353 			return lu->name;
3354 	}
3355 
3356 	return NULL;
3357 }
3358 
3359 static void debugfs_print_reg_v3_hw(u32 *regs_val, struct seq_file *s,
3360 				    const struct hisi_sas_debugfs_reg *reg)
3361 {
3362 	int i;
3363 
3364 	for (i = 0; i < reg->count; i++) {
3365 		int off = i * 4;
3366 		const char *name;
3367 
3368 		name = debugfs_to_reg_name_v3_hw(off, reg->base_off,
3369 						 reg->lu);
3370 
3371 		if (name)
3372 			seq_printf(s, "0x%08x 0x%08x %s\n", off,
3373 				   regs_val[i], name);
3374 		else
3375 			seq_printf(s, "0x%08x 0x%08x\n", off,
3376 				   regs_val[i]);
3377 	}
3378 }
3379 
3380 static int debugfs_global_v3_hw_show(struct seq_file *s, void *p)
3381 {
3382 	struct hisi_sas_debugfs_regs *global = s->private;
3383 
3384 	debugfs_print_reg_v3_hw(global->data, s,
3385 				&debugfs_global_reg);
3386 
3387 	return 0;
3388 }
3389 DEFINE_SHOW_ATTRIBUTE(debugfs_global_v3_hw);
3390 
3391 static int debugfs_axi_v3_hw_show(struct seq_file *s, void *p)
3392 {
3393 	struct hisi_sas_debugfs_regs *axi = s->private;
3394 
3395 	debugfs_print_reg_v3_hw(axi->data, s,
3396 				&debugfs_axi_reg);
3397 
3398 	return 0;
3399 }
3400 DEFINE_SHOW_ATTRIBUTE(debugfs_axi_v3_hw);
3401 
3402 static int debugfs_ras_v3_hw_show(struct seq_file *s, void *p)
3403 {
3404 	struct hisi_sas_debugfs_regs *ras = s->private;
3405 
3406 	debugfs_print_reg_v3_hw(ras->data, s,
3407 				&debugfs_ras_reg);
3408 
3409 	return 0;
3410 }
3411 DEFINE_SHOW_ATTRIBUTE(debugfs_ras_v3_hw);
3412 
3413 static int debugfs_port_v3_hw_show(struct seq_file *s, void *p)
3414 {
3415 	struct hisi_sas_debugfs_port *port = s->private;
3416 	const struct hisi_sas_debugfs_reg *reg_port = &debugfs_port_reg;
3417 
3418 	debugfs_print_reg_v3_hw(port->data, s, reg_port);
3419 
3420 	return 0;
3421 }
3422 DEFINE_SHOW_ATTRIBUTE(debugfs_port_v3_hw);
3423 
3424 static void debugfs_show_row_64_v3_hw(struct seq_file *s, int index,
3425 				      int sz, __le64 *ptr)
3426 {
3427 	int i;
3428 
3429 	/* completion header size not fixed per HW version */
3430 	seq_printf(s, "index %04d:\n\t", index);
3431 	for (i = 1; i <= sz / 8; i++, ptr++) {
3432 		seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
3433 		if (!(i % 2))
3434 			seq_puts(s, "\n\t");
3435 	}
3436 
3437 	seq_puts(s, "\n");
3438 }
3439 
3440 static void debugfs_show_row_32_v3_hw(struct seq_file *s, int index,
3441 				      int sz, __le32 *ptr)
3442 {
3443 	int i;
3444 
3445 	/* completion header size not fixed per HW version */
3446 	seq_printf(s, "index %04d:\n\t", index);
3447 	for (i = 1; i <= sz / 4; i++, ptr++) {
3448 		seq_printf(s, " 0x%08x", le32_to_cpu(*ptr));
3449 		if (!(i % 4))
3450 			seq_puts(s, "\n\t");
3451 	}
3452 	seq_puts(s, "\n");
3453 }
3454 
3455 static void debugfs_cq_show_slot_v3_hw(struct seq_file *s, int slot,
3456 				       struct hisi_sas_debugfs_cq *debugfs_cq)
3457 {
3458 	struct hisi_sas_cq *cq = debugfs_cq->cq;
3459 	struct hisi_hba *hisi_hba = cq->hisi_hba;
3460 	__le32 *complete_hdr = debugfs_cq->complete_hdr +
3461 			       (hisi_hba->hw->complete_hdr_size * slot);
3462 
3463 	debugfs_show_row_32_v3_hw(s, slot,
3464 				  hisi_hba->hw->complete_hdr_size,
3465 				  complete_hdr);
3466 }
3467 
3468 static int debugfs_cq_v3_hw_show(struct seq_file *s, void *p)
3469 {
3470 	struct hisi_sas_debugfs_cq *debugfs_cq = s->private;
3471 	int slot;
3472 
3473 	for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++)
3474 		debugfs_cq_show_slot_v3_hw(s, slot, debugfs_cq);
3475 
3476 	return 0;
3477 }
3478 DEFINE_SHOW_ATTRIBUTE(debugfs_cq_v3_hw);
3479 
3480 static void debugfs_dq_show_slot_v3_hw(struct seq_file *s, int slot,
3481 				       void *dq_ptr)
3482 {
3483 	struct hisi_sas_debugfs_dq *debugfs_dq = dq_ptr;
3484 	void *cmd_queue = debugfs_dq->hdr;
3485 	__le32 *cmd_hdr = cmd_queue +
3486 		sizeof(struct hisi_sas_cmd_hdr) * slot;
3487 
3488 	debugfs_show_row_32_v3_hw(s, slot, sizeof(struct hisi_sas_cmd_hdr),
3489 				  cmd_hdr);
3490 }
3491 
3492 static int debugfs_dq_v3_hw_show(struct seq_file *s, void *p)
3493 {
3494 	int slot;
3495 
3496 	for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++)
3497 		debugfs_dq_show_slot_v3_hw(s, slot, s->private);
3498 
3499 	return 0;
3500 }
3501 DEFINE_SHOW_ATTRIBUTE(debugfs_dq_v3_hw);
3502 
3503 static int debugfs_iost_v3_hw_show(struct seq_file *s, void *p)
3504 {
3505 	struct hisi_sas_debugfs_iost *debugfs_iost = s->private;
3506 	struct hisi_sas_iost *iost = debugfs_iost->iost;
3507 	int i, max_command_entries = HISI_SAS_MAX_COMMANDS;
3508 
3509 	for (i = 0; i < max_command_entries; i++, iost++) {
3510 		__le64 *data = &iost->qw0;
3511 
3512 		debugfs_show_row_64_v3_hw(s, i, sizeof(*iost), data);
3513 	}
3514 
3515 	return 0;
3516 }
3517 DEFINE_SHOW_ATTRIBUTE(debugfs_iost_v3_hw);
3518 
3519 static int debugfs_iost_cache_v3_hw_show(struct seq_file *s, void *p)
3520 {
3521 	struct hisi_sas_debugfs_iost_cache *debugfs_iost_cache = s->private;
3522 	struct hisi_sas_iost_itct_cache *iost_cache =
3523 						debugfs_iost_cache->cache;
3524 	u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3525 	int i, tab_idx;
3526 	__le64 *iost;
3527 
3528 	for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, iost_cache++) {
3529 		/*
3530 		 * Data struct of IOST cache:
3531 		 * Data[1]: BIT0~15: Table index
3532 		 *	    Bit16:   Valid mask
3533 		 * Data[2]~[9]: IOST table
3534 		 */
3535 		tab_idx = (iost_cache->data[1] & 0xffff);
3536 		iost = (__le64 *)iost_cache;
3537 
3538 		debugfs_show_row_64_v3_hw(s, tab_idx, cache_size, iost);
3539 	}
3540 
3541 	return 0;
3542 }
3543 DEFINE_SHOW_ATTRIBUTE(debugfs_iost_cache_v3_hw);
3544 
3545 static int debugfs_itct_v3_hw_show(struct seq_file *s, void *p)
3546 {
3547 	int i;
3548 	struct hisi_sas_debugfs_itct *debugfs_itct = s->private;
3549 	struct hisi_sas_itct *itct = debugfs_itct->itct;
3550 
3551 	for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
3552 		__le64 *data = &itct->qw0;
3553 
3554 		debugfs_show_row_64_v3_hw(s, i, sizeof(*itct), data);
3555 	}
3556 
3557 	return 0;
3558 }
3559 DEFINE_SHOW_ATTRIBUTE(debugfs_itct_v3_hw);
3560 
3561 static int debugfs_itct_cache_v3_hw_show(struct seq_file *s, void *p)
3562 {
3563 	struct hisi_sas_debugfs_itct_cache *debugfs_itct_cache = s->private;
3564 	struct hisi_sas_iost_itct_cache *itct_cache =
3565 						debugfs_itct_cache->cache;
3566 	u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3567 	int i, tab_idx;
3568 	__le64 *itct;
3569 
3570 	for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, itct_cache++) {
3571 		/*
3572 		 * Data struct of ITCT cache:
3573 		 * Data[1]: BIT0~15: Table index
3574 		 *	    Bit16:   Valid mask
3575 		 * Data[2]~[9]: ITCT table
3576 		 */
3577 		tab_idx = itct_cache->data[1] & 0xffff;
3578 		itct = (__le64 *)itct_cache;
3579 
3580 		debugfs_show_row_64_v3_hw(s, tab_idx, cache_size, itct);
3581 	}
3582 
3583 	return 0;
3584 }
3585 DEFINE_SHOW_ATTRIBUTE(debugfs_itct_cache_v3_hw);
3586 
3587 static void debugfs_create_files_v3_hw(struct hisi_hba *hisi_hba)
3588 {
3589 	u64 *debugfs_timestamp;
3590 	int dump_index = hisi_hba->debugfs_dump_index;
3591 	struct dentry *dump_dentry;
3592 	struct dentry *dentry;
3593 	char name[256];
3594 	int p;
3595 	int c;
3596 	int d;
3597 
3598 	snprintf(name, 256, "%d", dump_index);
3599 
3600 	dump_dentry = debugfs_create_dir(name, hisi_hba->debugfs_dump_dentry);
3601 
3602 	debugfs_timestamp = &hisi_hba->debugfs_timestamp[dump_index];
3603 
3604 	debugfs_create_u64("timestamp", 0400, dump_dentry,
3605 			   debugfs_timestamp);
3606 
3607 	debugfs_create_file("global", 0400, dump_dentry,
3608 			    &hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL],
3609 			    &debugfs_global_v3_hw_fops);
3610 
3611 	/* Create port dir and files */
3612 	dentry = debugfs_create_dir("port", dump_dentry);
3613 	for (p = 0; p < hisi_hba->n_phy; p++) {
3614 		snprintf(name, 256, "%d", p);
3615 
3616 		debugfs_create_file(name, 0400, dentry,
3617 				    &hisi_hba->debugfs_port_reg[dump_index][p],
3618 				    &debugfs_port_v3_hw_fops);
3619 	}
3620 
3621 	/* Create CQ dir and files */
3622 	dentry = debugfs_create_dir("cq", dump_dentry);
3623 	for (c = 0; c < hisi_hba->queue_count; c++) {
3624 		snprintf(name, 256, "%d", c);
3625 
3626 		debugfs_create_file(name, 0400, dentry,
3627 				    &hisi_hba->debugfs_cq[dump_index][c],
3628 				    &debugfs_cq_v3_hw_fops);
3629 	}
3630 
3631 	/* Create DQ dir and files */
3632 	dentry = debugfs_create_dir("dq", dump_dentry);
3633 	for (d = 0; d < hisi_hba->queue_count; d++) {
3634 		snprintf(name, 256, "%d", d);
3635 
3636 		debugfs_create_file(name, 0400, dentry,
3637 				    &hisi_hba->debugfs_dq[dump_index][d],
3638 				    &debugfs_dq_v3_hw_fops);
3639 	}
3640 
3641 	debugfs_create_file("iost", 0400, dump_dentry,
3642 			    &hisi_hba->debugfs_iost[dump_index],
3643 			    &debugfs_iost_v3_hw_fops);
3644 
3645 	debugfs_create_file("iost_cache", 0400, dump_dentry,
3646 			    &hisi_hba->debugfs_iost_cache[dump_index],
3647 			    &debugfs_iost_cache_v3_hw_fops);
3648 
3649 	debugfs_create_file("itct", 0400, dump_dentry,
3650 			    &hisi_hba->debugfs_itct[dump_index],
3651 			    &debugfs_itct_v3_hw_fops);
3652 
3653 	debugfs_create_file("itct_cache", 0400, dump_dentry,
3654 			    &hisi_hba->debugfs_itct_cache[dump_index],
3655 			    &debugfs_itct_cache_v3_hw_fops);
3656 
3657 	debugfs_create_file("axi", 0400, dump_dentry,
3658 			    &hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI],
3659 			    &debugfs_axi_v3_hw_fops);
3660 
3661 	debugfs_create_file("ras", 0400, dump_dentry,
3662 			    &hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS],
3663 			    &debugfs_ras_v3_hw_fops);
3664 }
3665 
3666 static void debugfs_snapshot_regs_v3_hw(struct hisi_hba *hisi_hba)
3667 {
3668 	debugfs_snapshot_prepare_v3_hw(hisi_hba);
3669 
3670 	debugfs_snapshot_global_reg_v3_hw(hisi_hba);
3671 	debugfs_snapshot_port_reg_v3_hw(hisi_hba);
3672 	debugfs_snapshot_axi_reg_v3_hw(hisi_hba);
3673 	debugfs_snapshot_ras_reg_v3_hw(hisi_hba);
3674 	debugfs_snapshot_cq_reg_v3_hw(hisi_hba);
3675 	debugfs_snapshot_dq_reg_v3_hw(hisi_hba);
3676 	debugfs_snapshot_itct_reg_v3_hw(hisi_hba);
3677 	debugfs_snapshot_iost_reg_v3_hw(hisi_hba);
3678 
3679 	debugfs_create_files_v3_hw(hisi_hba);
3680 
3681 	debugfs_snapshot_restore_v3_hw(hisi_hba);
3682 }
3683 
3684 static ssize_t debugfs_trigger_dump_v3_hw_write(struct file *file,
3685 						const char __user *user_buf,
3686 						size_t count, loff_t *ppos)
3687 {
3688 	struct hisi_hba *hisi_hba = file->f_inode->i_private;
3689 	char buf[8];
3690 
3691 	if (hisi_hba->debugfs_dump_index >= hisi_sas_debugfs_dump_count)
3692 		return -EFAULT;
3693 
3694 	if (count > 8)
3695 		return -EFAULT;
3696 
3697 	if (copy_from_user(buf, user_buf, count))
3698 		return -EFAULT;
3699 
3700 	if (buf[0] != '1')
3701 		return -EFAULT;
3702 
3703 	queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
3704 
3705 	return count;
3706 }
3707 
3708 static const struct file_operations debugfs_trigger_dump_v3_hw_fops = {
3709 	.write = &debugfs_trigger_dump_v3_hw_write,
3710 	.owner = THIS_MODULE,
3711 };
3712 
3713 enum {
3714 	HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL = 0,
3715 	HISI_SAS_BIST_LOOPBACK_MODE_SERDES,
3716 	HISI_SAS_BIST_LOOPBACK_MODE_REMOTE,
3717 };
3718 
3719 static const struct {
3720 	int		value;
3721 	char		*name;
3722 } debugfs_loop_linkrate_v3_hw[] = {
3723 	{ SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
3724 	{ SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
3725 	{ SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
3726 	{ SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" },
3727 };
3728 
3729 static int debugfs_bist_linkrate_v3_hw_show(struct seq_file *s, void *p)
3730 {
3731 	struct hisi_hba *hisi_hba = s->private;
3732 	int i;
3733 
3734 	for (i = 0; i < ARRAY_SIZE(debugfs_loop_linkrate_v3_hw); i++) {
3735 		int match = (hisi_hba->debugfs_bist_linkrate ==
3736 			     debugfs_loop_linkrate_v3_hw[i].value);
3737 
3738 		seq_printf(s, "%s%s%s ", match ? "[" : "",
3739 			   debugfs_loop_linkrate_v3_hw[i].name,
3740 			   match ? "]" : "");
3741 	}
3742 	seq_puts(s, "\n");
3743 
3744 	return 0;
3745 }
3746 
3747 static ssize_t debugfs_bist_linkrate_v3_hw_write(struct file *filp,
3748 						 const char __user *buf,
3749 						 size_t count, loff_t *ppos)
3750 {
3751 	struct seq_file *m = filp->private_data;
3752 	struct hisi_hba *hisi_hba = m->private;
3753 	char kbuf[16] = {}, *pkbuf;
3754 	bool found = false;
3755 	int i;
3756 
3757 	if (hisi_hba->debugfs_bist_enable)
3758 		return -EPERM;
3759 
3760 	if (count >= sizeof(kbuf))
3761 		return -EOVERFLOW;
3762 
3763 	if (copy_from_user(kbuf, buf, count))
3764 		return -EINVAL;
3765 
3766 	pkbuf = strstrip(kbuf);
3767 
3768 	for (i = 0; i < ARRAY_SIZE(debugfs_loop_linkrate_v3_hw); i++) {
3769 		if (!strncmp(debugfs_loop_linkrate_v3_hw[i].name,
3770 			     pkbuf, 16)) {
3771 			hisi_hba->debugfs_bist_linkrate =
3772 				debugfs_loop_linkrate_v3_hw[i].value;
3773 			found = true;
3774 			break;
3775 		}
3776 	}
3777 
3778 	if (!found)
3779 		return -EINVAL;
3780 
3781 	return count;
3782 }
3783 
3784 static int debugfs_bist_linkrate_v3_hw_open(struct inode *inode,
3785 					    struct file *filp)
3786 {
3787 	return single_open(filp, debugfs_bist_linkrate_v3_hw_show,
3788 			   inode->i_private);
3789 }
3790 
3791 static const struct file_operations debugfs_bist_linkrate_v3_hw_fops = {
3792 	.open = debugfs_bist_linkrate_v3_hw_open,
3793 	.read = seq_read,
3794 	.write = debugfs_bist_linkrate_v3_hw_write,
3795 	.llseek = seq_lseek,
3796 	.release = single_release,
3797 	.owner = THIS_MODULE,
3798 };
3799 
3800 static const struct {
3801 	int		value;
3802 	char		*name;
3803 } debugfs_loop_code_mode_v3_hw[] = {
3804 	{ HISI_SAS_BIST_CODE_MODE_PRBS7, "PRBS7" },
3805 	{ HISI_SAS_BIST_CODE_MODE_PRBS23, "PRBS23" },
3806 	{ HISI_SAS_BIST_CODE_MODE_PRBS31, "PRBS31" },
3807 	{ HISI_SAS_BIST_CODE_MODE_JTPAT, "JTPAT" },
3808 	{ HISI_SAS_BIST_CODE_MODE_CJTPAT, "CJTPAT" },
3809 	{ HISI_SAS_BIST_CODE_MODE_SCRAMBED_0, "SCRAMBED_0" },
3810 	{ HISI_SAS_BIST_CODE_MODE_TRAIN, "TRAIN" },
3811 	{ HISI_SAS_BIST_CODE_MODE_TRAIN_DONE, "TRAIN_DONE" },
3812 	{ HISI_SAS_BIST_CODE_MODE_HFTP, "HFTP" },
3813 	{ HISI_SAS_BIST_CODE_MODE_MFTP, "MFTP" },
3814 	{ HISI_SAS_BIST_CODE_MODE_LFTP, "LFTP" },
3815 	{ HISI_SAS_BIST_CODE_MODE_FIXED_DATA, "FIXED_DATA" },
3816 };
3817 
3818 static int debugfs_bist_code_mode_v3_hw_show(struct seq_file *s, void *p)
3819 {
3820 	struct hisi_hba *hisi_hba = s->private;
3821 	int i;
3822 
3823 	for (i = 0; i < ARRAY_SIZE(debugfs_loop_code_mode_v3_hw); i++) {
3824 		int match = (hisi_hba->debugfs_bist_code_mode ==
3825 			     debugfs_loop_code_mode_v3_hw[i].value);
3826 
3827 		seq_printf(s, "%s%s%s ", match ? "[" : "",
3828 			   debugfs_loop_code_mode_v3_hw[i].name,
3829 			   match ? "]" : "");
3830 	}
3831 	seq_puts(s, "\n");
3832 
3833 	return 0;
3834 }
3835 
3836 static ssize_t debugfs_bist_code_mode_v3_hw_write(struct file *filp,
3837 						  const char __user *buf,
3838 						  size_t count,
3839 						  loff_t *ppos)
3840 {
3841 	struct seq_file *m = filp->private_data;
3842 	struct hisi_hba *hisi_hba = m->private;
3843 	char kbuf[16] = {}, *pkbuf;
3844 	bool found = false;
3845 	int i;
3846 
3847 	if (hisi_hba->debugfs_bist_enable)
3848 		return -EPERM;
3849 
3850 	if (count >= sizeof(kbuf))
3851 		return -EINVAL;
3852 
3853 	if (copy_from_user(kbuf, buf, count))
3854 		return -EOVERFLOW;
3855 
3856 	pkbuf = strstrip(kbuf);
3857 
3858 	for (i = 0; i < ARRAY_SIZE(debugfs_loop_code_mode_v3_hw); i++) {
3859 		if (!strncmp(debugfs_loop_code_mode_v3_hw[i].name,
3860 			     pkbuf, 16)) {
3861 			hisi_hba->debugfs_bist_code_mode =
3862 				debugfs_loop_code_mode_v3_hw[i].value;
3863 			found = true;
3864 			break;
3865 		}
3866 	}
3867 
3868 	if (!found)
3869 		return -EINVAL;
3870 
3871 	return count;
3872 }
3873 
3874 static int debugfs_bist_code_mode_v3_hw_open(struct inode *inode,
3875 					     struct file *filp)
3876 {
3877 	return single_open(filp, debugfs_bist_code_mode_v3_hw_show,
3878 			   inode->i_private);
3879 }
3880 
3881 static const struct file_operations debugfs_bist_code_mode_v3_hw_fops = {
3882 	.open = debugfs_bist_code_mode_v3_hw_open,
3883 	.read = seq_read,
3884 	.write = debugfs_bist_code_mode_v3_hw_write,
3885 	.llseek = seq_lseek,
3886 	.release = single_release,
3887 	.owner = THIS_MODULE,
3888 };
3889 
3890 static ssize_t debugfs_bist_phy_v3_hw_write(struct file *filp,
3891 					    const char __user *buf,
3892 					    size_t count, loff_t *ppos)
3893 {
3894 	struct seq_file *m = filp->private_data;
3895 	struct hisi_hba *hisi_hba = m->private;
3896 	unsigned int phy_no;
3897 	int val;
3898 
3899 	if (hisi_hba->debugfs_bist_enable)
3900 		return -EPERM;
3901 
3902 	val = kstrtouint_from_user(buf, count, 0, &phy_no);
3903 	if (val)
3904 		return val;
3905 
3906 	if (phy_no >= hisi_hba->n_phy)
3907 		return -EINVAL;
3908 
3909 	hisi_hba->debugfs_bist_phy_no = phy_no;
3910 
3911 	return count;
3912 }
3913 
3914 static int debugfs_bist_phy_v3_hw_show(struct seq_file *s, void *p)
3915 {
3916 	struct hisi_hba *hisi_hba = s->private;
3917 
3918 	seq_printf(s, "%d\n", hisi_hba->debugfs_bist_phy_no);
3919 
3920 	return 0;
3921 }
3922 
3923 static int debugfs_bist_phy_v3_hw_open(struct inode *inode,
3924 				       struct file *filp)
3925 {
3926 	return single_open(filp, debugfs_bist_phy_v3_hw_show,
3927 			   inode->i_private);
3928 }
3929 
3930 static const struct file_operations debugfs_bist_phy_v3_hw_fops = {
3931 	.open = debugfs_bist_phy_v3_hw_open,
3932 	.read = seq_read,
3933 	.write = debugfs_bist_phy_v3_hw_write,
3934 	.llseek = seq_lseek,
3935 	.release = single_release,
3936 	.owner = THIS_MODULE,
3937 };
3938 
3939 static const struct {
3940 	int		value;
3941 	char		*name;
3942 } debugfs_loop_modes_v3_hw[] = {
3943 	{ HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL, "digital" },
3944 	{ HISI_SAS_BIST_LOOPBACK_MODE_SERDES, "serdes" },
3945 	{ HISI_SAS_BIST_LOOPBACK_MODE_REMOTE, "remote" },
3946 };
3947 
3948 static int debugfs_bist_mode_v3_hw_show(struct seq_file *s, void *p)
3949 {
3950 	struct hisi_hba *hisi_hba = s->private;
3951 	int i;
3952 
3953 	for (i = 0; i < ARRAY_SIZE(debugfs_loop_modes_v3_hw); i++) {
3954 		int match = (hisi_hba->debugfs_bist_mode ==
3955 			     debugfs_loop_modes_v3_hw[i].value);
3956 
3957 		seq_printf(s, "%s%s%s ", match ? "[" : "",
3958 			   debugfs_loop_modes_v3_hw[i].name,
3959 			   match ? "]" : "");
3960 	}
3961 	seq_puts(s, "\n");
3962 
3963 	return 0;
3964 }
3965 
3966 static ssize_t debugfs_bist_mode_v3_hw_write(struct file *filp,
3967 					     const char __user *buf,
3968 					     size_t count, loff_t *ppos)
3969 {
3970 	struct seq_file *m = filp->private_data;
3971 	struct hisi_hba *hisi_hba = m->private;
3972 	char kbuf[16] = {}, *pkbuf;
3973 	bool found = false;
3974 	int i;
3975 
3976 	if (hisi_hba->debugfs_bist_enable)
3977 		return -EPERM;
3978 
3979 	if (count >= sizeof(kbuf))
3980 		return -EINVAL;
3981 
3982 	if (copy_from_user(kbuf, buf, count))
3983 		return -EOVERFLOW;
3984 
3985 	pkbuf = strstrip(kbuf);
3986 
3987 	for (i = 0; i < ARRAY_SIZE(debugfs_loop_modes_v3_hw); i++) {
3988 		if (!strncmp(debugfs_loop_modes_v3_hw[i].name, pkbuf, 16)) {
3989 			hisi_hba->debugfs_bist_mode =
3990 				debugfs_loop_modes_v3_hw[i].value;
3991 			found = true;
3992 			break;
3993 		}
3994 	}
3995 
3996 	if (!found)
3997 		return -EINVAL;
3998 
3999 	return count;
4000 }
4001 
4002 static int debugfs_bist_mode_v3_hw_open(struct inode *inode,
4003 					struct file *filp)
4004 {
4005 	return single_open(filp, debugfs_bist_mode_v3_hw_show,
4006 			   inode->i_private);
4007 }
4008 
4009 static const struct file_operations debugfs_bist_mode_v3_hw_fops = {
4010 	.open = debugfs_bist_mode_v3_hw_open,
4011 	.read = seq_read,
4012 	.write = debugfs_bist_mode_v3_hw_write,
4013 	.llseek = seq_lseek,
4014 	.release = single_release,
4015 	.owner = THIS_MODULE,
4016 };
4017 
4018 static ssize_t debugfs_bist_enable_v3_hw_write(struct file *filp,
4019 					       const char __user *buf,
4020 					       size_t count, loff_t *ppos)
4021 {
4022 	struct seq_file *m = filp->private_data;
4023 	struct hisi_hba *hisi_hba = m->private;
4024 	unsigned int enable;
4025 	int val;
4026 
4027 	val = kstrtouint_from_user(buf, count, 0, &enable);
4028 	if (val)
4029 		return val;
4030 
4031 	if (enable > 1)
4032 		return -EINVAL;
4033 
4034 	if (enable == hisi_hba->debugfs_bist_enable)
4035 		return count;
4036 
4037 	val = debugfs_set_bist_v3_hw(hisi_hba, enable);
4038 	if (val < 0)
4039 		return val;
4040 
4041 	hisi_hba->debugfs_bist_enable = enable;
4042 
4043 	return count;
4044 }
4045 
4046 static int debugfs_bist_enable_v3_hw_show(struct seq_file *s, void *p)
4047 {
4048 	struct hisi_hba *hisi_hba = s->private;
4049 
4050 	seq_printf(s, "%d\n", hisi_hba->debugfs_bist_enable);
4051 
4052 	return 0;
4053 }
4054 
4055 static int debugfs_bist_enable_v3_hw_open(struct inode *inode,
4056 					  struct file *filp)
4057 {
4058 	return single_open(filp, debugfs_bist_enable_v3_hw_show,
4059 			   inode->i_private);
4060 }
4061 
4062 static const struct file_operations debugfs_bist_enable_v3_hw_fops = {
4063 	.open = debugfs_bist_enable_v3_hw_open,
4064 	.read = seq_read,
4065 	.write = debugfs_bist_enable_v3_hw_write,
4066 	.llseek = seq_lseek,
4067 	.release = single_release,
4068 	.owner = THIS_MODULE,
4069 };
4070 
4071 static const struct {
4072 	char *name;
4073 } debugfs_ffe_name_v3_hw[FFE_CFG_MAX] = {
4074 	{ "SAS_1_5_GBPS" },
4075 	{ "SAS_3_0_GBPS" },
4076 	{ "SAS_6_0_GBPS" },
4077 	{ "SAS_12_0_GBPS" },
4078 	{ "FFE_RESV" },
4079 	{ "SATA_1_5_GBPS" },
4080 	{ "SATA_3_0_GBPS" },
4081 	{ "SATA_6_0_GBPS" },
4082 };
4083 
4084 static ssize_t debugfs_v3_hw_write(struct file *filp,
4085 				   const char __user *buf,
4086 				   size_t count, loff_t *ppos)
4087 {
4088 	struct seq_file *m = filp->private_data;
4089 	u32 *val = m->private;
4090 	int res;
4091 
4092 	res = kstrtouint_from_user(buf, count, 0, val);
4093 	if (res)
4094 		return res;
4095 
4096 	return count;
4097 }
4098 
4099 static int debugfs_v3_hw_show(struct seq_file *s, void *p)
4100 {
4101 	u32 *val = s->private;
4102 
4103 	seq_printf(s, "0x%x\n", *val);
4104 
4105 	return 0;
4106 }
4107 
4108 static int debugfs_v3_hw_open(struct inode *inode, struct file *filp)
4109 {
4110 	return single_open(filp, debugfs_v3_hw_show,
4111 			   inode->i_private);
4112 }
4113 
4114 static const struct file_operations debugfs_v3_hw_fops = {
4115 	.open = debugfs_v3_hw_open,
4116 	.read = seq_read,
4117 	.write = debugfs_v3_hw_write,
4118 	.llseek = seq_lseek,
4119 	.release = single_release,
4120 	.owner = THIS_MODULE,
4121 };
4122 
4123 static ssize_t debugfs_phy_down_cnt_v3_hw_write(struct file *filp,
4124 						const char __user *buf,
4125 						size_t count, loff_t *ppos)
4126 {
4127 	struct seq_file *s = filp->private_data;
4128 	struct hisi_sas_phy *phy = s->private;
4129 	unsigned int set_val;
4130 	int res;
4131 
4132 	res = kstrtouint_from_user(buf, count, 0, &set_val);
4133 	if (res)
4134 		return res;
4135 
4136 	if (set_val > 0)
4137 		return -EINVAL;
4138 
4139 	atomic_set(&phy->down_cnt, 0);
4140 
4141 	return count;
4142 }
4143 
4144 static int debugfs_phy_down_cnt_v3_hw_show(struct seq_file *s, void *p)
4145 {
4146 	struct hisi_sas_phy *phy = s->private;
4147 
4148 	seq_printf(s, "%d\n", atomic_read(&phy->down_cnt));
4149 
4150 	return 0;
4151 }
4152 
4153 static int debugfs_phy_down_cnt_v3_hw_open(struct inode *inode,
4154 					   struct file *filp)
4155 {
4156 	return single_open(filp, debugfs_phy_down_cnt_v3_hw_show,
4157 			   inode->i_private);
4158 }
4159 
4160 static const struct file_operations debugfs_phy_down_cnt_v3_hw_fops = {
4161 	.open = debugfs_phy_down_cnt_v3_hw_open,
4162 	.read = seq_read,
4163 	.write = debugfs_phy_down_cnt_v3_hw_write,
4164 	.llseek = seq_lseek,
4165 	.release = single_release,
4166 	.owner = THIS_MODULE,
4167 };
4168 
4169 enum fifo_dump_mode_v3_hw {
4170 	FIFO_DUMP_FORVER =		(1U << 0),
4171 	FIFO_DUMP_AFTER_TRIGGER =	(1U << 1),
4172 	FIFO_DUMP_UNTILL_TRIGGER =	(1U << 2),
4173 };
4174 
4175 enum fifo_trigger_mode_v3_hw {
4176 	FIFO_TRIGGER_EDGE =		(1U << 0),
4177 	FIFO_TRIGGER_SAME_LEVEL =	(1U << 1),
4178 	FIFO_TRIGGER_DIFF_LEVEL =	(1U << 2),
4179 };
4180 
4181 static int debugfs_is_fifo_config_valid_v3_hw(struct hisi_sas_phy *phy)
4182 {
4183 	struct hisi_hba *hisi_hba = phy->hisi_hba;
4184 
4185 	if (phy->fifo.signal_sel > 0xf) {
4186 		dev_info(hisi_hba->dev, "Invalid signal select: %u\n",
4187 			 phy->fifo.signal_sel);
4188 		return -EINVAL;
4189 	}
4190 
4191 	switch (phy->fifo.dump_mode) {
4192 	case FIFO_DUMP_FORVER:
4193 	case FIFO_DUMP_AFTER_TRIGGER:
4194 	case FIFO_DUMP_UNTILL_TRIGGER:
4195 		break;
4196 	default:
4197 		dev_info(hisi_hba->dev, "Invalid dump mode: %u\n",
4198 			 phy->fifo.dump_mode);
4199 		return -EINVAL;
4200 	}
4201 
4202 	/* when FIFO_DUMP_FORVER, no need to check trigger_mode */
4203 	if (phy->fifo.dump_mode == FIFO_DUMP_FORVER)
4204 		return 0;
4205 
4206 	switch (phy->fifo.trigger_mode) {
4207 	case FIFO_TRIGGER_EDGE:
4208 	case FIFO_TRIGGER_SAME_LEVEL:
4209 	case FIFO_TRIGGER_DIFF_LEVEL:
4210 		break;
4211 	default:
4212 		dev_info(hisi_hba->dev, "Invalid trigger mode: %u\n",
4213 			 phy->fifo.trigger_mode);
4214 		return -EINVAL;
4215 	}
4216 	return 0;
4217 }
4218 
4219 static int debugfs_update_fifo_config_v3_hw(struct hisi_sas_phy *phy)
4220 {
4221 	u32 trigger_mode = phy->fifo.trigger_mode;
4222 	u32 signal_sel = phy->fifo.signal_sel;
4223 	u32 dump_mode = phy->fifo.dump_mode;
4224 	struct hisi_hba *hisi_hba = phy->hisi_hba;
4225 	int phy_no = phy->sas_phy.id;
4226 	u32 reg_val;
4227 	int res;
4228 
4229 	/* Check the validity of trace FIFO configuration */
4230 	res = debugfs_is_fifo_config_valid_v3_hw(phy);
4231 	if (res)
4232 		return res;
4233 
4234 	reg_val = hisi_sas_phy_read32(hisi_hba, phy_no, DFX_FIFO_CTRL);
4235 	/* Disable trace FIFO before update configuration */
4236 	reg_val |= DFX_FIFO_CTRL_DUMP_DISABLE_MSK;
4237 
4238 	/* Update trace FIFO configuration */
4239 	reg_val &= ~(DFX_FIFO_CTRL_DUMP_MODE_MSK |
4240 		     DFX_FIFO_CTRL_SIGNAL_SEL_MSK |
4241 		     DFX_FIFO_CTRL_TRIGGER_MODE_MSK);
4242 
4243 	reg_val |= ((trigger_mode << DFX_FIFO_CTRL_TRIGGER_MODE_OFF) |
4244 		    (dump_mode << DFX_FIFO_CTRL_DUMP_MODE_OFF) |
4245 		    (signal_sel << DFX_FIFO_CTRL_SIGNAL_SEL_OFF));
4246 	hisi_sas_phy_write32(hisi_hba, phy_no, DFX_FIFO_CTRL, reg_val);
4247 
4248 	hisi_sas_phy_write32(hisi_hba, phy_no, DFX_FIFO_DUMP_MSK,
4249 			     phy->fifo.dump_msk);
4250 
4251 	hisi_sas_phy_write32(hisi_hba, phy_no, DFX_FIFO_TRIGGER,
4252 			     phy->fifo.trigger);
4253 
4254 	hisi_sas_phy_write32(hisi_hba, phy_no, DFX_FIFO_TRIGGER_MSK,
4255 			     phy->fifo.trigger_msk);
4256 
4257 	/* Enable trace FIFO after updated configuration */
4258 	reg_val = hisi_sas_phy_read32(hisi_hba, phy_no, DFX_FIFO_CTRL);
4259 	reg_val &= ~DFX_FIFO_CTRL_DUMP_DISABLE_MSK;
4260 	hisi_sas_phy_write32(hisi_hba, phy_no, DFX_FIFO_CTRL, reg_val);
4261 
4262 	return 0;
4263 }
4264 
4265 static ssize_t debugfs_fifo_update_cfg_v3_hw_write(struct file *filp,
4266 						   const char __user *buf,
4267 						   size_t count, loff_t *ppos)
4268 {
4269 	struct hisi_sas_phy *phy = filp->private_data;
4270 	bool update;
4271 	int val;
4272 
4273 	val = kstrtobool_from_user(buf, count, &update);
4274 	if (val)
4275 		return val;
4276 
4277 	if (update != 1)
4278 		return -EINVAL;
4279 
4280 	val = debugfs_update_fifo_config_v3_hw(phy);
4281 	if (val)
4282 		return val;
4283 
4284 	return count;
4285 }
4286 
4287 static const struct file_operations debugfs_fifo_update_cfg_v3_hw_fops = {
4288 	.open = simple_open,
4289 	.write = debugfs_fifo_update_cfg_v3_hw_write,
4290 	.owner = THIS_MODULE,
4291 };
4292 
4293 static void debugfs_read_fifo_data_v3_hw(struct hisi_sas_phy *phy)
4294 {
4295 	struct hisi_hba *hisi_hba = phy->hisi_hba;
4296 	u32 *buf = phy->fifo.rd_data;
4297 	int phy_no = phy->sas_phy.id;
4298 	u32 val;
4299 	int i;
4300 
4301 	memset(buf, 0, sizeof(phy->fifo.rd_data));
4302 
4303 	/* Disable trace FIFO before read data */
4304 	val = hisi_sas_phy_read32(hisi_hba, phy_no, DFX_FIFO_CTRL);
4305 	val |= DFX_FIFO_CTRL_DUMP_DISABLE_MSK;
4306 	hisi_sas_phy_write32(hisi_hba, phy_no, DFX_FIFO_CTRL, val);
4307 
4308 	for (i = 0; i < HISI_SAS_FIFO_DATA_DW_SIZE; i++) {
4309 		val = hisi_sas_phy_read32(hisi_hba, phy_no,
4310 					  DFX_FIFO_RD_DATA);
4311 		buf[i] = val;
4312 	}
4313 
4314 	/* Enable trace FIFO after read data */
4315 	val = hisi_sas_phy_read32(hisi_hba, phy_no, DFX_FIFO_CTRL);
4316 	val &= ~DFX_FIFO_CTRL_DUMP_DISABLE_MSK;
4317 	hisi_sas_phy_write32(hisi_hba, phy_no, DFX_FIFO_CTRL, val);
4318 }
4319 
4320 static int debugfs_fifo_data_v3_hw_show(struct seq_file *s, void *p)
4321 {
4322 	struct hisi_sas_phy *phy = s->private;
4323 
4324 	debugfs_read_fifo_data_v3_hw(phy);
4325 
4326 	debugfs_show_row_32_v3_hw(s, 0, HISI_SAS_FIFO_DATA_DW_SIZE * 4,
4327 				  phy->fifo.rd_data);
4328 
4329 	return 0;
4330 }
4331 DEFINE_SHOW_ATTRIBUTE(debugfs_fifo_data_v3_hw);
4332 
4333 static void debugfs_fifo_init_v3_hw(struct hisi_hba *hisi_hba)
4334 {
4335 	int phy_no;
4336 
4337 	hisi_hba->debugfs_fifo_dentry =
4338 			debugfs_create_dir("fifo", hisi_hba->debugfs_dir);
4339 
4340 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
4341 		struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
4342 		struct dentry *port_dentry;
4343 		char name[256];
4344 		u32 val;
4345 
4346 		/* get default configuration for trace FIFO */
4347 		val = hisi_sas_phy_read32(hisi_hba, phy_no, DFX_FIFO_CTRL);
4348 		val &= DFX_FIFO_CTRL_DUMP_MODE_MSK;
4349 		val >>= DFX_FIFO_CTRL_DUMP_MODE_OFF;
4350 		phy->fifo.dump_mode = val;
4351 
4352 		val = hisi_sas_phy_read32(hisi_hba, phy_no, DFX_FIFO_CTRL);
4353 		val &= DFX_FIFO_CTRL_TRIGGER_MODE_MSK;
4354 		val >>= DFX_FIFO_CTRL_TRIGGER_MODE_OFF;
4355 		phy->fifo.trigger_mode = val;
4356 
4357 		val = hisi_sas_phy_read32(hisi_hba, phy_no, DFX_FIFO_CTRL);
4358 		val &= DFX_FIFO_CTRL_SIGNAL_SEL_MSK;
4359 		val >>= DFX_FIFO_CTRL_SIGNAL_SEL_OFF;
4360 		phy->fifo.signal_sel = val;
4361 
4362 		val = hisi_sas_phy_read32(hisi_hba, phy_no, DFX_FIFO_DUMP_MSK);
4363 		phy->fifo.dump_msk = val;
4364 
4365 		val = hisi_sas_phy_read32(hisi_hba, phy_no, DFX_FIFO_TRIGGER);
4366 		phy->fifo.trigger = val;
4367 		val = hisi_sas_phy_read32(hisi_hba, phy_no, DFX_FIFO_TRIGGER_MSK);
4368 		phy->fifo.trigger_msk = val;
4369 
4370 		snprintf(name, 256, "%d", phy_no);
4371 		port_dentry = debugfs_create_dir(name,
4372 						 hisi_hba->debugfs_fifo_dentry);
4373 
4374 		debugfs_create_file("update_config", 0200, port_dentry, phy,
4375 				    &debugfs_fifo_update_cfg_v3_hw_fops);
4376 
4377 		debugfs_create_file("signal_sel", 0600, port_dentry,
4378 				    &phy->fifo.signal_sel,
4379 				    &debugfs_v3_hw_fops);
4380 
4381 		debugfs_create_file("dump_msk", 0600, port_dentry,
4382 				    &phy->fifo.dump_msk,
4383 				    &debugfs_v3_hw_fops);
4384 
4385 		debugfs_create_file("dump_mode", 0600, port_dentry,
4386 				    &phy->fifo.dump_mode,
4387 				    &debugfs_v3_hw_fops);
4388 
4389 		debugfs_create_file("trigger_mode", 0600, port_dentry,
4390 				    &phy->fifo.trigger_mode,
4391 				    &debugfs_v3_hw_fops);
4392 
4393 		debugfs_create_file("trigger", 0600, port_dentry,
4394 				    &phy->fifo.trigger,
4395 				    &debugfs_v3_hw_fops);
4396 
4397 		debugfs_create_file("trigger_msk", 0600, port_dentry,
4398 				    &phy->fifo.trigger_msk,
4399 				    &debugfs_v3_hw_fops);
4400 
4401 		debugfs_create_file("fifo_data", 0400, port_dentry, phy,
4402 				    &debugfs_fifo_data_v3_hw_fops);
4403 	}
4404 }
4405 
4406 static void debugfs_work_handler_v3_hw(struct work_struct *work)
4407 {
4408 	struct hisi_hba *hisi_hba =
4409 		container_of(work, struct hisi_hba, debugfs_work);
4410 	int debugfs_dump_index = hisi_hba->debugfs_dump_index;
4411 	struct device *dev = hisi_hba->dev;
4412 	u64 timestamp = local_clock();
4413 
4414 	if (debugfs_dump_index >= hisi_sas_debugfs_dump_count) {
4415 		dev_warn(dev, "dump count exceeded!\n");
4416 		return;
4417 	}
4418 
4419 	do_div(timestamp, NSEC_PER_MSEC);
4420 	hisi_hba->debugfs_timestamp[debugfs_dump_index] = timestamp;
4421 
4422 	debugfs_snapshot_regs_v3_hw(hisi_hba);
4423 	hisi_hba->debugfs_dump_index++;
4424 }
4425 
4426 static void debugfs_release_v3_hw(struct hisi_hba *hisi_hba, int dump_index)
4427 {
4428 	struct device *dev = hisi_hba->dev;
4429 	int i;
4430 
4431 	devm_kfree(dev, hisi_hba->debugfs_iost_cache[dump_index].cache);
4432 	devm_kfree(dev, hisi_hba->debugfs_itct_cache[dump_index].cache);
4433 	devm_kfree(dev, hisi_hba->debugfs_iost[dump_index].iost);
4434 	devm_kfree(dev, hisi_hba->debugfs_itct[dump_index].itct);
4435 
4436 	for (i = 0; i < hisi_hba->queue_count; i++)
4437 		devm_kfree(dev, hisi_hba->debugfs_dq[dump_index][i].hdr);
4438 
4439 	for (i = 0; i < hisi_hba->queue_count; i++)
4440 		devm_kfree(dev,
4441 			   hisi_hba->debugfs_cq[dump_index][i].complete_hdr);
4442 
4443 	for (i = 0; i < DEBUGFS_REGS_NUM; i++)
4444 		devm_kfree(dev, hisi_hba->debugfs_regs[dump_index][i].data);
4445 
4446 	for (i = 0; i < hisi_hba->n_phy; i++)
4447 		devm_kfree(dev, hisi_hba->debugfs_port_reg[dump_index][i].data);
4448 }
4449 
4450 static const struct hisi_sas_debugfs_reg *debugfs_reg_array_v3_hw[DEBUGFS_REGS_NUM] = {
4451 	[DEBUGFS_GLOBAL] = &debugfs_global_reg,
4452 	[DEBUGFS_AXI] = &debugfs_axi_reg,
4453 	[DEBUGFS_RAS] = &debugfs_ras_reg,
4454 };
4455 
4456 static int debugfs_alloc_v3_hw(struct hisi_hba *hisi_hba, int dump_index)
4457 {
4458 	const struct hisi_sas_hw *hw = hisi_hba->hw;
4459 	struct device *dev = hisi_hba->dev;
4460 	int p, c, d, r, i;
4461 	size_t sz;
4462 
4463 	for (r = 0; r < DEBUGFS_REGS_NUM; r++) {
4464 		struct hisi_sas_debugfs_regs *regs =
4465 				&hisi_hba->debugfs_regs[dump_index][r];
4466 
4467 		sz = debugfs_reg_array_v3_hw[r]->count * 4;
4468 		regs->data = devm_kmalloc(dev, sz, GFP_KERNEL);
4469 		if (!regs->data)
4470 			goto fail;
4471 		regs->hisi_hba = hisi_hba;
4472 	}
4473 
4474 	sz = debugfs_port_reg.count * 4;
4475 	for (p = 0; p < hisi_hba->n_phy; p++) {
4476 		struct hisi_sas_debugfs_port *port =
4477 				&hisi_hba->debugfs_port_reg[dump_index][p];
4478 
4479 		port->data = devm_kmalloc(dev, sz, GFP_KERNEL);
4480 		if (!port->data)
4481 			goto fail;
4482 		port->phy = &hisi_hba->phy[p];
4483 	}
4484 
4485 	sz = hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
4486 	for (c = 0; c < hisi_hba->queue_count; c++) {
4487 		struct hisi_sas_debugfs_cq *cq =
4488 				&hisi_hba->debugfs_cq[dump_index][c];
4489 
4490 		cq->complete_hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
4491 		if (!cq->complete_hdr)
4492 			goto fail;
4493 		cq->cq = &hisi_hba->cq[c];
4494 	}
4495 
4496 	sz = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
4497 	for (d = 0; d < hisi_hba->queue_count; d++) {
4498 		struct hisi_sas_debugfs_dq *dq =
4499 				&hisi_hba->debugfs_dq[dump_index][d];
4500 
4501 		dq->hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
4502 		if (!dq->hdr)
4503 			goto fail;
4504 		dq->dq = &hisi_hba->dq[d];
4505 	}
4506 
4507 	sz = HISI_SAS_MAX_COMMANDS * sizeof(struct hisi_sas_iost);
4508 
4509 	hisi_hba->debugfs_iost[dump_index].iost =
4510 				devm_kmalloc(dev, sz, GFP_KERNEL);
4511 	if (!hisi_hba->debugfs_iost[dump_index].iost)
4512 		goto fail;
4513 
4514 	sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
4515 	     sizeof(struct hisi_sas_iost_itct_cache);
4516 
4517 	hisi_hba->debugfs_iost_cache[dump_index].cache =
4518 				devm_kmalloc(dev, sz, GFP_KERNEL);
4519 	if (!hisi_hba->debugfs_iost_cache[dump_index].cache)
4520 		goto fail;
4521 
4522 	sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
4523 	     sizeof(struct hisi_sas_iost_itct_cache);
4524 
4525 	hisi_hba->debugfs_itct_cache[dump_index].cache =
4526 				devm_kmalloc(dev, sz, GFP_KERNEL);
4527 	if (!hisi_hba->debugfs_itct_cache[dump_index].cache)
4528 		goto fail;
4529 
4530 	/* New memory allocation must be locate before itct */
4531 	sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
4532 
4533 	hisi_hba->debugfs_itct[dump_index].itct =
4534 				devm_kmalloc(dev, sz, GFP_KERNEL);
4535 	if (!hisi_hba->debugfs_itct[dump_index].itct)
4536 		goto fail;
4537 
4538 	return 0;
4539 fail:
4540 	for (i = 0; i < hisi_sas_debugfs_dump_count; i++)
4541 		debugfs_release_v3_hw(hisi_hba, i);
4542 	return -ENOMEM;
4543 }
4544 
4545 static void debugfs_phy_down_cnt_init_v3_hw(struct hisi_hba *hisi_hba)
4546 {
4547 	struct dentry *dir = debugfs_create_dir("phy_down_cnt",
4548 						hisi_hba->debugfs_dir);
4549 	char name[16];
4550 	int phy_no;
4551 
4552 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
4553 		snprintf(name, 16, "%d", phy_no);
4554 		debugfs_create_file(name, 0600, dir,
4555 				    &hisi_hba->phy[phy_no],
4556 				    &debugfs_phy_down_cnt_v3_hw_fops);
4557 	}
4558 }
4559 
4560 static void debugfs_bist_init_v3_hw(struct hisi_hba *hisi_hba)
4561 {
4562 	struct dentry *ports_dentry;
4563 	int phy_no;
4564 
4565 	hisi_hba->debugfs_bist_dentry =
4566 			debugfs_create_dir("bist", hisi_hba->debugfs_dir);
4567 	debugfs_create_file("link_rate", 0600,
4568 			    hisi_hba->debugfs_bist_dentry, hisi_hba,
4569 			    &debugfs_bist_linkrate_v3_hw_fops);
4570 
4571 	debugfs_create_file("code_mode", 0600,
4572 			    hisi_hba->debugfs_bist_dentry, hisi_hba,
4573 			    &debugfs_bist_code_mode_v3_hw_fops);
4574 
4575 	debugfs_create_file("fixed_code", 0600,
4576 			    hisi_hba->debugfs_bist_dentry,
4577 			    &hisi_hba->debugfs_bist_fixed_code[0],
4578 			    &debugfs_v3_hw_fops);
4579 
4580 	debugfs_create_file("fixed_code_1", 0600,
4581 			    hisi_hba->debugfs_bist_dentry,
4582 			    &hisi_hba->debugfs_bist_fixed_code[1],
4583 			    &debugfs_v3_hw_fops);
4584 
4585 	debugfs_create_file("phy_id", 0600, hisi_hba->debugfs_bist_dentry,
4586 			    hisi_hba, &debugfs_bist_phy_v3_hw_fops);
4587 
4588 	debugfs_create_u32("cnt", 0600, hisi_hba->debugfs_bist_dentry,
4589 			   &hisi_hba->debugfs_bist_cnt);
4590 
4591 	debugfs_create_file("loopback_mode", 0600,
4592 			    hisi_hba->debugfs_bist_dentry,
4593 			    hisi_hba, &debugfs_bist_mode_v3_hw_fops);
4594 
4595 	debugfs_create_file("enable", 0600, hisi_hba->debugfs_bist_dentry,
4596 			    hisi_hba, &debugfs_bist_enable_v3_hw_fops);
4597 
4598 	ports_dentry = debugfs_create_dir("port", hisi_hba->debugfs_bist_dentry);
4599 
4600 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
4601 		struct dentry *port_dentry;
4602 		struct dentry *ffe_dentry;
4603 		char name[256];
4604 		int i;
4605 
4606 		snprintf(name, 256, "%d", phy_no);
4607 		port_dentry = debugfs_create_dir(name, ports_dentry);
4608 		ffe_dentry = debugfs_create_dir("ffe", port_dentry);
4609 		for (i = 0; i < FFE_CFG_MAX; i++) {
4610 			if (i == FFE_RESV)
4611 				continue;
4612 			debugfs_create_file(debugfs_ffe_name_v3_hw[i].name,
4613 					    0600, ffe_dentry,
4614 					    &hisi_hba->debugfs_bist_ffe[phy_no][i],
4615 					    &debugfs_v3_hw_fops);
4616 		}
4617 	}
4618 
4619 	hisi_hba->debugfs_bist_linkrate = SAS_LINK_RATE_1_5_GBPS;
4620 }
4621 
4622 static void debugfs_init_v3_hw(struct hisi_hba *hisi_hba)
4623 {
4624 	struct device *dev = hisi_hba->dev;
4625 	int i;
4626 
4627 	hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
4628 						   hisi_sas_debugfs_dir);
4629 	debugfs_create_file("trigger_dump", 0200,
4630 			    hisi_hba->debugfs_dir,
4631 			    hisi_hba,
4632 			    &debugfs_trigger_dump_v3_hw_fops);
4633 
4634 	/* create bist structures */
4635 	debugfs_bist_init_v3_hw(hisi_hba);
4636 
4637 	hisi_hba->debugfs_dump_dentry =
4638 			debugfs_create_dir("dump", hisi_hba->debugfs_dir);
4639 
4640 	debugfs_phy_down_cnt_init_v3_hw(hisi_hba);
4641 	debugfs_fifo_init_v3_hw(hisi_hba);
4642 
4643 	for (i = 0; i < hisi_sas_debugfs_dump_count; i++) {
4644 		if (debugfs_alloc_v3_hw(hisi_hba, i)) {
4645 			debugfs_remove_recursive(hisi_hba->debugfs_dir);
4646 			dev_dbg(dev, "failed to init debugfs!\n");
4647 			break;
4648 		}
4649 	}
4650 }
4651 
4652 static void debugfs_exit_v3_hw(struct hisi_hba *hisi_hba)
4653 {
4654 	debugfs_remove_recursive(hisi_hba->debugfs_dir);
4655 }
4656 
4657 static int
4658 hisi_sas_v3_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4659 {
4660 	struct Scsi_Host *shost;
4661 	struct hisi_hba *hisi_hba;
4662 	struct device *dev = &pdev->dev;
4663 	struct asd_sas_phy **arr_phy;
4664 	struct asd_sas_port **arr_port;
4665 	struct sas_ha_struct *sha;
4666 	int rc, phy_nr, port_nr, i;
4667 
4668 	rc = pci_enable_device(pdev);
4669 	if (rc)
4670 		goto err_out;
4671 
4672 	pci_set_master(pdev);
4673 
4674 	rc = pci_request_regions(pdev, DRV_NAME);
4675 	if (rc)
4676 		goto err_out_disable_device;
4677 
4678 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
4679 	if (rc)
4680 		rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
4681 	if (rc) {
4682 		dev_err(dev, "No usable DMA addressing method\n");
4683 		rc = -ENODEV;
4684 		goto err_out_regions;
4685 	}
4686 
4687 	shost = hisi_sas_shost_alloc_pci(pdev);
4688 	if (!shost) {
4689 		rc = -ENOMEM;
4690 		goto err_out_regions;
4691 	}
4692 
4693 	sha = SHOST_TO_SAS_HA(shost);
4694 	hisi_hba = shost_priv(shost);
4695 	dev_set_drvdata(dev, sha);
4696 
4697 	hisi_hba->regs = pcim_iomap(pdev, 5, 0);
4698 	if (!hisi_hba->regs) {
4699 		dev_err(dev, "cannot map register\n");
4700 		rc = -ENOMEM;
4701 		goto err_out_ha;
4702 	}
4703 
4704 	phy_nr = port_nr = hisi_hba->n_phy;
4705 
4706 	arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
4707 	arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
4708 	if (!arr_phy || !arr_port) {
4709 		rc = -ENOMEM;
4710 		goto err_out_ha;
4711 	}
4712 
4713 	sha->sas_phy = arr_phy;
4714 	sha->sas_port = arr_port;
4715 	sha->core.shost = shost;
4716 	sha->lldd_ha = hisi_hba;
4717 
4718 	shost->transportt = hisi_sas_stt;
4719 	shost->max_id = HISI_SAS_MAX_DEVICES;
4720 	shost->max_lun = ~0;
4721 	shost->max_channel = 1;
4722 	shost->max_cmd_len = 16;
4723 	shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
4724 	shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
4725 
4726 	sha->sas_ha_name = DRV_NAME;
4727 	sha->dev = dev;
4728 	sha->lldd_module = THIS_MODULE;
4729 	sha->sas_addr = &hisi_hba->sas_addr[0];
4730 	sha->num_phys = hisi_hba->n_phy;
4731 
4732 	for (i = 0; i < hisi_hba->n_phy; i++) {
4733 		sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
4734 		sha->sas_port[i] = &hisi_hba->port[i].sas_port;
4735 	}
4736 
4737 	if (hisi_hba->prot_mask) {
4738 		dev_info(dev, "Registering for DIF/DIX prot_mask=0x%x\n",
4739 			 prot_mask);
4740 		scsi_host_set_prot(hisi_hba->shost, prot_mask);
4741 		if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
4742 			scsi_host_set_guard(hisi_hba->shost,
4743 					    SHOST_DIX_GUARD_CRC);
4744 	}
4745 
4746 	if (hisi_sas_debugfs_enable)
4747 		debugfs_init_v3_hw(hisi_hba);
4748 
4749 	rc = interrupt_preinit_v3_hw(hisi_hba);
4750 	if (rc)
4751 		goto err_out_debugfs;
4752 	dev_err(dev, "%d hw queues\n", shost->nr_hw_queues);
4753 	rc = scsi_add_host(shost, dev);
4754 	if (rc)
4755 		goto err_out_free_irq_vectors;
4756 
4757 	rc = sas_register_ha(sha);
4758 	if (rc)
4759 		goto err_out_register_ha;
4760 
4761 	rc = hisi_sas_v3_init(hisi_hba);
4762 	if (rc)
4763 		goto err_out_register_ha;
4764 
4765 	scsi_scan_host(shost);
4766 
4767 	/*
4768 	 * For the situation that there are ATA disks connected with SAS
4769 	 * controller, it additionally creates ata_port which will affect the
4770 	 * child_count of hisi_hba->dev. Even if suspended all the disks,
4771 	 * ata_port is still and the child_count of hisi_hba->dev is not 0.
4772 	 * So use pm_suspend_ignore_children() to ignore the effect to
4773 	 * hisi_hba->dev.
4774 	 */
4775 	pm_suspend_ignore_children(dev, true);
4776 	pm_runtime_put_noidle(&pdev->dev);
4777 
4778 	return 0;
4779 
4780 err_out_register_ha:
4781 	scsi_remove_host(shost);
4782 err_out_free_irq_vectors:
4783 	pci_free_irq_vectors(pdev);
4784 err_out_debugfs:
4785 	debugfs_exit_v3_hw(hisi_hba);
4786 err_out_ha:
4787 	hisi_sas_free(hisi_hba);
4788 	scsi_host_put(shost);
4789 err_out_regions:
4790 	pci_release_regions(pdev);
4791 err_out_disable_device:
4792 	pci_disable_device(pdev);
4793 err_out:
4794 	return rc;
4795 }
4796 
4797 static void
4798 hisi_sas_v3_destroy_irqs(struct pci_dev *pdev, struct hisi_hba *hisi_hba)
4799 {
4800 	int i;
4801 
4802 	free_irq(pci_irq_vector(pdev, 1), hisi_hba);
4803 	free_irq(pci_irq_vector(pdev, 2), hisi_hba);
4804 	free_irq(pci_irq_vector(pdev, 11), hisi_hba);
4805 	for (i = 0; i < hisi_hba->cq_nvecs; i++) {
4806 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
4807 		int nr = hisi_sas_intr_conv ? 16 : 16 + i;
4808 
4809 		free_irq(pci_irq_vector(pdev, nr), cq);
4810 	}
4811 	pci_free_irq_vectors(pdev);
4812 }
4813 
4814 static void hisi_sas_v3_remove(struct pci_dev *pdev)
4815 {
4816 	struct device *dev = &pdev->dev;
4817 	struct sas_ha_struct *sha = dev_get_drvdata(dev);
4818 	struct hisi_hba *hisi_hba = sha->lldd_ha;
4819 	struct Scsi_Host *shost = sha->core.shost;
4820 
4821 	pm_runtime_get_noresume(dev);
4822 	if (timer_pending(&hisi_hba->timer))
4823 		del_timer(&hisi_hba->timer);
4824 
4825 	sas_unregister_ha(sha);
4826 	flush_workqueue(hisi_hba->wq);
4827 	sas_remove_host(sha->core.shost);
4828 
4829 	hisi_sas_v3_destroy_irqs(pdev, hisi_hba);
4830 	pci_release_regions(pdev);
4831 	pci_disable_device(pdev);
4832 	hisi_sas_free(hisi_hba);
4833 	debugfs_exit_v3_hw(hisi_hba);
4834 	scsi_host_put(shost);
4835 }
4836 
4837 static void hisi_sas_reset_prepare_v3_hw(struct pci_dev *pdev)
4838 {
4839 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
4840 	struct hisi_hba *hisi_hba = sha->lldd_ha;
4841 	struct device *dev = hisi_hba->dev;
4842 	int rc;
4843 
4844 	dev_info(dev, "FLR prepare\n");
4845 	set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
4846 	hisi_sas_controller_reset_prepare(hisi_hba);
4847 
4848 	rc = disable_host_v3_hw(hisi_hba);
4849 	if (rc)
4850 		dev_err(dev, "FLR: disable host failed rc=%d\n", rc);
4851 }
4852 
4853 static void hisi_sas_reset_done_v3_hw(struct pci_dev *pdev)
4854 {
4855 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
4856 	struct hisi_hba *hisi_hba = sha->lldd_ha;
4857 	struct device *dev = hisi_hba->dev;
4858 	int rc;
4859 
4860 	hisi_sas_init_mem(hisi_hba);
4861 
4862 	rc = hw_init_v3_hw(hisi_hba);
4863 	if (rc) {
4864 		dev_err(dev, "FLR: hw init failed rc=%d\n", rc);
4865 		return;
4866 	}
4867 
4868 	hisi_sas_controller_reset_done(hisi_hba);
4869 	dev_info(dev, "FLR done\n");
4870 }
4871 
4872 enum {
4873 	/* instances of the controller */
4874 	hip08,
4875 };
4876 
4877 static int _suspend_v3_hw(struct device *device)
4878 {
4879 	struct pci_dev *pdev = to_pci_dev(device);
4880 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
4881 	struct hisi_hba *hisi_hba = sha->lldd_ha;
4882 	struct device *dev = hisi_hba->dev;
4883 	struct Scsi_Host *shost = hisi_hba->shost;
4884 	int rc;
4885 
4886 	if (!pdev->pm_cap) {
4887 		dev_err(dev, "PCI PM not supported\n");
4888 		return -ENODEV;
4889 	}
4890 
4891 	if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
4892 		return -1;
4893 
4894 	scsi_block_requests(shost);
4895 	set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
4896 	flush_workqueue(hisi_hba->wq);
4897 
4898 	rc = disable_host_v3_hw(hisi_hba);
4899 	if (rc) {
4900 		dev_err(dev, "PM suspend: disable host failed rc=%d\n", rc);
4901 		clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
4902 		clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
4903 		scsi_unblock_requests(shost);
4904 		return rc;
4905 	}
4906 
4907 	hisi_sas_init_mem(hisi_hba);
4908 
4909 	dev_warn(dev, "entering suspend state\n");
4910 
4911 	hisi_sas_release_tasks(hisi_hba);
4912 
4913 	sas_suspend_ha(sha);
4914 	return 0;
4915 }
4916 
4917 static int _resume_v3_hw(struct device *device)
4918 {
4919 	struct pci_dev *pdev = to_pci_dev(device);
4920 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
4921 	struct hisi_hba *hisi_hba = sha->lldd_ha;
4922 	struct Scsi_Host *shost = hisi_hba->shost;
4923 	struct device *dev = hisi_hba->dev;
4924 	unsigned int rc;
4925 	pci_power_t device_state = pdev->current_state;
4926 
4927 	dev_warn(dev, "resuming from operating state [D%d]\n",
4928 		 device_state);
4929 
4930 	scsi_unblock_requests(shost);
4931 	clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
4932 
4933 	sas_prep_resume_ha(sha);
4934 	rc = hw_init_v3_hw(hisi_hba);
4935 	if (rc) {
4936 		scsi_remove_host(shost);
4937 		return rc;
4938 	}
4939 	phys_init_v3_hw(hisi_hba);
4940 	sas_resume_ha(sha);
4941 	clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
4942 
4943 	return 0;
4944 }
4945 
4946 static int __maybe_unused suspend_v3_hw(struct device *device)
4947 {
4948 	struct pci_dev *pdev = to_pci_dev(device);
4949 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
4950 	struct hisi_hba *hisi_hba = sha->lldd_ha;
4951 	int rc;
4952 
4953 	set_bit(HISI_SAS_PM_BIT, &hisi_hba->flags);
4954 
4955 	rc = _suspend_v3_hw(device);
4956 	if (rc)
4957 		clear_bit(HISI_SAS_PM_BIT, &hisi_hba->flags);
4958 
4959 	return rc;
4960 }
4961 
4962 static int __maybe_unused resume_v3_hw(struct device *device)
4963 {
4964 	struct pci_dev *pdev = to_pci_dev(device);
4965 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
4966 	struct hisi_hba *hisi_hba = sha->lldd_ha;
4967 	int rc = _resume_v3_hw(device);
4968 
4969 	clear_bit(HISI_SAS_PM_BIT, &hisi_hba->flags);
4970 
4971 	return rc;
4972 }
4973 
4974 static const struct pci_device_id sas_v3_pci_table[] = {
4975 	{ PCI_VDEVICE(HUAWEI, 0xa230), hip08 },
4976 	{}
4977 };
4978 MODULE_DEVICE_TABLE(pci, sas_v3_pci_table);
4979 
4980 static const struct pci_error_handlers hisi_sas_err_handler = {
4981 	.reset_prepare	= hisi_sas_reset_prepare_v3_hw,
4982 	.reset_done	= hisi_sas_reset_done_v3_hw,
4983 };
4984 
4985 static UNIVERSAL_DEV_PM_OPS(hisi_sas_v3_pm_ops,
4986 			    suspend_v3_hw,
4987 			    resume_v3_hw,
4988 			    NULL);
4989 
4990 static struct pci_driver sas_v3_pci_driver = {
4991 	.name		= DRV_NAME,
4992 	.id_table	= sas_v3_pci_table,
4993 	.probe		= hisi_sas_v3_probe,
4994 	.remove		= hisi_sas_v3_remove,
4995 	.err_handler	= &hisi_sas_err_handler,
4996 	.driver.pm	= &hisi_sas_v3_pm_ops,
4997 };
4998 
4999 module_pci_driver(sas_v3_pci_driver);
5000 module_param_named(intr_conv, hisi_sas_intr_conv, bool, 0444);
5001 
5002 MODULE_LICENSE("GPL");
5003 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
5004 MODULE_DESCRIPTION("HISILICON SAS controller v3 hw driver based on pci device");
5005 MODULE_ALIAS("pci:" DRV_NAME);
5006