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