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