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