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