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