1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Hisilicon Limited.
4  */
5 
6 #include "hisi_sas.h"
7 #define DRV_NAME "hisi_sas_v3_hw"
8 
9 /* global registers need init */
10 #define DLVRY_QUEUE_ENABLE		0x0
11 #define IOST_BASE_ADDR_LO		0x8
12 #define IOST_BASE_ADDR_HI		0xc
13 #define ITCT_BASE_ADDR_LO		0x10
14 #define ITCT_BASE_ADDR_HI		0x14
15 #define IO_BROKEN_MSG_ADDR_LO		0x18
16 #define IO_BROKEN_MSG_ADDR_HI		0x1c
17 #define PHY_CONTEXT			0x20
18 #define PHY_STATE			0x24
19 #define PHY_PORT_NUM_MA			0x28
20 #define PHY_CONN_RATE			0x30
21 #define ITCT_CLR			0x44
22 #define ITCT_CLR_EN_OFF			16
23 #define ITCT_CLR_EN_MSK			(0x1 << ITCT_CLR_EN_OFF)
24 #define ITCT_DEV_OFF			0
25 #define ITCT_DEV_MSK			(0x7ff << ITCT_DEV_OFF)
26 #define SAS_AXI_USER3			0x50
27 #define IO_SATA_BROKEN_MSG_ADDR_LO	0x58
28 #define IO_SATA_BROKEN_MSG_ADDR_HI	0x5c
29 #define SATA_INITI_D2H_STORE_ADDR_LO	0x60
30 #define SATA_INITI_D2H_STORE_ADDR_HI	0x64
31 #define CFG_MAX_TAG			0x68
32 #define HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL	0x84
33 #define HGC_SAS_TXFAIL_RETRY_CTRL	0x88
34 #define HGC_GET_ITV_TIME		0x90
35 #define DEVICE_MSG_WORK_MODE		0x94
36 #define OPENA_WT_CONTI_TIME		0x9c
37 #define I_T_NEXUS_LOSS_TIME		0xa0
38 #define MAX_CON_TIME_LIMIT_TIME		0xa4
39 #define BUS_INACTIVE_LIMIT_TIME		0xa8
40 #define REJECT_TO_OPEN_LIMIT_TIME	0xac
41 #define CQ_INT_CONVERGE_EN		0xb0
42 #define CFG_AGING_TIME			0xbc
43 #define HGC_DFX_CFG2			0xc0
44 #define CFG_ABT_SET_QUERY_IPTT	0xd4
45 #define CFG_SET_ABORTED_IPTT_OFF	0
46 #define CFG_SET_ABORTED_IPTT_MSK	(0xfff << CFG_SET_ABORTED_IPTT_OFF)
47 #define CFG_SET_ABORTED_EN_OFF	12
48 #define CFG_ABT_SET_IPTT_DONE	0xd8
49 #define CFG_ABT_SET_IPTT_DONE_OFF	0
50 #define HGC_IOMB_PROC1_STATUS	0x104
51 #define HGC_LM_DFX_STATUS2		0x128
52 #define HGC_LM_DFX_STATUS2_IOSTLIST_OFF		0
53 #define HGC_LM_DFX_STATUS2_IOSTLIST_MSK	(0xfff << \
54 					 HGC_LM_DFX_STATUS2_IOSTLIST_OFF)
55 #define HGC_LM_DFX_STATUS2_ITCTLIST_OFF		12
56 #define HGC_LM_DFX_STATUS2_ITCTLIST_MSK	(0x7ff << \
57 					 HGC_LM_DFX_STATUS2_ITCTLIST_OFF)
58 #define HGC_CQE_ECC_ADDR		0x13c
59 #define HGC_CQE_ECC_1B_ADDR_OFF	0
60 #define HGC_CQE_ECC_1B_ADDR_MSK	(0x3f << HGC_CQE_ECC_1B_ADDR_OFF)
61 #define HGC_CQE_ECC_MB_ADDR_OFF	8
62 #define HGC_CQE_ECC_MB_ADDR_MSK (0x3f << HGC_CQE_ECC_MB_ADDR_OFF)
63 #define HGC_IOST_ECC_ADDR		0x140
64 #define HGC_IOST_ECC_1B_ADDR_OFF	0
65 #define HGC_IOST_ECC_1B_ADDR_MSK	(0x3ff << HGC_IOST_ECC_1B_ADDR_OFF)
66 #define HGC_IOST_ECC_MB_ADDR_OFF	16
67 #define HGC_IOST_ECC_MB_ADDR_MSK	(0x3ff << HGC_IOST_ECC_MB_ADDR_OFF)
68 #define HGC_DQE_ECC_ADDR		0x144
69 #define HGC_DQE_ECC_1B_ADDR_OFF	0
70 #define HGC_DQE_ECC_1B_ADDR_MSK	(0xfff << HGC_DQE_ECC_1B_ADDR_OFF)
71 #define HGC_DQE_ECC_MB_ADDR_OFF	16
72 #define HGC_DQE_ECC_MB_ADDR_MSK (0xfff << HGC_DQE_ECC_MB_ADDR_OFF)
73 #define CHNL_INT_STATUS			0x148
74 #define TAB_DFX				0x14c
75 #define HGC_ITCT_ECC_ADDR		0x150
76 #define HGC_ITCT_ECC_1B_ADDR_OFF		0
77 #define HGC_ITCT_ECC_1B_ADDR_MSK		(0x3ff << \
78 						 HGC_ITCT_ECC_1B_ADDR_OFF)
79 #define HGC_ITCT_ECC_MB_ADDR_OFF		16
80 #define HGC_ITCT_ECC_MB_ADDR_MSK		(0x3ff << \
81 						 HGC_ITCT_ECC_MB_ADDR_OFF)
82 #define HGC_AXI_FIFO_ERR_INFO  0x154
83 #define AXI_ERR_INFO_OFF               0
84 #define AXI_ERR_INFO_MSK               (0xff << AXI_ERR_INFO_OFF)
85 #define FIFO_ERR_INFO_OFF              8
86 #define FIFO_ERR_INFO_MSK              (0xff << FIFO_ERR_INFO_OFF)
87 #define TAB_RD_TYPE			0x15c
88 #define INT_COAL_EN			0x19c
89 #define OQ_INT_COAL_TIME		0x1a0
90 #define OQ_INT_COAL_CNT			0x1a4
91 #define ENT_INT_COAL_TIME		0x1a8
92 #define ENT_INT_COAL_CNT		0x1ac
93 #define OQ_INT_SRC			0x1b0
94 #define OQ_INT_SRC_MSK			0x1b4
95 #define ENT_INT_SRC1			0x1b8
96 #define ENT_INT_SRC1_D2H_FIS_CH0_OFF	0
97 #define ENT_INT_SRC1_D2H_FIS_CH0_MSK	(0x1 << ENT_INT_SRC1_D2H_FIS_CH0_OFF)
98 #define ENT_INT_SRC1_D2H_FIS_CH1_OFF	8
99 #define ENT_INT_SRC1_D2H_FIS_CH1_MSK	(0x1 << ENT_INT_SRC1_D2H_FIS_CH1_OFF)
100 #define ENT_INT_SRC2			0x1bc
101 #define ENT_INT_SRC3			0x1c0
102 #define ENT_INT_SRC3_WP_DEPTH_OFF		8
103 #define ENT_INT_SRC3_IPTT_SLOT_NOMATCH_OFF	9
104 #define ENT_INT_SRC3_RP_DEPTH_OFF		10
105 #define ENT_INT_SRC3_AXI_OFF			11
106 #define ENT_INT_SRC3_FIFO_OFF			12
107 #define ENT_INT_SRC3_LM_OFF				14
108 #define ENT_INT_SRC3_ITC_INT_OFF	15
109 #define ENT_INT_SRC3_ITC_INT_MSK	(0x1 << ENT_INT_SRC3_ITC_INT_OFF)
110 #define ENT_INT_SRC3_ABT_OFF		16
111 #define ENT_INT_SRC3_DQE_POISON_OFF	18
112 #define ENT_INT_SRC3_IOST_POISON_OFF	19
113 #define ENT_INT_SRC3_ITCT_POISON_OFF	20
114 #define ENT_INT_SRC3_ITCT_NCQ_POISON_OFF	21
115 #define ENT_INT_SRC_MSK1		0x1c4
116 #define ENT_INT_SRC_MSK2		0x1c8
117 #define ENT_INT_SRC_MSK3		0x1cc
118 #define ENT_INT_SRC_MSK3_ENT95_MSK_OFF	31
119 #define CHNL_PHYUPDOWN_INT_MSK		0x1d0
120 #define CHNL_ENT_INT_MSK			0x1d4
121 #define HGC_COM_INT_MSK				0x1d8
122 #define ENT_INT_SRC_MSK3_ENT95_MSK_MSK	(0x1 << ENT_INT_SRC_MSK3_ENT95_MSK_OFF)
123 #define SAS_ECC_INTR			0x1e8
124 #define SAS_ECC_INTR_DQE_ECC_1B_OFF		0
125 #define SAS_ECC_INTR_DQE_ECC_MB_OFF		1
126 #define SAS_ECC_INTR_IOST_ECC_1B_OFF	2
127 #define SAS_ECC_INTR_IOST_ECC_MB_OFF	3
128 #define SAS_ECC_INTR_ITCT_ECC_1B_OFF	4
129 #define SAS_ECC_INTR_ITCT_ECC_MB_OFF	5
130 #define SAS_ECC_INTR_ITCTLIST_ECC_1B_OFF	6
131 #define SAS_ECC_INTR_ITCTLIST_ECC_MB_OFF	7
132 #define SAS_ECC_INTR_IOSTLIST_ECC_1B_OFF	8
133 #define SAS_ECC_INTR_IOSTLIST_ECC_MB_OFF	9
134 #define SAS_ECC_INTR_CQE_ECC_1B_OFF		10
135 #define SAS_ECC_INTR_CQE_ECC_MB_OFF		11
136 #define SAS_ECC_INTR_NCQ_MEM0_ECC_1B_OFF	12
137 #define SAS_ECC_INTR_NCQ_MEM0_ECC_MB_OFF	13
138 #define SAS_ECC_INTR_NCQ_MEM1_ECC_1B_OFF	14
139 #define SAS_ECC_INTR_NCQ_MEM1_ECC_MB_OFF	15
140 #define SAS_ECC_INTR_NCQ_MEM2_ECC_1B_OFF	16
141 #define SAS_ECC_INTR_NCQ_MEM2_ECC_MB_OFF	17
142 #define SAS_ECC_INTR_NCQ_MEM3_ECC_1B_OFF	18
143 #define SAS_ECC_INTR_NCQ_MEM3_ECC_MB_OFF	19
144 #define SAS_ECC_INTR_OOO_RAM_ECC_1B_OFF		20
145 #define SAS_ECC_INTR_OOO_RAM_ECC_MB_OFF		21
146 #define SAS_ECC_INTR_MSK		0x1ec
147 #define HGC_ERR_STAT_EN			0x238
148 #define CQE_SEND_CNT			0x248
149 #define DLVRY_Q_0_BASE_ADDR_LO		0x260
150 #define DLVRY_Q_0_BASE_ADDR_HI		0x264
151 #define DLVRY_Q_0_DEPTH			0x268
152 #define DLVRY_Q_0_WR_PTR		0x26c
153 #define DLVRY_Q_0_RD_PTR		0x270
154 #define HYPER_STREAM_ID_EN_CFG		0xc80
155 #define OQ0_INT_SRC_MSK			0xc90
156 #define COMPL_Q_0_BASE_ADDR_LO		0x4e0
157 #define COMPL_Q_0_BASE_ADDR_HI		0x4e4
158 #define COMPL_Q_0_DEPTH			0x4e8
159 #define COMPL_Q_0_WR_PTR		0x4ec
160 #define COMPL_Q_0_RD_PTR		0x4f0
161 #define HGC_RXM_DFX_STATUS14		0xae8
162 #define HGC_RXM_DFX_STATUS14_MEM0_OFF	0
163 #define HGC_RXM_DFX_STATUS14_MEM0_MSK	(0x1ff << \
164 					 HGC_RXM_DFX_STATUS14_MEM0_OFF)
165 #define HGC_RXM_DFX_STATUS14_MEM1_OFF	9
166 #define HGC_RXM_DFX_STATUS14_MEM1_MSK	(0x1ff << \
167 					 HGC_RXM_DFX_STATUS14_MEM1_OFF)
168 #define HGC_RXM_DFX_STATUS14_MEM2_OFF	18
169 #define HGC_RXM_DFX_STATUS14_MEM2_MSK	(0x1ff << \
170 					 HGC_RXM_DFX_STATUS14_MEM2_OFF)
171 #define HGC_RXM_DFX_STATUS15		0xaec
172 #define HGC_RXM_DFX_STATUS15_MEM3_OFF	0
173 #define HGC_RXM_DFX_STATUS15_MEM3_MSK	(0x1ff << \
174 					 HGC_RXM_DFX_STATUS15_MEM3_OFF)
175 #define AWQOS_AWCACHE_CFG	0xc84
176 #define ARQOS_ARCACHE_CFG	0xc88
177 #define HILINK_ERR_DFX		0xe04
178 #define SAS_GPIO_CFG_0		0x1000
179 #define SAS_GPIO_CFG_1		0x1004
180 #define SAS_GPIO_TX_0_1	0x1040
181 #define SAS_CFG_DRIVE_VLD	0x1070
182 
183 /* phy registers requiring init */
184 #define PORT_BASE			(0x2000)
185 #define PHY_CFG				(PORT_BASE + 0x0)
186 #define HARD_PHY_LINKRATE		(PORT_BASE + 0x4)
187 #define PHY_CFG_ENA_OFF			0
188 #define PHY_CFG_ENA_MSK			(0x1 << PHY_CFG_ENA_OFF)
189 #define PHY_CFG_DC_OPT_OFF		2
190 #define PHY_CFG_DC_OPT_MSK		(0x1 << PHY_CFG_DC_OPT_OFF)
191 #define PHY_CFG_PHY_RST_OFF		3
192 #define PHY_CFG_PHY_RST_MSK		(0x1 << PHY_CFG_PHY_RST_OFF)
193 #define PROG_PHY_LINK_RATE		(PORT_BASE + 0x8)
194 #define CFG_PROG_PHY_LINK_RATE_OFF	8
195 #define CFG_PROG_PHY_LINK_RATE_MSK	(0xf << CFG_PROG_PHY_LINK_RATE_OFF)
196 #define PHY_CTRL			(PORT_BASE + 0x14)
197 #define PHY_CTRL_RESET_OFF		0
198 #define PHY_CTRL_RESET_MSK		(0x1 << PHY_CTRL_RESET_OFF)
199 #define CMD_HDR_PIR_OFF			8
200 #define CMD_HDR_PIR_MSK			(0x1 << CMD_HDR_PIR_OFF)
201 #define SERDES_CFG			(PORT_BASE + 0x1c)
202 #define CFG_ALOS_CHK_DISABLE_OFF	9
203 #define CFG_ALOS_CHK_DISABLE_MSK	(0x1 << CFG_ALOS_CHK_DISABLE_OFF)
204 #define SAS_PHY_BIST_CTRL		(PORT_BASE + 0x2c)
205 #define CFG_BIST_MODE_SEL_OFF		0
206 #define CFG_BIST_MODE_SEL_MSK		(0xf << CFG_BIST_MODE_SEL_OFF)
207 #define CFG_LOOP_TEST_MODE_OFF		14
208 #define CFG_LOOP_TEST_MODE_MSK		(0x3 << CFG_LOOP_TEST_MODE_OFF)
209 #define CFG_RX_BIST_EN_OFF		16
210 #define CFG_RX_BIST_EN_MSK		(0x1 << CFG_RX_BIST_EN_OFF)
211 #define CFG_TX_BIST_EN_OFF		17
212 #define CFG_TX_BIST_EN_MSK		(0x1 << CFG_TX_BIST_EN_OFF)
213 #define CFG_BIST_TEST_OFF		18
214 #define CFG_BIST_TEST_MSK		(0x1 << CFG_BIST_TEST_OFF)
215 #define SAS_PHY_BIST_CODE		(PORT_BASE + 0x30)
216 #define SAS_PHY_BIST_CODE1		(PORT_BASE + 0x34)
217 #define SAS_BIST_ERR_CNT		(PORT_BASE + 0x38)
218 #define SL_CFG				(PORT_BASE + 0x84)
219 #define AIP_LIMIT			(PORT_BASE + 0x90)
220 #define SL_CONTROL			(PORT_BASE + 0x94)
221 #define SL_CONTROL_NOTIFY_EN_OFF	0
222 #define SL_CONTROL_NOTIFY_EN_MSK	(0x1 << SL_CONTROL_NOTIFY_EN_OFF)
223 #define SL_CTA_OFF		17
224 #define SL_CTA_MSK		(0x1 << SL_CTA_OFF)
225 #define RX_PRIMS_STATUS			(PORT_BASE + 0x98)
226 #define RX_BCAST_CHG_OFF		1
227 #define RX_BCAST_CHG_MSK		(0x1 << RX_BCAST_CHG_OFF)
228 #define TX_ID_DWORD0			(PORT_BASE + 0x9c)
229 #define TX_ID_DWORD1			(PORT_BASE + 0xa0)
230 #define TX_ID_DWORD2			(PORT_BASE + 0xa4)
231 #define TX_ID_DWORD3			(PORT_BASE + 0xa8)
232 #define TX_ID_DWORD4			(PORT_BASE + 0xaC)
233 #define TX_ID_DWORD5			(PORT_BASE + 0xb0)
234 #define TX_ID_DWORD6			(PORT_BASE + 0xb4)
235 #define TXID_AUTO				(PORT_BASE + 0xb8)
236 #define CT3_OFF		1
237 #define CT3_MSK		(0x1 << CT3_OFF)
238 #define TX_HARDRST_OFF          2
239 #define TX_HARDRST_MSK          (0x1 << TX_HARDRST_OFF)
240 #define RX_IDAF_DWORD0			(PORT_BASE + 0xc4)
241 #define RXOP_CHECK_CFG_H		(PORT_BASE + 0xfc)
242 #define STP_LINK_TIMER			(PORT_BASE + 0x120)
243 #define STP_LINK_TIMEOUT_STATE		(PORT_BASE + 0x124)
244 #define CON_CFG_DRIVER			(PORT_BASE + 0x130)
245 #define SAS_SSP_CON_TIMER_CFG		(PORT_BASE + 0x134)
246 #define SAS_SMP_CON_TIMER_CFG		(PORT_BASE + 0x138)
247 #define SAS_STP_CON_TIMER_CFG		(PORT_BASE + 0x13c)
248 #define CHL_INT0			(PORT_BASE + 0x1b4)
249 #define CHL_INT0_HOTPLUG_TOUT_OFF	0
250 #define CHL_INT0_HOTPLUG_TOUT_MSK	(0x1 << CHL_INT0_HOTPLUG_TOUT_OFF)
251 #define CHL_INT0_SL_RX_BCST_ACK_OFF	1
252 #define CHL_INT0_SL_RX_BCST_ACK_MSK	(0x1 << CHL_INT0_SL_RX_BCST_ACK_OFF)
253 #define CHL_INT0_SL_PHY_ENABLE_OFF	2
254 #define CHL_INT0_SL_PHY_ENABLE_MSK	(0x1 << CHL_INT0_SL_PHY_ENABLE_OFF)
255 #define CHL_INT0_NOT_RDY_OFF		4
256 #define CHL_INT0_NOT_RDY_MSK		(0x1 << CHL_INT0_NOT_RDY_OFF)
257 #define CHL_INT0_PHY_RDY_OFF		5
258 #define CHL_INT0_PHY_RDY_MSK		(0x1 << CHL_INT0_PHY_RDY_OFF)
259 #define CHL_INT1			(PORT_BASE + 0x1b8)
260 #define CHL_INT1_DMAC_TX_ECC_MB_ERR_OFF	15
261 #define CHL_INT1_DMAC_TX_ECC_1B_ERR_OFF	16
262 #define CHL_INT1_DMAC_RX_ECC_MB_ERR_OFF	17
263 #define CHL_INT1_DMAC_RX_ECC_1B_ERR_OFF	18
264 #define CHL_INT1_DMAC_TX_AXI_WR_ERR_OFF	19
265 #define CHL_INT1_DMAC_TX_AXI_RD_ERR_OFF	20
266 #define CHL_INT1_DMAC_RX_AXI_WR_ERR_OFF	21
267 #define CHL_INT1_DMAC_RX_AXI_RD_ERR_OFF	22
268 #define CHL_INT1_DMAC_TX_FIFO_ERR_OFF	23
269 #define CHL_INT1_DMAC_RX_FIFO_ERR_OFF	24
270 #define CHL_INT1_DMAC_TX_AXI_RUSER_ERR_OFF	26
271 #define CHL_INT1_DMAC_RX_AXI_RUSER_ERR_OFF	27
272 #define CHL_INT2			(PORT_BASE + 0x1bc)
273 #define CHL_INT2_SL_IDAF_TOUT_CONF_OFF	0
274 #define CHL_INT2_RX_DISP_ERR_OFF	28
275 #define CHL_INT2_RX_CODE_ERR_OFF	29
276 #define CHL_INT2_RX_INVLD_DW_OFF	30
277 #define CHL_INT2_STP_LINK_TIMEOUT_OFF	31
278 #define CHL_INT0_MSK			(PORT_BASE + 0x1c0)
279 #define CHL_INT1_MSK			(PORT_BASE + 0x1c4)
280 #define CHL_INT2_MSK			(PORT_BASE + 0x1c8)
281 #define SAS_EC_INT_COAL_TIME		(PORT_BASE + 0x1cc)
282 #define CHL_INT_COAL_EN			(PORT_BASE + 0x1d0)
283 #define SAS_RX_TRAIN_TIMER		(PORT_BASE + 0x2a4)
284 #define PHY_CTRL_RDY_MSK		(PORT_BASE + 0x2b0)
285 #define PHYCTRL_NOT_RDY_MSK		(PORT_BASE + 0x2b4)
286 #define PHYCTRL_DWS_RESET_MSK		(PORT_BASE + 0x2b8)
287 #define PHYCTRL_PHY_ENA_MSK		(PORT_BASE + 0x2bc)
288 #define SL_RX_BCAST_CHK_MSK		(PORT_BASE + 0x2c0)
289 #define PHYCTRL_OOB_RESTART_MSK		(PORT_BASE + 0x2c4)
290 #define DMA_TX_STATUS			(PORT_BASE + 0x2d0)
291 #define DMA_TX_STATUS_BUSY_OFF		0
292 #define DMA_TX_STATUS_BUSY_MSK		(0x1 << DMA_TX_STATUS_BUSY_OFF)
293 #define DMA_RX_STATUS			(PORT_BASE + 0x2e8)
294 #define DMA_RX_STATUS_BUSY_OFF		0
295 #define DMA_RX_STATUS_BUSY_MSK		(0x1 << DMA_RX_STATUS_BUSY_OFF)
296 
297 #define COARSETUNE_TIME			(PORT_BASE + 0x304)
298 #define ERR_CNT_DWS_LOST		(PORT_BASE + 0x380)
299 #define ERR_CNT_RESET_PROB		(PORT_BASE + 0x384)
300 #define ERR_CNT_INVLD_DW		(PORT_BASE + 0x390)
301 #define ERR_CNT_CODE_ERR		(PORT_BASE + 0x394)
302 #define ERR_CNT_DISP_ERR		(PORT_BASE + 0x398)
303 
304 #define DEFAULT_ITCT_HW		2048 /* reset value, not reprogrammed */
305 #if (HISI_SAS_MAX_DEVICES > DEFAULT_ITCT_HW)
306 #error Max ITCT exceeded
307 #endif
308 
309 #define AXI_MASTER_CFG_BASE		(0x5000)
310 #define AM_CTRL_GLOBAL			(0x0)
311 #define AM_CTRL_SHUTDOWN_REQ_OFF	0
312 #define AM_CTRL_SHUTDOWN_REQ_MSK	(0x1 << AM_CTRL_SHUTDOWN_REQ_OFF)
313 #define AM_CURR_TRANS_RETURN	(0x150)
314 
315 #define AM_CFG_MAX_TRANS		(0x5010)
316 #define AM_CFG_SINGLE_PORT_MAX_TRANS	(0x5014)
317 #define AXI_CFG					(0x5100)
318 #define AM_ROB_ECC_ERR_ADDR		(0x510c)
319 #define AM_ROB_ECC_ERR_ADDR_OFF	0
320 #define AM_ROB_ECC_ERR_ADDR_MSK	0xffffffff
321 
322 /* RAS registers need init */
323 #define RAS_BASE		(0x6000)
324 #define SAS_RAS_INTR0			(RAS_BASE)
325 #define SAS_RAS_INTR1			(RAS_BASE + 0x04)
326 #define SAS_RAS_INTR0_MASK		(RAS_BASE + 0x08)
327 #define SAS_RAS_INTR1_MASK		(RAS_BASE + 0x0c)
328 #define CFG_SAS_RAS_INTR_MASK		(RAS_BASE + 0x1c)
329 #define SAS_RAS_INTR2			(RAS_BASE + 0x20)
330 #define SAS_RAS_INTR2_MASK		(RAS_BASE + 0x24)
331 
332 /* HW dma structures */
333 /* Delivery queue header */
334 /* dw0 */
335 #define CMD_HDR_ABORT_FLAG_OFF		0
336 #define CMD_HDR_ABORT_FLAG_MSK		(0x3 << CMD_HDR_ABORT_FLAG_OFF)
337 #define CMD_HDR_ABORT_DEVICE_TYPE_OFF	2
338 #define CMD_HDR_ABORT_DEVICE_TYPE_MSK	(0x1 << CMD_HDR_ABORT_DEVICE_TYPE_OFF)
339 #define CMD_HDR_RESP_REPORT_OFF		5
340 #define CMD_HDR_RESP_REPORT_MSK		(0x1 << CMD_HDR_RESP_REPORT_OFF)
341 #define CMD_HDR_TLR_CTRL_OFF		6
342 #define CMD_HDR_TLR_CTRL_MSK		(0x3 << CMD_HDR_TLR_CTRL_OFF)
343 #define CMD_HDR_PORT_OFF		18
344 #define CMD_HDR_PORT_MSK		(0xf << CMD_HDR_PORT_OFF)
345 #define CMD_HDR_PRIORITY_OFF		27
346 #define CMD_HDR_PRIORITY_MSK		(0x1 << CMD_HDR_PRIORITY_OFF)
347 #define CMD_HDR_CMD_OFF			29
348 #define CMD_HDR_CMD_MSK			(0x7 << CMD_HDR_CMD_OFF)
349 /* dw1 */
350 #define CMD_HDR_UNCON_CMD_OFF	3
351 #define CMD_HDR_DIR_OFF			5
352 #define CMD_HDR_DIR_MSK			(0x3 << CMD_HDR_DIR_OFF)
353 #define CMD_HDR_RESET_OFF		7
354 #define CMD_HDR_RESET_MSK		(0x1 << CMD_HDR_RESET_OFF)
355 #define CMD_HDR_VDTL_OFF		10
356 #define CMD_HDR_VDTL_MSK		(0x1 << CMD_HDR_VDTL_OFF)
357 #define CMD_HDR_FRAME_TYPE_OFF		11
358 #define CMD_HDR_FRAME_TYPE_MSK		(0x1f << CMD_HDR_FRAME_TYPE_OFF)
359 #define CMD_HDR_DEV_ID_OFF		16
360 #define CMD_HDR_DEV_ID_MSK		(0xffff << CMD_HDR_DEV_ID_OFF)
361 /* dw2 */
362 #define CMD_HDR_CFL_OFF			0
363 #define CMD_HDR_CFL_MSK			(0x1ff << CMD_HDR_CFL_OFF)
364 #define CMD_HDR_NCQ_TAG_OFF		10
365 #define CMD_HDR_NCQ_TAG_MSK		(0x1f << CMD_HDR_NCQ_TAG_OFF)
366 #define CMD_HDR_MRFL_OFF		15
367 #define CMD_HDR_MRFL_MSK		(0x1ff << CMD_HDR_MRFL_OFF)
368 #define CMD_HDR_SG_MOD_OFF		24
369 #define CMD_HDR_SG_MOD_MSK		(0x3 << CMD_HDR_SG_MOD_OFF)
370 /* dw3 */
371 #define CMD_HDR_IPTT_OFF		0
372 #define CMD_HDR_IPTT_MSK		(0xffff << CMD_HDR_IPTT_OFF)
373 /* dw6 */
374 #define CMD_HDR_DIF_SGL_LEN_OFF		0
375 #define CMD_HDR_DIF_SGL_LEN_MSK		(0xffff << CMD_HDR_DIF_SGL_LEN_OFF)
376 #define CMD_HDR_DATA_SGL_LEN_OFF	16
377 #define CMD_HDR_DATA_SGL_LEN_MSK	(0xffff << CMD_HDR_DATA_SGL_LEN_OFF)
378 /* dw7 */
379 #define CMD_HDR_ADDR_MODE_SEL_OFF		15
380 #define CMD_HDR_ADDR_MODE_SEL_MSK		(1 << CMD_HDR_ADDR_MODE_SEL_OFF)
381 #define CMD_HDR_ABORT_IPTT_OFF		16
382 #define CMD_HDR_ABORT_IPTT_MSK		(0xffff << CMD_HDR_ABORT_IPTT_OFF)
383 
384 /* Completion header */
385 /* dw0 */
386 #define CMPLT_HDR_CMPLT_OFF		0
387 #define CMPLT_HDR_CMPLT_MSK		(0x3 << CMPLT_HDR_CMPLT_OFF)
388 #define CMPLT_HDR_ERROR_PHASE_OFF   2
389 #define CMPLT_HDR_ERROR_PHASE_MSK   (0xff << CMPLT_HDR_ERROR_PHASE_OFF)
390 #define CMPLT_HDR_RSPNS_XFRD_OFF	10
391 #define CMPLT_HDR_RSPNS_XFRD_MSK	(0x1 << CMPLT_HDR_RSPNS_XFRD_OFF)
392 #define CMPLT_HDR_ERX_OFF		12
393 #define CMPLT_HDR_ERX_MSK		(0x1 << CMPLT_HDR_ERX_OFF)
394 #define CMPLT_HDR_ABORT_STAT_OFF	13
395 #define CMPLT_HDR_ABORT_STAT_MSK	(0x7 << CMPLT_HDR_ABORT_STAT_OFF)
396 /* abort_stat */
397 #define STAT_IO_NOT_VALID		0x1
398 #define STAT_IO_NO_DEVICE		0x2
399 #define STAT_IO_COMPLETE		0x3
400 #define STAT_IO_ABORTED			0x4
401 /* dw1 */
402 #define CMPLT_HDR_IPTT_OFF		0
403 #define CMPLT_HDR_IPTT_MSK		(0xffff << CMPLT_HDR_IPTT_OFF)
404 #define CMPLT_HDR_DEV_ID_OFF		16
405 #define CMPLT_HDR_DEV_ID_MSK		(0xffff << CMPLT_HDR_DEV_ID_OFF)
406 /* dw3 */
407 #define CMPLT_HDR_IO_IN_TARGET_OFF	17
408 #define CMPLT_HDR_IO_IN_TARGET_MSK	(0x1 << CMPLT_HDR_IO_IN_TARGET_OFF)
409 
410 /* ITCT header */
411 /* qw0 */
412 #define ITCT_HDR_DEV_TYPE_OFF		0
413 #define ITCT_HDR_DEV_TYPE_MSK		(0x3 << ITCT_HDR_DEV_TYPE_OFF)
414 #define ITCT_HDR_VALID_OFF		2
415 #define ITCT_HDR_VALID_MSK		(0x1 << ITCT_HDR_VALID_OFF)
416 #define ITCT_HDR_MCR_OFF		5
417 #define ITCT_HDR_MCR_MSK		(0xf << ITCT_HDR_MCR_OFF)
418 #define ITCT_HDR_VLN_OFF		9
419 #define ITCT_HDR_VLN_MSK		(0xf << ITCT_HDR_VLN_OFF)
420 #define ITCT_HDR_SMP_TIMEOUT_OFF	16
421 #define ITCT_HDR_AWT_CONTINUE_OFF	25
422 #define ITCT_HDR_PORT_ID_OFF		28
423 #define ITCT_HDR_PORT_ID_MSK		(0xf << ITCT_HDR_PORT_ID_OFF)
424 /* qw2 */
425 #define ITCT_HDR_INLT_OFF		0
426 #define ITCT_HDR_INLT_MSK		(0xffffULL << ITCT_HDR_INLT_OFF)
427 #define ITCT_HDR_RTOLT_OFF		48
428 #define ITCT_HDR_RTOLT_MSK		(0xffffULL << ITCT_HDR_RTOLT_OFF)
429 
430 struct hisi_sas_protect_iu_v3_hw {
431 	u32 dw0;
432 	u32 lbrtcv;
433 	u32 lbrtgv;
434 	u32 dw3;
435 	u32 dw4;
436 	u32 dw5;
437 	u32 rsv;
438 };
439 
440 struct hisi_sas_complete_v3_hdr {
441 	__le32 dw0;
442 	__le32 dw1;
443 	__le32 act;
444 	__le32 dw3;
445 };
446 
447 struct hisi_sas_err_record_v3 {
448 	/* dw0 */
449 	__le32 trans_tx_fail_type;
450 
451 	/* dw1 */
452 	__le32 trans_rx_fail_type;
453 
454 	/* dw2 */
455 	__le16 dma_tx_err_type;
456 	__le16 sipc_rx_err_type;
457 
458 	/* dw3 */
459 	__le32 dma_rx_err_type;
460 };
461 
462 #define RX_DATA_LEN_UNDERFLOW_OFF	6
463 #define RX_DATA_LEN_UNDERFLOW_MSK	(1 << RX_DATA_LEN_UNDERFLOW_OFF)
464 
465 #define HISI_SAS_COMMAND_ENTRIES_V3_HW 4096
466 #define HISI_SAS_MSI_COUNT_V3_HW 32
467 
468 #define DIR_NO_DATA 0
469 #define DIR_TO_INI 1
470 #define DIR_TO_DEVICE 2
471 #define DIR_RESERVED 3
472 
473 #define FIS_CMD_IS_UNCONSTRAINED(fis) \
474 	((fis.command == ATA_CMD_READ_LOG_EXT) || \
475 	(fis.command == ATA_CMD_READ_LOG_DMA_EXT) || \
476 	((fis.command == ATA_CMD_DEV_RESET) && \
477 	((fis.control & ATA_SRST) != 0)))
478 
479 #define T10_INSRT_EN_OFF    0
480 #define T10_INSRT_EN_MSK    (1 << T10_INSRT_EN_OFF)
481 #define T10_RMV_EN_OFF	    1
482 #define T10_RMV_EN_MSK	    (1 << T10_RMV_EN_OFF)
483 #define T10_RPLC_EN_OFF	    2
484 #define T10_RPLC_EN_MSK	    (1 << T10_RPLC_EN_OFF)
485 #define T10_CHK_EN_OFF	    3
486 #define T10_CHK_EN_MSK	    (1 << T10_CHK_EN_OFF)
487 #define INCR_LBRT_OFF	    5
488 #define INCR_LBRT_MSK	    (1 << INCR_LBRT_OFF)
489 #define USR_DATA_BLOCK_SZ_OFF	20
490 #define USR_DATA_BLOCK_SZ_MSK	(0x3 << USR_DATA_BLOCK_SZ_OFF)
491 #define T10_CHK_MSK_OFF	    16
492 #define T10_CHK_REF_TAG_MSK (0xf0 << T10_CHK_MSK_OFF)
493 #define T10_CHK_APP_TAG_MSK (0xc << T10_CHK_MSK_OFF)
494 
495 #define BASE_VECTORS_V3_HW  16
496 #define MIN_AFFINE_VECTORS_V3_HW  (BASE_VECTORS_V3_HW + 1)
497 
498 enum {
499 	DSM_FUNC_ERR_HANDLE_MSI = 0,
500 };
501 
502 static bool hisi_sas_intr_conv;
503 MODULE_PARM_DESC(intr_conv, "interrupt converge enable (0-1)");
504 
505 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
506 static int prot_mask;
507 module_param(prot_mask, int, 0);
508 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=0x0 ");
509 
510 static bool auto_affine_msi_experimental;
511 module_param(auto_affine_msi_experimental, bool, 0444);
512 MODULE_PARM_DESC(auto_affine_msi_experimental, "Enable auto-affinity of MSI IRQs as experimental:\n"
513 		 "default is off");
514 
515 static u32 hisi_sas_read32(struct hisi_hba *hisi_hba, u32 off)
516 {
517 	void __iomem *regs = hisi_hba->regs + off;
518 
519 	return readl(regs);
520 }
521 
522 static void hisi_sas_write32(struct hisi_hba *hisi_hba, u32 off, u32 val)
523 {
524 	void __iomem *regs = hisi_hba->regs + off;
525 
526 	writel(val, regs);
527 }
528 
529 static void hisi_sas_phy_write32(struct hisi_hba *hisi_hba, int phy_no,
530 				 u32 off, u32 val)
531 {
532 	void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
533 
534 	writel(val, regs);
535 }
536 
537 static u32 hisi_sas_phy_read32(struct hisi_hba *hisi_hba,
538 				      int phy_no, u32 off)
539 {
540 	void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
541 
542 	return readl(regs);
543 }
544 
545 #define hisi_sas_read32_poll_timeout(off, val, cond, delay_us,		\
546 				     timeout_us)			\
547 ({									\
548 	void __iomem *regs = hisi_hba->regs + off;			\
549 	readl_poll_timeout(regs, val, cond, delay_us, timeout_us);	\
550 })
551 
552 #define hisi_sas_read32_poll_timeout_atomic(off, val, cond, delay_us,	\
553 					    timeout_us)			\
554 ({									\
555 	void __iomem *regs = hisi_hba->regs + off;			\
556 	readl_poll_timeout_atomic(regs, val, cond, delay_us, timeout_us);\
557 })
558 
559 static void init_reg_v3_hw(struct hisi_hba *hisi_hba)
560 {
561 	int i;
562 
563 	/* Global registers init */
564 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE,
565 			 (u32)((1ULL << hisi_hba->queue_count) - 1));
566 	hisi_sas_write32(hisi_hba, SAS_AXI_USER3, 0);
567 	hisi_sas_write32(hisi_hba, CFG_MAX_TAG, 0xfff0400);
568 	hisi_sas_write32(hisi_hba, HGC_SAS_TXFAIL_RETRY_CTRL, 0x108);
569 	hisi_sas_write32(hisi_hba, CFG_AGING_TIME, 0x1);
570 	hisi_sas_write32(hisi_hba, INT_COAL_EN, 0x1);
571 	hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x1);
572 	hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 0x1);
573 	hisi_sas_write32(hisi_hba, CQ_INT_CONVERGE_EN,
574 			 hisi_sas_intr_conv);
575 	hisi_sas_write32(hisi_hba, OQ_INT_SRC, 0xffff);
576 	hisi_sas_write32(hisi_hba, ENT_INT_SRC1, 0xffffffff);
577 	hisi_sas_write32(hisi_hba, ENT_INT_SRC2, 0xffffffff);
578 	hisi_sas_write32(hisi_hba, ENT_INT_SRC3, 0xffffffff);
579 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0xfefefefe);
580 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0xfefefefe);
581 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0xffc220ff);
582 	hisi_sas_write32(hisi_hba, CHNL_PHYUPDOWN_INT_MSK, 0x0);
583 	hisi_sas_write32(hisi_hba, CHNL_ENT_INT_MSK, 0x0);
584 	hisi_sas_write32(hisi_hba, HGC_COM_INT_MSK, 0x0);
585 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0x155555);
586 	hisi_sas_write32(hisi_hba, AWQOS_AWCACHE_CFG, 0xf0f0);
587 	hisi_sas_write32(hisi_hba, ARQOS_ARCACHE_CFG, 0xf0f0);
588 	for (i = 0; i < hisi_hba->queue_count; i++)
589 		hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK+0x4*i, 0);
590 
591 	hisi_sas_write32(hisi_hba, HYPER_STREAM_ID_EN_CFG, 1);
592 
593 	for (i = 0; i < hisi_hba->n_phy; i++) {
594 		struct hisi_sas_phy *phy = &hisi_hba->phy[i];
595 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
596 		u32 prog_phy_link_rate = 0x800;
597 
598 		if (!sas_phy->phy || (sas_phy->phy->maximum_linkrate <
599 				SAS_LINK_RATE_1_5_GBPS)) {
600 			prog_phy_link_rate = 0x855;
601 		} else {
602 			enum sas_linkrate max = sas_phy->phy->maximum_linkrate;
603 
604 			prog_phy_link_rate =
605 				hisi_sas_get_prog_phy_linkrate_mask(max) |
606 				0x800;
607 		}
608 		hisi_sas_phy_write32(hisi_hba, i, PROG_PHY_LINK_RATE,
609 			prog_phy_link_rate);
610 		hisi_sas_phy_write32(hisi_hba, i, SERDES_CFG, 0xffc00);
611 		hisi_sas_phy_write32(hisi_hba, i, SAS_RX_TRAIN_TIMER, 0x13e80);
612 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT0, 0xffffffff);
613 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT1, 0xffffffff);
614 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT2, 0xffffffff);
615 		hisi_sas_phy_write32(hisi_hba, i, RXOP_CHECK_CFG_H, 0x1000);
616 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xf2057fff);
617 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0xffffbfe);
618 		hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL_RDY_MSK, 0x0);
619 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_NOT_RDY_MSK, 0x0);
620 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_DWS_RESET_MSK, 0x0);
621 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_PHY_ENA_MSK, 0x0);
622 		hisi_sas_phy_write32(hisi_hba, i, SL_RX_BCAST_CHK_MSK, 0x0);
623 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_OOB_RESTART_MSK, 0x1);
624 		hisi_sas_phy_write32(hisi_hba, i, STP_LINK_TIMER, 0x7f7a120);
625 		hisi_sas_phy_write32(hisi_hba, i, CON_CFG_DRIVER, 0x2a0a01);
626 		hisi_sas_phy_write32(hisi_hba, i, SAS_SSP_CON_TIMER_CFG, 0x32);
627 		hisi_sas_phy_write32(hisi_hba, i, SAS_EC_INT_COAL_TIME,
628 				     0x30f4240);
629 		/* used for 12G negotiate */
630 		hisi_sas_phy_write32(hisi_hba, i, COARSETUNE_TIME, 0x1e);
631 		hisi_sas_phy_write32(hisi_hba, i, AIP_LIMIT, 0x2ffff);
632 	}
633 
634 	for (i = 0; i < hisi_hba->queue_count; i++) {
635 		/* Delivery queue */
636 		hisi_sas_write32(hisi_hba,
637 				 DLVRY_Q_0_BASE_ADDR_HI + (i * 0x14),
638 				 upper_32_bits(hisi_hba->cmd_hdr_dma[i]));
639 
640 		hisi_sas_write32(hisi_hba, DLVRY_Q_0_BASE_ADDR_LO + (i * 0x14),
641 				 lower_32_bits(hisi_hba->cmd_hdr_dma[i]));
642 
643 		hisi_sas_write32(hisi_hba, DLVRY_Q_0_DEPTH + (i * 0x14),
644 				 HISI_SAS_QUEUE_SLOTS);
645 
646 		/* Completion queue */
647 		hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_HI + (i * 0x14),
648 				 upper_32_bits(hisi_hba->complete_hdr_dma[i]));
649 
650 		hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_LO + (i * 0x14),
651 				 lower_32_bits(hisi_hba->complete_hdr_dma[i]));
652 
653 		hisi_sas_write32(hisi_hba, COMPL_Q_0_DEPTH + (i * 0x14),
654 				 HISI_SAS_QUEUE_SLOTS);
655 	}
656 
657 	/* itct */
658 	hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_LO,
659 			 lower_32_bits(hisi_hba->itct_dma));
660 
661 	hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_HI,
662 			 upper_32_bits(hisi_hba->itct_dma));
663 
664 	/* iost */
665 	hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_LO,
666 			 lower_32_bits(hisi_hba->iost_dma));
667 
668 	hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_HI,
669 			 upper_32_bits(hisi_hba->iost_dma));
670 
671 	/* breakpoint */
672 	hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_LO,
673 			 lower_32_bits(hisi_hba->breakpoint_dma));
674 
675 	hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_HI,
676 			 upper_32_bits(hisi_hba->breakpoint_dma));
677 
678 	/* SATA broken msg */
679 	hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_LO,
680 			 lower_32_bits(hisi_hba->sata_breakpoint_dma));
681 
682 	hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_HI,
683 			 upper_32_bits(hisi_hba->sata_breakpoint_dma));
684 
685 	/* SATA initial fis */
686 	hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_LO,
687 			 lower_32_bits(hisi_hba->initial_fis_dma));
688 
689 	hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_HI,
690 			 upper_32_bits(hisi_hba->initial_fis_dma));
691 
692 	/* RAS registers init */
693 	hisi_sas_write32(hisi_hba, SAS_RAS_INTR0_MASK, 0x0);
694 	hisi_sas_write32(hisi_hba, SAS_RAS_INTR1_MASK, 0x0);
695 	hisi_sas_write32(hisi_hba, SAS_RAS_INTR2_MASK, 0x0);
696 	hisi_sas_write32(hisi_hba, CFG_SAS_RAS_INTR_MASK, 0x0);
697 
698 	/* LED registers init */
699 	hisi_sas_write32(hisi_hba, SAS_CFG_DRIVE_VLD, 0x80000ff);
700 	hisi_sas_write32(hisi_hba, SAS_GPIO_TX_0_1, 0x80808080);
701 	hisi_sas_write32(hisi_hba, SAS_GPIO_TX_0_1 + 0x4, 0x80808080);
702 	/* Configure blink generator rate A to 1Hz and B to 4Hz */
703 	hisi_sas_write32(hisi_hba, SAS_GPIO_CFG_1, 0x121700);
704 	hisi_sas_write32(hisi_hba, SAS_GPIO_CFG_0, 0x800000);
705 }
706 
707 static void config_phy_opt_mode_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
708 {
709 	u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
710 
711 	cfg &= ~PHY_CFG_DC_OPT_MSK;
712 	cfg |= 1 << PHY_CFG_DC_OPT_OFF;
713 	hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
714 }
715 
716 static void config_id_frame_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
717 {
718 	struct sas_identify_frame identify_frame;
719 	u32 *identify_buffer;
720 
721 	memset(&identify_frame, 0, sizeof(identify_frame));
722 	identify_frame.dev_type = SAS_END_DEVICE;
723 	identify_frame.frame_type = 0;
724 	identify_frame._un1 = 1;
725 	identify_frame.initiator_bits = SAS_PROTOCOL_ALL;
726 	identify_frame.target_bits = SAS_PROTOCOL_NONE;
727 	memcpy(&identify_frame._un4_11[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
728 	memcpy(&identify_frame.sas_addr[0], hisi_hba->sas_addr,	SAS_ADDR_SIZE);
729 	identify_frame.phy_id = phy_no;
730 	identify_buffer = (u32 *)(&identify_frame);
731 
732 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD0,
733 			__swab32(identify_buffer[0]));
734 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD1,
735 			__swab32(identify_buffer[1]));
736 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD2,
737 			__swab32(identify_buffer[2]));
738 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD3,
739 			__swab32(identify_buffer[3]));
740 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD4,
741 			__swab32(identify_buffer[4]));
742 	hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD5,
743 			__swab32(identify_buffer[5]));
744 }
745 
746 static void setup_itct_v3_hw(struct hisi_hba *hisi_hba,
747 			     struct hisi_sas_device *sas_dev)
748 {
749 	struct domain_device *device = sas_dev->sas_device;
750 	struct device *dev = hisi_hba->dev;
751 	u64 qw0, device_id = sas_dev->device_id;
752 	struct hisi_sas_itct *itct = &hisi_hba->itct[device_id];
753 	struct domain_device *parent_dev = device->parent;
754 	struct asd_sas_port *sas_port = device->port;
755 	struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
756 	u64 sas_addr;
757 
758 	memset(itct, 0, sizeof(*itct));
759 
760 	/* qw0 */
761 	qw0 = 0;
762 	switch (sas_dev->dev_type) {
763 	case SAS_END_DEVICE:
764 	case SAS_EDGE_EXPANDER_DEVICE:
765 	case SAS_FANOUT_EXPANDER_DEVICE:
766 		qw0 = HISI_SAS_DEV_TYPE_SSP << ITCT_HDR_DEV_TYPE_OFF;
767 		break;
768 	case SAS_SATA_DEV:
769 	case SAS_SATA_PENDING:
770 		if (parent_dev && dev_is_expander(parent_dev->dev_type))
771 			qw0 = HISI_SAS_DEV_TYPE_STP << ITCT_HDR_DEV_TYPE_OFF;
772 		else
773 			qw0 = HISI_SAS_DEV_TYPE_SATA << ITCT_HDR_DEV_TYPE_OFF;
774 		break;
775 	default:
776 		dev_warn(dev, "setup itct: unsupported dev type (%d)\n",
777 			 sas_dev->dev_type);
778 	}
779 
780 	qw0 |= ((1 << ITCT_HDR_VALID_OFF) |
781 		(device->linkrate << ITCT_HDR_MCR_OFF) |
782 		(1 << ITCT_HDR_VLN_OFF) |
783 		(0xfa << ITCT_HDR_SMP_TIMEOUT_OFF) |
784 		(1 << ITCT_HDR_AWT_CONTINUE_OFF) |
785 		(port->id << ITCT_HDR_PORT_ID_OFF));
786 	itct->qw0 = cpu_to_le64(qw0);
787 
788 	/* qw1 */
789 	memcpy(&sas_addr, device->sas_addr, SAS_ADDR_SIZE);
790 	itct->sas_addr = cpu_to_le64(__swab64(sas_addr));
791 
792 	/* qw2 */
793 	if (!dev_is_sata(device))
794 		itct->qw2 = cpu_to_le64((5000ULL << ITCT_HDR_INLT_OFF) |
795 					(0x1ULL << ITCT_HDR_RTOLT_OFF));
796 }
797 
798 static int clear_itct_v3_hw(struct hisi_hba *hisi_hba,
799 			    struct hisi_sas_device *sas_dev)
800 {
801 	DECLARE_COMPLETION_ONSTACK(completion);
802 	u64 dev_id = sas_dev->device_id;
803 	struct hisi_sas_itct *itct = &hisi_hba->itct[dev_id];
804 	u32 reg_val = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
805 	struct device *dev = hisi_hba->dev;
806 
807 	sas_dev->completion = &completion;
808 
809 	/* clear the itct interrupt state */
810 	if (ENT_INT_SRC3_ITC_INT_MSK & reg_val)
811 		hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
812 				 ENT_INT_SRC3_ITC_INT_MSK);
813 
814 	/* clear the itct table */
815 	reg_val = ITCT_CLR_EN_MSK | (dev_id & ITCT_DEV_MSK);
816 	hisi_sas_write32(hisi_hba, ITCT_CLR, reg_val);
817 
818 	if (!wait_for_completion_timeout(sas_dev->completion,
819 					 CLEAR_ITCT_TIMEOUT * HZ)) {
820 		dev_warn(dev, "failed to clear ITCT\n");
821 		return -ETIMEDOUT;
822 	}
823 
824 	memset(itct, 0, sizeof(struct hisi_sas_itct));
825 	return 0;
826 }
827 
828 static void dereg_device_v3_hw(struct hisi_hba *hisi_hba,
829 				struct domain_device *device)
830 {
831 	struct hisi_sas_slot *slot, *slot2;
832 	struct hisi_sas_device *sas_dev = device->lldd_dev;
833 	u32 cfg_abt_set_query_iptt;
834 
835 	cfg_abt_set_query_iptt = hisi_sas_read32(hisi_hba,
836 		CFG_ABT_SET_QUERY_IPTT);
837 	list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry) {
838 		cfg_abt_set_query_iptt &= ~CFG_SET_ABORTED_IPTT_MSK;
839 		cfg_abt_set_query_iptt |= (1 << CFG_SET_ABORTED_EN_OFF) |
840 			(slot->idx << CFG_SET_ABORTED_IPTT_OFF);
841 		hisi_sas_write32(hisi_hba, CFG_ABT_SET_QUERY_IPTT,
842 			cfg_abt_set_query_iptt);
843 	}
844 	cfg_abt_set_query_iptt &= ~(1 << CFG_SET_ABORTED_EN_OFF);
845 	hisi_sas_write32(hisi_hba, CFG_ABT_SET_QUERY_IPTT,
846 		cfg_abt_set_query_iptt);
847 	hisi_sas_write32(hisi_hba, CFG_ABT_SET_IPTT_DONE,
848 					1 << CFG_ABT_SET_IPTT_DONE_OFF);
849 }
850 
851 static int reset_hw_v3_hw(struct hisi_hba *hisi_hba)
852 {
853 	struct device *dev = hisi_hba->dev;
854 	int ret;
855 	u32 val;
856 
857 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0);
858 
859 	/* Disable all of the PHYs */
860 	hisi_sas_stop_phys(hisi_hba);
861 	udelay(50);
862 
863 	/* Ensure axi bus idle */
864 	ret = hisi_sas_read32_poll_timeout(AXI_CFG, val, !val,
865 					   20000, 1000000);
866 	if (ret) {
867 		dev_err(dev, "axi bus is not idle, ret = %d!\n", ret);
868 		return -EIO;
869 	}
870 
871 	if (ACPI_HANDLE(dev)) {
872 		acpi_status s;
873 
874 		s = acpi_evaluate_object(ACPI_HANDLE(dev), "_RST", NULL, NULL);
875 		if (ACPI_FAILURE(s)) {
876 			dev_err(dev, "Reset failed\n");
877 			return -EIO;
878 		}
879 	} else {
880 		dev_err(dev, "no reset method!\n");
881 		return -EINVAL;
882 	}
883 
884 	return 0;
885 }
886 
887 static int hw_init_v3_hw(struct hisi_hba *hisi_hba)
888 {
889 	struct device *dev = hisi_hba->dev;
890 	union acpi_object *obj;
891 	guid_t guid;
892 	int rc;
893 
894 	rc = reset_hw_v3_hw(hisi_hba);
895 	if (rc) {
896 		dev_err(dev, "hisi_sas_reset_hw failed, rc=%d", rc);
897 		return rc;
898 	}
899 
900 	msleep(100);
901 	init_reg_v3_hw(hisi_hba);
902 
903 	if (guid_parse("D5918B4B-37AE-4E10-A99F-E5E8A6EF4C1F", &guid)) {
904 		dev_err(dev, "Parse GUID failed\n");
905 		return -EINVAL;
906 	}
907 
908 	/* Switch over to MSI handling , from PCI AER default */
909 	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &guid, 0,
910 				DSM_FUNC_ERR_HANDLE_MSI, NULL);
911 	if (!obj)
912 		dev_warn(dev, "Switch over to MSI handling failed\n");
913 	else
914 		ACPI_FREE(obj);
915 
916 	return 0;
917 }
918 
919 static void enable_phy_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
920 {
921 	u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
922 
923 	cfg |= PHY_CFG_ENA_MSK;
924 	cfg &= ~PHY_CFG_PHY_RST_MSK;
925 	hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
926 }
927 
928 static void disable_phy_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
929 {
930 	u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
931 	u32 irq_msk = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT2_MSK);
932 	static const u32 msk = BIT(CHL_INT2_RX_DISP_ERR_OFF) |
933 			       BIT(CHL_INT2_RX_CODE_ERR_OFF) |
934 			       BIT(CHL_INT2_RX_INVLD_DW_OFF);
935 	u32 state;
936 
937 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2_MSK, msk | irq_msk);
938 
939 	cfg &= ~PHY_CFG_ENA_MSK;
940 	hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
941 
942 	mdelay(50);
943 
944 	state = hisi_sas_read32(hisi_hba, PHY_STATE);
945 	if (state & BIT(phy_no)) {
946 		cfg |= PHY_CFG_PHY_RST_MSK;
947 		hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
948 	}
949 
950 	udelay(1);
951 
952 	hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_INVLD_DW);
953 	hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_DISP_ERR);
954 	hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_CODE_ERR);
955 
956 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2, msk);
957 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2_MSK, irq_msk);
958 }
959 
960 static void start_phy_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
961 {
962 	config_id_frame_v3_hw(hisi_hba, phy_no);
963 	config_phy_opt_mode_v3_hw(hisi_hba, phy_no);
964 	enable_phy_v3_hw(hisi_hba, phy_no);
965 }
966 
967 static void phy_hard_reset_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
968 {
969 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
970 	u32 txid_auto;
971 
972 	hisi_sas_phy_enable(hisi_hba, phy_no, 0);
973 	if (phy->identify.device_type == SAS_END_DEVICE) {
974 		txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
975 		hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
976 					txid_auto | TX_HARDRST_MSK);
977 	}
978 	msleep(100);
979 	hisi_sas_phy_enable(hisi_hba, phy_no, 1);
980 }
981 
982 static enum sas_linkrate phy_get_max_linkrate_v3_hw(void)
983 {
984 	return SAS_LINK_RATE_12_0_GBPS;
985 }
986 
987 static void phys_init_v3_hw(struct hisi_hba *hisi_hba)
988 {
989 	int i;
990 
991 	for (i = 0; i < hisi_hba->n_phy; i++) {
992 		struct hisi_sas_phy *phy = &hisi_hba->phy[i];
993 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
994 
995 		if (!sas_phy->phy->enabled)
996 			continue;
997 
998 		hisi_sas_phy_enable(hisi_hba, i, 1);
999 	}
1000 }
1001 
1002 static void sl_notify_ssp_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1003 {
1004 	u32 sl_control;
1005 
1006 	sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1007 	sl_control |= SL_CONTROL_NOTIFY_EN_MSK;
1008 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
1009 	msleep(1);
1010 	sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1011 	sl_control &= ~SL_CONTROL_NOTIFY_EN_MSK;
1012 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
1013 }
1014 
1015 static int get_wideport_bitmap_v3_hw(struct hisi_hba *hisi_hba, int port_id)
1016 {
1017 	int i, bitmap = 0;
1018 	u32 phy_port_num_ma = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
1019 	u32 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1020 
1021 	for (i = 0; i < hisi_hba->n_phy; i++)
1022 		if (phy_state & BIT(i))
1023 			if (((phy_port_num_ma >> (i * 4)) & 0xf) == port_id)
1024 				bitmap |= BIT(i);
1025 
1026 	return bitmap;
1027 }
1028 
1029 static void start_delivery_v3_hw(struct hisi_sas_dq *dq)
1030 {
1031 	struct hisi_hba *hisi_hba = dq->hisi_hba;
1032 	struct hisi_sas_slot *s, *s1, *s2 = NULL;
1033 	int dlvry_queue = dq->id;
1034 	int wp;
1035 
1036 	list_for_each_entry_safe(s, s1, &dq->list, delivery) {
1037 		if (!s->ready)
1038 			break;
1039 		s2 = s;
1040 		list_del(&s->delivery);
1041 	}
1042 
1043 	if (!s2)
1044 		return;
1045 
1046 	/*
1047 	 * Ensure that memories for slots built on other CPUs is observed.
1048 	 */
1049 	smp_rmb();
1050 	wp = (s2->dlvry_queue_slot + 1) % HISI_SAS_QUEUE_SLOTS;
1051 
1052 	hisi_sas_write32(hisi_hba, DLVRY_Q_0_WR_PTR + (dlvry_queue * 0x14), wp);
1053 }
1054 
1055 static void prep_prd_sge_v3_hw(struct hisi_hba *hisi_hba,
1056 			      struct hisi_sas_slot *slot,
1057 			      struct hisi_sas_cmd_hdr *hdr,
1058 			      struct scatterlist *scatter,
1059 			      int n_elem)
1060 {
1061 	struct hisi_sas_sge_page *sge_page = hisi_sas_sge_addr_mem(slot);
1062 	struct scatterlist *sg;
1063 	int i;
1064 
1065 	for_each_sg(scatter, sg, n_elem, i) {
1066 		struct hisi_sas_sge *entry = &sge_page->sge[i];
1067 
1068 		entry->addr = cpu_to_le64(sg_dma_address(sg));
1069 		entry->page_ctrl_0 = entry->page_ctrl_1 = 0;
1070 		entry->data_len = cpu_to_le32(sg_dma_len(sg));
1071 		entry->data_off = 0;
1072 	}
1073 
1074 	hdr->prd_table_addr = cpu_to_le64(hisi_sas_sge_addr_dma(slot));
1075 
1076 	hdr->sg_len |= cpu_to_le32(n_elem << CMD_HDR_DATA_SGL_LEN_OFF);
1077 }
1078 
1079 static void prep_prd_sge_dif_v3_hw(struct hisi_hba *hisi_hba,
1080 				   struct hisi_sas_slot *slot,
1081 				   struct hisi_sas_cmd_hdr *hdr,
1082 				   struct scatterlist *scatter,
1083 				   int n_elem)
1084 {
1085 	struct hisi_sas_sge_dif_page *sge_dif_page;
1086 	struct scatterlist *sg;
1087 	int i;
1088 
1089 	sge_dif_page = hisi_sas_sge_dif_addr_mem(slot);
1090 
1091 	for_each_sg(scatter, sg, n_elem, i) {
1092 		struct hisi_sas_sge *entry = &sge_dif_page->sge[i];
1093 
1094 		entry->addr = cpu_to_le64(sg_dma_address(sg));
1095 		entry->page_ctrl_0 = 0;
1096 		entry->page_ctrl_1 = 0;
1097 		entry->data_len = cpu_to_le32(sg_dma_len(sg));
1098 		entry->data_off = 0;
1099 	}
1100 
1101 	hdr->dif_prd_table_addr =
1102 		cpu_to_le64(hisi_sas_sge_dif_addr_dma(slot));
1103 
1104 	hdr->sg_len |= cpu_to_le32(n_elem << CMD_HDR_DIF_SGL_LEN_OFF);
1105 }
1106 
1107 static u32 get_prot_chk_msk_v3_hw(struct scsi_cmnd *scsi_cmnd)
1108 {
1109 	unsigned char prot_flags = scsi_cmnd->prot_flags;
1110 
1111 	if (prot_flags & SCSI_PROT_REF_CHECK)
1112 		return T10_CHK_APP_TAG_MSK;
1113 	return T10_CHK_REF_TAG_MSK | T10_CHK_APP_TAG_MSK;
1114 }
1115 
1116 static void fill_prot_v3_hw(struct scsi_cmnd *scsi_cmnd,
1117 			    struct hisi_sas_protect_iu_v3_hw *prot)
1118 {
1119 	unsigned char prot_op = scsi_get_prot_op(scsi_cmnd);
1120 	unsigned int interval = scsi_prot_interval(scsi_cmnd);
1121 	u32 lbrt_chk_val = t10_pi_ref_tag(scsi_cmnd->request);
1122 
1123 	switch (prot_op) {
1124 	case SCSI_PROT_READ_INSERT:
1125 		prot->dw0 |= T10_INSRT_EN_MSK;
1126 		prot->lbrtgv = lbrt_chk_val;
1127 		break;
1128 	case SCSI_PROT_READ_STRIP:
1129 		prot->dw0 |= (T10_RMV_EN_MSK | T10_CHK_EN_MSK);
1130 		prot->lbrtcv = lbrt_chk_val;
1131 		prot->dw4 |= get_prot_chk_msk_v3_hw(scsi_cmnd);
1132 		break;
1133 	case SCSI_PROT_READ_PASS:
1134 		prot->dw0 |= T10_CHK_EN_MSK;
1135 		prot->lbrtcv = lbrt_chk_val;
1136 		prot->dw4 |= get_prot_chk_msk_v3_hw(scsi_cmnd);
1137 		break;
1138 	case SCSI_PROT_WRITE_INSERT:
1139 		prot->dw0 |= T10_INSRT_EN_MSK;
1140 		prot->lbrtgv = lbrt_chk_val;
1141 		break;
1142 	case SCSI_PROT_WRITE_STRIP:
1143 		prot->dw0 |= (T10_RMV_EN_MSK | T10_CHK_EN_MSK);
1144 		prot->lbrtcv = lbrt_chk_val;
1145 		break;
1146 	case SCSI_PROT_WRITE_PASS:
1147 		prot->dw0 |= T10_CHK_EN_MSK;
1148 		prot->lbrtcv = lbrt_chk_val;
1149 		prot->dw4 |= get_prot_chk_msk_v3_hw(scsi_cmnd);
1150 		break;
1151 	default:
1152 		WARN(1, "prot_op(0x%x) is not valid\n", prot_op);
1153 		break;
1154 	}
1155 
1156 	switch (interval) {
1157 	case 512:
1158 		break;
1159 	case 4096:
1160 		prot->dw0 |= (0x1 << USR_DATA_BLOCK_SZ_OFF);
1161 		break;
1162 	case 520:
1163 		prot->dw0 |= (0x2 << USR_DATA_BLOCK_SZ_OFF);
1164 		break;
1165 	default:
1166 		WARN(1, "protection interval (0x%x) invalid\n",
1167 		     interval);
1168 		break;
1169 	}
1170 
1171 	prot->dw0 |= INCR_LBRT_MSK;
1172 }
1173 
1174 static void prep_ssp_v3_hw(struct hisi_hba *hisi_hba,
1175 			  struct hisi_sas_slot *slot)
1176 {
1177 	struct sas_task *task = slot->task;
1178 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1179 	struct domain_device *device = task->dev;
1180 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1181 	struct hisi_sas_port *port = slot->port;
1182 	struct sas_ssp_task *ssp_task = &task->ssp_task;
1183 	struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
1184 	struct hisi_sas_tmf_task *tmf = slot->tmf;
1185 	int has_data = 0, priority = !!tmf;
1186 	unsigned char prot_op;
1187 	u8 *buf_cmd;
1188 	u32 dw1 = 0, dw2 = 0, len = 0;
1189 
1190 	hdr->dw0 = cpu_to_le32((1 << CMD_HDR_RESP_REPORT_OFF) |
1191 			       (2 << CMD_HDR_TLR_CTRL_OFF) |
1192 			       (port->id << CMD_HDR_PORT_OFF) |
1193 			       (priority << CMD_HDR_PRIORITY_OFF) |
1194 			       (1 << CMD_HDR_CMD_OFF)); /* ssp */
1195 
1196 	dw1 = 1 << CMD_HDR_VDTL_OFF;
1197 	if (tmf) {
1198 		dw1 |= 2 << CMD_HDR_FRAME_TYPE_OFF;
1199 		dw1 |= DIR_NO_DATA << CMD_HDR_DIR_OFF;
1200 	} else {
1201 		prot_op = scsi_get_prot_op(scsi_cmnd);
1202 		dw1 |= 1 << CMD_HDR_FRAME_TYPE_OFF;
1203 		switch (scsi_cmnd->sc_data_direction) {
1204 		case DMA_TO_DEVICE:
1205 			has_data = 1;
1206 			dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
1207 			break;
1208 		case DMA_FROM_DEVICE:
1209 			has_data = 1;
1210 			dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
1211 			break;
1212 		default:
1213 			dw1 &= ~CMD_HDR_DIR_MSK;
1214 		}
1215 	}
1216 
1217 	/* map itct entry */
1218 	dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
1219 
1220 	dw2 = (((sizeof(struct ssp_command_iu) + sizeof(struct ssp_frame_hdr)
1221 	      + 3) / 4) << CMD_HDR_CFL_OFF) |
1222 	      ((HISI_SAS_MAX_SSP_RESP_SZ / 4) << CMD_HDR_MRFL_OFF) |
1223 	      (2 << CMD_HDR_SG_MOD_OFF);
1224 	hdr->dw2 = cpu_to_le32(dw2);
1225 	hdr->transfer_tags = cpu_to_le32(slot->idx);
1226 
1227 	if (has_data) {
1228 		prep_prd_sge_v3_hw(hisi_hba, slot, hdr, task->scatter,
1229 				   slot->n_elem);
1230 
1231 		if (scsi_prot_sg_count(scsi_cmnd))
1232 			prep_prd_sge_dif_v3_hw(hisi_hba, slot, hdr,
1233 					       scsi_prot_sglist(scsi_cmnd),
1234 					       slot->n_elem_dif);
1235 	}
1236 
1237 	hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
1238 	hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
1239 
1240 	buf_cmd = hisi_sas_cmd_hdr_addr_mem(slot) +
1241 		sizeof(struct ssp_frame_hdr);
1242 
1243 	memcpy(buf_cmd, &task->ssp_task.LUN, 8);
1244 	if (!tmf) {
1245 		buf_cmd[9] = ssp_task->task_attr | (ssp_task->task_prio << 3);
1246 		memcpy(buf_cmd + 12, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
1247 	} else {
1248 		buf_cmd[10] = tmf->tmf;
1249 		switch (tmf->tmf) {
1250 		case TMF_ABORT_TASK:
1251 		case TMF_QUERY_TASK:
1252 			buf_cmd[12] =
1253 				(tmf->tag_of_task_to_be_managed >> 8) & 0xff;
1254 			buf_cmd[13] =
1255 				tmf->tag_of_task_to_be_managed & 0xff;
1256 			break;
1257 		default:
1258 			break;
1259 		}
1260 	}
1261 
1262 	if (has_data && (prot_op != SCSI_PROT_NORMAL)) {
1263 		struct hisi_sas_protect_iu_v3_hw prot;
1264 		u8 *buf_cmd_prot;
1265 
1266 		hdr->dw7 |= cpu_to_le32(1 << CMD_HDR_ADDR_MODE_SEL_OFF);
1267 		dw1 |= CMD_HDR_PIR_MSK;
1268 		buf_cmd_prot = hisi_sas_cmd_hdr_addr_mem(slot) +
1269 			       sizeof(struct ssp_frame_hdr) +
1270 			       sizeof(struct ssp_command_iu);
1271 
1272 		memset(&prot, 0, sizeof(struct hisi_sas_protect_iu_v3_hw));
1273 		fill_prot_v3_hw(scsi_cmnd, &prot);
1274 		memcpy(buf_cmd_prot, &prot,
1275 		       sizeof(struct hisi_sas_protect_iu_v3_hw));
1276 		/*
1277 		 * For READ, we need length of info read to memory, while for
1278 		 * WRITE we need length of data written to the disk.
1279 		 */
1280 		if (prot_op == SCSI_PROT_WRITE_INSERT ||
1281 		    prot_op == SCSI_PROT_READ_INSERT ||
1282 		    prot_op == SCSI_PROT_WRITE_PASS ||
1283 		    prot_op == SCSI_PROT_READ_PASS) {
1284 			unsigned int interval = scsi_prot_interval(scsi_cmnd);
1285 			unsigned int ilog2_interval = ilog2(interval);
1286 
1287 			len = (task->total_xfer_len >> ilog2_interval) * 8;
1288 		}
1289 	}
1290 
1291 	hdr->dw1 = cpu_to_le32(dw1);
1292 
1293 	hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len + len);
1294 }
1295 
1296 static void prep_smp_v3_hw(struct hisi_hba *hisi_hba,
1297 			  struct hisi_sas_slot *slot)
1298 {
1299 	struct sas_task *task = slot->task;
1300 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1301 	struct domain_device *device = task->dev;
1302 	struct hisi_sas_port *port = slot->port;
1303 	struct scatterlist *sg_req;
1304 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1305 	dma_addr_t req_dma_addr;
1306 	unsigned int req_len;
1307 
1308 	/* req */
1309 	sg_req = &task->smp_task.smp_req;
1310 	req_len = sg_dma_len(sg_req);
1311 	req_dma_addr = sg_dma_address(sg_req);
1312 
1313 	/* create header */
1314 	/* dw0 */
1315 	hdr->dw0 = cpu_to_le32((port->id << CMD_HDR_PORT_OFF) |
1316 			       (1 << CMD_HDR_PRIORITY_OFF) | /* high pri */
1317 			       (2 << CMD_HDR_CMD_OFF)); /* smp */
1318 
1319 	/* map itct entry */
1320 	hdr->dw1 = cpu_to_le32((sas_dev->device_id << CMD_HDR_DEV_ID_OFF) |
1321 			       (1 << CMD_HDR_FRAME_TYPE_OFF) |
1322 			       (DIR_NO_DATA << CMD_HDR_DIR_OFF));
1323 
1324 	/* dw2 */
1325 	hdr->dw2 = cpu_to_le32((((req_len - 4) / 4) << CMD_HDR_CFL_OFF) |
1326 			       (HISI_SAS_MAX_SMP_RESP_SZ / 4 <<
1327 			       CMD_HDR_MRFL_OFF));
1328 
1329 	hdr->transfer_tags = cpu_to_le32(slot->idx << CMD_HDR_IPTT_OFF);
1330 
1331 	hdr->cmd_table_addr = cpu_to_le64(req_dma_addr);
1332 	hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
1333 
1334 }
1335 
1336 static void prep_ata_v3_hw(struct hisi_hba *hisi_hba,
1337 			  struct hisi_sas_slot *slot)
1338 {
1339 	struct sas_task *task = slot->task;
1340 	struct domain_device *device = task->dev;
1341 	struct domain_device *parent_dev = device->parent;
1342 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1343 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1344 	struct asd_sas_port *sas_port = device->port;
1345 	struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
1346 	u8 *buf_cmd;
1347 	int has_data = 0, hdr_tag = 0;
1348 	u32 dw1 = 0, dw2 = 0;
1349 
1350 	hdr->dw0 = cpu_to_le32(port->id << CMD_HDR_PORT_OFF);
1351 	if (parent_dev && dev_is_expander(parent_dev->dev_type))
1352 		hdr->dw0 |= cpu_to_le32(3 << CMD_HDR_CMD_OFF);
1353 	else
1354 		hdr->dw0 |= cpu_to_le32(4U << CMD_HDR_CMD_OFF);
1355 
1356 	switch (task->data_dir) {
1357 	case DMA_TO_DEVICE:
1358 		has_data = 1;
1359 		dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
1360 		break;
1361 	case DMA_FROM_DEVICE:
1362 		has_data = 1;
1363 		dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
1364 		break;
1365 	default:
1366 		dw1 &= ~CMD_HDR_DIR_MSK;
1367 	}
1368 
1369 	if ((task->ata_task.fis.command == ATA_CMD_DEV_RESET) &&
1370 			(task->ata_task.fis.control & ATA_SRST))
1371 		dw1 |= 1 << CMD_HDR_RESET_OFF;
1372 
1373 	dw1 |= (hisi_sas_get_ata_protocol(
1374 		&task->ata_task.fis, task->data_dir))
1375 		<< CMD_HDR_FRAME_TYPE_OFF;
1376 	dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
1377 
1378 	if (FIS_CMD_IS_UNCONSTRAINED(task->ata_task.fis))
1379 		dw1 |= 1 << CMD_HDR_UNCON_CMD_OFF;
1380 
1381 	hdr->dw1 = cpu_to_le32(dw1);
1382 
1383 	/* dw2 */
1384 	if (task->ata_task.use_ncq) {
1385 		struct ata_queued_cmd *qc = task->uldd_task;
1386 
1387 		hdr_tag = qc->tag;
1388 		task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
1389 		dw2 |= hdr_tag << CMD_HDR_NCQ_TAG_OFF;
1390 	}
1391 
1392 	dw2 |= (HISI_SAS_MAX_STP_RESP_SZ / 4) << CMD_HDR_CFL_OFF |
1393 			2 << CMD_HDR_SG_MOD_OFF;
1394 	hdr->dw2 = cpu_to_le32(dw2);
1395 
1396 	/* dw3 */
1397 	hdr->transfer_tags = cpu_to_le32(slot->idx);
1398 
1399 	if (has_data)
1400 		prep_prd_sge_v3_hw(hisi_hba, slot, hdr, task->scatter,
1401 					slot->n_elem);
1402 
1403 	hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
1404 	hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
1405 	hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
1406 
1407 	buf_cmd = hisi_sas_cmd_hdr_addr_mem(slot);
1408 
1409 	if (likely(!task->ata_task.device_control_reg_update))
1410 		task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
1411 	/* fill in command FIS */
1412 	memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
1413 }
1414 
1415 static void prep_abort_v3_hw(struct hisi_hba *hisi_hba,
1416 		struct hisi_sas_slot *slot,
1417 		int device_id, int abort_flag, int tag_to_abort)
1418 {
1419 	struct sas_task *task = slot->task;
1420 	struct domain_device *dev = task->dev;
1421 	struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1422 	struct hisi_sas_port *port = slot->port;
1423 
1424 	/* dw0 */
1425 	hdr->dw0 = cpu_to_le32((5U << CMD_HDR_CMD_OFF) | /*abort*/
1426 			       (port->id << CMD_HDR_PORT_OFF) |
1427 				   (dev_is_sata(dev)
1428 					<< CMD_HDR_ABORT_DEVICE_TYPE_OFF) |
1429 					(abort_flag
1430 					 << CMD_HDR_ABORT_FLAG_OFF));
1431 
1432 	/* dw1 */
1433 	hdr->dw1 = cpu_to_le32(device_id
1434 			<< CMD_HDR_DEV_ID_OFF);
1435 
1436 	/* dw7 */
1437 	hdr->dw7 = cpu_to_le32(tag_to_abort << CMD_HDR_ABORT_IPTT_OFF);
1438 	hdr->transfer_tags = cpu_to_le32(slot->idx);
1439 
1440 }
1441 
1442 static irqreturn_t phy_up_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
1443 {
1444 	int i;
1445 	irqreturn_t res;
1446 	u32 context, port_id, link_rate;
1447 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1448 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1449 	struct device *dev = hisi_hba->dev;
1450 	unsigned long flags;
1451 
1452 	del_timer(&phy->timer);
1453 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 1);
1454 
1455 	port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
1456 	port_id = (port_id >> (4 * phy_no)) & 0xf;
1457 	link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
1458 	link_rate = (link_rate >> (phy_no * 4)) & 0xf;
1459 
1460 	if (port_id == 0xf) {
1461 		dev_err(dev, "phyup: phy%d invalid portid\n", phy_no);
1462 		res = IRQ_NONE;
1463 		goto end;
1464 	}
1465 	sas_phy->linkrate = link_rate;
1466 	phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1467 
1468 	/* Check for SATA dev */
1469 	context = hisi_sas_read32(hisi_hba, PHY_CONTEXT);
1470 	if (context & (1 << phy_no)) {
1471 		struct hisi_sas_initial_fis *initial_fis;
1472 		struct dev_to_host_fis *fis;
1473 		u8 attached_sas_addr[SAS_ADDR_SIZE] = {0};
1474 		struct Scsi_Host *shost = hisi_hba->shost;
1475 
1476 		dev_info(dev, "phyup: phy%d link_rate=%d(sata)\n", phy_no, link_rate);
1477 		initial_fis = &hisi_hba->initial_fis[phy_no];
1478 		fis = &initial_fis->fis;
1479 
1480 		/* check ERR bit of Status Register */
1481 		if (fis->status & ATA_ERR) {
1482 			dev_warn(dev, "sata int: phy%d FIS status: 0x%x\n",
1483 				 phy_no, fis->status);
1484 			hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
1485 			res = IRQ_NONE;
1486 			goto end;
1487 		}
1488 
1489 		sas_phy->oob_mode = SATA_OOB_MODE;
1490 		attached_sas_addr[0] = 0x50;
1491 		attached_sas_addr[6] = shost->host_no;
1492 		attached_sas_addr[7] = phy_no;
1493 		memcpy(sas_phy->attached_sas_addr,
1494 		       attached_sas_addr,
1495 		       SAS_ADDR_SIZE);
1496 		memcpy(sas_phy->frame_rcvd, fis,
1497 		       sizeof(struct dev_to_host_fis));
1498 		phy->phy_type |= PORT_TYPE_SATA;
1499 		phy->identify.device_type = SAS_SATA_DEV;
1500 		phy->frame_rcvd_size = sizeof(struct dev_to_host_fis);
1501 		phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
1502 	} else {
1503 		u32 *frame_rcvd = (u32 *)sas_phy->frame_rcvd;
1504 		struct sas_identify_frame *id =
1505 			(struct sas_identify_frame *)frame_rcvd;
1506 
1507 		dev_info(dev, "phyup: phy%d link_rate=%d\n", phy_no, link_rate);
1508 		for (i = 0; i < 6; i++) {
1509 			u32 idaf = hisi_sas_phy_read32(hisi_hba, phy_no,
1510 					       RX_IDAF_DWORD0 + (i * 4));
1511 			frame_rcvd[i] = __swab32(idaf);
1512 		}
1513 		sas_phy->oob_mode = SAS_OOB_MODE;
1514 		memcpy(sas_phy->attached_sas_addr,
1515 		       &id->sas_addr,
1516 		       SAS_ADDR_SIZE);
1517 		phy->phy_type |= PORT_TYPE_SAS;
1518 		phy->identify.device_type = id->dev_type;
1519 		phy->frame_rcvd_size = sizeof(struct sas_identify_frame);
1520 		if (phy->identify.device_type == SAS_END_DEVICE)
1521 			phy->identify.target_port_protocols =
1522 				SAS_PROTOCOL_SSP;
1523 		else if (phy->identify.device_type != SAS_PHY_UNUSED)
1524 			phy->identify.target_port_protocols =
1525 				SAS_PROTOCOL_SMP;
1526 	}
1527 
1528 	phy->port_id = port_id;
1529 	phy->phy_attached = 1;
1530 	hisi_sas_notify_phy_event(phy, HISI_PHYE_PHY_UP);
1531 	res = IRQ_HANDLED;
1532 	spin_lock_irqsave(&phy->lock, flags);
1533 	if (phy->reset_completion) {
1534 		phy->in_reset = 0;
1535 		complete(phy->reset_completion);
1536 	}
1537 	spin_unlock_irqrestore(&phy->lock, flags);
1538 end:
1539 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
1540 			     CHL_INT0_SL_PHY_ENABLE_MSK);
1541 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 0);
1542 
1543 	return res;
1544 }
1545 
1546 static irqreturn_t phy_down_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
1547 {
1548 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1549 	u32 phy_state, sl_ctrl, txid_auto;
1550 	struct device *dev = hisi_hba->dev;
1551 
1552 	atomic_inc(&phy->down_cnt);
1553 
1554 	del_timer(&phy->timer);
1555 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 1);
1556 
1557 	phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1558 	dev_info(dev, "phydown: phy%d phy_state=0x%x\n", phy_no, phy_state);
1559 	hisi_sas_phy_down(hisi_hba, phy_no, (phy_state & 1 << phy_no) ? 1 : 0);
1560 
1561 	sl_ctrl = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1562 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL,
1563 						sl_ctrl&(~SL_CTA_MSK));
1564 
1565 	txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
1566 	hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
1567 						txid_auto | CT3_MSK);
1568 
1569 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0, CHL_INT0_NOT_RDY_MSK);
1570 	hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 0);
1571 
1572 	return IRQ_HANDLED;
1573 }
1574 
1575 static irqreturn_t phy_bcast_v3_hw(int phy_no, struct hisi_hba *hisi_hba)
1576 {
1577 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1578 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1579 	struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1580 	u32 bcast_status;
1581 
1582 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 1);
1583 	bcast_status = hisi_sas_phy_read32(hisi_hba, phy_no, RX_PRIMS_STATUS);
1584 	if ((bcast_status & RX_BCAST_CHG_MSK) &&
1585 	    !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1586 		sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
1587 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
1588 			     CHL_INT0_SL_RX_BCST_ACK_MSK);
1589 	hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 0);
1590 
1591 	return IRQ_HANDLED;
1592 }
1593 
1594 static irqreturn_t int_phy_up_down_bcast_v3_hw(int irq_no, void *p)
1595 {
1596 	struct hisi_hba *hisi_hba = p;
1597 	u32 irq_msk;
1598 	int phy_no = 0;
1599 	irqreturn_t res = IRQ_NONE;
1600 
1601 	irq_msk = hisi_sas_read32(hisi_hba, CHNL_INT_STATUS)
1602 				& 0x11111111;
1603 	while (irq_msk) {
1604 		if (irq_msk  & 1) {
1605 			u32 irq_value = hisi_sas_phy_read32(hisi_hba, phy_no,
1606 							    CHL_INT0);
1607 			u32 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1608 			int rdy = phy_state & (1 << phy_no);
1609 
1610 			if (rdy) {
1611 				if (irq_value & CHL_INT0_SL_PHY_ENABLE_MSK)
1612 					/* phy up */
1613 					if (phy_up_v3_hw(phy_no, hisi_hba)
1614 							== IRQ_HANDLED)
1615 						res = IRQ_HANDLED;
1616 				if (irq_value & CHL_INT0_SL_RX_BCST_ACK_MSK)
1617 					/* phy bcast */
1618 					if (phy_bcast_v3_hw(phy_no, hisi_hba)
1619 							== IRQ_HANDLED)
1620 						res = IRQ_HANDLED;
1621 			} else {
1622 				if (irq_value & CHL_INT0_NOT_RDY_MSK)
1623 					/* phy down */
1624 					if (phy_down_v3_hw(phy_no, hisi_hba)
1625 							== IRQ_HANDLED)
1626 						res = IRQ_HANDLED;
1627 			}
1628 		}
1629 		irq_msk >>= 4;
1630 		phy_no++;
1631 	}
1632 
1633 	return res;
1634 }
1635 
1636 static const struct hisi_sas_hw_error port_axi_error[] = {
1637 	{
1638 		.irq_msk = BIT(CHL_INT1_DMAC_TX_ECC_MB_ERR_OFF),
1639 		.msg = "dmac_tx_ecc_bad_err",
1640 	},
1641 	{
1642 		.irq_msk = BIT(CHL_INT1_DMAC_RX_ECC_MB_ERR_OFF),
1643 		.msg = "dmac_rx_ecc_bad_err",
1644 	},
1645 	{
1646 		.irq_msk = BIT(CHL_INT1_DMAC_TX_AXI_WR_ERR_OFF),
1647 		.msg = "dma_tx_axi_wr_err",
1648 	},
1649 	{
1650 		.irq_msk = BIT(CHL_INT1_DMAC_TX_AXI_RD_ERR_OFF),
1651 		.msg = "dma_tx_axi_rd_err",
1652 	},
1653 	{
1654 		.irq_msk = BIT(CHL_INT1_DMAC_RX_AXI_WR_ERR_OFF),
1655 		.msg = "dma_rx_axi_wr_err",
1656 	},
1657 	{
1658 		.irq_msk = BIT(CHL_INT1_DMAC_RX_AXI_RD_ERR_OFF),
1659 		.msg = "dma_rx_axi_rd_err",
1660 	},
1661 	{
1662 		.irq_msk = BIT(CHL_INT1_DMAC_TX_FIFO_ERR_OFF),
1663 		.msg = "dma_tx_fifo_err",
1664 	},
1665 	{
1666 		.irq_msk = BIT(CHL_INT1_DMAC_RX_FIFO_ERR_OFF),
1667 		.msg = "dma_rx_fifo_err",
1668 	},
1669 	{
1670 		.irq_msk = BIT(CHL_INT1_DMAC_TX_AXI_RUSER_ERR_OFF),
1671 		.msg = "dma_tx_axi_ruser_err",
1672 	},
1673 	{
1674 		.irq_msk = BIT(CHL_INT1_DMAC_RX_AXI_RUSER_ERR_OFF),
1675 		.msg = "dma_rx_axi_ruser_err",
1676 	},
1677 };
1678 
1679 static void handle_chl_int1_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1680 {
1681 	u32 irq_value = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT1);
1682 	u32 irq_msk = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT1_MSK);
1683 	struct device *dev = hisi_hba->dev;
1684 	int i;
1685 
1686 	irq_value &= ~irq_msk;
1687 	if (!irq_value)
1688 		return;
1689 
1690 	for (i = 0; i < ARRAY_SIZE(port_axi_error); i++) {
1691 		const struct hisi_sas_hw_error *error = &port_axi_error[i];
1692 
1693 		if (!(irq_value & error->irq_msk))
1694 			continue;
1695 
1696 		dev_err(dev, "%s error (phy%d 0x%x) found!\n",
1697 			error->msg, phy_no, irq_value);
1698 		queue_work(hisi_hba->wq, &hisi_hba->rst_work);
1699 	}
1700 
1701 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT1, irq_value);
1702 }
1703 
1704 static void phy_get_events_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1705 {
1706 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1707 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1708 	struct sas_phy *sphy = sas_phy->phy;
1709 	unsigned long flags;
1710 	u32 reg_value;
1711 
1712 	spin_lock_irqsave(&phy->lock, flags);
1713 
1714 	/* loss dword sync */
1715 	reg_value = hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_DWS_LOST);
1716 	sphy->loss_of_dword_sync_count += reg_value;
1717 
1718 	/* phy reset problem */
1719 	reg_value = hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_RESET_PROB);
1720 	sphy->phy_reset_problem_count += reg_value;
1721 
1722 	/* invalid dword */
1723 	reg_value = hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_INVLD_DW);
1724 	sphy->invalid_dword_count += reg_value;
1725 
1726 	/* disparity err */
1727 	reg_value = hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_DISP_ERR);
1728 	sphy->running_disparity_error_count += reg_value;
1729 
1730 	/* code violation error */
1731 	reg_value = hisi_sas_phy_read32(hisi_hba, phy_no, ERR_CNT_CODE_ERR);
1732 	phy->code_violation_err_count += reg_value;
1733 
1734 	spin_unlock_irqrestore(&phy->lock, flags);
1735 }
1736 
1737 static void handle_chl_int2_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1738 {
1739 	u32 irq_msk = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT2_MSK);
1740 	u32 irq_value = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT2);
1741 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1742 	struct pci_dev *pci_dev = hisi_hba->pci_dev;
1743 	struct device *dev = hisi_hba->dev;
1744 	static const u32 msk = BIT(CHL_INT2_RX_DISP_ERR_OFF) |
1745 			BIT(CHL_INT2_RX_CODE_ERR_OFF) |
1746 			BIT(CHL_INT2_RX_INVLD_DW_OFF);
1747 
1748 	irq_value &= ~irq_msk;
1749 	if (!irq_value)
1750 		return;
1751 
1752 	if (irq_value & BIT(CHL_INT2_SL_IDAF_TOUT_CONF_OFF)) {
1753 		dev_warn(dev, "phy%d identify timeout\n", phy_no);
1754 		hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
1755 	}
1756 
1757 	if (irq_value & BIT(CHL_INT2_STP_LINK_TIMEOUT_OFF)) {
1758 		u32 reg_value = hisi_sas_phy_read32(hisi_hba, phy_no,
1759 				STP_LINK_TIMEOUT_STATE);
1760 
1761 		dev_warn(dev, "phy%d stp link timeout (0x%x)\n",
1762 			 phy_no, reg_value);
1763 		if (reg_value & BIT(4))
1764 			hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
1765 	}
1766 
1767 	if (pci_dev->revision > 0x20 && (irq_value & msk)) {
1768 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
1769 		struct sas_phy *sphy = sas_phy->phy;
1770 
1771 		phy_get_events_v3_hw(hisi_hba, phy_no);
1772 
1773 		if (irq_value & BIT(CHL_INT2_RX_INVLD_DW_OFF))
1774 			dev_info(dev, "phy%d invalid dword cnt:   %u\n", phy_no,
1775 				 sphy->invalid_dword_count);
1776 
1777 		if (irq_value & BIT(CHL_INT2_RX_CODE_ERR_OFF))
1778 			dev_info(dev, "phy%d code violation cnt:  %u\n", phy_no,
1779 				 phy->code_violation_err_count);
1780 
1781 		if (irq_value & BIT(CHL_INT2_RX_DISP_ERR_OFF))
1782 			dev_info(dev, "phy%d disparity error cnt: %u\n", phy_no,
1783 				 sphy->running_disparity_error_count);
1784 	}
1785 
1786 	if ((irq_value & BIT(CHL_INT2_RX_INVLD_DW_OFF)) &&
1787 	    (pci_dev->revision == 0x20)) {
1788 		u32 reg_value;
1789 		int rc;
1790 
1791 		rc = hisi_sas_read32_poll_timeout_atomic(
1792 				HILINK_ERR_DFX, reg_value,
1793 				!((reg_value >> 8) & BIT(phy_no)),
1794 				1000, 10000);
1795 		if (rc)
1796 			hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
1797 	}
1798 
1799 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2, irq_value);
1800 }
1801 
1802 static void handle_chl_int0_v3_hw(struct hisi_hba *hisi_hba, int phy_no)
1803 {
1804 	u32 irq_value0 = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT0);
1805 
1806 	if (irq_value0 & CHL_INT0_PHY_RDY_MSK)
1807 		hisi_sas_phy_oob_ready(hisi_hba, phy_no);
1808 
1809 	hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
1810 			     irq_value0 & (~CHL_INT0_SL_RX_BCST_ACK_MSK)
1811 			     & (~CHL_INT0_SL_PHY_ENABLE_MSK)
1812 			     & (~CHL_INT0_NOT_RDY_MSK));
1813 }
1814 
1815 static irqreturn_t int_chnl_int_v3_hw(int irq_no, void *p)
1816 {
1817 	struct hisi_hba *hisi_hba = p;
1818 	u32 irq_msk;
1819 	int phy_no = 0;
1820 
1821 	irq_msk = hisi_sas_read32(hisi_hba, CHNL_INT_STATUS)
1822 				& 0xeeeeeeee;
1823 
1824 	while (irq_msk) {
1825 		if (irq_msk & (2 << (phy_no * 4)))
1826 			handle_chl_int0_v3_hw(hisi_hba, phy_no);
1827 
1828 		if (irq_msk & (4 << (phy_no * 4)))
1829 			handle_chl_int1_v3_hw(hisi_hba, phy_no);
1830 
1831 		if (irq_msk & (8 << (phy_no * 4)))
1832 			handle_chl_int2_v3_hw(hisi_hba, phy_no);
1833 
1834 		irq_msk &= ~(0xe << (phy_no * 4));
1835 		phy_no++;
1836 	}
1837 
1838 	return IRQ_HANDLED;
1839 }
1840 
1841 static const struct hisi_sas_hw_error multi_bit_ecc_errors[] = {
1842 	{
1843 		.irq_msk = BIT(SAS_ECC_INTR_DQE_ECC_MB_OFF),
1844 		.msk = HGC_DQE_ECC_MB_ADDR_MSK,
1845 		.shift = HGC_DQE_ECC_MB_ADDR_OFF,
1846 		.msg = "hgc_dqe_eccbad_intr",
1847 		.reg = HGC_DQE_ECC_ADDR,
1848 	},
1849 	{
1850 		.irq_msk = BIT(SAS_ECC_INTR_IOST_ECC_MB_OFF),
1851 		.msk = HGC_IOST_ECC_MB_ADDR_MSK,
1852 		.shift = HGC_IOST_ECC_MB_ADDR_OFF,
1853 		.msg = "hgc_iost_eccbad_intr",
1854 		.reg = HGC_IOST_ECC_ADDR,
1855 	},
1856 	{
1857 		.irq_msk = BIT(SAS_ECC_INTR_ITCT_ECC_MB_OFF),
1858 		.msk = HGC_ITCT_ECC_MB_ADDR_MSK,
1859 		.shift = HGC_ITCT_ECC_MB_ADDR_OFF,
1860 		.msg = "hgc_itct_eccbad_intr",
1861 		.reg = HGC_ITCT_ECC_ADDR,
1862 	},
1863 	{
1864 		.irq_msk = BIT(SAS_ECC_INTR_IOSTLIST_ECC_MB_OFF),
1865 		.msk = HGC_LM_DFX_STATUS2_IOSTLIST_MSK,
1866 		.shift = HGC_LM_DFX_STATUS2_IOSTLIST_OFF,
1867 		.msg = "hgc_iostl_eccbad_intr",
1868 		.reg = HGC_LM_DFX_STATUS2,
1869 	},
1870 	{
1871 		.irq_msk = BIT(SAS_ECC_INTR_ITCTLIST_ECC_MB_OFF),
1872 		.msk = HGC_LM_DFX_STATUS2_ITCTLIST_MSK,
1873 		.shift = HGC_LM_DFX_STATUS2_ITCTLIST_OFF,
1874 		.msg = "hgc_itctl_eccbad_intr",
1875 		.reg = HGC_LM_DFX_STATUS2,
1876 	},
1877 	{
1878 		.irq_msk = BIT(SAS_ECC_INTR_CQE_ECC_MB_OFF),
1879 		.msk = HGC_CQE_ECC_MB_ADDR_MSK,
1880 		.shift = HGC_CQE_ECC_MB_ADDR_OFF,
1881 		.msg = "hgc_cqe_eccbad_intr",
1882 		.reg = HGC_CQE_ECC_ADDR,
1883 	},
1884 	{
1885 		.irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM0_ECC_MB_OFF),
1886 		.msk = HGC_RXM_DFX_STATUS14_MEM0_MSK,
1887 		.shift = HGC_RXM_DFX_STATUS14_MEM0_OFF,
1888 		.msg = "rxm_mem0_eccbad_intr",
1889 		.reg = HGC_RXM_DFX_STATUS14,
1890 	},
1891 	{
1892 		.irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM1_ECC_MB_OFF),
1893 		.msk = HGC_RXM_DFX_STATUS14_MEM1_MSK,
1894 		.shift = HGC_RXM_DFX_STATUS14_MEM1_OFF,
1895 		.msg = "rxm_mem1_eccbad_intr",
1896 		.reg = HGC_RXM_DFX_STATUS14,
1897 	},
1898 	{
1899 		.irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM2_ECC_MB_OFF),
1900 		.msk = HGC_RXM_DFX_STATUS14_MEM2_MSK,
1901 		.shift = HGC_RXM_DFX_STATUS14_MEM2_OFF,
1902 		.msg = "rxm_mem2_eccbad_intr",
1903 		.reg = HGC_RXM_DFX_STATUS14,
1904 	},
1905 	{
1906 		.irq_msk = BIT(SAS_ECC_INTR_NCQ_MEM3_ECC_MB_OFF),
1907 		.msk = HGC_RXM_DFX_STATUS15_MEM3_MSK,
1908 		.shift = HGC_RXM_DFX_STATUS15_MEM3_OFF,
1909 		.msg = "rxm_mem3_eccbad_intr",
1910 		.reg = HGC_RXM_DFX_STATUS15,
1911 	},
1912 	{
1913 		.irq_msk = BIT(SAS_ECC_INTR_OOO_RAM_ECC_MB_OFF),
1914 		.msk = AM_ROB_ECC_ERR_ADDR_MSK,
1915 		.shift = AM_ROB_ECC_ERR_ADDR_OFF,
1916 		.msg = "ooo_ram_eccbad_intr",
1917 		.reg = AM_ROB_ECC_ERR_ADDR,
1918 	},
1919 };
1920 
1921 static void multi_bit_ecc_error_process_v3_hw(struct hisi_hba *hisi_hba,
1922 					      u32 irq_value)
1923 {
1924 	struct device *dev = hisi_hba->dev;
1925 	const struct hisi_sas_hw_error *ecc_error;
1926 	u32 val;
1927 	int i;
1928 
1929 	for (i = 0; i < ARRAY_SIZE(multi_bit_ecc_errors); i++) {
1930 		ecc_error = &multi_bit_ecc_errors[i];
1931 		if (irq_value & ecc_error->irq_msk) {
1932 			val = hisi_sas_read32(hisi_hba, ecc_error->reg);
1933 			val &= ecc_error->msk;
1934 			val >>= ecc_error->shift;
1935 			dev_err(dev, "%s (0x%x) found: mem addr is 0x%08X\n",
1936 				ecc_error->msg, irq_value, val);
1937 			queue_work(hisi_hba->wq, &hisi_hba->rst_work);
1938 		}
1939 	}
1940 }
1941 
1942 static void fatal_ecc_int_v3_hw(struct hisi_hba *hisi_hba)
1943 {
1944 	u32 irq_value, irq_msk;
1945 
1946 	irq_msk = hisi_sas_read32(hisi_hba, SAS_ECC_INTR_MSK);
1947 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xffffffff);
1948 
1949 	irq_value = hisi_sas_read32(hisi_hba, SAS_ECC_INTR);
1950 	if (irq_value)
1951 		multi_bit_ecc_error_process_v3_hw(hisi_hba, irq_value);
1952 
1953 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR, irq_value);
1954 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, irq_msk);
1955 }
1956 
1957 static const struct hisi_sas_hw_error axi_error[] = {
1958 	{ .msk = BIT(0), .msg = "IOST_AXI_W_ERR" },
1959 	{ .msk = BIT(1), .msg = "IOST_AXI_R_ERR" },
1960 	{ .msk = BIT(2), .msg = "ITCT_AXI_W_ERR" },
1961 	{ .msk = BIT(3), .msg = "ITCT_AXI_R_ERR" },
1962 	{ .msk = BIT(4), .msg = "SATA_AXI_W_ERR" },
1963 	{ .msk = BIT(5), .msg = "SATA_AXI_R_ERR" },
1964 	{ .msk = BIT(6), .msg = "DQE_AXI_R_ERR" },
1965 	{ .msk = BIT(7), .msg = "CQE_AXI_W_ERR" },
1966 	{}
1967 };
1968 
1969 static const struct hisi_sas_hw_error fifo_error[] = {
1970 	{ .msk = BIT(8),  .msg = "CQE_WINFO_FIFO" },
1971 	{ .msk = BIT(9),  .msg = "CQE_MSG_FIFIO" },
1972 	{ .msk = BIT(10), .msg = "GETDQE_FIFO" },
1973 	{ .msk = BIT(11), .msg = "CMDP_FIFO" },
1974 	{ .msk = BIT(12), .msg = "AWTCTRL_FIFO" },
1975 	{}
1976 };
1977 
1978 static const struct hisi_sas_hw_error fatal_axi_error[] = {
1979 	{
1980 		.irq_msk = BIT(ENT_INT_SRC3_WP_DEPTH_OFF),
1981 		.msg = "write pointer and depth",
1982 	},
1983 	{
1984 		.irq_msk = BIT(ENT_INT_SRC3_IPTT_SLOT_NOMATCH_OFF),
1985 		.msg = "iptt no match slot",
1986 	},
1987 	{
1988 		.irq_msk = BIT(ENT_INT_SRC3_RP_DEPTH_OFF),
1989 		.msg = "read pointer and depth",
1990 	},
1991 	{
1992 		.irq_msk = BIT(ENT_INT_SRC3_AXI_OFF),
1993 		.reg = HGC_AXI_FIFO_ERR_INFO,
1994 		.sub = axi_error,
1995 	},
1996 	{
1997 		.irq_msk = BIT(ENT_INT_SRC3_FIFO_OFF),
1998 		.reg = HGC_AXI_FIFO_ERR_INFO,
1999 		.sub = fifo_error,
2000 	},
2001 	{
2002 		.irq_msk = BIT(ENT_INT_SRC3_LM_OFF),
2003 		.msg = "LM add/fetch list",
2004 	},
2005 	{
2006 		.irq_msk = BIT(ENT_INT_SRC3_ABT_OFF),
2007 		.msg = "SAS_HGC_ABT fetch LM list",
2008 	},
2009 	{
2010 		.irq_msk = BIT(ENT_INT_SRC3_DQE_POISON_OFF),
2011 		.msg = "read dqe poison",
2012 	},
2013 	{
2014 		.irq_msk = BIT(ENT_INT_SRC3_IOST_POISON_OFF),
2015 		.msg = "read iost poison",
2016 	},
2017 	{
2018 		.irq_msk = BIT(ENT_INT_SRC3_ITCT_POISON_OFF),
2019 		.msg = "read itct poison",
2020 	},
2021 	{
2022 		.irq_msk = BIT(ENT_INT_SRC3_ITCT_NCQ_POISON_OFF),
2023 		.msg = "read itct ncq poison",
2024 	},
2025 
2026 };
2027 
2028 static irqreturn_t fatal_axi_int_v3_hw(int irq_no, void *p)
2029 {
2030 	u32 irq_value, irq_msk;
2031 	struct hisi_hba *hisi_hba = p;
2032 	struct device *dev = hisi_hba->dev;
2033 	struct pci_dev *pdev = hisi_hba->pci_dev;
2034 	int i;
2035 
2036 	irq_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK3);
2037 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, irq_msk | 0x1df00);
2038 
2039 	irq_value = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
2040 	irq_value &= ~irq_msk;
2041 
2042 	for (i = 0; i < ARRAY_SIZE(fatal_axi_error); i++) {
2043 		const struct hisi_sas_hw_error *error = &fatal_axi_error[i];
2044 
2045 		if (!(irq_value & error->irq_msk))
2046 			continue;
2047 
2048 		if (error->sub) {
2049 			const struct hisi_sas_hw_error *sub = error->sub;
2050 			u32 err_value = hisi_sas_read32(hisi_hba, error->reg);
2051 
2052 			for (; sub->msk || sub->msg; sub++) {
2053 				if (!(err_value & sub->msk))
2054 					continue;
2055 
2056 				dev_err(dev, "%s error (0x%x) found!\n",
2057 					sub->msg, irq_value);
2058 				queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2059 			}
2060 		} else {
2061 			dev_err(dev, "%s error (0x%x) found!\n",
2062 				error->msg, irq_value);
2063 			queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2064 		}
2065 
2066 		if (pdev->revision < 0x21) {
2067 			u32 reg_val;
2068 
2069 			reg_val = hisi_sas_read32(hisi_hba,
2070 						  AXI_MASTER_CFG_BASE +
2071 						  AM_CTRL_GLOBAL);
2072 			reg_val |= AM_CTRL_SHUTDOWN_REQ_MSK;
2073 			hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE +
2074 					 AM_CTRL_GLOBAL, reg_val);
2075 		}
2076 	}
2077 
2078 	fatal_ecc_int_v3_hw(hisi_hba);
2079 
2080 	if (irq_value & BIT(ENT_INT_SRC3_ITC_INT_OFF)) {
2081 		u32 reg_val = hisi_sas_read32(hisi_hba, ITCT_CLR);
2082 		u32 dev_id = reg_val & ITCT_DEV_MSK;
2083 		struct hisi_sas_device *sas_dev =
2084 				&hisi_hba->devices[dev_id];
2085 
2086 		hisi_sas_write32(hisi_hba, ITCT_CLR, 0);
2087 		dev_dbg(dev, "clear ITCT ok\n");
2088 		complete(sas_dev->completion);
2089 	}
2090 
2091 	hisi_sas_write32(hisi_hba, ENT_INT_SRC3, irq_value & 0x1df00);
2092 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, irq_msk);
2093 
2094 	return IRQ_HANDLED;
2095 }
2096 
2097 static void
2098 slot_err_v3_hw(struct hisi_hba *hisi_hba, struct sas_task *task,
2099 	       struct hisi_sas_slot *slot)
2100 {
2101 	struct task_status_struct *ts = &task->task_status;
2102 	struct hisi_sas_complete_v3_hdr *complete_queue =
2103 			hisi_hba->complete_hdr[slot->cmplt_queue];
2104 	struct hisi_sas_complete_v3_hdr *complete_hdr =
2105 			&complete_queue[slot->cmplt_queue_slot];
2106 	struct hisi_sas_err_record_v3 *record =
2107 			hisi_sas_status_buf_addr_mem(slot);
2108 	u32 dma_rx_err_type = le32_to_cpu(record->dma_rx_err_type);
2109 	u32 trans_tx_fail_type = le32_to_cpu(record->trans_tx_fail_type);
2110 	u32 dw3 = le32_to_cpu(complete_hdr->dw3);
2111 
2112 	switch (task->task_proto) {
2113 	case SAS_PROTOCOL_SSP:
2114 		if (dma_rx_err_type & RX_DATA_LEN_UNDERFLOW_MSK) {
2115 			ts->residual = trans_tx_fail_type;
2116 			ts->stat = SAS_DATA_UNDERRUN;
2117 		} else if (dw3 & CMPLT_HDR_IO_IN_TARGET_MSK) {
2118 			ts->stat = SAS_QUEUE_FULL;
2119 			slot->abort = 1;
2120 		} else {
2121 			ts->stat = SAS_OPEN_REJECT;
2122 			ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
2123 		}
2124 		break;
2125 	case SAS_PROTOCOL_SATA:
2126 	case SAS_PROTOCOL_STP:
2127 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
2128 		if (dma_rx_err_type & RX_DATA_LEN_UNDERFLOW_MSK) {
2129 			ts->residual = trans_tx_fail_type;
2130 			ts->stat = SAS_DATA_UNDERRUN;
2131 		} else if (dw3 & CMPLT_HDR_IO_IN_TARGET_MSK) {
2132 			ts->stat = SAS_PHY_DOWN;
2133 			slot->abort = 1;
2134 		} else {
2135 			ts->stat = SAS_OPEN_REJECT;
2136 			ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
2137 		}
2138 		hisi_sas_sata_done(task, slot);
2139 		break;
2140 	case SAS_PROTOCOL_SMP:
2141 		ts->stat = SAM_STAT_CHECK_CONDITION;
2142 		break;
2143 	default:
2144 		break;
2145 	}
2146 }
2147 
2148 static int
2149 slot_complete_v3_hw(struct hisi_hba *hisi_hba, struct hisi_sas_slot *slot)
2150 {
2151 	struct sas_task *task = slot->task;
2152 	struct hisi_sas_device *sas_dev;
2153 	struct device *dev = hisi_hba->dev;
2154 	struct task_status_struct *ts;
2155 	struct domain_device *device;
2156 	struct sas_ha_struct *ha;
2157 	enum exec_status sts;
2158 	struct hisi_sas_complete_v3_hdr *complete_queue =
2159 			hisi_hba->complete_hdr[slot->cmplt_queue];
2160 	struct hisi_sas_complete_v3_hdr *complete_hdr =
2161 			&complete_queue[slot->cmplt_queue_slot];
2162 	unsigned long flags;
2163 	bool is_internal = slot->is_internal;
2164 	u32 dw0, dw1, dw3;
2165 
2166 	if (unlikely(!task || !task->lldd_task || !task->dev))
2167 		return -EINVAL;
2168 
2169 	ts = &task->task_status;
2170 	device = task->dev;
2171 	ha = device->port->ha;
2172 	sas_dev = device->lldd_dev;
2173 
2174 	spin_lock_irqsave(&task->task_state_lock, flags);
2175 	task->task_state_flags &=
2176 		~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
2177 	spin_unlock_irqrestore(&task->task_state_lock, flags);
2178 
2179 	memset(ts, 0, sizeof(*ts));
2180 	ts->resp = SAS_TASK_COMPLETE;
2181 
2182 	if (unlikely(!sas_dev)) {
2183 		dev_dbg(dev, "slot complete: port has not device\n");
2184 		ts->stat = SAS_PHY_DOWN;
2185 		goto out;
2186 	}
2187 
2188 	dw0 = le32_to_cpu(complete_hdr->dw0);
2189 	dw1 = le32_to_cpu(complete_hdr->dw1);
2190 	dw3 = le32_to_cpu(complete_hdr->dw3);
2191 
2192 	/*
2193 	 * Use SAS+TMF status codes
2194 	 */
2195 	switch ((dw0 & CMPLT_HDR_ABORT_STAT_MSK) >> CMPLT_HDR_ABORT_STAT_OFF) {
2196 	case STAT_IO_ABORTED:
2197 		/* this IO has been aborted by abort command */
2198 		ts->stat = SAS_ABORTED_TASK;
2199 		goto out;
2200 	case STAT_IO_COMPLETE:
2201 		/* internal abort command complete */
2202 		ts->stat = TMF_RESP_FUNC_SUCC;
2203 		goto out;
2204 	case STAT_IO_NO_DEVICE:
2205 		ts->stat = TMF_RESP_FUNC_COMPLETE;
2206 		goto out;
2207 	case STAT_IO_NOT_VALID:
2208 		/*
2209 		 * abort single IO, the controller can't find the IO
2210 		 */
2211 		ts->stat = TMF_RESP_FUNC_FAILED;
2212 		goto out;
2213 	default:
2214 		break;
2215 	}
2216 
2217 	/* check for erroneous completion */
2218 	if ((dw0 & CMPLT_HDR_CMPLT_MSK) == 0x3) {
2219 		u32 *error_info = hisi_sas_status_buf_addr_mem(slot);
2220 
2221 		slot_err_v3_hw(hisi_hba, task, slot);
2222 		if (ts->stat != SAS_DATA_UNDERRUN)
2223 			dev_info(dev, "erroneous completion iptt=%d task=%pK dev id=%d CQ hdr: 0x%x 0x%x 0x%x 0x%x Error info: 0x%x 0x%x 0x%x 0x%x\n",
2224 				 slot->idx, task, sas_dev->device_id,
2225 				 dw0, dw1, complete_hdr->act, dw3,
2226 				 error_info[0], error_info[1],
2227 				 error_info[2], error_info[3]);
2228 		if (unlikely(slot->abort))
2229 			return ts->stat;
2230 		goto out;
2231 	}
2232 
2233 	switch (task->task_proto) {
2234 	case SAS_PROTOCOL_SSP: {
2235 		struct ssp_response_iu *iu =
2236 			hisi_sas_status_buf_addr_mem(slot) +
2237 			sizeof(struct hisi_sas_err_record);
2238 
2239 		sas_ssp_task_response(dev, task, iu);
2240 		break;
2241 	}
2242 	case SAS_PROTOCOL_SMP: {
2243 		struct scatterlist *sg_resp = &task->smp_task.smp_resp;
2244 		void *to = page_address(sg_page(sg_resp));
2245 
2246 		ts->stat = SAM_STAT_GOOD;
2247 
2248 		dma_unmap_sg(dev, &task->smp_task.smp_req, 1,
2249 			     DMA_TO_DEVICE);
2250 		memcpy(to + sg_resp->offset,
2251 			hisi_sas_status_buf_addr_mem(slot) +
2252 		       sizeof(struct hisi_sas_err_record),
2253 		       sg_resp->length);
2254 		break;
2255 	}
2256 	case SAS_PROTOCOL_SATA:
2257 	case SAS_PROTOCOL_STP:
2258 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
2259 		ts->stat = SAM_STAT_GOOD;
2260 		hisi_sas_sata_done(task, slot);
2261 		break;
2262 	default:
2263 		ts->stat = SAM_STAT_CHECK_CONDITION;
2264 		break;
2265 	}
2266 
2267 	if (!slot->port->port_attached) {
2268 		dev_warn(dev, "slot complete: port %d has removed\n",
2269 			slot->port->sas_port.id);
2270 		ts->stat = SAS_PHY_DOWN;
2271 	}
2272 
2273 out:
2274 	sts = ts->stat;
2275 	spin_lock_irqsave(&task->task_state_lock, flags);
2276 	if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
2277 		spin_unlock_irqrestore(&task->task_state_lock, flags);
2278 		dev_info(dev, "slot complete: task(%pK) aborted\n", task);
2279 		return SAS_ABORTED_TASK;
2280 	}
2281 	task->task_state_flags |= SAS_TASK_STATE_DONE;
2282 	spin_unlock_irqrestore(&task->task_state_lock, flags);
2283 	hisi_sas_slot_task_free(hisi_hba, task, slot);
2284 
2285 	if (!is_internal && (task->task_proto != SAS_PROTOCOL_SMP)) {
2286 		spin_lock_irqsave(&device->done_lock, flags);
2287 		if (test_bit(SAS_HA_FROZEN, &ha->state)) {
2288 			spin_unlock_irqrestore(&device->done_lock, flags);
2289 			dev_info(dev, "slot complete: task(%pK) ignored\n ",
2290 				 task);
2291 			return sts;
2292 		}
2293 		spin_unlock_irqrestore(&device->done_lock, flags);
2294 	}
2295 
2296 	if (task->task_done)
2297 		task->task_done(task);
2298 
2299 	return sts;
2300 }
2301 
2302 static void cq_tasklet_v3_hw(unsigned long val)
2303 {
2304 	struct hisi_sas_cq *cq = (struct hisi_sas_cq *)val;
2305 	struct hisi_hba *hisi_hba = cq->hisi_hba;
2306 	struct hisi_sas_slot *slot;
2307 	struct hisi_sas_complete_v3_hdr *complete_queue;
2308 	u32 rd_point = cq->rd_point, wr_point;
2309 	int queue = cq->id;
2310 
2311 	complete_queue = hisi_hba->complete_hdr[queue];
2312 
2313 	wr_point = hisi_sas_read32(hisi_hba, COMPL_Q_0_WR_PTR +
2314 				   (0x14 * queue));
2315 
2316 	while (rd_point != wr_point) {
2317 		struct hisi_sas_complete_v3_hdr *complete_hdr;
2318 		struct device *dev = hisi_hba->dev;
2319 		u32 dw1;
2320 		int iptt;
2321 
2322 		complete_hdr = &complete_queue[rd_point];
2323 		dw1 = le32_to_cpu(complete_hdr->dw1);
2324 
2325 		iptt = dw1 & CMPLT_HDR_IPTT_MSK;
2326 		if (likely(iptt < HISI_SAS_COMMAND_ENTRIES_V3_HW)) {
2327 			slot = &hisi_hba->slot_info[iptt];
2328 			slot->cmplt_queue_slot = rd_point;
2329 			slot->cmplt_queue = queue;
2330 			slot_complete_v3_hw(hisi_hba, slot);
2331 		} else
2332 			dev_err(dev, "IPTT %d is invalid, discard it.\n", iptt);
2333 
2334 		if (++rd_point >= HISI_SAS_QUEUE_SLOTS)
2335 			rd_point = 0;
2336 	}
2337 
2338 	/* update rd_point */
2339 	cq->rd_point = rd_point;
2340 	hisi_sas_write32(hisi_hba, COMPL_Q_0_RD_PTR + (0x14 * queue), rd_point);
2341 }
2342 
2343 static irqreturn_t cq_interrupt_v3_hw(int irq_no, void *p)
2344 {
2345 	struct hisi_sas_cq *cq = p;
2346 	struct hisi_hba *hisi_hba = cq->hisi_hba;
2347 	int queue = cq->id;
2348 
2349 	hisi_sas_write32(hisi_hba, OQ_INT_SRC, 1 << queue);
2350 
2351 	tasklet_schedule(&cq->tasklet);
2352 
2353 	return IRQ_HANDLED;
2354 }
2355 
2356 static void setup_reply_map_v3_hw(struct hisi_hba *hisi_hba, int nvecs)
2357 {
2358 	const struct cpumask *mask;
2359 	int queue, cpu;
2360 
2361 	for (queue = 0; queue < nvecs; queue++) {
2362 		struct hisi_sas_cq *cq = &hisi_hba->cq[queue];
2363 
2364 		mask = pci_irq_get_affinity(hisi_hba->pci_dev, queue +
2365 					    BASE_VECTORS_V3_HW);
2366 		if (!mask)
2367 			goto fallback;
2368 		cq->pci_irq_mask = mask;
2369 		for_each_cpu(cpu, mask)
2370 			hisi_hba->reply_map[cpu] = queue;
2371 	}
2372 	return;
2373 
2374 fallback:
2375 	for_each_possible_cpu(cpu)
2376 		hisi_hba->reply_map[cpu] = cpu % hisi_hba->queue_count;
2377 	/* Don't clean all CQ masks */
2378 }
2379 
2380 static int interrupt_init_v3_hw(struct hisi_hba *hisi_hba)
2381 {
2382 	struct device *dev = hisi_hba->dev;
2383 	struct pci_dev *pdev = hisi_hba->pci_dev;
2384 	int vectors, rc, i;
2385 	int max_msi = HISI_SAS_MSI_COUNT_V3_HW, min_msi;
2386 
2387 	if (auto_affine_msi_experimental) {
2388 		struct irq_affinity desc = {
2389 			.pre_vectors = BASE_VECTORS_V3_HW,
2390 		};
2391 
2392 		min_msi = MIN_AFFINE_VECTORS_V3_HW;
2393 
2394 		hisi_hba->reply_map = devm_kcalloc(dev, nr_cpu_ids,
2395 						   sizeof(unsigned int),
2396 						   GFP_KERNEL);
2397 		if (!hisi_hba->reply_map)
2398 			return -ENOMEM;
2399 		vectors = pci_alloc_irq_vectors_affinity(hisi_hba->pci_dev,
2400 							 min_msi, max_msi,
2401 							 PCI_IRQ_MSI |
2402 							 PCI_IRQ_AFFINITY,
2403 							 &desc);
2404 		if (vectors < 0)
2405 			return -ENOENT;
2406 		setup_reply_map_v3_hw(hisi_hba, vectors - BASE_VECTORS_V3_HW);
2407 	} else {
2408 		min_msi = max_msi;
2409 		vectors = pci_alloc_irq_vectors(hisi_hba->pci_dev, min_msi,
2410 						max_msi, PCI_IRQ_MSI);
2411 		if (vectors < 0)
2412 			return vectors;
2413 	}
2414 
2415 	hisi_hba->cq_nvecs = vectors - BASE_VECTORS_V3_HW;
2416 
2417 	rc = devm_request_irq(dev, pci_irq_vector(pdev, 1),
2418 			      int_phy_up_down_bcast_v3_hw, 0,
2419 			      DRV_NAME " phy", hisi_hba);
2420 	if (rc) {
2421 		dev_err(dev, "could not request phy interrupt, rc=%d\n", rc);
2422 		rc = -ENOENT;
2423 		goto free_irq_vectors;
2424 	}
2425 
2426 	rc = devm_request_irq(dev, pci_irq_vector(pdev, 2),
2427 			      int_chnl_int_v3_hw, 0,
2428 			      DRV_NAME " channel", hisi_hba);
2429 	if (rc) {
2430 		dev_err(dev, "could not request chnl interrupt, rc=%d\n", rc);
2431 		rc = -ENOENT;
2432 		goto free_irq_vectors;
2433 	}
2434 
2435 	rc = devm_request_irq(dev, pci_irq_vector(pdev, 11),
2436 			      fatal_axi_int_v3_hw, 0,
2437 			      DRV_NAME " fatal", hisi_hba);
2438 	if (rc) {
2439 		dev_err(dev, "could not request fatal interrupt, rc=%d\n", rc);
2440 		rc = -ENOENT;
2441 		goto free_irq_vectors;
2442 	}
2443 
2444 	/* Init tasklets for cq only */
2445 	for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2446 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2447 		struct tasklet_struct *t = &cq->tasklet;
2448 		int nr = hisi_sas_intr_conv ? 16 : 16 + i;
2449 		unsigned long irqflags = hisi_sas_intr_conv ? IRQF_SHARED : 0;
2450 
2451 		rc = devm_request_irq(dev, pci_irq_vector(pdev, nr),
2452 				      cq_interrupt_v3_hw, irqflags,
2453 				      DRV_NAME " cq", cq);
2454 		if (rc) {
2455 			dev_err(dev, "could not request cq%d interrupt, rc=%d\n",
2456 				i, rc);
2457 			rc = -ENOENT;
2458 			goto free_irq_vectors;
2459 		}
2460 
2461 		tasklet_init(t, cq_tasklet_v3_hw, (unsigned long)cq);
2462 	}
2463 
2464 	return 0;
2465 
2466 free_irq_vectors:
2467 	pci_free_irq_vectors(pdev);
2468 	return rc;
2469 }
2470 
2471 static int hisi_sas_v3_init(struct hisi_hba *hisi_hba)
2472 {
2473 	int rc;
2474 
2475 	rc = hw_init_v3_hw(hisi_hba);
2476 	if (rc)
2477 		return rc;
2478 
2479 	rc = interrupt_init_v3_hw(hisi_hba);
2480 	if (rc)
2481 		return rc;
2482 
2483 	return 0;
2484 }
2485 
2486 static void phy_set_linkrate_v3_hw(struct hisi_hba *hisi_hba, int phy_no,
2487 		struct sas_phy_linkrates *r)
2488 {
2489 	enum sas_linkrate max = r->maximum_linkrate;
2490 	u32 prog_phy_link_rate = 0x800;
2491 
2492 	prog_phy_link_rate |= hisi_sas_get_prog_phy_linkrate_mask(max);
2493 	hisi_sas_phy_write32(hisi_hba, phy_no, PROG_PHY_LINK_RATE,
2494 			     prog_phy_link_rate);
2495 }
2496 
2497 static void interrupt_disable_v3_hw(struct hisi_hba *hisi_hba)
2498 {
2499 	struct pci_dev *pdev = hisi_hba->pci_dev;
2500 	int i;
2501 
2502 	synchronize_irq(pci_irq_vector(pdev, 1));
2503 	synchronize_irq(pci_irq_vector(pdev, 2));
2504 	synchronize_irq(pci_irq_vector(pdev, 11));
2505 	for (i = 0; i < hisi_hba->queue_count; i++) {
2506 		hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK + 0x4 * i, 0x1);
2507 		synchronize_irq(pci_irq_vector(pdev, i + 16));
2508 	}
2509 
2510 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0xffffffff);
2511 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0xffffffff);
2512 	hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0xffffffff);
2513 	hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xffffffff);
2514 
2515 	for (i = 0; i < hisi_hba->n_phy; i++) {
2516 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xffffffff);
2517 		hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0xffffffff);
2518 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_NOT_RDY_MSK, 0x1);
2519 		hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_PHY_ENA_MSK, 0x1);
2520 		hisi_sas_phy_write32(hisi_hba, i, SL_RX_BCAST_CHK_MSK, 0x1);
2521 	}
2522 }
2523 
2524 static u32 get_phys_state_v3_hw(struct hisi_hba *hisi_hba)
2525 {
2526 	return hisi_sas_read32(hisi_hba, PHY_STATE);
2527 }
2528 
2529 static int disable_host_v3_hw(struct hisi_hba *hisi_hba)
2530 {
2531 	struct device *dev = hisi_hba->dev;
2532 	u32 status, reg_val;
2533 	int rc;
2534 
2535 	interrupt_disable_v3_hw(hisi_hba);
2536 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0x0);
2537 	hisi_sas_kill_tasklets(hisi_hba);
2538 
2539 	hisi_sas_stop_phys(hisi_hba);
2540 
2541 	mdelay(10);
2542 
2543 	reg_val = hisi_sas_read32(hisi_hba, AXI_MASTER_CFG_BASE +
2544 				  AM_CTRL_GLOBAL);
2545 	reg_val |= AM_CTRL_SHUTDOWN_REQ_MSK;
2546 	hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE +
2547 			 AM_CTRL_GLOBAL, reg_val);
2548 
2549 	/* wait until bus idle */
2550 	rc = hisi_sas_read32_poll_timeout(AXI_MASTER_CFG_BASE +
2551 					  AM_CURR_TRANS_RETURN, status,
2552 					  status == 0x3, 10, 100);
2553 	if (rc) {
2554 		dev_err(dev, "axi bus is not idle, rc=%d\n", rc);
2555 		return rc;
2556 	}
2557 
2558 	return 0;
2559 }
2560 
2561 static int soft_reset_v3_hw(struct hisi_hba *hisi_hba)
2562 {
2563 	struct device *dev = hisi_hba->dev;
2564 	int rc;
2565 
2566 	rc = disable_host_v3_hw(hisi_hba);
2567 	if (rc) {
2568 		dev_err(dev, "soft reset: disable host failed rc=%d\n", rc);
2569 		return rc;
2570 	}
2571 
2572 	hisi_sas_init_mem(hisi_hba);
2573 
2574 	return hw_init_v3_hw(hisi_hba);
2575 }
2576 
2577 static int write_gpio_v3_hw(struct hisi_hba *hisi_hba, u8 reg_type,
2578 			u8 reg_index, u8 reg_count, u8 *write_data)
2579 {
2580 	struct device *dev = hisi_hba->dev;
2581 	u32 *data = (u32 *)write_data;
2582 	int i;
2583 
2584 	switch (reg_type) {
2585 	case SAS_GPIO_REG_TX:
2586 		if ((reg_index + reg_count) > ((hisi_hba->n_phy + 3) / 4)) {
2587 			dev_err(dev, "write gpio: invalid reg range[%d, %d]\n",
2588 				reg_index, reg_index + reg_count - 1);
2589 			return -EINVAL;
2590 		}
2591 
2592 		for (i = 0; i < reg_count; i++)
2593 			hisi_sas_write32(hisi_hba,
2594 					 SAS_GPIO_TX_0_1 + (reg_index + i) * 4,
2595 					 data[i]);
2596 		break;
2597 	default:
2598 		dev_err(dev, "write gpio: unsupported or bad reg type %d\n",
2599 			reg_type);
2600 		return -EINVAL;
2601 	}
2602 
2603 	return 0;
2604 }
2605 
2606 static void wait_cmds_complete_timeout_v3_hw(struct hisi_hba *hisi_hba,
2607 					     int delay_ms, int timeout_ms)
2608 {
2609 	struct device *dev = hisi_hba->dev;
2610 	int entries, entries_old = 0, time;
2611 
2612 	for (time = 0; time < timeout_ms; time += delay_ms) {
2613 		entries = hisi_sas_read32(hisi_hba, CQE_SEND_CNT);
2614 		if (entries == entries_old)
2615 			break;
2616 
2617 		entries_old = entries;
2618 		msleep(delay_ms);
2619 	}
2620 
2621 	if (time >= timeout_ms) {
2622 		dev_dbg(dev, "Wait commands complete timeout!\n");
2623 		return;
2624 	}
2625 
2626 	dev_dbg(dev, "wait commands complete %dms\n", time);
2627 }
2628 
2629 static ssize_t intr_conv_v3_hw_show(struct device *dev,
2630 				    struct device_attribute *attr, char *buf)
2631 {
2632 	return scnprintf(buf, PAGE_SIZE, "%u\n", hisi_sas_intr_conv);
2633 }
2634 static DEVICE_ATTR_RO(intr_conv_v3_hw);
2635 
2636 static void config_intr_coal_v3_hw(struct hisi_hba *hisi_hba)
2637 {
2638 	/* config those registers between enable and disable PHYs */
2639 	hisi_sas_stop_phys(hisi_hba);
2640 
2641 	if (hisi_hba->intr_coal_ticks == 0 ||
2642 	    hisi_hba->intr_coal_count == 0) {
2643 		hisi_sas_write32(hisi_hba, INT_COAL_EN, 0x1);
2644 		hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x1);
2645 		hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 0x1);
2646 	} else {
2647 		hisi_sas_write32(hisi_hba, INT_COAL_EN, 0x3);
2648 		hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME,
2649 				 hisi_hba->intr_coal_ticks);
2650 		hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT,
2651 				 hisi_hba->intr_coal_count);
2652 	}
2653 	phys_init_v3_hw(hisi_hba);
2654 }
2655 
2656 static ssize_t intr_coal_ticks_v3_hw_show(struct device *dev,
2657 					  struct device_attribute *attr,
2658 					  char *buf)
2659 {
2660 	struct Scsi_Host *shost = class_to_shost(dev);
2661 	struct hisi_hba *hisi_hba = shost_priv(shost);
2662 
2663 	return scnprintf(buf, PAGE_SIZE, "%u\n",
2664 			 hisi_hba->intr_coal_ticks);
2665 }
2666 
2667 static ssize_t intr_coal_ticks_v3_hw_store(struct device *dev,
2668 					   struct device_attribute *attr,
2669 					   const char *buf, size_t count)
2670 {
2671 	struct Scsi_Host *shost = class_to_shost(dev);
2672 	struct hisi_hba *hisi_hba = shost_priv(shost);
2673 	u32 intr_coal_ticks;
2674 	int ret;
2675 
2676 	ret = kstrtou32(buf, 10, &intr_coal_ticks);
2677 	if (ret) {
2678 		dev_err(dev, "Input data of interrupt coalesce unmatch\n");
2679 		return -EINVAL;
2680 	}
2681 
2682 	if (intr_coal_ticks >= BIT(24)) {
2683 		dev_err(dev, "intr_coal_ticks must be less than 2^24!\n");
2684 		return -EINVAL;
2685 	}
2686 
2687 	hisi_hba->intr_coal_ticks = intr_coal_ticks;
2688 
2689 	config_intr_coal_v3_hw(hisi_hba);
2690 
2691 	return count;
2692 }
2693 static DEVICE_ATTR_RW(intr_coal_ticks_v3_hw);
2694 
2695 static ssize_t intr_coal_count_v3_hw_show(struct device *dev,
2696 					  struct device_attribute
2697 					  *attr, char *buf)
2698 {
2699 	struct Scsi_Host *shost = class_to_shost(dev);
2700 	struct hisi_hba *hisi_hba = shost_priv(shost);
2701 
2702 	return scnprintf(buf, PAGE_SIZE, "%u\n",
2703 			 hisi_hba->intr_coal_count);
2704 }
2705 
2706 static ssize_t intr_coal_count_v3_hw_store(struct device *dev,
2707 		struct device_attribute
2708 		*attr, const char *buf, size_t count)
2709 {
2710 	struct Scsi_Host *shost = class_to_shost(dev);
2711 	struct hisi_hba *hisi_hba = shost_priv(shost);
2712 	u32 intr_coal_count;
2713 	int ret;
2714 
2715 	ret = kstrtou32(buf, 10, &intr_coal_count);
2716 	if (ret) {
2717 		dev_err(dev, "Input data of interrupt coalesce unmatch\n");
2718 		return -EINVAL;
2719 	}
2720 
2721 	if (intr_coal_count >= BIT(8)) {
2722 		dev_err(dev, "intr_coal_count must be less than 2^8!\n");
2723 		return -EINVAL;
2724 	}
2725 
2726 	hisi_hba->intr_coal_count = intr_coal_count;
2727 
2728 	config_intr_coal_v3_hw(hisi_hba);
2729 
2730 	return count;
2731 }
2732 static DEVICE_ATTR_RW(intr_coal_count_v3_hw);
2733 
2734 static struct device_attribute *host_attrs_v3_hw[] = {
2735 	&dev_attr_phy_event_threshold,
2736 	&dev_attr_intr_conv_v3_hw,
2737 	&dev_attr_intr_coal_ticks_v3_hw,
2738 	&dev_attr_intr_coal_count_v3_hw,
2739 	NULL
2740 };
2741 
2742 static const struct hisi_sas_debugfs_reg_lu debugfs_port_reg_lu[] = {
2743 	HISI_SAS_DEBUGFS_REG(PHY_CFG),
2744 	HISI_SAS_DEBUGFS_REG(HARD_PHY_LINKRATE),
2745 	HISI_SAS_DEBUGFS_REG(PROG_PHY_LINK_RATE),
2746 	HISI_SAS_DEBUGFS_REG(PHY_CTRL),
2747 	HISI_SAS_DEBUGFS_REG(SL_CFG),
2748 	HISI_SAS_DEBUGFS_REG(AIP_LIMIT),
2749 	HISI_SAS_DEBUGFS_REG(SL_CONTROL),
2750 	HISI_SAS_DEBUGFS_REG(RX_PRIMS_STATUS),
2751 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD0),
2752 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD1),
2753 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD2),
2754 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD3),
2755 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD4),
2756 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD5),
2757 	HISI_SAS_DEBUGFS_REG(TX_ID_DWORD6),
2758 	HISI_SAS_DEBUGFS_REG(TXID_AUTO),
2759 	HISI_SAS_DEBUGFS_REG(RX_IDAF_DWORD0),
2760 	HISI_SAS_DEBUGFS_REG(RXOP_CHECK_CFG_H),
2761 	HISI_SAS_DEBUGFS_REG(STP_LINK_TIMER),
2762 	HISI_SAS_DEBUGFS_REG(STP_LINK_TIMEOUT_STATE),
2763 	HISI_SAS_DEBUGFS_REG(CON_CFG_DRIVER),
2764 	HISI_SAS_DEBUGFS_REG(SAS_SSP_CON_TIMER_CFG),
2765 	HISI_SAS_DEBUGFS_REG(SAS_SMP_CON_TIMER_CFG),
2766 	HISI_SAS_DEBUGFS_REG(SAS_STP_CON_TIMER_CFG),
2767 	HISI_SAS_DEBUGFS_REG(CHL_INT0),
2768 	HISI_SAS_DEBUGFS_REG(CHL_INT1),
2769 	HISI_SAS_DEBUGFS_REG(CHL_INT2),
2770 	HISI_SAS_DEBUGFS_REG(CHL_INT0_MSK),
2771 	HISI_SAS_DEBUGFS_REG(CHL_INT1_MSK),
2772 	HISI_SAS_DEBUGFS_REG(CHL_INT2_MSK),
2773 	HISI_SAS_DEBUGFS_REG(SAS_EC_INT_COAL_TIME),
2774 	HISI_SAS_DEBUGFS_REG(CHL_INT_COAL_EN),
2775 	HISI_SAS_DEBUGFS_REG(SAS_RX_TRAIN_TIMER),
2776 	HISI_SAS_DEBUGFS_REG(PHY_CTRL_RDY_MSK),
2777 	HISI_SAS_DEBUGFS_REG(PHYCTRL_NOT_RDY_MSK),
2778 	HISI_SAS_DEBUGFS_REG(PHYCTRL_DWS_RESET_MSK),
2779 	HISI_SAS_DEBUGFS_REG(PHYCTRL_PHY_ENA_MSK),
2780 	HISI_SAS_DEBUGFS_REG(SL_RX_BCAST_CHK_MSK),
2781 	HISI_SAS_DEBUGFS_REG(PHYCTRL_OOB_RESTART_MSK),
2782 	HISI_SAS_DEBUGFS_REG(DMA_TX_STATUS),
2783 	HISI_SAS_DEBUGFS_REG(DMA_RX_STATUS),
2784 	HISI_SAS_DEBUGFS_REG(COARSETUNE_TIME),
2785 	HISI_SAS_DEBUGFS_REG(ERR_CNT_DWS_LOST),
2786 	HISI_SAS_DEBUGFS_REG(ERR_CNT_RESET_PROB),
2787 	HISI_SAS_DEBUGFS_REG(ERR_CNT_INVLD_DW),
2788 	HISI_SAS_DEBUGFS_REG(ERR_CNT_CODE_ERR),
2789 	HISI_SAS_DEBUGFS_REG(ERR_CNT_DISP_ERR),
2790 	{}
2791 };
2792 
2793 static const struct hisi_sas_debugfs_reg debugfs_port_reg = {
2794 	.lu = debugfs_port_reg_lu,
2795 	.count = 0x100,
2796 	.base_off = PORT_BASE,
2797 	.read_port_reg = hisi_sas_phy_read32,
2798 };
2799 
2800 static const struct hisi_sas_debugfs_reg_lu debugfs_global_reg_lu[] = {
2801 	HISI_SAS_DEBUGFS_REG(DLVRY_QUEUE_ENABLE),
2802 	HISI_SAS_DEBUGFS_REG(PHY_CONTEXT),
2803 	HISI_SAS_DEBUGFS_REG(PHY_STATE),
2804 	HISI_SAS_DEBUGFS_REG(PHY_PORT_NUM_MA),
2805 	HISI_SAS_DEBUGFS_REG(PHY_CONN_RATE),
2806 	HISI_SAS_DEBUGFS_REG(ITCT_CLR),
2807 	HISI_SAS_DEBUGFS_REG(IO_SATA_BROKEN_MSG_ADDR_LO),
2808 	HISI_SAS_DEBUGFS_REG(IO_SATA_BROKEN_MSG_ADDR_HI),
2809 	HISI_SAS_DEBUGFS_REG(SATA_INITI_D2H_STORE_ADDR_LO),
2810 	HISI_SAS_DEBUGFS_REG(SATA_INITI_D2H_STORE_ADDR_HI),
2811 	HISI_SAS_DEBUGFS_REG(CFG_MAX_TAG),
2812 	HISI_SAS_DEBUGFS_REG(HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL),
2813 	HISI_SAS_DEBUGFS_REG(HGC_SAS_TXFAIL_RETRY_CTRL),
2814 	HISI_SAS_DEBUGFS_REG(HGC_GET_ITV_TIME),
2815 	HISI_SAS_DEBUGFS_REG(DEVICE_MSG_WORK_MODE),
2816 	HISI_SAS_DEBUGFS_REG(OPENA_WT_CONTI_TIME),
2817 	HISI_SAS_DEBUGFS_REG(I_T_NEXUS_LOSS_TIME),
2818 	HISI_SAS_DEBUGFS_REG(MAX_CON_TIME_LIMIT_TIME),
2819 	HISI_SAS_DEBUGFS_REG(BUS_INACTIVE_LIMIT_TIME),
2820 	HISI_SAS_DEBUGFS_REG(REJECT_TO_OPEN_LIMIT_TIME),
2821 	HISI_SAS_DEBUGFS_REG(CQ_INT_CONVERGE_EN),
2822 	HISI_SAS_DEBUGFS_REG(CFG_AGING_TIME),
2823 	HISI_SAS_DEBUGFS_REG(HGC_DFX_CFG2),
2824 	HISI_SAS_DEBUGFS_REG(CFG_ABT_SET_QUERY_IPTT),
2825 	HISI_SAS_DEBUGFS_REG(CFG_ABT_SET_IPTT_DONE),
2826 	HISI_SAS_DEBUGFS_REG(HGC_IOMB_PROC1_STATUS),
2827 	HISI_SAS_DEBUGFS_REG(CHNL_INT_STATUS),
2828 	HISI_SAS_DEBUGFS_REG(HGC_AXI_FIFO_ERR_INFO),
2829 	HISI_SAS_DEBUGFS_REG(INT_COAL_EN),
2830 	HISI_SAS_DEBUGFS_REG(OQ_INT_COAL_TIME),
2831 	HISI_SAS_DEBUGFS_REG(OQ_INT_COAL_CNT),
2832 	HISI_SAS_DEBUGFS_REG(ENT_INT_COAL_TIME),
2833 	HISI_SAS_DEBUGFS_REG(ENT_INT_COAL_CNT),
2834 	HISI_SAS_DEBUGFS_REG(OQ_INT_SRC),
2835 	HISI_SAS_DEBUGFS_REG(OQ_INT_SRC_MSK),
2836 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC1),
2837 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC2),
2838 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC3),
2839 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC_MSK1),
2840 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC_MSK2),
2841 	HISI_SAS_DEBUGFS_REG(ENT_INT_SRC_MSK3),
2842 	HISI_SAS_DEBUGFS_REG(CHNL_PHYUPDOWN_INT_MSK),
2843 	HISI_SAS_DEBUGFS_REG(CHNL_ENT_INT_MSK),
2844 	HISI_SAS_DEBUGFS_REG(HGC_COM_INT_MSK),
2845 	HISI_SAS_DEBUGFS_REG(SAS_ECC_INTR),
2846 	HISI_SAS_DEBUGFS_REG(SAS_ECC_INTR_MSK),
2847 	HISI_SAS_DEBUGFS_REG(HGC_ERR_STAT_EN),
2848 	HISI_SAS_DEBUGFS_REG(CQE_SEND_CNT),
2849 	HISI_SAS_DEBUGFS_REG(DLVRY_Q_0_DEPTH),
2850 	HISI_SAS_DEBUGFS_REG(DLVRY_Q_0_WR_PTR),
2851 	HISI_SAS_DEBUGFS_REG(DLVRY_Q_0_RD_PTR),
2852 	HISI_SAS_DEBUGFS_REG(HYPER_STREAM_ID_EN_CFG),
2853 	HISI_SAS_DEBUGFS_REG(OQ0_INT_SRC_MSK),
2854 	HISI_SAS_DEBUGFS_REG(COMPL_Q_0_DEPTH),
2855 	HISI_SAS_DEBUGFS_REG(COMPL_Q_0_WR_PTR),
2856 	HISI_SAS_DEBUGFS_REG(COMPL_Q_0_RD_PTR),
2857 	HISI_SAS_DEBUGFS_REG(AWQOS_AWCACHE_CFG),
2858 	HISI_SAS_DEBUGFS_REG(ARQOS_ARCACHE_CFG),
2859 	HISI_SAS_DEBUGFS_REG(HILINK_ERR_DFX),
2860 	HISI_SAS_DEBUGFS_REG(SAS_GPIO_CFG_0),
2861 	HISI_SAS_DEBUGFS_REG(SAS_GPIO_CFG_1),
2862 	HISI_SAS_DEBUGFS_REG(SAS_GPIO_TX_0_1),
2863 	HISI_SAS_DEBUGFS_REG(SAS_CFG_DRIVE_VLD),
2864 	{}
2865 };
2866 
2867 static const struct hisi_sas_debugfs_reg debugfs_global_reg = {
2868 	.lu = debugfs_global_reg_lu,
2869 	.count = 0x800,
2870 	.read_global_reg = hisi_sas_read32,
2871 };
2872 
2873 static const struct hisi_sas_debugfs_reg_lu debugfs_axi_reg_lu[] = {
2874 	HISI_SAS_DEBUGFS_REG(AM_CFG_MAX_TRANS),
2875 	HISI_SAS_DEBUGFS_REG(AM_CFG_SINGLE_PORT_MAX_TRANS),
2876 	HISI_SAS_DEBUGFS_REG(AXI_CFG),
2877 	HISI_SAS_DEBUGFS_REG(AM_ROB_ECC_ERR_ADDR),
2878 	{}
2879 };
2880 
2881 static const struct hisi_sas_debugfs_reg debugfs_axi_reg = {
2882 	.lu = debugfs_axi_reg_lu,
2883 	.count = 0x61,
2884 	.base_off = AXI_MASTER_CFG_BASE,
2885 	.read_global_reg = hisi_sas_read32,
2886 };
2887 
2888 static const struct hisi_sas_debugfs_reg_lu debugfs_ras_reg_lu[] = {
2889 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR1),
2890 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR0_MASK),
2891 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR1_MASK),
2892 	HISI_SAS_DEBUGFS_REG(CFG_SAS_RAS_INTR_MASK),
2893 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR2),
2894 	HISI_SAS_DEBUGFS_REG(SAS_RAS_INTR2_MASK),
2895 	{}
2896 };
2897 
2898 static const struct hisi_sas_debugfs_reg debugfs_ras_reg = {
2899 	.lu = debugfs_ras_reg_lu,
2900 	.count = 0x10,
2901 	.base_off = RAS_BASE,
2902 	.read_global_reg = hisi_sas_read32,
2903 };
2904 
2905 static void debugfs_snapshot_prepare_v3_hw(struct hisi_hba *hisi_hba)
2906 {
2907 	set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
2908 
2909 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0);
2910 
2911 	wait_cmds_complete_timeout_v3_hw(hisi_hba, 100, 5000);
2912 
2913 	hisi_sas_kill_tasklets(hisi_hba);
2914 }
2915 
2916 static void debugfs_snapshot_restore_v3_hw(struct hisi_hba *hisi_hba)
2917 {
2918 	hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE,
2919 			 (u32)((1ULL << hisi_hba->queue_count) - 1));
2920 
2921 	clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
2922 }
2923 
2924 static void read_iost_itct_cache_v3_hw(struct hisi_hba *hisi_hba,
2925 				       enum hisi_sas_debugfs_cache_type type,
2926 				       u32 *cache)
2927 {
2928 	u32 cache_dw_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ *
2929 			    HISI_SAS_IOST_ITCT_CACHE_NUM;
2930 	u32 *buf = cache;
2931 	u32 i, val;
2932 
2933 	hisi_sas_write32(hisi_hba, TAB_RD_TYPE, type);
2934 
2935 	for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_DW_SZ; i++) {
2936 		val = hisi_sas_read32(hisi_hba, TAB_DFX);
2937 		if (val == 0xffffffff)
2938 			break;
2939 	}
2940 
2941 	if (val != 0xffffffff) {
2942 		pr_err("Issue occur when reading IOST/ITCT cache!\n");
2943 		return;
2944 	}
2945 
2946 	memset(buf, 0, cache_dw_size * 4);
2947 	buf[0] = val;
2948 
2949 	for (i = 1; i < cache_dw_size; i++)
2950 		buf[i] = hisi_sas_read32(hisi_hba, TAB_DFX);
2951 }
2952 
2953 static void hisi_sas_bist_test_prep_v3_hw(struct hisi_hba *hisi_hba)
2954 {
2955 	u32 reg_val;
2956 	int phy_id = hisi_hba->debugfs_bist_phy_no;
2957 
2958 	/* disable PHY */
2959 	hisi_sas_phy_enable(hisi_hba, phy_id, 0);
2960 
2961 	/* disable ALOS */
2962 	reg_val = hisi_sas_phy_read32(hisi_hba, phy_id, SERDES_CFG);
2963 	reg_val |= CFG_ALOS_CHK_DISABLE_MSK;
2964 	hisi_sas_phy_write32(hisi_hba, phy_id, SERDES_CFG, reg_val);
2965 }
2966 
2967 static void hisi_sas_bist_test_restore_v3_hw(struct hisi_hba *hisi_hba)
2968 {
2969 	u32 reg_val;
2970 	int phy_id = hisi_hba->debugfs_bist_phy_no;
2971 
2972 	/* disable loopback */
2973 	reg_val = hisi_sas_phy_read32(hisi_hba, phy_id, SAS_PHY_BIST_CTRL);
2974 	reg_val &= ~(CFG_RX_BIST_EN_MSK | CFG_TX_BIST_EN_MSK |
2975 		     CFG_BIST_TEST_MSK);
2976 	hisi_sas_phy_write32(hisi_hba, phy_id, SAS_PHY_BIST_CTRL, reg_val);
2977 
2978 	/* enable ALOS */
2979 	reg_val = hisi_sas_phy_read32(hisi_hba, phy_id, SERDES_CFG);
2980 	reg_val &= ~CFG_ALOS_CHK_DISABLE_MSK;
2981 	hisi_sas_phy_write32(hisi_hba, phy_id, SERDES_CFG, reg_val);
2982 
2983 	/* restore the linkrate */
2984 	reg_val = hisi_sas_phy_read32(hisi_hba, phy_id, PROG_PHY_LINK_RATE);
2985 	/* init OOB link rate as 1.5 Gbits */
2986 	reg_val &= ~CFG_PROG_PHY_LINK_RATE_MSK;
2987 	reg_val |= (0x8 << CFG_PROG_PHY_LINK_RATE_OFF);
2988 	hisi_sas_phy_write32(hisi_hba, phy_id, PROG_PHY_LINK_RATE, reg_val);
2989 
2990 	/* enable PHY */
2991 	hisi_sas_phy_enable(hisi_hba, phy_id, 1);
2992 }
2993 
2994 #define SAS_PHY_BIST_CODE_INIT	0x1
2995 #define SAS_PHY_BIST_CODE1_INIT	0X80
2996 static int debugfs_set_bist_v3_hw(struct hisi_hba *hisi_hba, bool enable)
2997 {
2998 	u32 reg_val, mode_tmp;
2999 	u32 linkrate = hisi_hba->debugfs_bist_linkrate;
3000 	u32 phy_id = hisi_hba->debugfs_bist_phy_no;
3001 	u32 code_mode = hisi_hba->debugfs_bist_code_mode;
3002 	u32 path_mode = hisi_hba->debugfs_bist_mode;
3003 	struct device *dev = hisi_hba->dev;
3004 
3005 	dev_info(dev, "BIST info:linkrate=%d phy_id=%d code_mode=%d path_mode=%d\n",
3006 		 linkrate, phy_id, code_mode, path_mode);
3007 	mode_tmp = path_mode ? 2 : 1;
3008 	if (enable) {
3009 		/* some preparations before bist test */
3010 		hisi_sas_bist_test_prep_v3_hw(hisi_hba);
3011 
3012 		/* set linkrate of bit test*/
3013 		reg_val = hisi_sas_phy_read32(hisi_hba, phy_id,
3014 					      PROG_PHY_LINK_RATE);
3015 		reg_val &= ~CFG_PROG_PHY_LINK_RATE_MSK;
3016 		reg_val |= (linkrate << CFG_PROG_PHY_LINK_RATE_OFF);
3017 		hisi_sas_phy_write32(hisi_hba, phy_id,
3018 				     PROG_PHY_LINK_RATE, reg_val);
3019 
3020 		/* set code mode of bit test */
3021 		reg_val = hisi_sas_phy_read32(hisi_hba, phy_id,
3022 					      SAS_PHY_BIST_CTRL);
3023 		reg_val &= ~(CFG_BIST_MODE_SEL_MSK |
3024 				CFG_LOOP_TEST_MODE_MSK |
3025 				CFG_RX_BIST_EN_MSK |
3026 				CFG_TX_BIST_EN_MSK |
3027 				CFG_BIST_TEST_MSK);
3028 		reg_val |= ((code_mode << CFG_BIST_MODE_SEL_OFF) |
3029 			    (mode_tmp << CFG_LOOP_TEST_MODE_OFF) |
3030 			    CFG_BIST_TEST_MSK);
3031 		hisi_sas_phy_write32(hisi_hba, phy_id,
3032 				     SAS_PHY_BIST_CTRL, reg_val);
3033 
3034 		/* set the bist init value */
3035 		hisi_sas_phy_write32(hisi_hba, phy_id,
3036 				     SAS_PHY_BIST_CODE,
3037 				     SAS_PHY_BIST_CODE_INIT);
3038 		hisi_sas_phy_write32(hisi_hba, phy_id,
3039 				     SAS_PHY_BIST_CODE1,
3040 				     SAS_PHY_BIST_CODE1_INIT);
3041 
3042 		mdelay(100);
3043 		reg_val |= (CFG_RX_BIST_EN_MSK | CFG_TX_BIST_EN_MSK);
3044 		hisi_sas_phy_write32(hisi_hba, phy_id,
3045 				     SAS_PHY_BIST_CTRL, reg_val);
3046 
3047 		/* clear error bit */
3048 		mdelay(100);
3049 		hisi_sas_phy_read32(hisi_hba, phy_id, SAS_BIST_ERR_CNT);
3050 	} else {
3051 		/* disable bist test and recover it */
3052 		hisi_hba->debugfs_bist_cnt += hisi_sas_phy_read32(hisi_hba,
3053 				phy_id, SAS_BIST_ERR_CNT);
3054 		hisi_sas_bist_test_restore_v3_hw(hisi_hba);
3055 	}
3056 
3057 	return 0;
3058 }
3059 
3060 static struct scsi_host_template sht_v3_hw = {
3061 	.name			= DRV_NAME,
3062 	.module			= THIS_MODULE,
3063 	.queuecommand		= sas_queuecommand,
3064 	.target_alloc		= sas_target_alloc,
3065 	.slave_configure	= hisi_sas_slave_configure,
3066 	.scan_finished		= hisi_sas_scan_finished,
3067 	.scan_start		= hisi_sas_scan_start,
3068 	.change_queue_depth	= sas_change_queue_depth,
3069 	.bios_param		= sas_bios_param,
3070 	.this_id		= -1,
3071 	.sg_tablesize		= HISI_SAS_SGE_PAGE_CNT,
3072 	.sg_prot_tablesize	= HISI_SAS_SGE_PAGE_CNT,
3073 	.max_sectors		= SCSI_DEFAULT_MAX_SECTORS,
3074 	.eh_device_reset_handler = sas_eh_device_reset_handler,
3075 	.eh_target_reset_handler = sas_eh_target_reset_handler,
3076 	.target_destroy		= sas_target_destroy,
3077 	.ioctl			= sas_ioctl,
3078 	.shost_attrs		= host_attrs_v3_hw,
3079 	.tag_alloc_policy	= BLK_TAG_ALLOC_RR,
3080 	.host_reset             = hisi_sas_host_reset,
3081 };
3082 
3083 static const struct hisi_sas_hw hisi_sas_v3_hw = {
3084 	.hw_init = hisi_sas_v3_init,
3085 	.setup_itct = setup_itct_v3_hw,
3086 	.get_wideport_bitmap = get_wideport_bitmap_v3_hw,
3087 	.complete_hdr_size = sizeof(struct hisi_sas_complete_v3_hdr),
3088 	.clear_itct = clear_itct_v3_hw,
3089 	.sl_notify_ssp = sl_notify_ssp_v3_hw,
3090 	.prep_ssp = prep_ssp_v3_hw,
3091 	.prep_smp = prep_smp_v3_hw,
3092 	.prep_stp = prep_ata_v3_hw,
3093 	.prep_abort = prep_abort_v3_hw,
3094 	.start_delivery = start_delivery_v3_hw,
3095 	.phys_init = phys_init_v3_hw,
3096 	.phy_start = start_phy_v3_hw,
3097 	.phy_disable = disable_phy_v3_hw,
3098 	.phy_hard_reset = phy_hard_reset_v3_hw,
3099 	.phy_get_max_linkrate = phy_get_max_linkrate_v3_hw,
3100 	.phy_set_linkrate = phy_set_linkrate_v3_hw,
3101 	.dereg_device = dereg_device_v3_hw,
3102 	.soft_reset = soft_reset_v3_hw,
3103 	.get_phys_state = get_phys_state_v3_hw,
3104 	.get_events = phy_get_events_v3_hw,
3105 	.write_gpio = write_gpio_v3_hw,
3106 	.wait_cmds_complete_timeout = wait_cmds_complete_timeout_v3_hw,
3107 	.debugfs_reg_array[DEBUGFS_GLOBAL] = &debugfs_global_reg,
3108 	.debugfs_reg_array[DEBUGFS_AXI] = &debugfs_axi_reg,
3109 	.debugfs_reg_array[DEBUGFS_RAS] = &debugfs_ras_reg,
3110 	.debugfs_reg_port = &debugfs_port_reg,
3111 	.snapshot_prepare = debugfs_snapshot_prepare_v3_hw,
3112 	.snapshot_restore = debugfs_snapshot_restore_v3_hw,
3113 	.read_iost_itct_cache = read_iost_itct_cache_v3_hw,
3114 	.set_bist = debugfs_set_bist_v3_hw,
3115 };
3116 
3117 static struct Scsi_Host *
3118 hisi_sas_shost_alloc_pci(struct pci_dev *pdev)
3119 {
3120 	struct Scsi_Host *shost;
3121 	struct hisi_hba *hisi_hba;
3122 	struct device *dev = &pdev->dev;
3123 
3124 	shost = scsi_host_alloc(&sht_v3_hw, sizeof(*hisi_hba));
3125 	if (!shost) {
3126 		dev_err(dev, "shost alloc failed\n");
3127 		return NULL;
3128 	}
3129 	hisi_hba = shost_priv(shost);
3130 
3131 	INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
3132 	INIT_WORK(&hisi_hba->debugfs_work, hisi_sas_debugfs_work_handler);
3133 	hisi_hba->hw = &hisi_sas_v3_hw;
3134 	hisi_hba->pci_dev = pdev;
3135 	hisi_hba->dev = dev;
3136 	hisi_hba->shost = shost;
3137 	SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
3138 
3139 	if (prot_mask & ~HISI_SAS_PROT_MASK)
3140 		dev_err(dev, "unsupported protection mask 0x%x, using default (0x0)\n",
3141 			prot_mask);
3142 	else
3143 		hisi_hba->prot_mask = prot_mask;
3144 
3145 	if (hisi_sas_get_fw_info(hisi_hba) < 0)
3146 		goto err_out;
3147 
3148 	if (hisi_sas_alloc(hisi_hba)) {
3149 		hisi_sas_free(hisi_hba);
3150 		goto err_out;
3151 	}
3152 
3153 	return shost;
3154 err_out:
3155 	scsi_host_put(shost);
3156 	dev_err(dev, "shost alloc failed\n");
3157 	return NULL;
3158 }
3159 
3160 static int
3161 hisi_sas_v3_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3162 {
3163 	struct Scsi_Host *shost;
3164 	struct hisi_hba *hisi_hba;
3165 	struct device *dev = &pdev->dev;
3166 	struct asd_sas_phy **arr_phy;
3167 	struct asd_sas_port **arr_port;
3168 	struct sas_ha_struct *sha;
3169 	int rc, phy_nr, port_nr, i;
3170 
3171 	rc = pci_enable_device(pdev);
3172 	if (rc)
3173 		goto err_out;
3174 
3175 	pci_set_master(pdev);
3176 
3177 	rc = pci_request_regions(pdev, DRV_NAME);
3178 	if (rc)
3179 		goto err_out_disable_device;
3180 
3181 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3182 	if (rc)
3183 		rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3184 	if (rc) {
3185 		dev_err(dev, "No usable DMA addressing method\n");
3186 		rc = -ENODEV;
3187 		goto err_out_regions;
3188 	}
3189 
3190 	shost = hisi_sas_shost_alloc_pci(pdev);
3191 	if (!shost) {
3192 		rc = -ENOMEM;
3193 		goto err_out_regions;
3194 	}
3195 
3196 	sha = SHOST_TO_SAS_HA(shost);
3197 	hisi_hba = shost_priv(shost);
3198 	dev_set_drvdata(dev, sha);
3199 
3200 	hisi_hba->regs = pcim_iomap(pdev, 5, 0);
3201 	if (!hisi_hba->regs) {
3202 		dev_err(dev, "cannot map register\n");
3203 		rc = -ENOMEM;
3204 		goto err_out_ha;
3205 	}
3206 
3207 	phy_nr = port_nr = hisi_hba->n_phy;
3208 
3209 	arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
3210 	arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
3211 	if (!arr_phy || !arr_port) {
3212 		rc = -ENOMEM;
3213 		goto err_out_ha;
3214 	}
3215 
3216 	sha->sas_phy = arr_phy;
3217 	sha->sas_port = arr_port;
3218 	sha->core.shost = shost;
3219 	sha->lldd_ha = hisi_hba;
3220 
3221 	shost->transportt = hisi_sas_stt;
3222 	shost->max_id = HISI_SAS_MAX_DEVICES;
3223 	shost->max_lun = ~0;
3224 	shost->max_channel = 1;
3225 	shost->max_cmd_len = 16;
3226 	shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
3227 	shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
3228 
3229 	sha->sas_ha_name = DRV_NAME;
3230 	sha->dev = dev;
3231 	sha->lldd_module = THIS_MODULE;
3232 	sha->sas_addr = &hisi_hba->sas_addr[0];
3233 	sha->num_phys = hisi_hba->n_phy;
3234 
3235 	for (i = 0; i < hisi_hba->n_phy; i++) {
3236 		sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
3237 		sha->sas_port[i] = &hisi_hba->port[i].sas_port;
3238 	}
3239 
3240 	if (hisi_hba->prot_mask) {
3241 		dev_info(dev, "Registering for DIF/DIX prot_mask=0x%x\n",
3242 			 prot_mask);
3243 		scsi_host_set_prot(hisi_hba->shost, prot_mask);
3244 		if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
3245 			scsi_host_set_guard(hisi_hba->shost,
3246 					    SHOST_DIX_GUARD_CRC);
3247 	}
3248 
3249 	if (hisi_sas_debugfs_enable)
3250 		hisi_sas_debugfs_init(hisi_hba);
3251 
3252 	rc = scsi_add_host(shost, dev);
3253 	if (rc)
3254 		goto err_out_ha;
3255 
3256 	rc = sas_register_ha(sha);
3257 	if (rc)
3258 		goto err_out_register_ha;
3259 
3260 	rc = hisi_hba->hw->hw_init(hisi_hba);
3261 	if (rc)
3262 		goto err_out_register_ha;
3263 
3264 	scsi_scan_host(shost);
3265 
3266 	return 0;
3267 
3268 err_out_register_ha:
3269 	scsi_remove_host(shost);
3270 err_out_ha:
3271 	hisi_sas_debugfs_exit(hisi_hba);
3272 	scsi_host_put(shost);
3273 err_out_regions:
3274 	pci_release_regions(pdev);
3275 err_out_disable_device:
3276 	pci_disable_device(pdev);
3277 err_out:
3278 	return rc;
3279 }
3280 
3281 static void
3282 hisi_sas_v3_destroy_irqs(struct pci_dev *pdev, struct hisi_hba *hisi_hba)
3283 {
3284 	int i;
3285 
3286 	free_irq(pci_irq_vector(pdev, 1), hisi_hba);
3287 	free_irq(pci_irq_vector(pdev, 2), hisi_hba);
3288 	free_irq(pci_irq_vector(pdev, 11), hisi_hba);
3289 	for (i = 0; i < hisi_hba->cq_nvecs; i++) {
3290 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
3291 		int nr = hisi_sas_intr_conv ? 16 : 16 + i;
3292 
3293 		free_irq(pci_irq_vector(pdev, nr), cq);
3294 	}
3295 	pci_free_irq_vectors(pdev);
3296 }
3297 
3298 static void hisi_sas_v3_remove(struct pci_dev *pdev)
3299 {
3300 	struct device *dev = &pdev->dev;
3301 	struct sas_ha_struct *sha = dev_get_drvdata(dev);
3302 	struct hisi_hba *hisi_hba = sha->lldd_ha;
3303 	struct Scsi_Host *shost = sha->core.shost;
3304 
3305 	if (timer_pending(&hisi_hba->timer))
3306 		del_timer(&hisi_hba->timer);
3307 
3308 	sas_unregister_ha(sha);
3309 	sas_remove_host(sha->core.shost);
3310 
3311 	hisi_sas_v3_destroy_irqs(pdev, hisi_hba);
3312 	hisi_sas_kill_tasklets(hisi_hba);
3313 	pci_release_regions(pdev);
3314 	pci_disable_device(pdev);
3315 	hisi_sas_free(hisi_hba);
3316 	hisi_sas_debugfs_exit(hisi_hba);
3317 	scsi_host_put(shost);
3318 }
3319 
3320 static void hisi_sas_reset_prepare_v3_hw(struct pci_dev *pdev)
3321 {
3322 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
3323 	struct hisi_hba *hisi_hba = sha->lldd_ha;
3324 	struct device *dev = hisi_hba->dev;
3325 	int rc;
3326 
3327 	dev_info(dev, "FLR prepare\n");
3328 	set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
3329 	hisi_sas_controller_reset_prepare(hisi_hba);
3330 
3331 	rc = disable_host_v3_hw(hisi_hba);
3332 	if (rc)
3333 		dev_err(dev, "FLR: disable host failed rc=%d\n", rc);
3334 }
3335 
3336 static void hisi_sas_reset_done_v3_hw(struct pci_dev *pdev)
3337 {
3338 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
3339 	struct hisi_hba *hisi_hba = sha->lldd_ha;
3340 	struct device *dev = hisi_hba->dev;
3341 	int rc;
3342 
3343 	hisi_sas_init_mem(hisi_hba);
3344 
3345 	rc = hw_init_v3_hw(hisi_hba);
3346 	if (rc) {
3347 		dev_err(dev, "FLR: hw init failed rc=%d\n", rc);
3348 		return;
3349 	}
3350 
3351 	hisi_sas_controller_reset_done(hisi_hba);
3352 	dev_info(dev, "FLR done\n");
3353 }
3354 
3355 enum {
3356 	/* instances of the controller */
3357 	hip08,
3358 };
3359 
3360 static int hisi_sas_v3_suspend(struct pci_dev *pdev, pm_message_t state)
3361 {
3362 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
3363 	struct hisi_hba *hisi_hba = sha->lldd_ha;
3364 	struct device *dev = hisi_hba->dev;
3365 	struct Scsi_Host *shost = hisi_hba->shost;
3366 	pci_power_t device_state;
3367 	int rc;
3368 
3369 	if (!pdev->pm_cap) {
3370 		dev_err(dev, "PCI PM not supported\n");
3371 		return -ENODEV;
3372 	}
3373 
3374 	if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
3375 		return -1;
3376 
3377 	scsi_block_requests(shost);
3378 	set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
3379 	flush_workqueue(hisi_hba->wq);
3380 
3381 	rc = disable_host_v3_hw(hisi_hba);
3382 	if (rc) {
3383 		dev_err(dev, "PM suspend: disable host failed rc=%d\n", rc);
3384 		clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
3385 		clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
3386 		scsi_unblock_requests(shost);
3387 		return rc;
3388 	}
3389 
3390 	hisi_sas_init_mem(hisi_hba);
3391 
3392 	device_state = pci_choose_state(pdev, state);
3393 	dev_warn(dev, "entering operating state [D%d]\n",
3394 			device_state);
3395 	pci_save_state(pdev);
3396 	pci_disable_device(pdev);
3397 	pci_set_power_state(pdev, device_state);
3398 
3399 	hisi_sas_release_tasks(hisi_hba);
3400 
3401 	sas_suspend_ha(sha);
3402 	return 0;
3403 }
3404 
3405 static int hisi_sas_v3_resume(struct pci_dev *pdev)
3406 {
3407 	struct sas_ha_struct *sha = pci_get_drvdata(pdev);
3408 	struct hisi_hba *hisi_hba = sha->lldd_ha;
3409 	struct Scsi_Host *shost = hisi_hba->shost;
3410 	struct device *dev = hisi_hba->dev;
3411 	unsigned int rc;
3412 	pci_power_t device_state = pdev->current_state;
3413 
3414 	dev_warn(dev, "resuming from operating state [D%d]\n",
3415 		 device_state);
3416 	pci_set_power_state(pdev, PCI_D0);
3417 	pci_enable_wake(pdev, PCI_D0, 0);
3418 	pci_restore_state(pdev);
3419 	rc = pci_enable_device(pdev);
3420 	if (rc) {
3421 		dev_err(dev, "enable device failed during resume (%d)\n", rc);
3422 		return rc;
3423 	}
3424 
3425 	pci_set_master(pdev);
3426 	scsi_unblock_requests(shost);
3427 	clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
3428 
3429 	sas_prep_resume_ha(sha);
3430 	rc = hw_init_v3_hw(hisi_hba);
3431 	if (rc) {
3432 		scsi_remove_host(shost);
3433 		pci_disable_device(pdev);
3434 		return rc;
3435 	}
3436 	hisi_hba->hw->phys_init(hisi_hba);
3437 	sas_resume_ha(sha);
3438 	clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
3439 
3440 	return 0;
3441 }
3442 
3443 static const struct pci_device_id sas_v3_pci_table[] = {
3444 	{ PCI_VDEVICE(HUAWEI, 0xa230), hip08 },
3445 	{}
3446 };
3447 MODULE_DEVICE_TABLE(pci, sas_v3_pci_table);
3448 
3449 static const struct pci_error_handlers hisi_sas_err_handler = {
3450 	.reset_prepare	= hisi_sas_reset_prepare_v3_hw,
3451 	.reset_done	= hisi_sas_reset_done_v3_hw,
3452 };
3453 
3454 static struct pci_driver sas_v3_pci_driver = {
3455 	.name		= DRV_NAME,
3456 	.id_table	= sas_v3_pci_table,
3457 	.probe		= hisi_sas_v3_probe,
3458 	.remove		= hisi_sas_v3_remove,
3459 	.suspend	= hisi_sas_v3_suspend,
3460 	.resume		= hisi_sas_v3_resume,
3461 	.err_handler	= &hisi_sas_err_handler,
3462 };
3463 
3464 module_pci_driver(sas_v3_pci_driver);
3465 module_param_named(intr_conv, hisi_sas_intr_conv, bool, 0444);
3466 
3467 MODULE_LICENSE("GPL");
3468 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
3469 MODULE_DESCRIPTION("HISILICON SAS controller v3 hw driver based on pci device");
3470 MODULE_ALIAS("pci:" DRV_NAME);
3471