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