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