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