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