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