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