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