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