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