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