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