MAINTAINERS: remove pmchba list for PM8001
[linux-2.6-block.git] / drivers / scsi / hisi_sas / hisi_sas_v2_hw.c
CommitLineData
3417ba8a
JG
1/*
2 * Copyright (c) 2016 Linaro Ltd.
3 * Copyright (c) 2016 Hisilicon Limited.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 */
11
12#include "hisi_sas.h"
13#define DRV_NAME "hisi_sas_v2_hw"
14
45c901b8
JG
15/* global registers need init*/
16#define DLVRY_QUEUE_ENABLE 0x0
17#define IOST_BASE_ADDR_LO 0x8
18#define IOST_BASE_ADDR_HI 0xc
19#define ITCT_BASE_ADDR_LO 0x10
20#define ITCT_BASE_ADDR_HI 0x14
21#define IO_BROKEN_MSG_ADDR_LO 0x18
22#define IO_BROKEN_MSG_ADDR_HI 0x1c
23#define PHY_CONTEXT 0x20
24#define PHY_STATE 0x24
25#define PHY_PORT_NUM_MA 0x28
26#define PORT_STATE 0x2c
27#define PORT_STATE_PHY8_PORT_NUM_OFF 16
28#define PORT_STATE_PHY8_PORT_NUM_MSK (0xf << PORT_STATE_PHY8_PORT_NUM_OFF)
29#define PORT_STATE_PHY8_CONN_RATE_OFF 20
30#define PORT_STATE_PHY8_CONN_RATE_MSK (0xf << PORT_STATE_PHY8_CONN_RATE_OFF)
31#define PHY_CONN_RATE 0x30
32#define HGC_TRANS_TASK_CNT_LIMIT 0x38
33#define AXI_AHB_CLK_CFG 0x3c
34#define ITCT_CLR 0x44
35#define ITCT_CLR_EN_OFF 16
36#define ITCT_CLR_EN_MSK (0x1 << ITCT_CLR_EN_OFF)
37#define ITCT_DEV_OFF 0
38#define ITCT_DEV_MSK (0x7ff << ITCT_DEV_OFF)
39#define AXI_USER1 0x48
40#define AXI_USER2 0x4c
41#define IO_SATA_BROKEN_MSG_ADDR_LO 0x58
42#define IO_SATA_BROKEN_MSG_ADDR_HI 0x5c
43#define SATA_INITI_D2H_STORE_ADDR_LO 0x60
44#define SATA_INITI_D2H_STORE_ADDR_HI 0x64
45#define HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL 0x84
46#define HGC_SAS_TXFAIL_RETRY_CTRL 0x88
47#define HGC_GET_ITV_TIME 0x90
48#define DEVICE_MSG_WORK_MODE 0x94
49#define OPENA_WT_CONTI_TIME 0x9c
50#define I_T_NEXUS_LOSS_TIME 0xa0
51#define MAX_CON_TIME_LIMIT_TIME 0xa4
52#define BUS_INACTIVE_LIMIT_TIME 0xa8
53#define REJECT_TO_OPEN_LIMIT_TIME 0xac
54#define CFG_AGING_TIME 0xbc
55#define HGC_DFX_CFG2 0xc0
56#define HGC_IOMB_PROC1_STATUS 0x104
57#define CFG_1US_TIMER_TRSH 0xcc
d3b688d3
XC
58#define HGC_LM_DFX_STATUS2 0x128
59#define HGC_LM_DFX_STATUS2_IOSTLIST_OFF 0
60#define HGC_LM_DFX_STATUS2_IOSTLIST_MSK (0xfff << \
61 HGC_LM_DFX_STATUS2_IOSTLIST_OFF)
62#define HGC_LM_DFX_STATUS2_ITCTLIST_OFF 12
63#define HGC_LM_DFX_STATUS2_ITCTLIST_MSK (0x7ff << \
64 HGC_LM_DFX_STATUS2_ITCTLIST_OFF)
65#define HGC_CQE_ECC_ADDR 0x13c
66#define HGC_CQE_ECC_1B_ADDR_OFF 0
ce41b41e 67#define HGC_CQE_ECC_1B_ADDR_MSK (0x3f << HGC_CQE_ECC_1B_ADDR_OFF)
d3b688d3 68#define HGC_CQE_ECC_MB_ADDR_OFF 8
ce41b41e 69#define HGC_CQE_ECC_MB_ADDR_MSK (0x3f << HGC_CQE_ECC_MB_ADDR_OFF)
d3b688d3
XC
70#define HGC_IOST_ECC_ADDR 0x140
71#define HGC_IOST_ECC_1B_ADDR_OFF 0
ce41b41e 72#define HGC_IOST_ECC_1B_ADDR_MSK (0x3ff << HGC_IOST_ECC_1B_ADDR_OFF)
d3b688d3 73#define HGC_IOST_ECC_MB_ADDR_OFF 16
ce41b41e 74#define HGC_IOST_ECC_MB_ADDR_MSK (0x3ff << HGC_IOST_ECC_MB_ADDR_OFF)
d3b688d3
XC
75#define HGC_DQE_ECC_ADDR 0x144
76#define HGC_DQE_ECC_1B_ADDR_OFF 0
ce41b41e 77#define HGC_DQE_ECC_1B_ADDR_MSK (0xfff << HGC_DQE_ECC_1B_ADDR_OFF)
d3b688d3 78#define HGC_DQE_ECC_MB_ADDR_OFF 16
ce41b41e 79#define HGC_DQE_ECC_MB_ADDR_MSK (0xfff << HGC_DQE_ECC_MB_ADDR_OFF)
45c901b8
JG
80#define HGC_INVLD_DQE_INFO 0x148
81#define HGC_INVLD_DQE_INFO_FB_CH0_OFF 9
82#define HGC_INVLD_DQE_INFO_FB_CH0_MSK (0x1 << HGC_INVLD_DQE_INFO_FB_CH0_OFF)
83#define HGC_INVLD_DQE_INFO_FB_CH3_OFF 18
d3b688d3
XC
84#define HGC_ITCT_ECC_ADDR 0x150
85#define HGC_ITCT_ECC_1B_ADDR_OFF 0
86#define HGC_ITCT_ECC_1B_ADDR_MSK (0x3ff << \
87 HGC_ITCT_ECC_1B_ADDR_OFF)
88#define HGC_ITCT_ECC_MB_ADDR_OFF 16
89#define HGC_ITCT_ECC_MB_ADDR_MSK (0x3ff << \
90 HGC_ITCT_ECC_MB_ADDR_OFF)
91#define HGC_AXI_FIFO_ERR_INFO 0x154
92#define AXI_ERR_INFO_OFF 0
93#define AXI_ERR_INFO_MSK (0xff << AXI_ERR_INFO_OFF)
94#define FIFO_ERR_INFO_OFF 8
95#define FIFO_ERR_INFO_MSK (0xff << FIFO_ERR_INFO_OFF)
45c901b8
JG
96#define INT_COAL_EN 0x19c
97#define OQ_INT_COAL_TIME 0x1a0
98#define OQ_INT_COAL_CNT 0x1a4
99#define ENT_INT_COAL_TIME 0x1a8
100#define ENT_INT_COAL_CNT 0x1ac
101#define OQ_INT_SRC 0x1b0
102#define OQ_INT_SRC_MSK 0x1b4
103#define ENT_INT_SRC1 0x1b8
104#define ENT_INT_SRC1_D2H_FIS_CH0_OFF 0
105#define ENT_INT_SRC1_D2H_FIS_CH0_MSK (0x1 << ENT_INT_SRC1_D2H_FIS_CH0_OFF)
106#define ENT_INT_SRC1_D2H_FIS_CH1_OFF 8
107#define ENT_INT_SRC1_D2H_FIS_CH1_MSK (0x1 << ENT_INT_SRC1_D2H_FIS_CH1_OFF)
108#define ENT_INT_SRC2 0x1bc
109#define ENT_INT_SRC3 0x1c0
d3b688d3
XC
110#define ENT_INT_SRC3_WP_DEPTH_OFF 8
111#define ENT_INT_SRC3_IPTT_SLOT_NOMATCH_OFF 9
112#define ENT_INT_SRC3_RP_DEPTH_OFF 10
113#define ENT_INT_SRC3_AXI_OFF 11
114#define ENT_INT_SRC3_FIFO_OFF 12
115#define ENT_INT_SRC3_LM_OFF 14
45c901b8
JG
116#define ENT_INT_SRC3_ITC_INT_OFF 15
117#define ENT_INT_SRC3_ITC_INT_MSK (0x1 << ENT_INT_SRC3_ITC_INT_OFF)
d3b688d3 118#define ENT_INT_SRC3_ABT_OFF 16
45c901b8
JG
119#define ENT_INT_SRC_MSK1 0x1c4
120#define ENT_INT_SRC_MSK2 0x1c8
121#define ENT_INT_SRC_MSK3 0x1cc
122#define ENT_INT_SRC_MSK3_ENT95_MSK_OFF 31
123#define ENT_INT_SRC_MSK3_ENT95_MSK_MSK (0x1 << ENT_INT_SRC_MSK3_ENT95_MSK_OFF)
d3b688d3
XC
124#define SAS_ECC_INTR 0x1e8
125#define SAS_ECC_INTR_DQE_ECC_1B_OFF 0
126#define SAS_ECC_INTR_DQE_ECC_MB_OFF 1
127#define SAS_ECC_INTR_IOST_ECC_1B_OFF 2
128#define SAS_ECC_INTR_IOST_ECC_MB_OFF 3
129#define SAS_ECC_INTR_ITCT_ECC_MB_OFF 4
130#define SAS_ECC_INTR_ITCT_ECC_1B_OFF 5
131#define SAS_ECC_INTR_IOSTLIST_ECC_MB_OFF 6
132#define SAS_ECC_INTR_IOSTLIST_ECC_1B_OFF 7
133#define SAS_ECC_INTR_ITCTLIST_ECC_1B_OFF 8
134#define SAS_ECC_INTR_ITCTLIST_ECC_MB_OFF 9
135#define SAS_ECC_INTR_CQE_ECC_1B_OFF 10
136#define SAS_ECC_INTR_CQE_ECC_MB_OFF 11
137#define SAS_ECC_INTR_NCQ_MEM0_ECC_MB_OFF 12
138#define SAS_ECC_INTR_NCQ_MEM0_ECC_1B_OFF 13
139#define SAS_ECC_INTR_NCQ_MEM1_ECC_MB_OFF 14
140#define SAS_ECC_INTR_NCQ_MEM1_ECC_1B_OFF 15
141#define SAS_ECC_INTR_NCQ_MEM2_ECC_MB_OFF 16
142#define SAS_ECC_INTR_NCQ_MEM2_ECC_1B_OFF 17
143#define SAS_ECC_INTR_NCQ_MEM3_ECC_MB_OFF 18
144#define SAS_ECC_INTR_NCQ_MEM3_ECC_1B_OFF 19
45c901b8
JG
145#define SAS_ECC_INTR_MSK 0x1ec
146#define HGC_ERR_STAT_EN 0x238
147#define DLVRY_Q_0_BASE_ADDR_LO 0x260
148#define DLVRY_Q_0_BASE_ADDR_HI 0x264
149#define DLVRY_Q_0_DEPTH 0x268
150#define DLVRY_Q_0_WR_PTR 0x26c
151#define DLVRY_Q_0_RD_PTR 0x270
152#define HYPER_STREAM_ID_EN_CFG 0xc80
153#define OQ0_INT_SRC_MSK 0xc90
154#define COMPL_Q_0_BASE_ADDR_LO 0x4e0
155#define COMPL_Q_0_BASE_ADDR_HI 0x4e4
156#define COMPL_Q_0_DEPTH 0x4e8
157#define COMPL_Q_0_WR_PTR 0x4ec
158#define COMPL_Q_0_RD_PTR 0x4f0
d3b688d3
XC
159#define HGC_RXM_DFX_STATUS14 0xae8
160#define HGC_RXM_DFX_STATUS14_MEM0_OFF 0
161#define HGC_RXM_DFX_STATUS14_MEM0_MSK (0x1ff << \
162 HGC_RXM_DFX_STATUS14_MEM0_OFF)
163#define HGC_RXM_DFX_STATUS14_MEM1_OFF 9
164#define HGC_RXM_DFX_STATUS14_MEM1_MSK (0x1ff << \
165 HGC_RXM_DFX_STATUS14_MEM1_OFF)
166#define HGC_RXM_DFX_STATUS14_MEM2_OFF 18
167#define HGC_RXM_DFX_STATUS14_MEM2_MSK (0x1ff << \
168 HGC_RXM_DFX_STATUS14_MEM2_OFF)
169#define HGC_RXM_DFX_STATUS15 0xaec
170#define HGC_RXM_DFX_STATUS15_MEM3_OFF 0
171#define HGC_RXM_DFX_STATUS15_MEM3_MSK (0x1ff << \
172 HGC_RXM_DFX_STATUS15_MEM3_OFF)
45c901b8
JG
173/* phy registers need init */
174#define PORT_BASE (0x2000)
175
176#define PHY_CFG (PORT_BASE + 0x0)
177#define HARD_PHY_LINKRATE (PORT_BASE + 0x4)
178#define PHY_CFG_ENA_OFF 0
179#define PHY_CFG_ENA_MSK (0x1 << PHY_CFG_ENA_OFF)
180#define PHY_CFG_DC_OPT_OFF 2
181#define PHY_CFG_DC_OPT_MSK (0x1 << PHY_CFG_DC_OPT_OFF)
182#define PROG_PHY_LINK_RATE (PORT_BASE + 0x8)
183#define PROG_PHY_LINK_RATE_MAX_OFF 0
184#define PROG_PHY_LINK_RATE_MAX_MSK (0xff << PROG_PHY_LINK_RATE_MAX_OFF)
185#define PHY_CTRL (PORT_BASE + 0x14)
186#define PHY_CTRL_RESET_OFF 0
187#define PHY_CTRL_RESET_MSK (0x1 << PHY_CTRL_RESET_OFF)
188#define SAS_PHY_CTRL (PORT_BASE + 0x20)
189#define SL_CFG (PORT_BASE + 0x84)
190#define PHY_PCN (PORT_BASE + 0x44)
191#define SL_TOUT_CFG (PORT_BASE + 0x8c)
192#define SL_CONTROL (PORT_BASE + 0x94)
193#define SL_CONTROL_NOTIFY_EN_OFF 0
194#define SL_CONTROL_NOTIFY_EN_MSK (0x1 << SL_CONTROL_NOTIFY_EN_OFF)
9c81e2cf
JG
195#define SL_CONTROL_CTA_OFF 17
196#define SL_CONTROL_CTA_MSK (0x1 << SL_CONTROL_CTA_OFF)
85080a25
XC
197#define RX_PRIMS_STATUS (PORT_BASE + 0x98)
198#define RX_BCAST_CHG_OFF 1
199#define RX_BCAST_CHG_MSK (0x1 << RX_BCAST_CHG_OFF)
45c901b8
JG
200#define TX_ID_DWORD0 (PORT_BASE + 0x9c)
201#define TX_ID_DWORD1 (PORT_BASE + 0xa0)
202#define TX_ID_DWORD2 (PORT_BASE + 0xa4)
203#define TX_ID_DWORD3 (PORT_BASE + 0xa8)
204#define TX_ID_DWORD4 (PORT_BASE + 0xaC)
205#define TX_ID_DWORD5 (PORT_BASE + 0xb0)
206#define TX_ID_DWORD6 (PORT_BASE + 0xb4)
9c81e2cf
JG
207#define TXID_AUTO (PORT_BASE + 0xb8)
208#define TXID_AUTO_CT3_OFF 1
209#define TXID_AUTO_CT3_MSK (0x1 << TXID_AUTO_CT3_OFF)
0edef7e4
XC
210#define TX_HARDRST_OFF 2
211#define TX_HARDRST_MSK (0x1 << TX_HARDRST_OFF)
45c901b8
JG
212#define RX_IDAF_DWORD0 (PORT_BASE + 0xc4)
213#define RX_IDAF_DWORD1 (PORT_BASE + 0xc8)
214#define RX_IDAF_DWORD2 (PORT_BASE + 0xcc)
215#define RX_IDAF_DWORD3 (PORT_BASE + 0xd0)
216#define RX_IDAF_DWORD4 (PORT_BASE + 0xd4)
217#define RX_IDAF_DWORD5 (PORT_BASE + 0xd8)
218#define RX_IDAF_DWORD6 (PORT_BASE + 0xdc)
219#define RXOP_CHECK_CFG_H (PORT_BASE + 0xfc)
f2f89c32 220#define CON_CONTROL (PORT_BASE + 0x118)
45c901b8
JG
221#define DONE_RECEIVED_TIME (PORT_BASE + 0x11c)
222#define CHL_INT0 (PORT_BASE + 0x1b4)
223#define CHL_INT0_HOTPLUG_TOUT_OFF 0
224#define CHL_INT0_HOTPLUG_TOUT_MSK (0x1 << CHL_INT0_HOTPLUG_TOUT_OFF)
225#define CHL_INT0_SL_RX_BCST_ACK_OFF 1
226#define CHL_INT0_SL_RX_BCST_ACK_MSK (0x1 << CHL_INT0_SL_RX_BCST_ACK_OFF)
227#define CHL_INT0_SL_PHY_ENABLE_OFF 2
228#define CHL_INT0_SL_PHY_ENABLE_MSK (0x1 << CHL_INT0_SL_PHY_ENABLE_OFF)
229#define CHL_INT0_NOT_RDY_OFF 4
230#define CHL_INT0_NOT_RDY_MSK (0x1 << CHL_INT0_NOT_RDY_OFF)
231#define CHL_INT0_PHY_RDY_OFF 5
232#define CHL_INT0_PHY_RDY_MSK (0x1 << CHL_INT0_PHY_RDY_OFF)
233#define CHL_INT1 (PORT_BASE + 0x1b8)
234#define CHL_INT1_DMAC_TX_ECC_ERR_OFF 15
235#define CHL_INT1_DMAC_TX_ECC_ERR_MSK (0x1 << CHL_INT1_DMAC_TX_ECC_ERR_OFF)
236#define CHL_INT1_DMAC_RX_ECC_ERR_OFF 17
237#define CHL_INT1_DMAC_RX_ECC_ERR_MSK (0x1 << CHL_INT1_DMAC_RX_ECC_ERR_OFF)
238#define CHL_INT2 (PORT_BASE + 0x1bc)
239#define CHL_INT0_MSK (PORT_BASE + 0x1c0)
240#define CHL_INT1_MSK (PORT_BASE + 0x1c4)
241#define CHL_INT2_MSK (PORT_BASE + 0x1c8)
242#define CHL_INT_COAL_EN (PORT_BASE + 0x1d0)
243#define PHY_CTRL_RDY_MSK (PORT_BASE + 0x2b0)
244#define PHYCTRL_NOT_RDY_MSK (PORT_BASE + 0x2b4)
245#define PHYCTRL_DWS_RESET_MSK (PORT_BASE + 0x2b8)
246#define PHYCTRL_PHY_ENA_MSK (PORT_BASE + 0x2bc)
247#define SL_RX_BCAST_CHK_MSK (PORT_BASE + 0x2c0)
248#define PHYCTRL_OOB_RESTART_MSK (PORT_BASE + 0x2c4)
249#define DMA_TX_STATUS (PORT_BASE + 0x2d0)
250#define DMA_TX_STATUS_BUSY_OFF 0
251#define DMA_TX_STATUS_BUSY_MSK (0x1 << DMA_TX_STATUS_BUSY_OFF)
252#define DMA_RX_STATUS (PORT_BASE + 0x2e8)
253#define DMA_RX_STATUS_BUSY_OFF 0
254#define DMA_RX_STATUS_BUSY_MSK (0x1 << DMA_RX_STATUS_BUSY_OFF)
255
256#define AXI_CFG (0x5100)
257#define AM_CFG_MAX_TRANS (0x5010)
258#define AM_CFG_SINGLE_PORT_MAX_TRANS (0x5014)
259
06ec0fb9
XC
260#define AXI_MASTER_CFG_BASE (0x5000)
261#define AM_CTRL_GLOBAL (0x0)
262#define AM_CURR_TRANS_RETURN (0x150)
263
45c901b8
JG
264/* HW dma structures */
265/* Delivery queue header */
266/* dw0 */
a3e665d9
JG
267#define CMD_HDR_ABORT_FLAG_OFF 0
268#define CMD_HDR_ABORT_FLAG_MSK (0x3 << CMD_HDR_ABORT_FLAG_OFF)
269#define CMD_HDR_ABORT_DEVICE_TYPE_OFF 2
270#define CMD_HDR_ABORT_DEVICE_TYPE_MSK (0x1 << CMD_HDR_ABORT_DEVICE_TYPE_OFF)
45c901b8
JG
271#define CMD_HDR_RESP_REPORT_OFF 5
272#define CMD_HDR_RESP_REPORT_MSK (0x1 << CMD_HDR_RESP_REPORT_OFF)
273#define CMD_HDR_TLR_CTRL_OFF 6
274#define CMD_HDR_TLR_CTRL_MSK (0x3 << CMD_HDR_TLR_CTRL_OFF)
275#define CMD_HDR_PORT_OFF 18
276#define CMD_HDR_PORT_MSK (0xf << CMD_HDR_PORT_OFF)
277#define CMD_HDR_PRIORITY_OFF 27
278#define CMD_HDR_PRIORITY_MSK (0x1 << CMD_HDR_PRIORITY_OFF)
279#define CMD_HDR_CMD_OFF 29
280#define CMD_HDR_CMD_MSK (0x7 << CMD_HDR_CMD_OFF)
281/* dw1 */
282#define CMD_HDR_DIR_OFF 5
283#define CMD_HDR_DIR_MSK (0x3 << CMD_HDR_DIR_OFF)
284#define CMD_HDR_RESET_OFF 7
285#define CMD_HDR_RESET_MSK (0x1 << CMD_HDR_RESET_OFF)
286#define CMD_HDR_VDTL_OFF 10
287#define CMD_HDR_VDTL_MSK (0x1 << CMD_HDR_VDTL_OFF)
288#define CMD_HDR_FRAME_TYPE_OFF 11
289#define CMD_HDR_FRAME_TYPE_MSK (0x1f << CMD_HDR_FRAME_TYPE_OFF)
290#define CMD_HDR_DEV_ID_OFF 16
291#define CMD_HDR_DEV_ID_MSK (0xffff << CMD_HDR_DEV_ID_OFF)
292/* dw2 */
293#define CMD_HDR_CFL_OFF 0
294#define CMD_HDR_CFL_MSK (0x1ff << CMD_HDR_CFL_OFF)
295#define CMD_HDR_NCQ_TAG_OFF 10
296#define CMD_HDR_NCQ_TAG_MSK (0x1f << CMD_HDR_NCQ_TAG_OFF)
297#define CMD_HDR_MRFL_OFF 15
298#define CMD_HDR_MRFL_MSK (0x1ff << CMD_HDR_MRFL_OFF)
299#define CMD_HDR_SG_MOD_OFF 24
300#define CMD_HDR_SG_MOD_MSK (0x3 << CMD_HDR_SG_MOD_OFF)
301#define CMD_HDR_FIRST_BURST_OFF 26
302#define CMD_HDR_FIRST_BURST_MSK (0x1 << CMD_HDR_SG_MOD_OFF)
303/* dw3 */
304#define CMD_HDR_IPTT_OFF 0
305#define CMD_HDR_IPTT_MSK (0xffff << CMD_HDR_IPTT_OFF)
306/* dw6 */
307#define CMD_HDR_DIF_SGL_LEN_OFF 0
308#define CMD_HDR_DIF_SGL_LEN_MSK (0xffff << CMD_HDR_DIF_SGL_LEN_OFF)
309#define CMD_HDR_DATA_SGL_LEN_OFF 16
310#define CMD_HDR_DATA_SGL_LEN_MSK (0xffff << CMD_HDR_DATA_SGL_LEN_OFF)
a3e665d9
JG
311#define CMD_HDR_ABORT_IPTT_OFF 16
312#define CMD_HDR_ABORT_IPTT_MSK (0xffff << CMD_HDR_ABORT_IPTT_OFF)
45c901b8
JG
313
314/* Completion header */
315/* dw0 */
634a9585
XC
316#define CMPLT_HDR_ERR_PHASE_OFF 2
317#define CMPLT_HDR_ERR_PHASE_MSK (0xff << CMPLT_HDR_ERR_PHASE_OFF)
45c901b8
JG
318#define CMPLT_HDR_RSPNS_XFRD_OFF 10
319#define CMPLT_HDR_RSPNS_XFRD_MSK (0x1 << CMPLT_HDR_RSPNS_XFRD_OFF)
320#define CMPLT_HDR_ERX_OFF 12
321#define CMPLT_HDR_ERX_MSK (0x1 << CMPLT_HDR_ERX_OFF)
df032d0e
JG
322#define CMPLT_HDR_ABORT_STAT_OFF 13
323#define CMPLT_HDR_ABORT_STAT_MSK (0x7 << CMPLT_HDR_ABORT_STAT_OFF)
324/* abort_stat */
325#define STAT_IO_NOT_VALID 0x1
326#define STAT_IO_NO_DEVICE 0x2
327#define STAT_IO_COMPLETE 0x3
328#define STAT_IO_ABORTED 0x4
45c901b8
JG
329/* dw1 */
330#define CMPLT_HDR_IPTT_OFF 0
331#define CMPLT_HDR_IPTT_MSK (0xffff << CMPLT_HDR_IPTT_OFF)
332#define CMPLT_HDR_DEV_ID_OFF 16
333#define CMPLT_HDR_DEV_ID_MSK (0xffff << CMPLT_HDR_DEV_ID_OFF)
334
335/* ITCT header */
336/* qw0 */
337#define ITCT_HDR_DEV_TYPE_OFF 0
338#define ITCT_HDR_DEV_TYPE_MSK (0x3 << ITCT_HDR_DEV_TYPE_OFF)
339#define ITCT_HDR_VALID_OFF 2
340#define ITCT_HDR_VALID_MSK (0x1 << ITCT_HDR_VALID_OFF)
341#define ITCT_HDR_MCR_OFF 5
342#define ITCT_HDR_MCR_MSK (0xf << ITCT_HDR_MCR_OFF)
343#define ITCT_HDR_VLN_OFF 9
344#define ITCT_HDR_VLN_MSK (0xf << ITCT_HDR_VLN_OFF)
c399acfb
XC
345#define ITCT_HDR_SMP_TIMEOUT_OFF 16
346#define ITCT_HDR_SMP_TIMEOUT_8US 1
347#define ITCT_HDR_SMP_TIMEOUT (ITCT_HDR_SMP_TIMEOUT_8US * \
348 250) /* 2ms */
349#define ITCT_HDR_AWT_CONTINUE_OFF 25
45c901b8
JG
350#define ITCT_HDR_PORT_ID_OFF 28
351#define ITCT_HDR_PORT_ID_MSK (0xf << ITCT_HDR_PORT_ID_OFF)
352/* qw2 */
353#define ITCT_HDR_INLT_OFF 0
354#define ITCT_HDR_INLT_MSK (0xffffULL << ITCT_HDR_INLT_OFF)
355#define ITCT_HDR_BITLT_OFF 16
356#define ITCT_HDR_BITLT_MSK (0xffffULL << ITCT_HDR_BITLT_OFF)
357#define ITCT_HDR_MCTLT_OFF 32
358#define ITCT_HDR_MCTLT_MSK (0xffffULL << ITCT_HDR_MCTLT_OFF)
359#define ITCT_HDR_RTOLT_OFF 48
360#define ITCT_HDR_RTOLT_MSK (0xffffULL << ITCT_HDR_RTOLT_OFF)
361
d3b688d3
XC
362#define HISI_SAS_FATAL_INT_NR 2
363
94eac9e1
JG
364struct hisi_sas_complete_v2_hdr {
365 __le32 dw0;
366 __le32 dw1;
367 __le32 act;
368 __le32 dw3;
369};
370
e8fed0e9
JG
371struct hisi_sas_err_record_v2 {
372 /* dw0 */
373 __le32 trans_tx_fail_type;
374
375 /* dw1 */
376 __le32 trans_rx_fail_type;
377
378 /* dw2 */
379 __le16 dma_tx_err_type;
380 __le16 sipc_rx_err_type;
381
382 /* dw3 */
383 __le32 dma_rx_err_type;
384};
385
7911e66f
JG
386enum {
387 HISI_SAS_PHY_PHY_UPDOWN,
d3bf3d84 388 HISI_SAS_PHY_CHNL_INT,
7911e66f
JG
389 HISI_SAS_PHY_INT_NR
390};
391
e8fed0e9
JG
392enum {
393 TRANS_TX_FAIL_BASE = 0x0, /* dw0 */
634a9585
XC
394 TRANS_RX_FAIL_BASE = 0x20, /* dw1 */
395 DMA_TX_ERR_BASE = 0x40, /* dw2 bit 15-0 */
396 SIPC_RX_ERR_BASE = 0x50, /* dw2 bit 31-16*/
397 DMA_RX_ERR_BASE = 0x60, /* dw3 */
e8fed0e9
JG
398
399 /* trans tx*/
400 TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS = TRANS_TX_FAIL_BASE, /* 0x0 */
401 TRANS_TX_ERR_PHY_NOT_ENABLE, /* 0x1 */
402 TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION, /* 0x2 */
403 TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION, /* 0x3 */
404 TRANS_TX_OPEN_CNX_ERR_BY_OTHER, /* 0x4 */
405 RESERVED0, /* 0x5 */
406 TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT, /* 0x6 */
407 TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY, /* 0x7 */
408 TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED, /* 0x8 */
409 TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED, /* 0x9 */
410 TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION, /* 0xa */
411 TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD, /* 0xb */
412 TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER, /* 0xc */
413 TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED, /* 0xd */
414 TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT, /* 0xe */
415 TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION, /* 0xf */
416 TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED, /* 0x10 */
417 TRANS_TX_ERR_FRAME_TXED, /* 0x11 */
418 TRANS_TX_ERR_WITH_BREAK_TIMEOUT, /* 0x12 */
419 TRANS_TX_ERR_WITH_BREAK_REQUEST, /* 0x13 */
420 TRANS_TX_ERR_WITH_BREAK_RECEVIED, /* 0x14 */
421 TRANS_TX_ERR_WITH_CLOSE_TIMEOUT, /* 0x15 */
422 TRANS_TX_ERR_WITH_CLOSE_NORMAL, /* 0x16 for ssp*/
423 TRANS_TX_ERR_WITH_CLOSE_PHYDISALE, /* 0x17 */
424 TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT, /* 0x18 */
425 TRANS_TX_ERR_WITH_CLOSE_COMINIT, /* 0x19 */
426 TRANS_TX_ERR_WITH_NAK_RECEVIED, /* 0x1a for ssp*/
427 TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT, /* 0x1b for ssp*/
428 /*IO_TX_ERR_WITH_R_ERR_RECEVIED, [> 0x1b for sata/stp<] */
429 TRANS_TX_ERR_WITH_CREDIT_TIMEOUT, /* 0x1c for ssp */
430 /*IO_RX_ERR_WITH_SATA_DEVICE_LOST 0x1c for sata/stp */
431 TRANS_TX_ERR_WITH_IPTT_CONFLICT, /* 0x1d for ssp/smp */
432 TRANS_TX_ERR_WITH_OPEN_BY_DES_OR_OTHERS, /* 0x1e */
433 /*IO_TX_ERR_WITH_SYNC_RXD, [> 0x1e <] for sata/stp */
434 TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT, /* 0x1f for sata/stp */
435
436 /* trans rx */
634a9585
XC
437 TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR = TRANS_RX_FAIL_BASE, /* 0x20 */
438 TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR, /* 0x21 for sata/stp */
439 TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM, /* 0x22 for ssp/smp */
440 /*IO_ERR_WITH_RXFIS_8B10B_CODE_ERR, [> 0x22 <] for sata/stp */
441 TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR, /* 0x23 for sata/stp */
442 TRANS_RX_ERR_WITH_RXFIS_CRC_ERR, /* 0x24 for sata/stp */
443 TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN, /* 0x25 for smp */
444 /*IO_ERR_WITH_RXFIS_TX SYNCP, [> 0x25 <] for sata/stp */
445 TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP, /* 0x26 for sata/stp*/
446 TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN, /* 0x27 */
447 TRANS_RX_ERR_WITH_BREAK_TIMEOUT, /* 0x28 */
448 TRANS_RX_ERR_WITH_BREAK_REQUEST, /* 0x29 */
449 TRANS_RX_ERR_WITH_BREAK_RECEVIED, /* 0x2a */
450 RESERVED1, /* 0x2b */
451 TRANS_RX_ERR_WITH_CLOSE_NORMAL, /* 0x2c */
452 TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE, /* 0x2d */
453 TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT, /* 0x2e */
454 TRANS_RX_ERR_WITH_CLOSE_COMINIT, /* 0x2f */
455 TRANS_RX_ERR_WITH_DATA_LEN0, /* 0x30 for ssp/smp */
456 TRANS_RX_ERR_WITH_BAD_HASH, /* 0x31 for ssp */
457 /*IO_RX_ERR_WITH_FIS_TOO_SHORT, [> 0x31 <] for sata/stp */
458 TRANS_RX_XRDY_WLEN_ZERO_ERR, /* 0x32 for ssp*/
459 /*IO_RX_ERR_WITH_FIS_TOO_LONG, [> 0x32 <] for sata/stp */
460 TRANS_RX_SSP_FRM_LEN_ERR, /* 0x33 for ssp */
461 /*IO_RX_ERR_WITH_SATA_DEVICE_LOST, [> 0x33 <] for sata */
462 RESERVED2, /* 0x34 */
463 RESERVED3, /* 0x35 */
464 RESERVED4, /* 0x36 */
465 RESERVED5, /* 0x37 */
466 TRANS_RX_ERR_WITH_BAD_FRM_TYPE, /* 0x38 */
467 TRANS_RX_SMP_FRM_LEN_ERR, /* 0x39 */
468 TRANS_RX_SMP_RESP_TIMEOUT_ERR, /* 0x3a */
469 RESERVED6, /* 0x3b */
470 RESERVED7, /* 0x3c */
471 RESERVED8, /* 0x3d */
472 RESERVED9, /* 0x3e */
473 TRANS_RX_R_ERR, /* 0x3f */
e8fed0e9
JG
474
475 /* dma tx */
634a9585
XC
476 DMA_TX_DIF_CRC_ERR = DMA_TX_ERR_BASE, /* 0x40 */
477 DMA_TX_DIF_APP_ERR, /* 0x41 */
478 DMA_TX_DIF_RPP_ERR, /* 0x42 */
479 DMA_TX_DATA_SGL_OVERFLOW, /* 0x43 */
480 DMA_TX_DIF_SGL_OVERFLOW, /* 0x44 */
481 DMA_TX_UNEXP_XFER_ERR, /* 0x45 */
482 DMA_TX_UNEXP_RETRANS_ERR, /* 0x46 */
483 DMA_TX_XFER_LEN_OVERFLOW, /* 0x47 */
484 DMA_TX_XFER_OFFSET_ERR, /* 0x48 */
485 DMA_TX_RAM_ECC_ERR, /* 0x49 */
486 DMA_TX_DIF_LEN_ALIGN_ERR, /* 0x4a */
487 DMA_TX_MAX_ERR_CODE,
e8fed0e9
JG
488
489 /* sipc rx */
634a9585
XC
490 SIPC_RX_FIS_STATUS_ERR_BIT_VLD = SIPC_RX_ERR_BASE, /* 0x50 */
491 SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR, /* 0x51 */
492 SIPC_RX_FIS_STATUS_BSY_BIT_ERR, /* 0x52 */
493 SIPC_RX_WRSETUP_LEN_ODD_ERR, /* 0x53 */
494 SIPC_RX_WRSETUP_LEN_ZERO_ERR, /* 0x54 */
495 SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR, /* 0x55 */
496 SIPC_RX_NCQ_WRSETUP_OFFSET_ERR, /* 0x56 */
497 SIPC_RX_NCQ_WRSETUP_AUTO_ACTIVE_ERR, /* 0x57 */
498 SIPC_RX_SATA_UNEXP_FIS_ERR, /* 0x58 */
499 SIPC_RX_WRSETUP_ESTATUS_ERR, /* 0x59 */
500 SIPC_RX_DATA_UNDERFLOW_ERR, /* 0x5a */
501 SIPC_RX_MAX_ERR_CODE,
e8fed0e9
JG
502
503 /* dma rx */
634a9585
XC
504 DMA_RX_DIF_CRC_ERR = DMA_RX_ERR_BASE, /* 0x60 */
505 DMA_RX_DIF_APP_ERR, /* 0x61 */
506 DMA_RX_DIF_RPP_ERR, /* 0x62 */
507 DMA_RX_DATA_SGL_OVERFLOW, /* 0x63 */
508 DMA_RX_DIF_SGL_OVERFLOW, /* 0x64 */
509 DMA_RX_DATA_LEN_OVERFLOW, /* 0x65 */
510 DMA_RX_DATA_LEN_UNDERFLOW, /* 0x66 */
511 DMA_RX_DATA_OFFSET_ERR, /* 0x67 */
512 RESERVED10, /* 0x68 */
513 DMA_RX_SATA_FRAME_TYPE_ERR, /* 0x69 */
514 DMA_RX_RESP_BUF_OVERFLOW, /* 0x6a */
515 DMA_RX_UNEXP_RETRANS_RESP_ERR, /* 0x6b */
516 DMA_RX_UNEXP_NORM_RESP_ERR, /* 0x6c */
517 DMA_RX_UNEXP_RDFRAME_ERR, /* 0x6d */
518 DMA_RX_PIO_DATA_LEN_ERR, /* 0x6e */
519 DMA_RX_RDSETUP_STATUS_ERR, /* 0x6f */
520 DMA_RX_RDSETUP_STATUS_DRQ_ERR, /* 0x70 */
521 DMA_RX_RDSETUP_STATUS_BSY_ERR, /* 0x71 */
522 DMA_RX_RDSETUP_LEN_ODD_ERR, /* 0x72 */
523 DMA_RX_RDSETUP_LEN_ZERO_ERR, /* 0x73 */
524 DMA_RX_RDSETUP_LEN_OVER_ERR, /* 0x74 */
525 DMA_RX_RDSETUP_OFFSET_ERR, /* 0x75 */
526 DMA_RX_RDSETUP_ACTIVE_ERR, /* 0x76 */
527 DMA_RX_RDSETUP_ESTATUS_ERR, /* 0x77 */
528 DMA_RX_RAM_ECC_ERR, /* 0x78 */
529 DMA_RX_UNKNOWN_FRM_ERR, /* 0x79 */
530 DMA_RX_MAX_ERR_CODE,
e8fed0e9
JG
531};
532
94eac9e1
JG
533#define HISI_SAS_COMMAND_ENTRIES_V2_HW 4096
534
8c36e31d
JG
535#define DIR_NO_DATA 0
536#define DIR_TO_INI 1
537#define DIR_TO_DEVICE 2
538#define DIR_RESERVED 3
539
6f2ff1a1
JG
540#define SATA_PROTOCOL_NONDATA 0x1
541#define SATA_PROTOCOL_PIO 0x2
542#define SATA_PROTOCOL_DMA 0x4
543#define SATA_PROTOCOL_FPDMA 0x8
544#define SATA_PROTOCOL_ATAPI 0x10
545
634a9585
XC
546#define ERR_ON_TX_PHASE(err_phase) (err_phase == 0x2 || \
547 err_phase == 0x4 || err_phase == 0x8 ||\
548 err_phase == 0x6 || err_phase == 0xa)
549#define ERR_ON_RX_PHASE(err_phase) (err_phase == 0x10 || \
550 err_phase == 0x20 || err_phase == 0x40)
551
4df642db 552static void link_timeout_disable_link(unsigned long data);
f2f89c32 553
94eac9e1
JG
554static u32 hisi_sas_read32(struct hisi_hba *hisi_hba, u32 off)
555{
556 void __iomem *regs = hisi_hba->regs + off;
557
558 return readl(regs);
559}
560
8c36e31d
JG
561static u32 hisi_sas_read32_relaxed(struct hisi_hba *hisi_hba, u32 off)
562{
563 void __iomem *regs = hisi_hba->regs + off;
564
565 return readl_relaxed(regs);
566}
567
94eac9e1
JG
568static void hisi_sas_write32(struct hisi_hba *hisi_hba, u32 off, u32 val)
569{
570 void __iomem *regs = hisi_hba->regs + off;
571
572 writel(val, regs);
573}
574
575static void hisi_sas_phy_write32(struct hisi_hba *hisi_hba, int phy_no,
576 u32 off, u32 val)
577{
578 void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
579
580 writel(val, regs);
581}
582
583static u32 hisi_sas_phy_read32(struct hisi_hba *hisi_hba,
584 int phy_no, u32 off)
585{
586 void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
587
588 return readl(regs);
589}
590
330fa7f3
JG
591/* This function needs to be protected from pre-emption. */
592static int
593slot_index_alloc_quirk_v2_hw(struct hisi_hba *hisi_hba, int *slot_idx,
594 struct domain_device *device)
595{
596 unsigned int index = 0;
597 void *bitmap = hisi_hba->slot_index_tags;
598 int sata_dev = dev_is_sata(device);
599
600 while (1) {
601 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
602 index);
603 if (index >= hisi_hba->slot_index_count)
604 return -SAS_QUEUE_FULL;
605 /*
606 * SAS IPTT bit0 should be 1
607 */
608 if (sata_dev || (index & 1))
609 break;
610 index++;
611 }
612
613 set_bit(index, bitmap);
614 *slot_idx = index;
615 return 0;
616}
617
b2bdaf2b
JG
618static struct
619hisi_sas_device *alloc_dev_quirk_v2_hw(struct domain_device *device)
620{
621 struct hisi_hba *hisi_hba = device->port->ha->lldd_ha;
622 struct hisi_sas_device *sas_dev = NULL;
623 int i, sata_dev = dev_is_sata(device);
624
625 spin_lock(&hisi_hba->lock);
626 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
627 /*
628 * SATA device id bit0 should be 0
629 */
630 if (sata_dev && (i & 1))
631 continue;
632 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
633 hisi_hba->devices[i].device_id = i;
634 sas_dev = &hisi_hba->devices[i];
635 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
636 sas_dev->dev_type = device->dev_type;
637 sas_dev->hisi_hba = hisi_hba;
638 sas_dev->sas_device = device;
405314df 639 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
b2bdaf2b
JG
640 break;
641 }
642 }
643 spin_unlock(&hisi_hba->lock);
644
645 return sas_dev;
646}
647
29a20428
JG
648static void config_phy_opt_mode_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
649{
650 u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
651
652 cfg &= ~PHY_CFG_DC_OPT_MSK;
653 cfg |= 1 << PHY_CFG_DC_OPT_OFF;
654 hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
655}
656
806bb768
JG
657static void config_id_frame_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
658{
659 struct sas_identify_frame identify_frame;
660 u32 *identify_buffer;
661
662 memset(&identify_frame, 0, sizeof(identify_frame));
663 identify_frame.dev_type = SAS_END_DEVICE;
664 identify_frame.frame_type = 0;
665 identify_frame._un1 = 1;
666 identify_frame.initiator_bits = SAS_PROTOCOL_ALL;
667 identify_frame.target_bits = SAS_PROTOCOL_NONE;
668 memcpy(&identify_frame._un4_11[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
669 memcpy(&identify_frame.sas_addr[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
670 identify_frame.phy_id = phy_no;
671 identify_buffer = (u32 *)(&identify_frame);
672
673 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD0,
674 __swab32(identify_buffer[0]));
675 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD1,
d82debec 676 __swab32(identify_buffer[1]));
806bb768 677 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD2,
d82debec 678 __swab32(identify_buffer[2]));
806bb768 679 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD3,
d82debec 680 __swab32(identify_buffer[3]));
806bb768 681 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD4,
d82debec 682 __swab32(identify_buffer[4]));
806bb768
JG
683 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD5,
684 __swab32(identify_buffer[5]));
685}
686
85b2c3c0
JG
687static void setup_itct_v2_hw(struct hisi_hba *hisi_hba,
688 struct hisi_sas_device *sas_dev)
689{
690 struct domain_device *device = sas_dev->sas_device;
691 struct device *dev = &hisi_hba->pdev->dev;
692 u64 qw0, device_id = sas_dev->device_id;
693 struct hisi_sas_itct *itct = &hisi_hba->itct[device_id];
694 struct domain_device *parent_dev = device->parent;
2e244f0f
JG
695 struct asd_sas_port *sas_port = device->port;
696 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
85b2c3c0
JG
697
698 memset(itct, 0, sizeof(*itct));
699
700 /* qw0 */
701 qw0 = 0;
702 switch (sas_dev->dev_type) {
703 case SAS_END_DEVICE:
704 case SAS_EDGE_EXPANDER_DEVICE:
705 case SAS_FANOUT_EXPANDER_DEVICE:
706 qw0 = HISI_SAS_DEV_TYPE_SSP << ITCT_HDR_DEV_TYPE_OFF;
707 break;
708 case SAS_SATA_DEV:
56cc74b9 709 case SAS_SATA_PENDING:
85b2c3c0
JG
710 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type))
711 qw0 = HISI_SAS_DEV_TYPE_STP << ITCT_HDR_DEV_TYPE_OFF;
712 else
713 qw0 = HISI_SAS_DEV_TYPE_SATA << ITCT_HDR_DEV_TYPE_OFF;
714 break;
715 default:
716 dev_warn(dev, "setup itct: unsupported dev type (%d)\n",
717 sas_dev->dev_type);
718 }
719
720 qw0 |= ((1 << ITCT_HDR_VALID_OFF) |
75249268 721 (device->linkrate << ITCT_HDR_MCR_OFF) |
85b2c3c0 722 (1 << ITCT_HDR_VLN_OFF) |
c399acfb
XC
723 (ITCT_HDR_SMP_TIMEOUT << ITCT_HDR_SMP_TIMEOUT_OFF) |
724 (1 << ITCT_HDR_AWT_CONTINUE_OFF) |
85b2c3c0
JG
725 (port->id << ITCT_HDR_PORT_ID_OFF));
726 itct->qw0 = cpu_to_le64(qw0);
727
728 /* qw1 */
729 memcpy(&itct->sas_addr, device->sas_addr, SAS_ADDR_SIZE);
730 itct->sas_addr = __swab64(itct->sas_addr);
731
732 /* qw2 */
f76a0b49 733 if (!dev_is_sata(device))
c399acfb 734 itct->qw2 = cpu_to_le64((5000ULL << ITCT_HDR_INLT_OFF) |
f76a0b49
JG
735 (0x1ULL << ITCT_HDR_BITLT_OFF) |
736 (0x32ULL << ITCT_HDR_MCTLT_OFF) |
737 (0x1ULL << ITCT_HDR_RTOLT_OFF));
85b2c3c0
JG
738}
739
740static void free_device_v2_hw(struct hisi_hba *hisi_hba,
741 struct hisi_sas_device *sas_dev)
742{
c399acfb 743 u64 dev_id = sas_dev->device_id;
85b2c3c0
JG
744 struct device *dev = &hisi_hba->pdev->dev;
745 struct hisi_sas_itct *itct = &hisi_hba->itct[dev_id];
746 u32 reg_val = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
747 int i;
748
749 /* clear the itct interrupt state */
750 if (ENT_INT_SRC3_ITC_INT_MSK & reg_val)
751 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
752 ENT_INT_SRC3_ITC_INT_MSK);
753
754 /* clear the itct int*/
755 for (i = 0; i < 2; i++) {
756 /* clear the itct table*/
757 reg_val = hisi_sas_read32(hisi_hba, ITCT_CLR);
758 reg_val |= ITCT_CLR_EN_MSK | (dev_id & ITCT_DEV_MSK);
759 hisi_sas_write32(hisi_hba, ITCT_CLR, reg_val);
760
761 udelay(10);
762 reg_val = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
763 if (ENT_INT_SRC3_ITC_INT_MSK & reg_val) {
764 dev_dbg(dev, "got clear ITCT done interrupt\n");
765
766 /* invalid the itct state*/
c399acfb 767 memset(itct, 0, sizeof(struct hisi_sas_itct));
85b2c3c0
JG
768 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
769 ENT_INT_SRC3_ITC_INT_MSK);
85b2c3c0
JG
770
771 /* clear the itct */
772 hisi_sas_write32(hisi_hba, ITCT_CLR, 0);
773 dev_dbg(dev, "clear ITCT ok\n");
774 break;
775 }
776 }
777}
778
94eac9e1
JG
779static int reset_hw_v2_hw(struct hisi_hba *hisi_hba)
780{
781 int i, reset_val;
782 u32 val;
783 unsigned long end_time;
784 struct device *dev = &hisi_hba->pdev->dev;
785
786 /* The mask needs to be set depending on the number of phys */
787 if (hisi_hba->n_phy == 9)
788 reset_val = 0x1fffff;
789 else
790 reset_val = 0x7ffff;
791
d0df8f9a 792 hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0);
94eac9e1
JG
793
794 /* Disable all of the PHYs */
795 for (i = 0; i < hisi_hba->n_phy; i++) {
796 u32 phy_cfg = hisi_sas_phy_read32(hisi_hba, i, PHY_CFG);
797
798 phy_cfg &= ~PHY_CTRL_RESET_MSK;
799 hisi_sas_phy_write32(hisi_hba, i, PHY_CFG, phy_cfg);
800 }
801 udelay(50);
802
803 /* Ensure DMA tx & rx idle */
804 for (i = 0; i < hisi_hba->n_phy; i++) {
805 u32 dma_tx_status, dma_rx_status;
806
807 end_time = jiffies + msecs_to_jiffies(1000);
808
809 while (1) {
810 dma_tx_status = hisi_sas_phy_read32(hisi_hba, i,
811 DMA_TX_STATUS);
812 dma_rx_status = hisi_sas_phy_read32(hisi_hba, i,
813 DMA_RX_STATUS);
814
815 if (!(dma_tx_status & DMA_TX_STATUS_BUSY_MSK) &&
816 !(dma_rx_status & DMA_RX_STATUS_BUSY_MSK))
817 break;
818
819 msleep(20);
820 if (time_after(jiffies, end_time))
821 return -EIO;
822 }
823 }
824
825 /* Ensure axi bus idle */
826 end_time = jiffies + msecs_to_jiffies(1000);
827 while (1) {
828 u32 axi_status =
829 hisi_sas_read32(hisi_hba, AXI_CFG);
830
831 if (axi_status == 0)
832 break;
833
834 msleep(20);
835 if (time_after(jiffies, end_time))
836 return -EIO;
837 }
838
50408712
JG
839 if (ACPI_HANDLE(dev)) {
840 acpi_status s;
94eac9e1 841
50408712
JG
842 s = acpi_evaluate_object(ACPI_HANDLE(dev), "_RST", NULL, NULL);
843 if (ACPI_FAILURE(s)) {
844 dev_err(dev, "Reset failed\n");
845 return -EIO;
846 }
847 } else if (hisi_hba->ctrl) {
848 /* reset and disable clock*/
849 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg,
850 reset_val);
851 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg + 4,
852 reset_val);
853 msleep(1);
854 regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg, &val);
855 if (reset_val != (val & reset_val)) {
856 dev_err(dev, "SAS reset fail.\n");
857 return -EIO;
858 }
859
860 /* De-reset and enable clock*/
861 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg + 4,
862 reset_val);
863 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg,
864 reset_val);
865 msleep(1);
866 regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg,
867 &val);
868 if (val & reset_val) {
869 dev_err(dev, "SAS de-reset fail.\n");
870 return -EIO;
871 }
872 } else
873 dev_warn(dev, "no reset method\n");
94eac9e1
JG
874
875 return 0;
876}
877
878static void init_reg_v2_hw(struct hisi_hba *hisi_hba)
879{
880 struct device *dev = &hisi_hba->pdev->dev;
94eac9e1
JG
881 int i;
882
883 /* Global registers init */
884
885 /* Deal with am-max-transmissions quirk */
50408712 886 if (device_property_present(dev, "hip06-sas-v2-quirk-amt")) {
94eac9e1
JG
887 hisi_sas_write32(hisi_hba, AM_CFG_MAX_TRANS, 0x2020);
888 hisi_sas_write32(hisi_hba, AM_CFG_SINGLE_PORT_MAX_TRANS,
889 0x2020);
890 } /* Else, use defaults -> do nothing */
891
892 hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE,
893 (u32)((1ULL << hisi_hba->queue_count) - 1));
894 hisi_sas_write32(hisi_hba, AXI_USER1, 0xc0000000);
895 hisi_sas_write32(hisi_hba, AXI_USER2, 0x10000);
f1dc7518 896 hisi_sas_write32(hisi_hba, HGC_SAS_TXFAIL_RETRY_CTRL, 0x0);
94eac9e1
JG
897 hisi_sas_write32(hisi_hba, HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL, 0x7FF);
898 hisi_sas_write32(hisi_hba, OPENA_WT_CONTI_TIME, 0x1);
899 hisi_sas_write32(hisi_hba, I_T_NEXUS_LOSS_TIME, 0x1F4);
f76a0b49 900 hisi_sas_write32(hisi_hba, MAX_CON_TIME_LIMIT_TIME, 0x32);
94eac9e1
JG
901 hisi_sas_write32(hisi_hba, BUS_INACTIVE_LIMIT_TIME, 0x1);
902 hisi_sas_write32(hisi_hba, CFG_AGING_TIME, 0x1);
903 hisi_sas_write32(hisi_hba, HGC_ERR_STAT_EN, 0x1);
904 hisi_sas_write32(hisi_hba, HGC_GET_ITV_TIME, 0x1);
f1dc7518
JG
905 hisi_sas_write32(hisi_hba, INT_COAL_EN, 0xc);
906 hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x60);
907 hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 0x3);
94eac9e1
JG
908 hisi_sas_write32(hisi_hba, ENT_INT_COAL_TIME, 0x1);
909 hisi_sas_write32(hisi_hba, ENT_INT_COAL_CNT, 0x1);
910 hisi_sas_write32(hisi_hba, OQ_INT_SRC, 0x0);
911 hisi_sas_write32(hisi_hba, ENT_INT_SRC1, 0xffffffff);
912 hisi_sas_write32(hisi_hba, ENT_INT_SRC2, 0xffffffff);
913 hisi_sas_write32(hisi_hba, ENT_INT_SRC3, 0xffffffff);
914 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0x7efefefe);
915 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0x7efefefe);
916 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0x7ffffffe);
d3b688d3 917 hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xfff00c30);
94eac9e1
JG
918 for (i = 0; i < hisi_hba->queue_count; i++)
919 hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK+0x4*i, 0);
920
921 hisi_sas_write32(hisi_hba, AXI_AHB_CLK_CFG, 1);
922 hisi_sas_write32(hisi_hba, HYPER_STREAM_ID_EN_CFG, 1);
923
924 for (i = 0; i < hisi_hba->n_phy; i++) {
925 hisi_sas_phy_write32(hisi_hba, i, PROG_PHY_LINK_RATE, 0x855);
926 hisi_sas_phy_write32(hisi_hba, i, SAS_PHY_CTRL, 0x30b9908);
927 hisi_sas_phy_write32(hisi_hba, i, SL_TOUT_CFG, 0x7d7d7d7d);
9c81e2cf
JG
928 hisi_sas_phy_write32(hisi_hba, i, SL_CONTROL, 0x0);
929 hisi_sas_phy_write32(hisi_hba, i, TXID_AUTO, 0x2);
f1dc7518 930 hisi_sas_phy_write32(hisi_hba, i, DONE_RECEIVED_TIME, 0x8);
94eac9e1
JG
931 hisi_sas_phy_write32(hisi_hba, i, CHL_INT0, 0xffffffff);
932 hisi_sas_phy_write32(hisi_hba, i, CHL_INT1, 0xffffffff);
d3b688d3 933 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2, 0xfff87fff);
94eac9e1
JG
934 hisi_sas_phy_write32(hisi_hba, i, RXOP_CHECK_CFG_H, 0x1000);
935 hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xffffffff);
936 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0x8ffffbff);
f1dc7518 937 hisi_sas_phy_write32(hisi_hba, i, SL_CFG, 0x13f801fc);
94eac9e1
JG
938 hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL_RDY_MSK, 0x0);
939 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_NOT_RDY_MSK, 0x0);
940 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_DWS_RESET_MSK, 0x0);
941 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_PHY_ENA_MSK, 0x0);
942 hisi_sas_phy_write32(hisi_hba, i, SL_RX_BCAST_CHK_MSK, 0x0);
943 hisi_sas_phy_write32(hisi_hba, i, CHL_INT_COAL_EN, 0x0);
944 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_OOB_RESTART_MSK, 0x0);
3bc45af8
JG
945 if (hisi_hba->refclk_frequency_mhz == 66)
946 hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL, 0x199B694);
947 /* else, do nothing -> leave it how you found it */
94eac9e1
JG
948 }
949
950 for (i = 0; i < hisi_hba->queue_count; i++) {
951 /* Delivery queue */
952 hisi_sas_write32(hisi_hba,
953 DLVRY_Q_0_BASE_ADDR_HI + (i * 0x14),
954 upper_32_bits(hisi_hba->cmd_hdr_dma[i]));
955
956 hisi_sas_write32(hisi_hba, DLVRY_Q_0_BASE_ADDR_LO + (i * 0x14),
957 lower_32_bits(hisi_hba->cmd_hdr_dma[i]));
958
959 hisi_sas_write32(hisi_hba, DLVRY_Q_0_DEPTH + (i * 0x14),
960 HISI_SAS_QUEUE_SLOTS);
961
962 /* Completion queue */
963 hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_HI + (i * 0x14),
964 upper_32_bits(hisi_hba->complete_hdr_dma[i]));
965
966 hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_LO + (i * 0x14),
967 lower_32_bits(hisi_hba->complete_hdr_dma[i]));
968
969 hisi_sas_write32(hisi_hba, COMPL_Q_0_DEPTH + (i * 0x14),
970 HISI_SAS_QUEUE_SLOTS);
971 }
972
973 /* itct */
974 hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_LO,
975 lower_32_bits(hisi_hba->itct_dma));
976
977 hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_HI,
978 upper_32_bits(hisi_hba->itct_dma));
979
980 /* iost */
981 hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_LO,
982 lower_32_bits(hisi_hba->iost_dma));
983
984 hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_HI,
985 upper_32_bits(hisi_hba->iost_dma));
986
987 /* breakpoint */
988 hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_LO,
989 lower_32_bits(hisi_hba->breakpoint_dma));
990
991 hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_HI,
992 upper_32_bits(hisi_hba->breakpoint_dma));
993
994 /* SATA broken msg */
995 hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_LO,
996 lower_32_bits(hisi_hba->sata_breakpoint_dma));
997
998 hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_HI,
999 upper_32_bits(hisi_hba->sata_breakpoint_dma));
1000
1001 /* SATA initial fis */
1002 hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_LO,
1003 lower_32_bits(hisi_hba->initial_fis_dma));
1004
1005 hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_HI,
1006 upper_32_bits(hisi_hba->initial_fis_dma));
1007}
1008
4df642db 1009static void link_timeout_enable_link(unsigned long data)
f2f89c32
XC
1010{
1011 struct hisi_hba *hisi_hba = (struct hisi_hba *)data;
1012 int i, reg_val;
1013
1014 for (i = 0; i < hisi_hba->n_phy; i++) {
1015 reg_val = hisi_sas_phy_read32(hisi_hba, i, CON_CONTROL);
1016 if (!(reg_val & BIT(0))) {
1017 hisi_sas_phy_write32(hisi_hba, i,
1018 CON_CONTROL, 0x7);
1019 break;
1020 }
1021 }
1022
4df642db 1023 hisi_hba->timer.function = link_timeout_disable_link;
f2f89c32
XC
1024 mod_timer(&hisi_hba->timer, jiffies + msecs_to_jiffies(900));
1025}
1026
4df642db 1027static void link_timeout_disable_link(unsigned long data)
f2f89c32
XC
1028{
1029 struct hisi_hba *hisi_hba = (struct hisi_hba *)data;
1030 int i, reg_val;
1031
1032 reg_val = hisi_sas_read32(hisi_hba, PHY_STATE);
1033 for (i = 0; i < hisi_hba->n_phy && reg_val; i++) {
1034 if (reg_val & BIT(i)) {
1035 hisi_sas_phy_write32(hisi_hba, i,
1036 CON_CONTROL, 0x6);
1037 break;
1038 }
1039 }
1040
4df642db 1041 hisi_hba->timer.function = link_timeout_enable_link;
f2f89c32
XC
1042 mod_timer(&hisi_hba->timer, jiffies + msecs_to_jiffies(100));
1043}
1044
1045static void set_link_timer_quirk(struct hisi_hba *hisi_hba)
1046{
1047 hisi_hba->timer.data = (unsigned long)hisi_hba;
4df642db 1048 hisi_hba->timer.function = link_timeout_disable_link;
f2f89c32
XC
1049 hisi_hba->timer.expires = jiffies + msecs_to_jiffies(1000);
1050 add_timer(&hisi_hba->timer);
1051}
1052
94eac9e1
JG
1053static int hw_init_v2_hw(struct hisi_hba *hisi_hba)
1054{
1055 struct device *dev = &hisi_hba->pdev->dev;
1056 int rc;
1057
1058 rc = reset_hw_v2_hw(hisi_hba);
1059 if (rc) {
1060 dev_err(dev, "hisi_sas_reset_hw failed, rc=%d", rc);
1061 return rc;
1062 }
1063
1064 msleep(100);
1065 init_reg_v2_hw(hisi_hba);
806bb768 1066
94eac9e1
JG
1067 return 0;
1068}
1069
29a20428
JG
1070static void enable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1071{
1072 u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
1073
1074 cfg |= PHY_CFG_ENA_MSK;
1075 hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
1076}
1077
4935933e
XT
1078static bool is_sata_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1079{
1080 u32 context;
1081
1082 context = hisi_sas_read32(hisi_hba, PHY_CONTEXT);
1083 if (context & (1 << phy_no))
1084 return true;
1085
1086 return false;
1087}
1088
63fb11b8
JG
1089static void disable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1090{
1091 u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
1092
1093 cfg &= ~PHY_CFG_ENA_MSK;
1094 hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
1095}
1096
29a20428
JG
1097static void start_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1098{
1099 config_id_frame_v2_hw(hisi_hba, phy_no);
1100 config_phy_opt_mode_v2_hw(hisi_hba, phy_no);
1101 enable_phy_v2_hw(hisi_hba, phy_no);
1102}
1103
63fb11b8
JG
1104static void stop_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1105{
1106 disable_phy_v2_hw(hisi_hba, phy_no);
1107}
1108
06ec0fb9
XC
1109static void stop_phys_v2_hw(struct hisi_hba *hisi_hba)
1110{
1111 int i;
1112
1113 for (i = 0; i < hisi_hba->n_phy; i++)
1114 stop_phy_v2_hw(hisi_hba, i);
1115}
1116
63fb11b8
JG
1117static void phy_hard_reset_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1118{
0edef7e4
XC
1119 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1120 u32 txid_auto;
1121
63fb11b8 1122 stop_phy_v2_hw(hisi_hba, phy_no);
0edef7e4
XC
1123 if (phy->identify.device_type == SAS_END_DEVICE) {
1124 txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
1125 hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
1126 txid_auto | TX_HARDRST_MSK);
1127 }
63fb11b8
JG
1128 msleep(100);
1129 start_phy_v2_hw(hisi_hba, phy_no);
1130}
1131
0757f041 1132static void start_phys_v2_hw(struct hisi_hba *hisi_hba)
29a20428 1133{
29a20428
JG
1134 int i;
1135
1136 for (i = 0; i < hisi_hba->n_phy; i++)
1137 start_phy_v2_hw(hisi_hba, i);
1138}
1139
1140static void phys_init_v2_hw(struct hisi_hba *hisi_hba)
1141{
0757f041 1142 start_phys_v2_hw(hisi_hba);
29a20428
JG
1143}
1144
7911e66f
JG
1145static void sl_notify_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
1146{
1147 u32 sl_control;
1148
1149 sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1150 sl_control |= SL_CONTROL_NOTIFY_EN_MSK;
1151 hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
1152 msleep(1);
1153 sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
1154 sl_control &= ~SL_CONTROL_NOTIFY_EN_MSK;
1155 hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
1156}
1157
2ae75787
XC
1158static enum sas_linkrate phy_get_max_linkrate_v2_hw(void)
1159{
1160 return SAS_LINK_RATE_12_0_GBPS;
1161}
1162
1163static void phy_set_linkrate_v2_hw(struct hisi_hba *hisi_hba, int phy_no,
1164 struct sas_phy_linkrates *r)
1165{
1166 u32 prog_phy_link_rate =
1167 hisi_sas_phy_read32(hisi_hba, phy_no, PROG_PHY_LINK_RATE);
1168 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1169 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1170 int i;
1171 enum sas_linkrate min, max;
1172 u32 rate_mask = 0;
1173
1174 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1175 max = sas_phy->phy->maximum_linkrate;
1176 min = r->minimum_linkrate;
1177 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1178 max = r->maximum_linkrate;
1179 min = sas_phy->phy->minimum_linkrate;
1180 } else
1181 return;
1182
1183 sas_phy->phy->maximum_linkrate = max;
1184 sas_phy->phy->minimum_linkrate = min;
1185
1186 min -= SAS_LINK_RATE_1_5_GBPS;
1187 max -= SAS_LINK_RATE_1_5_GBPS;
1188
1189 for (i = 0; i <= max; i++)
1190 rate_mask |= 1 << (i * 2);
1191
1192 prog_phy_link_rate &= ~0xff;
1193 prog_phy_link_rate |= rate_mask;
1194
1195 hisi_sas_phy_write32(hisi_hba, phy_no, PROG_PHY_LINK_RATE,
1196 prog_phy_link_rate);
1197
1198 phy_hard_reset_v2_hw(hisi_hba, phy_no);
1199}
1200
5473c060
JG
1201static int get_wideport_bitmap_v2_hw(struct hisi_hba *hisi_hba, int port_id)
1202{
1203 int i, bitmap = 0;
1204 u32 phy_port_num_ma = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
1205 u32 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1206
1207 for (i = 0; i < (hisi_hba->n_phy < 9 ? hisi_hba->n_phy : 8); i++)
1208 if (phy_state & 1 << i)
1209 if (((phy_port_num_ma >> (i * 4)) & 0xf) == port_id)
1210 bitmap |= 1 << i;
1211
1212 if (hisi_hba->n_phy == 9) {
1213 u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
1214
1215 if (phy_state & 1 << 8)
1216 if (((port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
1217 PORT_STATE_PHY8_PORT_NUM_OFF) == port_id)
1218 bitmap |= 1 << 9;
1219 }
1220
1221 return bitmap;
1222}
1223
8c36e31d
JG
1224/**
1225 * This function allocates across all queues to load balance.
1226 * Slots are allocated from queues in a round-robin fashion.
1227 *
1228 * The callpath to this function and upto writing the write
1229 * queue pointer should be safe from interruption.
1230 */
c70f1fb7
XC
1231static int get_free_slot_v2_hw(struct hisi_hba *hisi_hba, u32 dev_id,
1232 int *q, int *s)
8c36e31d
JG
1233{
1234 struct device *dev = &hisi_hba->pdev->dev;
4fde02ad 1235 struct hisi_sas_dq *dq;
8c36e31d 1236 u32 r, w;
c70f1fb7
XC
1237 int queue = dev_id % hisi_hba->queue_count;
1238
1239 dq = &hisi_hba->dq[queue];
1240 w = dq->wr_point;
1241 r = hisi_sas_read32_relaxed(hisi_hba,
1242 DLVRY_Q_0_RD_PTR + (queue * 0x14));
1243 if (r == (w+1) % HISI_SAS_QUEUE_SLOTS) {
1244 dev_warn(dev, "full queue=%d r=%d w=%d\n\n",
1245 queue, r, w);
1246 return -EAGAIN;
8c36e31d 1247 }
c70f1fb7 1248
8c36e31d
JG
1249 *q = queue;
1250 *s = w;
1251 return 0;
1252}
1253
1254static void start_delivery_v2_hw(struct hisi_hba *hisi_hba)
1255{
1256 int dlvry_queue = hisi_hba->slot_prep->dlvry_queue;
1257 int dlvry_queue_slot = hisi_hba->slot_prep->dlvry_queue_slot;
4fde02ad 1258 struct hisi_sas_dq *dq = &hisi_hba->dq[dlvry_queue];
8c36e31d 1259
4fde02ad 1260 dq->wr_point = ++dlvry_queue_slot % HISI_SAS_QUEUE_SLOTS;
8c36e31d 1261 hisi_sas_write32(hisi_hba, DLVRY_Q_0_WR_PTR + (dlvry_queue * 0x14),
4fde02ad 1262 dq->wr_point);
8c36e31d
JG
1263}
1264
1265static int prep_prd_sge_v2_hw(struct hisi_hba *hisi_hba,
1266 struct hisi_sas_slot *slot,
1267 struct hisi_sas_cmd_hdr *hdr,
1268 struct scatterlist *scatter,
1269 int n_elem)
1270{
1271 struct device *dev = &hisi_hba->pdev->dev;
1272 struct scatterlist *sg;
1273 int i;
1274
1275 if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
1276 dev_err(dev, "prd err: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
1277 n_elem);
1278 return -EINVAL;
1279 }
1280
1281 slot->sge_page = dma_pool_alloc(hisi_hba->sge_page_pool, GFP_ATOMIC,
1282 &slot->sge_page_dma);
1283 if (!slot->sge_page)
1284 return -ENOMEM;
1285
1286 for_each_sg(scatter, sg, n_elem, i) {
1287 struct hisi_sas_sge *entry = &slot->sge_page->sge[i];
1288
1289 entry->addr = cpu_to_le64(sg_dma_address(sg));
1290 entry->page_ctrl_0 = entry->page_ctrl_1 = 0;
1291 entry->data_len = cpu_to_le32(sg_dma_len(sg));
1292 entry->data_off = 0;
1293 }
1294
1295 hdr->prd_table_addr = cpu_to_le64(slot->sge_page_dma);
1296
1297 hdr->sg_len = cpu_to_le32(n_elem << CMD_HDR_DATA_SGL_LEN_OFF);
1298
1299 return 0;
1300}
1301
c2d89392
JG
1302static int prep_smp_v2_hw(struct hisi_hba *hisi_hba,
1303 struct hisi_sas_slot *slot)
1304{
1305 struct sas_task *task = slot->task;
1306 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1307 struct domain_device *device = task->dev;
1308 struct device *dev = &hisi_hba->pdev->dev;
1309 struct hisi_sas_port *port = slot->port;
1310 struct scatterlist *sg_req, *sg_resp;
1311 struct hisi_sas_device *sas_dev = device->lldd_dev;
1312 dma_addr_t req_dma_addr;
1313 unsigned int req_len, resp_len;
1314 int elem, rc;
1315
1316 /*
1317 * DMA-map SMP request, response buffers
1318 */
1319 /* req */
1320 sg_req = &task->smp_task.smp_req;
1321 elem = dma_map_sg(dev, sg_req, 1, DMA_TO_DEVICE);
1322 if (!elem)
1323 return -ENOMEM;
1324 req_len = sg_dma_len(sg_req);
1325 req_dma_addr = sg_dma_address(sg_req);
1326
1327 /* resp */
1328 sg_resp = &task->smp_task.smp_resp;
1329 elem = dma_map_sg(dev, sg_resp, 1, DMA_FROM_DEVICE);
1330 if (!elem) {
1331 rc = -ENOMEM;
1332 goto err_out_req;
1333 }
1334 resp_len = sg_dma_len(sg_resp);
1335 if ((req_len & 0x3) || (resp_len & 0x3)) {
1336 rc = -EINVAL;
1337 goto err_out_resp;
1338 }
1339
1340 /* create header */
1341 /* dw0 */
1342 hdr->dw0 = cpu_to_le32((port->id << CMD_HDR_PORT_OFF) |
1343 (1 << CMD_HDR_PRIORITY_OFF) | /* high pri */
1344 (2 << CMD_HDR_CMD_OFF)); /* smp */
1345
1346 /* map itct entry */
1347 hdr->dw1 = cpu_to_le32((sas_dev->device_id << CMD_HDR_DEV_ID_OFF) |
1348 (1 << CMD_HDR_FRAME_TYPE_OFF) |
1349 (DIR_NO_DATA << CMD_HDR_DIR_OFF));
1350
1351 /* dw2 */
1352 hdr->dw2 = cpu_to_le32((((req_len - 4) / 4) << CMD_HDR_CFL_OFF) |
1353 (HISI_SAS_MAX_SMP_RESP_SZ / 4 <<
1354 CMD_HDR_MRFL_OFF));
1355
1356 hdr->transfer_tags = cpu_to_le32(slot->idx << CMD_HDR_IPTT_OFF);
1357
1358 hdr->cmd_table_addr = cpu_to_le64(req_dma_addr);
1359 hdr->sts_buffer_addr = cpu_to_le64(slot->status_buffer_dma);
1360
1361 return 0;
1362
1363err_out_resp:
1364 dma_unmap_sg(dev, &slot->task->smp_task.smp_resp, 1,
1365 DMA_FROM_DEVICE);
1366err_out_req:
1367 dma_unmap_sg(dev, &slot->task->smp_task.smp_req, 1,
1368 DMA_TO_DEVICE);
1369 return rc;
1370}
1371
8c36e31d
JG
1372static int prep_ssp_v2_hw(struct hisi_hba *hisi_hba,
1373 struct hisi_sas_slot *slot, int is_tmf,
1374 struct hisi_sas_tmf_task *tmf)
1375{
1376 struct sas_task *task = slot->task;
1377 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1378 struct domain_device *device = task->dev;
1379 struct hisi_sas_device *sas_dev = device->lldd_dev;
1380 struct hisi_sas_port *port = slot->port;
1381 struct sas_ssp_task *ssp_task = &task->ssp_task;
1382 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
1383 int has_data = 0, rc, priority = is_tmf;
1384 u8 *buf_cmd;
1385 u32 dw1 = 0, dw2 = 0;
1386
1387 hdr->dw0 = cpu_to_le32((1 << CMD_HDR_RESP_REPORT_OFF) |
1388 (2 << CMD_HDR_TLR_CTRL_OFF) |
1389 (port->id << CMD_HDR_PORT_OFF) |
1390 (priority << CMD_HDR_PRIORITY_OFF) |
1391 (1 << CMD_HDR_CMD_OFF)); /* ssp */
1392
1393 dw1 = 1 << CMD_HDR_VDTL_OFF;
1394 if (is_tmf) {
1395 dw1 |= 2 << CMD_HDR_FRAME_TYPE_OFF;
1396 dw1 |= DIR_NO_DATA << CMD_HDR_DIR_OFF;
1397 } else {
1398 dw1 |= 1 << CMD_HDR_FRAME_TYPE_OFF;
1399 switch (scsi_cmnd->sc_data_direction) {
1400 case DMA_TO_DEVICE:
1401 has_data = 1;
1402 dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
1403 break;
1404 case DMA_FROM_DEVICE:
1405 has_data = 1;
1406 dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
1407 break;
1408 default:
1409 dw1 &= ~CMD_HDR_DIR_MSK;
1410 }
1411 }
1412
1413 /* map itct entry */
1414 dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
1415 hdr->dw1 = cpu_to_le32(dw1);
1416
1417 dw2 = (((sizeof(struct ssp_command_iu) + sizeof(struct ssp_frame_hdr)
1418 + 3) / 4) << CMD_HDR_CFL_OFF) |
1419 ((HISI_SAS_MAX_SSP_RESP_SZ / 4) << CMD_HDR_MRFL_OFF) |
1420 (2 << CMD_HDR_SG_MOD_OFF);
1421 hdr->dw2 = cpu_to_le32(dw2);
1422
1423 hdr->transfer_tags = cpu_to_le32(slot->idx);
1424
1425 if (has_data) {
1426 rc = prep_prd_sge_v2_hw(hisi_hba, slot, hdr, task->scatter,
1427 slot->n_elem);
1428 if (rc)
1429 return rc;
1430 }
1431
1432 hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
1433 hdr->cmd_table_addr = cpu_to_le64(slot->command_table_dma);
1434 hdr->sts_buffer_addr = cpu_to_le64(slot->status_buffer_dma);
1435
1436 buf_cmd = slot->command_table + sizeof(struct ssp_frame_hdr);
1437
1438 memcpy(buf_cmd, &task->ssp_task.LUN, 8);
1439 if (!is_tmf) {
1440 buf_cmd[9] = task->ssp_task.task_attr |
1441 (task->ssp_task.task_prio << 3);
1442 memcpy(buf_cmd + 12, task->ssp_task.cmd->cmnd,
1443 task->ssp_task.cmd->cmd_len);
1444 } else {
1445 buf_cmd[10] = tmf->tmf;
1446 switch (tmf->tmf) {
1447 case TMF_ABORT_TASK:
1448 case TMF_QUERY_TASK:
1449 buf_cmd[12] =
1450 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
1451 buf_cmd[13] =
1452 tmf->tag_of_task_to_be_managed & 0xff;
1453 break;
1454 default:
1455 break;
1456 }
1457 }
1458
1459 return 0;
1460}
1461
6f2ff1a1
JG
1462static void sata_done_v2_hw(struct hisi_hba *hisi_hba, struct sas_task *task,
1463 struct hisi_sas_slot *slot)
1464{
1465 struct task_status_struct *ts = &task->task_status;
1466 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
1467 struct dev_to_host_fis *d2h = slot->status_buffer +
1468 sizeof(struct hisi_sas_err_record);
1469
1470 resp->frame_len = sizeof(struct dev_to_host_fis);
1471 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
1472
1473 ts->buf_valid_size = sizeof(*resp);
1474}
e8fed0e9 1475
634a9585
XC
1476#define TRANS_TX_ERR 0
1477#define TRANS_RX_ERR 1
1478#define DMA_TX_ERR 2
1479#define SIPC_RX_ERR 3
1480#define DMA_RX_ERR 4
1481
1482#define DMA_TX_ERR_OFF 0
1483#define DMA_TX_ERR_MSK (0xffff << DMA_TX_ERR_OFF)
1484#define SIPC_RX_ERR_OFF 16
1485#define SIPC_RX_ERR_MSK (0xffff << SIPC_RX_ERR_OFF)
1486
1487static int parse_trans_tx_err_code_v2_hw(u32 err_msk)
1488{
1489 const u8 trans_tx_err_code_prio[] = {
1490 TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS,
1491 TRANS_TX_ERR_PHY_NOT_ENABLE,
1492 TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION,
1493 TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION,
1494 TRANS_TX_OPEN_CNX_ERR_BY_OTHER,
1495 RESERVED0,
1496 TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT,
1497 TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY,
1498 TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED,
1499 TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED,
1500 TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION,
1501 TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD,
1502 TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER,
1503 TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED,
1504 TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT,
1505 TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION,
1506 TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED,
1507 TRANS_TX_ERR_WITH_CLOSE_PHYDISALE,
1508 TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT,
1509 TRANS_TX_ERR_WITH_CLOSE_COMINIT,
1510 TRANS_TX_ERR_WITH_BREAK_TIMEOUT,
1511 TRANS_TX_ERR_WITH_BREAK_REQUEST,
1512 TRANS_TX_ERR_WITH_BREAK_RECEVIED,
1513 TRANS_TX_ERR_WITH_CLOSE_TIMEOUT,
1514 TRANS_TX_ERR_WITH_CLOSE_NORMAL,
1515 TRANS_TX_ERR_WITH_NAK_RECEVIED,
1516 TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT,
1517 TRANS_TX_ERR_WITH_CREDIT_TIMEOUT,
1518 TRANS_TX_ERR_WITH_IPTT_CONFLICT,
1519 TRANS_TX_ERR_WITH_OPEN_BY_DES_OR_OTHERS,
1520 TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT,
1521 };
1522 int index, i;
1523
1524 for (i = 0; i < ARRAY_SIZE(trans_tx_err_code_prio); i++) {
1525 index = trans_tx_err_code_prio[i] - TRANS_TX_FAIL_BASE;
1526 if (err_msk & (1 << index))
1527 return trans_tx_err_code_prio[i];
1528 }
1529 return -1;
1530}
1531
1532static int parse_trans_rx_err_code_v2_hw(u32 err_msk)
1533{
1534 const u8 trans_rx_err_code_prio[] = {
1535 TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR,
1536 TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR,
1537 TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM,
1538 TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR,
1539 TRANS_RX_ERR_WITH_RXFIS_CRC_ERR,
1540 TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN,
1541 TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP,
1542 TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN,
1543 TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE,
1544 TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT,
1545 TRANS_RX_ERR_WITH_CLOSE_COMINIT,
1546 TRANS_RX_ERR_WITH_BREAK_TIMEOUT,
1547 TRANS_RX_ERR_WITH_BREAK_REQUEST,
1548 TRANS_RX_ERR_WITH_BREAK_RECEVIED,
1549 RESERVED1,
1550 TRANS_RX_ERR_WITH_CLOSE_NORMAL,
1551 TRANS_RX_ERR_WITH_DATA_LEN0,
1552 TRANS_RX_ERR_WITH_BAD_HASH,
1553 TRANS_RX_XRDY_WLEN_ZERO_ERR,
1554 TRANS_RX_SSP_FRM_LEN_ERR,
1555 RESERVED2,
1556 RESERVED3,
1557 RESERVED4,
1558 RESERVED5,
1559 TRANS_RX_ERR_WITH_BAD_FRM_TYPE,
1560 TRANS_RX_SMP_FRM_LEN_ERR,
1561 TRANS_RX_SMP_RESP_TIMEOUT_ERR,
1562 RESERVED6,
1563 RESERVED7,
1564 RESERVED8,
1565 RESERVED9,
1566 TRANS_RX_R_ERR,
1567 };
1568 int index, i;
1569
1570 for (i = 0; i < ARRAY_SIZE(trans_rx_err_code_prio); i++) {
1571 index = trans_rx_err_code_prio[i] - TRANS_RX_FAIL_BASE;
1572 if (err_msk & (1 << index))
1573 return trans_rx_err_code_prio[i];
1574 }
1575 return -1;
1576}
1577
1578static int parse_dma_tx_err_code_v2_hw(u32 err_msk)
1579{
1580 const u8 dma_tx_err_code_prio[] = {
1581 DMA_TX_UNEXP_XFER_ERR,
1582 DMA_TX_UNEXP_RETRANS_ERR,
1583 DMA_TX_XFER_LEN_OVERFLOW,
1584 DMA_TX_XFER_OFFSET_ERR,
1585 DMA_TX_RAM_ECC_ERR,
1586 DMA_TX_DIF_LEN_ALIGN_ERR,
1587 DMA_TX_DIF_CRC_ERR,
1588 DMA_TX_DIF_APP_ERR,
1589 DMA_TX_DIF_RPP_ERR,
1590 DMA_TX_DATA_SGL_OVERFLOW,
1591 DMA_TX_DIF_SGL_OVERFLOW,
1592 };
1593 int index, i;
1594
1595 for (i = 0; i < ARRAY_SIZE(dma_tx_err_code_prio); i++) {
1596 index = dma_tx_err_code_prio[i] - DMA_TX_ERR_BASE;
1597 err_msk = err_msk & DMA_TX_ERR_MSK;
1598 if (err_msk & (1 << index))
1599 return dma_tx_err_code_prio[i];
1600 }
1601 return -1;
1602}
1603
1604static int parse_sipc_rx_err_code_v2_hw(u32 err_msk)
1605{
1606 const u8 sipc_rx_err_code_prio[] = {
1607 SIPC_RX_FIS_STATUS_ERR_BIT_VLD,
1608 SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR,
1609 SIPC_RX_FIS_STATUS_BSY_BIT_ERR,
1610 SIPC_RX_WRSETUP_LEN_ODD_ERR,
1611 SIPC_RX_WRSETUP_LEN_ZERO_ERR,
1612 SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR,
1613 SIPC_RX_NCQ_WRSETUP_OFFSET_ERR,
1614 SIPC_RX_NCQ_WRSETUP_AUTO_ACTIVE_ERR,
1615 SIPC_RX_SATA_UNEXP_FIS_ERR,
1616 SIPC_RX_WRSETUP_ESTATUS_ERR,
1617 SIPC_RX_DATA_UNDERFLOW_ERR,
1618 };
1619 int index, i;
1620
1621 for (i = 0; i < ARRAY_SIZE(sipc_rx_err_code_prio); i++) {
1622 index = sipc_rx_err_code_prio[i] - SIPC_RX_ERR_BASE;
1623 err_msk = err_msk & SIPC_RX_ERR_MSK;
1624 if (err_msk & (1 << (index + 0x10)))
1625 return sipc_rx_err_code_prio[i];
1626 }
1627 return -1;
1628}
1629
1630static int parse_dma_rx_err_code_v2_hw(u32 err_msk)
1631{
1632 const u8 dma_rx_err_code_prio[] = {
1633 DMA_RX_UNKNOWN_FRM_ERR,
1634 DMA_RX_DATA_LEN_OVERFLOW,
1635 DMA_RX_DATA_LEN_UNDERFLOW,
1636 DMA_RX_DATA_OFFSET_ERR,
1637 RESERVED10,
1638 DMA_RX_SATA_FRAME_TYPE_ERR,
1639 DMA_RX_RESP_BUF_OVERFLOW,
1640 DMA_RX_UNEXP_RETRANS_RESP_ERR,
1641 DMA_RX_UNEXP_NORM_RESP_ERR,
1642 DMA_RX_UNEXP_RDFRAME_ERR,
1643 DMA_RX_PIO_DATA_LEN_ERR,
1644 DMA_RX_RDSETUP_STATUS_ERR,
1645 DMA_RX_RDSETUP_STATUS_DRQ_ERR,
1646 DMA_RX_RDSETUP_STATUS_BSY_ERR,
1647 DMA_RX_RDSETUP_LEN_ODD_ERR,
1648 DMA_RX_RDSETUP_LEN_ZERO_ERR,
1649 DMA_RX_RDSETUP_LEN_OVER_ERR,
1650 DMA_RX_RDSETUP_OFFSET_ERR,
1651 DMA_RX_RDSETUP_ACTIVE_ERR,
1652 DMA_RX_RDSETUP_ESTATUS_ERR,
1653 DMA_RX_RAM_ECC_ERR,
1654 DMA_RX_DIF_CRC_ERR,
1655 DMA_RX_DIF_APP_ERR,
1656 DMA_RX_DIF_RPP_ERR,
1657 DMA_RX_DATA_SGL_OVERFLOW,
1658 DMA_RX_DIF_SGL_OVERFLOW,
1659 };
1660 int index, i;
1661
1662 for (i = 0; i < ARRAY_SIZE(dma_rx_err_code_prio); i++) {
1663 index = dma_rx_err_code_prio[i] - DMA_RX_ERR_BASE;
1664 if (err_msk & (1 << index))
1665 return dma_rx_err_code_prio[i];
1666 }
1667 return -1;
1668}
1669
e8fed0e9
JG
1670/* by default, task resp is complete */
1671static void slot_err_v2_hw(struct hisi_hba *hisi_hba,
1672 struct sas_task *task,
634a9585
XC
1673 struct hisi_sas_slot *slot,
1674 int err_phase)
e8fed0e9
JG
1675{
1676 struct task_status_struct *ts = &task->task_status;
1677 struct hisi_sas_err_record_v2 *err_record = slot->status_buffer;
1678 u32 trans_tx_fail_type = cpu_to_le32(err_record->trans_tx_fail_type);
1679 u32 trans_rx_fail_type = cpu_to_le32(err_record->trans_rx_fail_type);
1680 u16 dma_tx_err_type = cpu_to_le16(err_record->dma_tx_err_type);
1681 u16 sipc_rx_err_type = cpu_to_le16(err_record->sipc_rx_err_type);
1682 u32 dma_rx_err_type = cpu_to_le32(err_record->dma_rx_err_type);
1683 int error = -1;
1684
634a9585
XC
1685 if (err_phase == 1) {
1686 /* error in TX phase, the priority of error is: DW2 > DW0 */
1687 error = parse_dma_tx_err_code_v2_hw(dma_tx_err_type);
1688 if (error == -1)
1689 error = parse_trans_tx_err_code_v2_hw(
1690 trans_tx_fail_type);
1691 } else if (err_phase == 2) {
1692 /* error in RX phase, the priority is: DW1 > DW3 > DW2 */
1693 error = parse_trans_rx_err_code_v2_hw(
1694 trans_rx_fail_type);
1695 if (error == -1) {
1696 error = parse_dma_rx_err_code_v2_hw(
1697 dma_rx_err_type);
1698 if (error == -1)
1699 error = parse_sipc_rx_err_code_v2_hw(
1700 sipc_rx_err_type);
1701 }
e8fed0e9
JG
1702 }
1703
1704 switch (task->task_proto) {
1705 case SAS_PROTOCOL_SSP:
1706 {
1707 switch (error) {
1708 case TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION:
1709 {
1710 ts->stat = SAS_OPEN_REJECT;
1711 ts->open_rej_reason = SAS_OREJ_NO_DEST;
e8fed0e9
JG
1712 }
1713 case TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED:
1714 {
1715 ts->stat = SAS_OPEN_REJECT;
1716 ts->open_rej_reason = SAS_OREJ_EPROTO;
1717 break;
1718 }
1719 case TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED:
1720 {
1721 ts->stat = SAS_OPEN_REJECT;
1722 ts->open_rej_reason = SAS_OREJ_CONN_RATE;
1723 break;
1724 }
1725 case TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION:
1726 {
1727 ts->stat = SAS_OPEN_REJECT;
1728 ts->open_rej_reason = SAS_OREJ_BAD_DEST;
1729 break;
1730 }
e8fed0e9
JG
1731 case TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION:
1732 {
1733 ts->stat = SAS_OPEN_REJECT;
1734 ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
1735 break;
1736 }
634a9585 1737 case DMA_RX_UNEXP_NORM_RESP_ERR:
e8fed0e9 1738 case TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION:
634a9585 1739 case DMA_RX_RESP_BUF_OVERFLOW:
e8fed0e9
JG
1740 {
1741 ts->stat = SAS_OPEN_REJECT;
1742 ts->open_rej_reason = SAS_OREJ_UNKNOWN;
1743 break;
1744 }
1745 case TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER:
1746 {
1747 /* not sure */
1748 ts->stat = SAS_DEV_NO_RESPONSE;
1749 break;
1750 }
e8fed0e9
JG
1751 case DMA_RX_DATA_LEN_OVERFLOW:
1752 {
1753 ts->stat = SAS_DATA_OVERRUN;
1754 ts->residual = 0;
1755 break;
1756 }
1757 case DMA_RX_DATA_LEN_UNDERFLOW:
e8fed0e9 1758 {
634a9585 1759 ts->residual = dma_rx_err_type;
e8fed0e9
JG
1760 ts->stat = SAS_DATA_UNDERRUN;
1761 break;
1762 }
1763 case TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS:
1764 case TRANS_TX_ERR_PHY_NOT_ENABLE:
1765 case TRANS_TX_OPEN_CNX_ERR_BY_OTHER:
1766 case TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT:
634a9585
XC
1767 case TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD:
1768 case TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED:
1769 case TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT:
e8fed0e9
JG
1770 case TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED:
1771 case TRANS_TX_ERR_WITH_BREAK_TIMEOUT:
1772 case TRANS_TX_ERR_WITH_BREAK_REQUEST:
1773 case TRANS_TX_ERR_WITH_BREAK_RECEVIED:
1774 case TRANS_TX_ERR_WITH_CLOSE_TIMEOUT:
1775 case TRANS_TX_ERR_WITH_CLOSE_NORMAL:
634a9585 1776 case TRANS_TX_ERR_WITH_CLOSE_PHYDISALE:
e8fed0e9
JG
1777 case TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT:
1778 case TRANS_TX_ERR_WITH_CLOSE_COMINIT:
1779 case TRANS_TX_ERR_WITH_NAK_RECEVIED:
1780 case TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT:
e8fed0e9 1781 case TRANS_TX_ERR_WITH_CREDIT_TIMEOUT:
634a9585 1782 case TRANS_TX_ERR_WITH_IPTT_CONFLICT:
e8fed0e9
JG
1783 case TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR:
1784 case TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR:
1785 case TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM:
634a9585 1786 case TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN:
e8fed0e9
JG
1787 case TRANS_RX_ERR_WITH_BREAK_TIMEOUT:
1788 case TRANS_RX_ERR_WITH_BREAK_REQUEST:
1789 case TRANS_RX_ERR_WITH_BREAK_RECEVIED:
1790 case TRANS_RX_ERR_WITH_CLOSE_NORMAL:
1791 case TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT:
1792 case TRANS_RX_ERR_WITH_CLOSE_COMINIT:
634a9585
XC
1793 case TRANS_TX_ERR_FRAME_TXED:
1794 case TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE:
e8fed0e9
JG
1795 case TRANS_RX_ERR_WITH_DATA_LEN0:
1796 case TRANS_RX_ERR_WITH_BAD_HASH:
1797 case TRANS_RX_XRDY_WLEN_ZERO_ERR:
1798 case TRANS_RX_SSP_FRM_LEN_ERR:
1799 case TRANS_RX_ERR_WITH_BAD_FRM_TYPE:
634a9585 1800 case DMA_TX_DATA_SGL_OVERFLOW:
e8fed0e9
JG
1801 case DMA_TX_UNEXP_XFER_ERR:
1802 case DMA_TX_UNEXP_RETRANS_ERR:
1803 case DMA_TX_XFER_LEN_OVERFLOW:
1804 case DMA_TX_XFER_OFFSET_ERR:
634a9585
XC
1805 case SIPC_RX_DATA_UNDERFLOW_ERR:
1806 case DMA_RX_DATA_SGL_OVERFLOW:
e8fed0e9 1807 case DMA_RX_DATA_OFFSET_ERR:
634a9585
XC
1808 case DMA_RX_RDSETUP_LEN_ODD_ERR:
1809 case DMA_RX_RDSETUP_LEN_ZERO_ERR:
1810 case DMA_RX_RDSETUP_LEN_OVER_ERR:
1811 case DMA_RX_SATA_FRAME_TYPE_ERR:
e8fed0e9
JG
1812 case DMA_RX_UNKNOWN_FRM_ERR:
1813 {
634a9585
XC
1814 /* This will request a retry */
1815 ts->stat = SAS_QUEUE_FULL;
1816 slot->abort = 1;
e8fed0e9
JG
1817 break;
1818 }
1819 default:
1820 break;
1821 }
1822 }
1823 break;
1824 case SAS_PROTOCOL_SMP:
1825 ts->stat = SAM_STAT_CHECK_CONDITION;
1826 break;
1827
1828 case SAS_PROTOCOL_SATA:
1829 case SAS_PROTOCOL_STP:
1830 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
1831 {
1832 switch (error) {
e8fed0e9 1833 case TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION:
634a9585
XC
1834 {
1835 ts->stat = SAS_OPEN_REJECT;
1836 ts->open_rej_reason = SAS_OREJ_NO_DEST;
1837 break;
1838 }
1839 case TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER:
e8fed0e9
JG
1840 {
1841 ts->resp = SAS_TASK_UNDELIVERED;
1842 ts->stat = SAS_DEV_NO_RESPONSE;
1843 break;
1844 }
1845 case TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED:
634a9585
XC
1846 {
1847 ts->stat = SAS_OPEN_REJECT;
1848 ts->open_rej_reason = SAS_OREJ_EPROTO;
1849 break;
1850 }
e8fed0e9 1851 case TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED:
634a9585
XC
1852 {
1853 ts->stat = SAS_OPEN_REJECT;
1854 ts->open_rej_reason = SAS_OREJ_CONN_RATE;
1855 break;
1856 }
e8fed0e9 1857 case TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION:
634a9585
XC
1858 {
1859 ts->stat = SAS_OPEN_REJECT;
1860 ts->open_rej_reason = SAS_OREJ_CONN_RATE;
1861 break;
1862 }
e8fed0e9 1863 case TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION:
e8fed0e9
JG
1864 {
1865 ts->stat = SAS_OPEN_REJECT;
634a9585 1866 ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
e8fed0e9
JG
1867 break;
1868 }
634a9585
XC
1869 case DMA_RX_RESP_BUF_OVERFLOW:
1870 case DMA_RX_UNEXP_NORM_RESP_ERR:
1871 case TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION:
e8fed0e9 1872 {
634a9585
XC
1873 ts->stat = SAS_OPEN_REJECT;
1874 ts->open_rej_reason = SAS_OREJ_UNKNOWN;
e8fed0e9
JG
1875 break;
1876 }
1877 case DMA_RX_DATA_LEN_OVERFLOW:
1878 {
1879 ts->stat = SAS_DATA_OVERRUN;
634a9585
XC
1880 ts->residual = 0;
1881 break;
1882 }
1883 case DMA_RX_DATA_LEN_UNDERFLOW:
1884 {
1885 ts->residual = dma_rx_err_type;
1886 ts->stat = SAS_DATA_UNDERRUN;
e8fed0e9
JG
1887 break;
1888 }
1889 case TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS:
1890 case TRANS_TX_ERR_PHY_NOT_ENABLE:
1891 case TRANS_TX_OPEN_CNX_ERR_BY_OTHER:
1892 case TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT:
634a9585
XC
1893 case TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD:
1894 case TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED:
1895 case TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT:
e8fed0e9
JG
1896 case TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED:
1897 case TRANS_TX_ERR_WITH_BREAK_TIMEOUT:
1898 case TRANS_TX_ERR_WITH_BREAK_REQUEST:
1899 case TRANS_TX_ERR_WITH_BREAK_RECEVIED:
1900 case TRANS_TX_ERR_WITH_CLOSE_TIMEOUT:
1901 case TRANS_TX_ERR_WITH_CLOSE_NORMAL:
634a9585 1902 case TRANS_TX_ERR_WITH_CLOSE_PHYDISALE:
e8fed0e9
JG
1903 case TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT:
1904 case TRANS_TX_ERR_WITH_CLOSE_COMINIT:
e8fed0e9
JG
1905 case TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT:
1906 case TRANS_TX_ERR_WITH_CREDIT_TIMEOUT:
634a9585 1907 case TRANS_TX_ERR_WITH_OPEN_BY_DES_OR_OTHERS:
e8fed0e9 1908 case TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT:
e8fed0e9 1909 case TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM:
634a9585 1910 case TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR:
e8fed0e9
JG
1911 case TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR:
1912 case TRANS_RX_ERR_WITH_RXFIS_CRC_ERR:
1913 case TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN:
1914 case TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP:
634a9585
XC
1915 case TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN:
1916 case TRANS_RX_ERR_WITH_BREAK_TIMEOUT:
1917 case TRANS_RX_ERR_WITH_BREAK_REQUEST:
1918 case TRANS_RX_ERR_WITH_BREAK_RECEVIED:
e8fed0e9
JG
1919 case TRANS_RX_ERR_WITH_CLOSE_NORMAL:
1920 case TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE:
1921 case TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT:
1922 case TRANS_RX_ERR_WITH_CLOSE_COMINIT:
1923 case TRANS_RX_ERR_WITH_DATA_LEN0:
1924 case TRANS_RX_ERR_WITH_BAD_HASH:
1925 case TRANS_RX_XRDY_WLEN_ZERO_ERR:
634a9585
XC
1926 case TRANS_RX_ERR_WITH_BAD_FRM_TYPE:
1927 case DMA_TX_DATA_SGL_OVERFLOW:
1928 case DMA_TX_UNEXP_XFER_ERR:
1929 case DMA_TX_UNEXP_RETRANS_ERR:
1930 case DMA_TX_XFER_LEN_OVERFLOW:
1931 case DMA_TX_XFER_OFFSET_ERR:
e8fed0e9
JG
1932 case SIPC_RX_FIS_STATUS_ERR_BIT_VLD:
1933 case SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR:
1934 case SIPC_RX_FIS_STATUS_BSY_BIT_ERR:
1935 case SIPC_RX_WRSETUP_LEN_ODD_ERR:
1936 case SIPC_RX_WRSETUP_LEN_ZERO_ERR:
1937 case SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR:
1938 case SIPC_RX_SATA_UNEXP_FIS_ERR:
634a9585
XC
1939 case DMA_RX_DATA_SGL_OVERFLOW:
1940 case DMA_RX_DATA_OFFSET_ERR:
e8fed0e9
JG
1941 case DMA_RX_SATA_FRAME_TYPE_ERR:
1942 case DMA_RX_UNEXP_RDFRAME_ERR:
1943 case DMA_RX_PIO_DATA_LEN_ERR:
1944 case DMA_RX_RDSETUP_STATUS_ERR:
1945 case DMA_RX_RDSETUP_STATUS_DRQ_ERR:
1946 case DMA_RX_RDSETUP_STATUS_BSY_ERR:
1947 case DMA_RX_RDSETUP_LEN_ODD_ERR:
1948 case DMA_RX_RDSETUP_LEN_ZERO_ERR:
1949 case DMA_RX_RDSETUP_LEN_OVER_ERR:
1950 case DMA_RX_RDSETUP_OFFSET_ERR:
1951 case DMA_RX_RDSETUP_ACTIVE_ERR:
1952 case DMA_RX_RDSETUP_ESTATUS_ERR:
1953 case DMA_RX_UNKNOWN_FRM_ERR:
634a9585
XC
1954 case TRANS_RX_SSP_FRM_LEN_ERR:
1955 case TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY:
e8fed0e9 1956 {
634a9585
XC
1957 slot->abort = 1;
1958 ts->stat = SAS_PHY_DOWN;
e8fed0e9
JG
1959 break;
1960 }
1961 default:
1962 {
1963 ts->stat = SAS_PROTO_RESPONSE;
1964 break;
1965 }
1966 }
1967 sata_done_v2_hw(hisi_hba, task, slot);
1968 }
1969 break;
1970 default:
1971 break;
1972 }
1973}
1974
31a9cfa6 1975static int
405314df 1976slot_complete_v2_hw(struct hisi_hba *hisi_hba, struct hisi_sas_slot *slot)
31a9cfa6
JG
1977{
1978 struct sas_task *task = slot->task;
1979 struct hisi_sas_device *sas_dev;
1980 struct device *dev = &hisi_hba->pdev->dev;
1981 struct task_status_struct *ts;
1982 struct domain_device *device;
1983 enum exec_status sts;
1984 struct hisi_sas_complete_v2_hdr *complete_queue =
1985 hisi_hba->complete_hdr[slot->cmplt_queue];
1986 struct hisi_sas_complete_v2_hdr *complete_hdr =
1987 &complete_queue[slot->cmplt_queue_slot];
54c9dd2d 1988 unsigned long flags;
a305f337 1989 int aborted;
31a9cfa6
JG
1990
1991 if (unlikely(!task || !task->lldd_task || !task->dev))
1992 return -EINVAL;
1993
1994 ts = &task->task_status;
1995 device = task->dev;
1996 sas_dev = device->lldd_dev;
1997
54c9dd2d 1998 spin_lock_irqsave(&task->task_state_lock, flags);
a305f337 1999 aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
31a9cfa6
JG
2000 task->task_state_flags &=
2001 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
54c9dd2d 2002 spin_unlock_irqrestore(&task->task_state_lock, flags);
31a9cfa6
JG
2003
2004 memset(ts, 0, sizeof(*ts));
2005 ts->resp = SAS_TASK_COMPLETE;
2006
a305f337
JG
2007 if (unlikely(aborted)) {
2008 ts->stat = SAS_ABORTED_TASK;
2009 hisi_sas_slot_task_free(hisi_hba, task, slot);
2010 return -1;
2011 }
2012
405314df
JG
2013 if (unlikely(!sas_dev)) {
2014 dev_dbg(dev, "slot complete: port has no device\n");
31a9cfa6
JG
2015 ts->stat = SAS_PHY_DOWN;
2016 goto out;
2017 }
2018
df032d0e
JG
2019 /* Use SAS+TMF status codes */
2020 switch ((complete_hdr->dw0 & CMPLT_HDR_ABORT_STAT_MSK)
2021 >> CMPLT_HDR_ABORT_STAT_OFF) {
2022 case STAT_IO_ABORTED:
2023 /* this io has been aborted by abort command */
2024 ts->stat = SAS_ABORTED_TASK;
2025 goto out;
2026 case STAT_IO_COMPLETE:
2027 /* internal abort command complete */
c35279f2 2028 ts->stat = TMF_RESP_FUNC_SUCC;
df032d0e
JG
2029 goto out;
2030 case STAT_IO_NO_DEVICE:
2031 ts->stat = TMF_RESP_FUNC_COMPLETE;
2032 goto out;
2033 case STAT_IO_NOT_VALID:
2034 /* abort single io, controller don't find
2035 * the io need to abort
2036 */
2037 ts->stat = TMF_RESP_FUNC_FAILED;
2038 goto out;
2039 default:
2040 break;
2041 }
2042
31a9cfa6
JG
2043 if ((complete_hdr->dw0 & CMPLT_HDR_ERX_MSK) &&
2044 (!(complete_hdr->dw0 & CMPLT_HDR_RSPNS_XFRD_MSK))) {
634a9585
XC
2045 u32 err_phase = (complete_hdr->dw0 & CMPLT_HDR_ERR_PHASE_MSK)
2046 >> CMPLT_HDR_ERR_PHASE_OFF;
2047
2048 /* Analyse error happens on which phase TX or RX */
2049 if (ERR_ON_TX_PHASE(err_phase))
2050 slot_err_v2_hw(hisi_hba, task, slot, 1);
2051 else if (ERR_ON_RX_PHASE(err_phase))
2052 slot_err_v2_hw(hisi_hba, task, slot, 2);
fc866951
XC
2053
2054 if (unlikely(slot->abort))
9c8ee657 2055 return ts->stat;
31a9cfa6
JG
2056 goto out;
2057 }
2058
2059 switch (task->task_proto) {
2060 case SAS_PROTOCOL_SSP:
2061 {
2062 struct ssp_response_iu *iu = slot->status_buffer +
2063 sizeof(struct hisi_sas_err_record);
2064
2065 sas_ssp_task_response(dev, task, iu);
2066 break;
2067 }
2068 case SAS_PROTOCOL_SMP:
2069 {
2070 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
2071 void *to;
2072
2073 ts->stat = SAM_STAT_GOOD;
2074 to = kmap_atomic(sg_page(sg_resp));
2075
2076 dma_unmap_sg(dev, &task->smp_task.smp_resp, 1,
2077 DMA_FROM_DEVICE);
2078 dma_unmap_sg(dev, &task->smp_task.smp_req, 1,
2079 DMA_TO_DEVICE);
2080 memcpy(to + sg_resp->offset,
2081 slot->status_buffer +
2082 sizeof(struct hisi_sas_err_record),
2083 sg_dma_len(sg_resp));
2084 kunmap_atomic(to);
2085 break;
2086 }
2087 case SAS_PROTOCOL_SATA:
2088 case SAS_PROTOCOL_STP:
2089 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
6f2ff1a1
JG
2090 {
2091 ts->stat = SAM_STAT_GOOD;
2092 sata_done_v2_hw(hisi_hba, task, slot);
2093 break;
2094 }
31a9cfa6
JG
2095 default:
2096 ts->stat = SAM_STAT_CHECK_CONDITION;
2097 break;
2098 }
2099
2100 if (!slot->port->port_attached) {
2101 dev_err(dev, "slot complete: port %d has removed\n",
2102 slot->port->sas_port.id);
2103 ts->stat = SAS_PHY_DOWN;
2104 }
2105
2106out:
54c9dd2d 2107 spin_lock_irqsave(&task->task_state_lock, flags);
fc866951 2108 task->task_state_flags |= SAS_TASK_STATE_DONE;
54c9dd2d 2109 spin_unlock_irqrestore(&task->task_state_lock, flags);
31a9cfa6
JG
2110 hisi_sas_slot_task_free(hisi_hba, task, slot);
2111 sts = ts->stat;
2112
2113 if (task->task_done)
2114 task->task_done(task);
2115
2116 return sts;
2117}
2118
6f2ff1a1
JG
2119static u8 get_ata_protocol(u8 cmd, int direction)
2120{
2121 switch (cmd) {
2122 case ATA_CMD_FPDMA_WRITE:
2123 case ATA_CMD_FPDMA_READ:
ef026b18
HR
2124 case ATA_CMD_FPDMA_RECV:
2125 case ATA_CMD_FPDMA_SEND:
661ce1f0 2126 case ATA_CMD_NCQ_NON_DATA:
6f2ff1a1
JG
2127 return SATA_PROTOCOL_FPDMA;
2128
ee44bfe4 2129 case ATA_CMD_DOWNLOAD_MICRO:
6f2ff1a1
JG
2130 case ATA_CMD_ID_ATA:
2131 case ATA_CMD_PMP_READ:
2132 case ATA_CMD_READ_LOG_EXT:
2133 case ATA_CMD_PIO_READ:
2134 case ATA_CMD_PIO_READ_EXT:
2135 case ATA_CMD_PMP_WRITE:
2136 case ATA_CMD_WRITE_LOG_EXT:
2137 case ATA_CMD_PIO_WRITE:
2138 case ATA_CMD_PIO_WRITE_EXT:
2139 return SATA_PROTOCOL_PIO;
2140
ee44bfe4
XC
2141 case ATA_CMD_DSM:
2142 case ATA_CMD_DOWNLOAD_MICRO_DMA:
2143 case ATA_CMD_PMP_READ_DMA:
2144 case ATA_CMD_PMP_WRITE_DMA:
6f2ff1a1
JG
2145 case ATA_CMD_READ:
2146 case ATA_CMD_READ_EXT:
2147 case ATA_CMD_READ_LOG_DMA_EXT:
ee44bfe4
XC
2148 case ATA_CMD_READ_STREAM_DMA_EXT:
2149 case ATA_CMD_TRUSTED_RCV_DMA:
2150 case ATA_CMD_TRUSTED_SND_DMA:
6f2ff1a1
JG
2151 case ATA_CMD_WRITE:
2152 case ATA_CMD_WRITE_EXT:
ee44bfe4 2153 case ATA_CMD_WRITE_FUA_EXT:
6f2ff1a1
JG
2154 case ATA_CMD_WRITE_QUEUED:
2155 case ATA_CMD_WRITE_LOG_DMA_EXT:
ee44bfe4 2156 case ATA_CMD_WRITE_STREAM_DMA_EXT:
6f2ff1a1
JG
2157 return SATA_PROTOCOL_DMA;
2158
6f2ff1a1 2159 case ATA_CMD_CHK_POWER:
ee44bfe4
XC
2160 case ATA_CMD_DEV_RESET:
2161 case ATA_CMD_EDD:
6f2ff1a1
JG
2162 case ATA_CMD_FLUSH:
2163 case ATA_CMD_FLUSH_EXT:
2164 case ATA_CMD_VERIFY:
2165 case ATA_CMD_VERIFY_EXT:
2166 case ATA_CMD_SET_FEATURES:
2167 case ATA_CMD_STANDBY:
2168 case ATA_CMD_STANDBYNOW1:
2169 return SATA_PROTOCOL_NONDATA;
2170 default:
2171 if (direction == DMA_NONE)
2172 return SATA_PROTOCOL_NONDATA;
2173 return SATA_PROTOCOL_PIO;
2174 }
2175}
2176
2177static int get_ncq_tag_v2_hw(struct sas_task *task, u32 *tag)
2178{
2179 struct ata_queued_cmd *qc = task->uldd_task;
2180
2181 if (qc) {
2182 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
2183 qc->tf.command == ATA_CMD_FPDMA_READ) {
2184 *tag = qc->tag;
2185 return 1;
2186 }
2187 }
2188 return 0;
2189}
2190
2191static int prep_ata_v2_hw(struct hisi_hba *hisi_hba,
2192 struct hisi_sas_slot *slot)
2193{
2194 struct sas_task *task = slot->task;
2195 struct domain_device *device = task->dev;
2196 struct domain_device *parent_dev = device->parent;
2197 struct hisi_sas_device *sas_dev = device->lldd_dev;
2198 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
2e244f0f
JG
2199 struct asd_sas_port *sas_port = device->port;
2200 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
6f2ff1a1
JG
2201 u8 *buf_cmd;
2202 int has_data = 0, rc = 0, hdr_tag = 0;
2203 u32 dw1 = 0, dw2 = 0;
2204
2205 /* create header */
2206 /* dw0 */
2207 hdr->dw0 = cpu_to_le32(port->id << CMD_HDR_PORT_OFF);
2208 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type))
2209 hdr->dw0 |= cpu_to_le32(3 << CMD_HDR_CMD_OFF);
2210 else
2211 hdr->dw0 |= cpu_to_le32(4 << CMD_HDR_CMD_OFF);
2212
2213 /* dw1 */
2214 switch (task->data_dir) {
2215 case DMA_TO_DEVICE:
2216 has_data = 1;
2217 dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
2218 break;
2219 case DMA_FROM_DEVICE:
2220 has_data = 1;
2221 dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
2222 break;
2223 default:
2224 dw1 &= ~CMD_HDR_DIR_MSK;
2225 }
2226
7c594f04
XC
2227 if ((task->ata_task.fis.command == ATA_CMD_DEV_RESET) &&
2228 (task->ata_task.fis.control & ATA_SRST))
6f2ff1a1
JG
2229 dw1 |= 1 << CMD_HDR_RESET_OFF;
2230
2231 dw1 |= (get_ata_protocol(task->ata_task.fis.command, task->data_dir))
2232 << CMD_HDR_FRAME_TYPE_OFF;
2233 dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
2234 hdr->dw1 = cpu_to_le32(dw1);
2235
2236 /* dw2 */
2237 if (task->ata_task.use_ncq && get_ncq_tag_v2_hw(task, &hdr_tag)) {
2238 task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
2239 dw2 |= hdr_tag << CMD_HDR_NCQ_TAG_OFF;
2240 }
2241
2242 dw2 |= (HISI_SAS_MAX_STP_RESP_SZ / 4) << CMD_HDR_CFL_OFF |
2243 2 << CMD_HDR_SG_MOD_OFF;
2244 hdr->dw2 = cpu_to_le32(dw2);
2245
2246 /* dw3 */
2247 hdr->transfer_tags = cpu_to_le32(slot->idx);
2248
2249 if (has_data) {
2250 rc = prep_prd_sge_v2_hw(hisi_hba, slot, hdr, task->scatter,
2251 slot->n_elem);
2252 if (rc)
2253 return rc;
2254 }
2255
2256
2257 hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
2258 hdr->cmd_table_addr = cpu_to_le64(slot->command_table_dma);
2259 hdr->sts_buffer_addr = cpu_to_le64(slot->status_buffer_dma);
2260
2261 buf_cmd = slot->command_table;
2262
2263 if (likely(!task->ata_task.device_control_reg_update))
2264 task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
2265 /* fill in command FIS */
2266 memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
2267
2268 return 0;
2269}
2270
a3e665d9
JG
2271static int prep_abort_v2_hw(struct hisi_hba *hisi_hba,
2272 struct hisi_sas_slot *slot,
2273 int device_id, int abort_flag, int tag_to_abort)
2274{
2275 struct sas_task *task = slot->task;
2276 struct domain_device *dev = task->dev;
2277 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
2278 struct hisi_sas_port *port = slot->port;
2279
2280 /* dw0 */
2281 hdr->dw0 = cpu_to_le32((5 << CMD_HDR_CMD_OFF) | /*abort*/
2282 (port->id << CMD_HDR_PORT_OFF) |
2283 ((dev_is_sata(dev) ? 1:0) <<
2284 CMD_HDR_ABORT_DEVICE_TYPE_OFF) |
2285 (abort_flag << CMD_HDR_ABORT_FLAG_OFF));
2286
2287 /* dw1 */
2288 hdr->dw1 = cpu_to_le32(device_id << CMD_HDR_DEV_ID_OFF);
2289
2290 /* dw7 */
2291 hdr->dw7 = cpu_to_le32(tag_to_abort << CMD_HDR_ABORT_IPTT_OFF);
2292 hdr->transfer_tags = cpu_to_le32(slot->idx);
2293
2294 return 0;
2295}
2296
7911e66f
JG
2297static int phy_up_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
2298{
981843c6 2299 int i, res = IRQ_HANDLED;
4935933e 2300 u32 port_id, link_rate, hard_phy_linkrate;
7911e66f
JG
2301 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2302 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2303 struct device *dev = &hisi_hba->pdev->dev;
2304 u32 *frame_rcvd = (u32 *)sas_phy->frame_rcvd;
2305 struct sas_identify_frame *id = (struct sas_identify_frame *)frame_rcvd;
2306
2307 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 1);
2308
4935933e 2309 if (is_sata_phy_v2_hw(hisi_hba, phy_no))
7911e66f
JG
2310 goto end;
2311
2312 if (phy_no == 8) {
2313 u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
2314
2315 port_id = (port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
2316 PORT_STATE_PHY8_PORT_NUM_OFF;
2317 link_rate = (port_state & PORT_STATE_PHY8_CONN_RATE_MSK) >>
2318 PORT_STATE_PHY8_CONN_RATE_OFF;
2319 } else {
2320 port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
2321 port_id = (port_id >> (4 * phy_no)) & 0xf;
2322 link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
2323 link_rate = (link_rate >> (phy_no * 4)) & 0xf;
2324 }
2325
2326 if (port_id == 0xf) {
2327 dev_err(dev, "phyup: phy%d invalid portid\n", phy_no);
2328 res = IRQ_NONE;
2329 goto end;
2330 }
2331
2332 for (i = 0; i < 6; i++) {
2333 u32 idaf = hisi_sas_phy_read32(hisi_hba, phy_no,
2334 RX_IDAF_DWORD0 + (i * 4));
2335 frame_rcvd[i] = __swab32(idaf);
2336 }
2337
7911e66f
JG
2338 sas_phy->linkrate = link_rate;
2339 hard_phy_linkrate = hisi_sas_phy_read32(hisi_hba, phy_no,
2340 HARD_PHY_LINKRATE);
2341 phy->maximum_linkrate = hard_phy_linkrate & 0xf;
2342 phy->minimum_linkrate = (hard_phy_linkrate >> 4) & 0xf;
2343
2344 sas_phy->oob_mode = SAS_OOB_MODE;
2345 memcpy(sas_phy->attached_sas_addr, &id->sas_addr, SAS_ADDR_SIZE);
2346 dev_info(dev, "phyup: phy%d link_rate=%d\n", phy_no, link_rate);
2347 phy->port_id = port_id;
2348 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
2349 phy->phy_type |= PORT_TYPE_SAS;
2350 phy->phy_attached = 1;
2351 phy->identify.device_type = id->dev_type;
2352 phy->frame_rcvd_size = sizeof(struct sas_identify_frame);
2353 if (phy->identify.device_type == SAS_END_DEVICE)
2354 phy->identify.target_port_protocols =
2355 SAS_PROTOCOL_SSP;
f2f89c32 2356 else if (phy->identify.device_type != SAS_PHY_UNUSED) {
7911e66f
JG
2357 phy->identify.target_port_protocols =
2358 SAS_PROTOCOL_SMP;
f2f89c32
XC
2359 if (!timer_pending(&hisi_hba->timer))
2360 set_link_timer_quirk(hisi_hba);
2361 }
7911e66f
JG
2362 queue_work(hisi_hba->wq, &phy->phyup_ws);
2363
2364end:
2365 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
2366 CHL_INT0_SL_PHY_ENABLE_MSK);
2367 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 0);
2368
2369 return res;
2370}
2371
f2f89c32
XC
2372static bool check_any_wideports_v2_hw(struct hisi_hba *hisi_hba)
2373{
2374 u32 port_state;
2375
2376 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
2377 if (port_state & 0x1ff)
2378 return true;
2379
2380 return false;
2381}
2382
5473c060
JG
2383static int phy_down_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
2384{
9c81e2cf 2385 u32 phy_state, sl_ctrl, txid_auto;
f2f89c32
XC
2386 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2387 struct hisi_sas_port *port = phy->port;
5473c060
JG
2388
2389 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 1);
2390
5473c060 2391 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
5473c060
JG
2392 hisi_sas_phy_down(hisi_hba, phy_no, (phy_state & 1 << phy_no) ? 1 : 0);
2393
9c81e2cf
JG
2394 sl_ctrl = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
2395 hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL,
2396 sl_ctrl & ~SL_CONTROL_CTA_MSK);
f2f89c32
XC
2397 if (port && !get_wideport_bitmap_v2_hw(hisi_hba, port->id))
2398 if (!check_any_wideports_v2_hw(hisi_hba) &&
2399 timer_pending(&hisi_hba->timer))
2400 del_timer(&hisi_hba->timer);
9c81e2cf
JG
2401
2402 txid_auto = hisi_sas_phy_read32(hisi_hba, phy_no, TXID_AUTO);
2403 hisi_sas_phy_write32(hisi_hba, phy_no, TXID_AUTO,
2404 txid_auto | TXID_AUTO_CT3_MSK);
2405
5473c060
JG
2406 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0, CHL_INT0_NOT_RDY_MSK);
2407 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 0);
2408
981843c6 2409 return IRQ_HANDLED;
5473c060
JG
2410}
2411
7911e66f
JG
2412static irqreturn_t int_phy_updown_v2_hw(int irq_no, void *p)
2413{
2414 struct hisi_hba *hisi_hba = p;
2415 u32 irq_msk;
2416 int phy_no = 0;
7911e66f
JG
2417
2418 irq_msk = (hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO)
2419 >> HGC_INVLD_DQE_INFO_FB_CH0_OFF) & 0x1ff;
2420 while (irq_msk) {
2421 if (irq_msk & 1) {
981843c6
XT
2422 u32 reg_value = hisi_sas_phy_read32(hisi_hba, phy_no,
2423 CHL_INT0);
2424
2425 switch (reg_value & (CHL_INT0_NOT_RDY_MSK |
2426 CHL_INT0_SL_PHY_ENABLE_MSK)) {
7911e66f 2427
981843c6 2428 case CHL_INT0_SL_PHY_ENABLE_MSK:
7911e66f 2429 /* phy up */
981843c6
XT
2430 if (phy_up_v2_hw(phy_no, hisi_hba) ==
2431 IRQ_NONE)
2432 return IRQ_NONE;
2433 break;
7911e66f 2434
981843c6 2435 case CHL_INT0_NOT_RDY_MSK:
5473c060 2436 /* phy down */
981843c6
XT
2437 if (phy_down_v2_hw(phy_no, hisi_hba) ==
2438 IRQ_NONE)
2439 return IRQ_NONE;
2440 break;
2441
2442 case (CHL_INT0_NOT_RDY_MSK |
2443 CHL_INT0_SL_PHY_ENABLE_MSK):
2444 reg_value = hisi_sas_read32(hisi_hba,
2445 PHY_STATE);
2446 if (reg_value & BIT(phy_no)) {
2447 /* phy up */
2448 if (phy_up_v2_hw(phy_no, hisi_hba) ==
2449 IRQ_NONE)
2450 return IRQ_NONE;
2451 } else {
2452 /* phy down */
2453 if (phy_down_v2_hw(phy_no, hisi_hba) ==
2454 IRQ_NONE)
2455 return IRQ_NONE;
5473c060 2456 }
981843c6
XT
2457 break;
2458
2459 default:
2460 break;
2461 }
2462
7911e66f
JG
2463 }
2464 irq_msk >>= 1;
2465 phy_no++;
2466 }
2467
981843c6 2468 return IRQ_HANDLED;
7911e66f
JG
2469}
2470
d3bf3d84
JG
2471static void phy_bcast_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
2472{
2473 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2474 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2475 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
85080a25 2476 u32 bcast_status;
d3bf3d84
JG
2477
2478 hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 1);
85080a25
XC
2479 bcast_status = hisi_sas_phy_read32(hisi_hba, phy_no, RX_PRIMS_STATUS);
2480 if (bcast_status & RX_BCAST_CHG_MSK)
2481 sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
d3bf3d84
JG
2482 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
2483 CHL_INT0_SL_RX_BCST_ACK_MSK);
2484 hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 0);
2485}
2486
2487static irqreturn_t int_chnl_int_v2_hw(int irq_no, void *p)
2488{
2489 struct hisi_hba *hisi_hba = p;
2490 struct device *dev = &hisi_hba->pdev->dev;
2491 u32 ent_msk, ent_tmp, irq_msk;
2492 int phy_no = 0;
2493
2494 ent_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK3);
2495 ent_tmp = ent_msk;
2496 ent_msk |= ENT_INT_SRC_MSK3_ENT95_MSK_MSK;
2497 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, ent_msk);
2498
2499 irq_msk = (hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO) >>
2500 HGC_INVLD_DQE_INFO_FB_CH3_OFF) & 0x1ff;
2501
2502 while (irq_msk) {
2503 if (irq_msk & (1 << phy_no)) {
2504 u32 irq_value0 = hisi_sas_phy_read32(hisi_hba, phy_no,
2505 CHL_INT0);
2506 u32 irq_value1 = hisi_sas_phy_read32(hisi_hba, phy_no,
2507 CHL_INT1);
2508 u32 irq_value2 = hisi_sas_phy_read32(hisi_hba, phy_no,
2509 CHL_INT2);
2510
2511 if (irq_value1) {
2512 if (irq_value1 & (CHL_INT1_DMAC_RX_ECC_ERR_MSK |
2513 CHL_INT1_DMAC_TX_ECC_ERR_MSK))
d3b688d3
XC
2514 panic("%s: DMAC RX/TX ecc bad error!\
2515 (0x%x)",
2516 dev_name(dev), irq_value1);
d3bf3d84
JG
2517
2518 hisi_sas_phy_write32(hisi_hba, phy_no,
2519 CHL_INT1, irq_value1);
2520 }
2521
2522 if (irq_value2)
2523 hisi_sas_phy_write32(hisi_hba, phy_no,
2524 CHL_INT2, irq_value2);
2525
2526
2527 if (irq_value0) {
2528 if (irq_value0 & CHL_INT0_SL_RX_BCST_ACK_MSK)
2529 phy_bcast_v2_hw(phy_no, hisi_hba);
2530
2531 hisi_sas_phy_write32(hisi_hba, phy_no,
2532 CHL_INT0, irq_value0
2533 & (~CHL_INT0_HOTPLUG_TOUT_MSK)
2534 & (~CHL_INT0_SL_PHY_ENABLE_MSK)
2535 & (~CHL_INT0_NOT_RDY_MSK));
2536 }
2537 }
2538 irq_msk &= ~(1 << phy_no);
2539 phy_no++;
2540 }
2541
2542 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, ent_tmp);
2543
2544 return IRQ_HANDLED;
2545}
2546
d3b688d3
XC
2547static void
2548one_bit_ecc_error_process_v2_hw(struct hisi_hba *hisi_hba, u32 irq_value)
2549{
2550 struct device *dev = &hisi_hba->pdev->dev;
2551 u32 reg_val;
2552
2553 if (irq_value & BIT(SAS_ECC_INTR_DQE_ECC_1B_OFF)) {
2554 reg_val = hisi_sas_read32(hisi_hba, HGC_DQE_ECC_ADDR);
2555 dev_warn(dev, "hgc_dqe_acc1b_intr found: \
2556 Ram address is 0x%08X\n",
2557 (reg_val & HGC_DQE_ECC_1B_ADDR_MSK) >>
2558 HGC_DQE_ECC_1B_ADDR_OFF);
2559 }
2560
2561 if (irq_value & BIT(SAS_ECC_INTR_IOST_ECC_1B_OFF)) {
2562 reg_val = hisi_sas_read32(hisi_hba, HGC_IOST_ECC_ADDR);
2563 dev_warn(dev, "hgc_iost_acc1b_intr found: \
2564 Ram address is 0x%08X\n",
2565 (reg_val & HGC_IOST_ECC_1B_ADDR_MSK) >>
2566 HGC_IOST_ECC_1B_ADDR_OFF);
2567 }
2568
2569 if (irq_value & BIT(SAS_ECC_INTR_ITCT_ECC_1B_OFF)) {
2570 reg_val = hisi_sas_read32(hisi_hba, HGC_ITCT_ECC_ADDR);
2571 dev_warn(dev, "hgc_itct_acc1b_intr found: \
2572 Ram address is 0x%08X\n",
2573 (reg_val & HGC_ITCT_ECC_1B_ADDR_MSK) >>
2574 HGC_ITCT_ECC_1B_ADDR_OFF);
2575 }
2576
2577 if (irq_value & BIT(SAS_ECC_INTR_IOSTLIST_ECC_1B_OFF)) {
2578 reg_val = hisi_sas_read32(hisi_hba, HGC_LM_DFX_STATUS2);
2579 dev_warn(dev, "hgc_iostl_acc1b_intr found: \
2580 memory address is 0x%08X\n",
2581 (reg_val & HGC_LM_DFX_STATUS2_IOSTLIST_MSK) >>
2582 HGC_LM_DFX_STATUS2_IOSTLIST_OFF);
2583 }
2584
2585 if (irq_value & BIT(SAS_ECC_INTR_ITCTLIST_ECC_1B_OFF)) {
2586 reg_val = hisi_sas_read32(hisi_hba, HGC_LM_DFX_STATUS2);
2587 dev_warn(dev, "hgc_itctl_acc1b_intr found: \
2588 memory address is 0x%08X\n",
2589 (reg_val & HGC_LM_DFX_STATUS2_ITCTLIST_MSK) >>
2590 HGC_LM_DFX_STATUS2_ITCTLIST_OFF);
2591 }
2592
2593 if (irq_value & BIT(SAS_ECC_INTR_CQE_ECC_1B_OFF)) {
2594 reg_val = hisi_sas_read32(hisi_hba, HGC_CQE_ECC_ADDR);
2595 dev_warn(dev, "hgc_cqe_acc1b_intr found: \
2596 Ram address is 0x%08X\n",
2597 (reg_val & HGC_CQE_ECC_1B_ADDR_MSK) >>
2598 HGC_CQE_ECC_1B_ADDR_OFF);
2599 }
2600
2601 if (irq_value & BIT(SAS_ECC_INTR_NCQ_MEM0_ECC_1B_OFF)) {
2602 reg_val = hisi_sas_read32(hisi_hba, HGC_RXM_DFX_STATUS14);
2603 dev_warn(dev, "rxm_mem0_acc1b_intr found: \
2604 memory address is 0x%08X\n",
2605 (reg_val & HGC_RXM_DFX_STATUS14_MEM0_MSK) >>
2606 HGC_RXM_DFX_STATUS14_MEM0_OFF);
2607 }
2608
2609 if (irq_value & BIT(SAS_ECC_INTR_NCQ_MEM1_ECC_1B_OFF)) {
2610 reg_val = hisi_sas_read32(hisi_hba, HGC_RXM_DFX_STATUS14);
2611 dev_warn(dev, "rxm_mem1_acc1b_intr found: \
2612 memory address is 0x%08X\n",
2613 (reg_val & HGC_RXM_DFX_STATUS14_MEM1_MSK) >>
2614 HGC_RXM_DFX_STATUS14_MEM1_OFF);
2615 }
2616
2617 if (irq_value & BIT(SAS_ECC_INTR_NCQ_MEM2_ECC_1B_OFF)) {
2618 reg_val = hisi_sas_read32(hisi_hba, HGC_RXM_DFX_STATUS14);
2619 dev_warn(dev, "rxm_mem2_acc1b_intr found: \
2620 memory address is 0x%08X\n",
2621 (reg_val & HGC_RXM_DFX_STATUS14_MEM2_MSK) >>
2622 HGC_RXM_DFX_STATUS14_MEM2_OFF);
2623 }
2624
2625 if (irq_value & BIT(SAS_ECC_INTR_NCQ_MEM3_ECC_1B_OFF)) {
2626 reg_val = hisi_sas_read32(hisi_hba, HGC_RXM_DFX_STATUS15);
2627 dev_warn(dev, "rxm_mem3_acc1b_intr found: \
2628 memory address is 0x%08X\n",
2629 (reg_val & HGC_RXM_DFX_STATUS15_MEM3_MSK) >>
2630 HGC_RXM_DFX_STATUS15_MEM3_OFF);
2631 }
2632
2633}
2634
2635static void multi_bit_ecc_error_process_v2_hw(struct hisi_hba *hisi_hba,
2636 u32 irq_value)
2637{
2638 u32 reg_val;
2639 struct device *dev = &hisi_hba->pdev->dev;
2640
2641 if (irq_value & BIT(SAS_ECC_INTR_DQE_ECC_MB_OFF)) {
2642 reg_val = hisi_sas_read32(hisi_hba, HGC_DQE_ECC_ADDR);
2643 panic("%s: hgc_dqe_accbad_intr (0x%x) found: \
2644 Ram address is 0x%08X\n",
2645 dev_name(dev), irq_value,
2646 (reg_val & HGC_DQE_ECC_MB_ADDR_MSK) >>
2647 HGC_DQE_ECC_MB_ADDR_OFF);
2648 }
2649
2650 if (irq_value & BIT(SAS_ECC_INTR_IOST_ECC_MB_OFF)) {
2651 reg_val = hisi_sas_read32(hisi_hba, HGC_IOST_ECC_ADDR);
2652 panic("%s: hgc_iost_accbad_intr (0x%x) found: \
2653 Ram address is 0x%08X\n",
2654 dev_name(dev), irq_value,
2655 (reg_val & HGC_IOST_ECC_MB_ADDR_MSK) >>
2656 HGC_IOST_ECC_MB_ADDR_OFF);
2657 }
2658
2659 if (irq_value & BIT(SAS_ECC_INTR_ITCT_ECC_MB_OFF)) {
2660 reg_val = hisi_sas_read32(hisi_hba, HGC_ITCT_ECC_ADDR);
2661 panic("%s: hgc_itct_accbad_intr (0x%x) found: \
2662 Ram address is 0x%08X\n",
2663 dev_name(dev), irq_value,
2664 (reg_val & HGC_ITCT_ECC_MB_ADDR_MSK) >>
2665 HGC_ITCT_ECC_MB_ADDR_OFF);
2666 }
2667
2668 if (irq_value & BIT(SAS_ECC_INTR_IOSTLIST_ECC_MB_OFF)) {
2669 reg_val = hisi_sas_read32(hisi_hba, HGC_LM_DFX_STATUS2);
2670 panic("%s: hgc_iostl_accbad_intr (0x%x) found: \
2671 memory address is 0x%08X\n",
2672 dev_name(dev), irq_value,
2673 (reg_val & HGC_LM_DFX_STATUS2_IOSTLIST_MSK) >>
2674 HGC_LM_DFX_STATUS2_IOSTLIST_OFF);
2675 }
2676
2677 if (irq_value & BIT(SAS_ECC_INTR_ITCTLIST_ECC_MB_OFF)) {
2678 reg_val = hisi_sas_read32(hisi_hba, HGC_LM_DFX_STATUS2);
2679 panic("%s: hgc_itctl_accbad_intr (0x%x) found: \
2680 memory address is 0x%08X\n",
2681 dev_name(dev), irq_value,
2682 (reg_val & HGC_LM_DFX_STATUS2_ITCTLIST_MSK) >>
2683 HGC_LM_DFX_STATUS2_ITCTLIST_OFF);
2684 }
2685
2686 if (irq_value & BIT(SAS_ECC_INTR_CQE_ECC_MB_OFF)) {
2687 reg_val = hisi_sas_read32(hisi_hba, HGC_CQE_ECC_ADDR);
2688 panic("%s: hgc_cqe_accbad_intr (0x%x) found: \
2689 Ram address is 0x%08X\n",
2690 dev_name(dev), irq_value,
2691 (reg_val & HGC_CQE_ECC_MB_ADDR_MSK) >>
2692 HGC_CQE_ECC_MB_ADDR_OFF);
2693 }
2694
2695 if (irq_value & BIT(SAS_ECC_INTR_NCQ_MEM0_ECC_MB_OFF)) {
2696 reg_val = hisi_sas_read32(hisi_hba, HGC_RXM_DFX_STATUS14);
2697 panic("%s: rxm_mem0_accbad_intr (0x%x) found: \
2698 memory address is 0x%08X\n",
2699 dev_name(dev), irq_value,
2700 (reg_val & HGC_RXM_DFX_STATUS14_MEM0_MSK) >>
2701 HGC_RXM_DFX_STATUS14_MEM0_OFF);
2702 }
2703
2704 if (irq_value & BIT(SAS_ECC_INTR_NCQ_MEM1_ECC_MB_OFF)) {
2705 reg_val = hisi_sas_read32(hisi_hba, HGC_RXM_DFX_STATUS14);
2706 panic("%s: rxm_mem1_accbad_intr (0x%x) found: \
2707 memory address is 0x%08X\n",
2708 dev_name(dev), irq_value,
2709 (reg_val & HGC_RXM_DFX_STATUS14_MEM1_MSK) >>
2710 HGC_RXM_DFX_STATUS14_MEM1_OFF);
2711 }
2712
2713 if (irq_value & BIT(SAS_ECC_INTR_NCQ_MEM2_ECC_MB_OFF)) {
2714 reg_val = hisi_sas_read32(hisi_hba, HGC_RXM_DFX_STATUS14);
2715 panic("%s: rxm_mem2_accbad_intr (0x%x) found: \
2716 memory address is 0x%08X\n",
2717 dev_name(dev), irq_value,
2718 (reg_val & HGC_RXM_DFX_STATUS14_MEM2_MSK) >>
2719 HGC_RXM_DFX_STATUS14_MEM2_OFF);
2720 }
2721
2722 if (irq_value & BIT(SAS_ECC_INTR_NCQ_MEM3_ECC_MB_OFF)) {
2723 reg_val = hisi_sas_read32(hisi_hba, HGC_RXM_DFX_STATUS15);
2724 panic("%s: rxm_mem3_accbad_intr (0x%x) found: \
2725 memory address is 0x%08X\n",
2726 dev_name(dev), irq_value,
2727 (reg_val & HGC_RXM_DFX_STATUS15_MEM3_MSK) >>
2728 HGC_RXM_DFX_STATUS15_MEM3_OFF);
2729 }
2730
2731}
2732
2733static irqreturn_t fatal_ecc_int_v2_hw(int irq_no, void *p)
2734{
2735 struct hisi_hba *hisi_hba = p;
2736 u32 irq_value, irq_msk;
2737
2738 irq_msk = hisi_sas_read32(hisi_hba, SAS_ECC_INTR_MSK);
2739 hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, irq_msk | 0xffffffff);
2740
2741 irq_value = hisi_sas_read32(hisi_hba, SAS_ECC_INTR);
2742 if (irq_value) {
2743 one_bit_ecc_error_process_v2_hw(hisi_hba, irq_value);
2744 multi_bit_ecc_error_process_v2_hw(hisi_hba, irq_value);
2745 }
2746
2747 hisi_sas_write32(hisi_hba, SAS_ECC_INTR, irq_value);
2748 hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, irq_msk);
2749
2750 return IRQ_HANDLED;
2751}
2752
2753#define AXI_ERR_NR 8
2754static const char axi_err_info[AXI_ERR_NR][32] = {
2755 "IOST_AXI_W_ERR",
2756 "IOST_AXI_R_ERR",
2757 "ITCT_AXI_W_ERR",
2758 "ITCT_AXI_R_ERR",
2759 "SATA_AXI_W_ERR",
2760 "SATA_AXI_R_ERR",
2761 "DQE_AXI_R_ERR",
2762 "CQE_AXI_W_ERR"
2763};
2764
2765#define FIFO_ERR_NR 5
2766static const char fifo_err_info[FIFO_ERR_NR][32] = {
2767 "CQE_WINFO_FIFO",
2768 "CQE_MSG_FIFIO",
2769 "GETDQE_FIFO",
2770 "CMDP_FIFO",
2771 "AWTCTRL_FIFO"
2772};
2773
2774static irqreturn_t fatal_axi_int_v2_hw(int irq_no, void *p)
2775{
2776 struct hisi_hba *hisi_hba = p;
2777 u32 irq_value, irq_msk, err_value;
2778 struct device *dev = &hisi_hba->pdev->dev;
2779
2780 irq_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK3);
2781 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, irq_msk | 0xfffffffe);
2782
2783 irq_value = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
2784 if (irq_value) {
2785 if (irq_value & BIT(ENT_INT_SRC3_WP_DEPTH_OFF)) {
2786 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
2787 1 << ENT_INT_SRC3_WP_DEPTH_OFF);
2788 panic("%s: write pointer and depth error (0x%x) \
2789 found!\n",
2790 dev_name(dev), irq_value);
2791 }
2792
2793 if (irq_value & BIT(ENT_INT_SRC3_IPTT_SLOT_NOMATCH_OFF)) {
2794 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
2795 1 <<
2796 ENT_INT_SRC3_IPTT_SLOT_NOMATCH_OFF);
2797 panic("%s: iptt no match slot error (0x%x) found!\n",
2798 dev_name(dev), irq_value);
2799 }
2800
2801 if (irq_value & BIT(ENT_INT_SRC3_RP_DEPTH_OFF))
2802 panic("%s: read pointer and depth error (0x%x) \
2803 found!\n",
2804 dev_name(dev), irq_value);
2805
2806 if (irq_value & BIT(ENT_INT_SRC3_AXI_OFF)) {
2807 int i;
2808
2809 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
2810 1 << ENT_INT_SRC3_AXI_OFF);
2811 err_value = hisi_sas_read32(hisi_hba,
2812 HGC_AXI_FIFO_ERR_INFO);
2813
2814 for (i = 0; i < AXI_ERR_NR; i++) {
2815 if (err_value & BIT(i))
2816 panic("%s: %s (0x%x) found!\n",
2817 dev_name(dev),
2818 axi_err_info[i], irq_value);
2819 }
2820 }
2821
2822 if (irq_value & BIT(ENT_INT_SRC3_FIFO_OFF)) {
2823 int i;
2824
2825 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
2826 1 << ENT_INT_SRC3_FIFO_OFF);
2827 err_value = hisi_sas_read32(hisi_hba,
2828 HGC_AXI_FIFO_ERR_INFO);
2829
2830 for (i = 0; i < FIFO_ERR_NR; i++) {
2831 if (err_value & BIT(AXI_ERR_NR + i))
2832 panic("%s: %s (0x%x) found!\n",
2833 dev_name(dev),
2834 fifo_err_info[i], irq_value);
2835 }
2836
2837 }
2838
2839 if (irq_value & BIT(ENT_INT_SRC3_LM_OFF)) {
2840 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
2841 1 << ENT_INT_SRC3_LM_OFF);
2842 panic("%s: LM add/fetch list error (0x%x) found!\n",
2843 dev_name(dev), irq_value);
2844 }
2845
2846 if (irq_value & BIT(ENT_INT_SRC3_ABT_OFF)) {
2847 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
2848 1 << ENT_INT_SRC3_ABT_OFF);
2849 panic("%s: SAS_HGC_ABT fetch LM list error (0x%x) found!\n",
2850 dev_name(dev), irq_value);
2851 }
2852 }
2853
2854 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, irq_msk);
2855
2856 return IRQ_HANDLED;
2857}
2858
d177c408 2859static void cq_tasklet_v2_hw(unsigned long val)
31a9cfa6 2860{
d177c408 2861 struct hisi_sas_cq *cq = (struct hisi_sas_cq *)val;
31a9cfa6
JG
2862 struct hisi_hba *hisi_hba = cq->hisi_hba;
2863 struct hisi_sas_slot *slot;
2864 struct hisi_sas_itct *itct;
2865 struct hisi_sas_complete_v2_hdr *complete_queue;
d177c408 2866 u32 rd_point = cq->rd_point, wr_point, dev_id;
31a9cfa6
JG
2867 int queue = cq->id;
2868
2869 complete_queue = hisi_hba->complete_hdr[queue];
31a9cfa6 2870
64d63187 2871 spin_lock(&hisi_hba->lock);
31a9cfa6
JG
2872 wr_point = hisi_sas_read32(hisi_hba, COMPL_Q_0_WR_PTR +
2873 (0x14 * queue));
2874
2875 while (rd_point != wr_point) {
2876 struct hisi_sas_complete_v2_hdr *complete_hdr;
2877 int iptt;
2878
2879 complete_hdr = &complete_queue[rd_point];
2880
2881 /* Check for NCQ completion */
2882 if (complete_hdr->act) {
2883 u32 act_tmp = complete_hdr->act;
2884 int ncq_tag_count = ffs(act_tmp);
2885
2886 dev_id = (complete_hdr->dw1 & CMPLT_HDR_DEV_ID_MSK) >>
2887 CMPLT_HDR_DEV_ID_OFF;
2888 itct = &hisi_hba->itct[dev_id];
2889
2890 /* The NCQ tags are held in the itct header */
2891 while (ncq_tag_count) {
2892 __le64 *ncq_tag = &itct->qw4_15[0];
2893
2894 ncq_tag_count -= 1;
2895 iptt = (ncq_tag[ncq_tag_count / 5]
2896 >> (ncq_tag_count % 5) * 12) & 0xfff;
2897
2898 slot = &hisi_hba->slot_info[iptt];
2899 slot->cmplt_queue_slot = rd_point;
2900 slot->cmplt_queue = queue;
405314df 2901 slot_complete_v2_hw(hisi_hba, slot);
31a9cfa6
JG
2902
2903 act_tmp &= ~(1 << ncq_tag_count);
2904 ncq_tag_count = ffs(act_tmp);
2905 }
2906 } else {
2907 iptt = (complete_hdr->dw1) & CMPLT_HDR_IPTT_MSK;
2908 slot = &hisi_hba->slot_info[iptt];
2909 slot->cmplt_queue_slot = rd_point;
2910 slot->cmplt_queue = queue;
405314df 2911 slot_complete_v2_hw(hisi_hba, slot);
31a9cfa6
JG
2912 }
2913
2914 if (++rd_point >= HISI_SAS_QUEUE_SLOTS)
2915 rd_point = 0;
2916 }
2917
2918 /* update rd_point */
e6c346f3 2919 cq->rd_point = rd_point;
31a9cfa6 2920 hisi_sas_write32(hisi_hba, COMPL_Q_0_RD_PTR + (0x14 * queue), rd_point);
64d63187 2921 spin_unlock(&hisi_hba->lock);
d177c408
JG
2922}
2923
2924static irqreturn_t cq_interrupt_v2_hw(int irq_no, void *p)
2925{
2926 struct hisi_sas_cq *cq = p;
2927 struct hisi_hba *hisi_hba = cq->hisi_hba;
2928 int queue = cq->id;
2929
2930 hisi_sas_write32(hisi_hba, OQ_INT_SRC, 1 << queue);
2931
2932 tasklet_schedule(&cq->tasklet);
2933
31a9cfa6
JG
2934 return IRQ_HANDLED;
2935}
2936
d43f9cdb
JG
2937static irqreturn_t sata_int_v2_hw(int irq_no, void *p)
2938{
2939 struct hisi_sas_phy *phy = p;
2940 struct hisi_hba *hisi_hba = phy->hisi_hba;
2941 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2942 struct device *dev = &hisi_hba->pdev->dev;
2943 struct hisi_sas_initial_fis *initial_fis;
2944 struct dev_to_host_fis *fis;
2945 u32 ent_tmp, ent_msk, ent_int, port_id, link_rate, hard_phy_linkrate;
2946 irqreturn_t res = IRQ_HANDLED;
2947 u8 attached_sas_addr[SAS_ADDR_SIZE] = {0};
11826e5d 2948 int phy_no, offset;
d43f9cdb
JG
2949
2950 phy_no = sas_phy->id;
2951 initial_fis = &hisi_hba->initial_fis[phy_no];
2952 fis = &initial_fis->fis;
2953
11826e5d
JG
2954 offset = 4 * (phy_no / 4);
2955 ent_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK1 + offset);
2956 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1 + offset,
2957 ent_msk | 1 << ((phy_no % 4) * 8));
d43f9cdb 2958
11826e5d
JG
2959 ent_int = hisi_sas_read32(hisi_hba, ENT_INT_SRC1 + offset);
2960 ent_tmp = ent_int & (1 << (ENT_INT_SRC1_D2H_FIS_CH1_OFF *
2961 (phy_no % 4)));
d43f9cdb
JG
2962 ent_int >>= ENT_INT_SRC1_D2H_FIS_CH1_OFF * (phy_no % 4);
2963 if ((ent_int & ENT_INT_SRC1_D2H_FIS_CH0_MSK) == 0) {
2964 dev_warn(dev, "sata int: phy%d did not receive FIS\n", phy_no);
d43f9cdb
JG
2965 res = IRQ_NONE;
2966 goto end;
04708ff4
XC
2967 }
2968
2969 /* check ERR bit of Status Register */
2970 if (fis->status & ATA_ERR) {
2971 dev_warn(dev, "sata int: phy%d FIS status: 0x%x\n", phy_no,
2972 fis->status);
2973 disable_phy_v2_hw(hisi_hba, phy_no);
2974 enable_phy_v2_hw(hisi_hba, phy_no);
2975 res = IRQ_NONE;
2976 goto end;
d43f9cdb
JG
2977 }
2978
2979 if (unlikely(phy_no == 8)) {
2980 u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
2981
2982 port_id = (port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
2983 PORT_STATE_PHY8_PORT_NUM_OFF;
2984 link_rate = (port_state & PORT_STATE_PHY8_CONN_RATE_MSK) >>
2985 PORT_STATE_PHY8_CONN_RATE_OFF;
2986 } else {
2987 port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
2988 port_id = (port_id >> (4 * phy_no)) & 0xf;
2989 link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
2990 link_rate = (link_rate >> (phy_no * 4)) & 0xf;
2991 }
2992
2993 if (port_id == 0xf) {
2994 dev_err(dev, "sata int: phy%d invalid portid\n", phy_no);
2995 res = IRQ_NONE;
2996 goto end;
2997 }
2998
2999 sas_phy->linkrate = link_rate;
3000 hard_phy_linkrate = hisi_sas_phy_read32(hisi_hba, phy_no,
3001 HARD_PHY_LINKRATE);
3002 phy->maximum_linkrate = hard_phy_linkrate & 0xf;
3003 phy->minimum_linkrate = (hard_phy_linkrate >> 4) & 0xf;
3004
3005 sas_phy->oob_mode = SATA_OOB_MODE;
3006 /* Make up some unique SAS address */
3007 attached_sas_addr[0] = 0x50;
3008 attached_sas_addr[7] = phy_no;
3009 memcpy(sas_phy->attached_sas_addr, attached_sas_addr, SAS_ADDR_SIZE);
3010 memcpy(sas_phy->frame_rcvd, fis, sizeof(struct dev_to_host_fis));
3011 dev_info(dev, "sata int phyup: phy%d link_rate=%d\n", phy_no, link_rate);
3012 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
3013 phy->port_id = port_id;
3014 phy->phy_type |= PORT_TYPE_SATA;
3015 phy->phy_attached = 1;
3016 phy->identify.device_type = SAS_SATA_DEV;
3017 phy->frame_rcvd_size = sizeof(struct dev_to_host_fis);
3018 phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
3019 queue_work(hisi_hba->wq, &phy->phyup_ws);
3020
3021end:
11826e5d
JG
3022 hisi_sas_write32(hisi_hba, ENT_INT_SRC1 + offset, ent_tmp);
3023 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1 + offset, ent_msk);
d43f9cdb
JG
3024
3025 return res;
3026}
3027
7911e66f
JG
3028static irq_handler_t phy_interrupts[HISI_SAS_PHY_INT_NR] = {
3029 int_phy_updown_v2_hw,
d3bf3d84 3030 int_chnl_int_v2_hw,
7911e66f
JG
3031};
3032
d3b688d3
XC
3033static irq_handler_t fatal_interrupts[HISI_SAS_FATAL_INT_NR] = {
3034 fatal_ecc_int_v2_hw,
3035 fatal_axi_int_v2_hw
3036};
3037
7911e66f
JG
3038/**
3039 * There is a limitation in the hip06 chipset that we need
3040 * to map in all mbigen interrupts, even if they are not used.
3041 */
3042static int interrupt_init_v2_hw(struct hisi_hba *hisi_hba)
3043{
3044 struct platform_device *pdev = hisi_hba->pdev;
3045 struct device *dev = &pdev->dev;
3046 int i, irq, rc, irq_map[128];
3047
3048
3049 for (i = 0; i < 128; i++)
3050 irq_map[i] = platform_get_irq(pdev, i);
3051
3052 for (i = 0; i < HISI_SAS_PHY_INT_NR; i++) {
3053 int idx = i;
3054
3055 irq = irq_map[idx + 1]; /* Phy up/down is irq1 */
3056 if (!irq) {
3057 dev_err(dev, "irq init: fail map phy interrupt %d\n",
3058 idx);
3059 return -ENOENT;
3060 }
3061
3062 rc = devm_request_irq(dev, irq, phy_interrupts[i], 0,
3063 DRV_NAME " phy", hisi_hba);
3064 if (rc) {
3065 dev_err(dev, "irq init: could not request "
3066 "phy interrupt %d, rc=%d\n",
3067 irq, rc);
3068 return -ENOENT;
3069 }
3070 }
3071
d43f9cdb
JG
3072 for (i = 0; i < hisi_hba->n_phy; i++) {
3073 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
3074 int idx = i + 72; /* First SATA interrupt is irq72 */
3075
3076 irq = irq_map[idx];
3077 if (!irq) {
3078 dev_err(dev, "irq init: fail map phy interrupt %d\n",
3079 idx);
3080 return -ENOENT;
3081 }
3082
3083 rc = devm_request_irq(dev, irq, sata_int_v2_hw, 0,
3084 DRV_NAME " sata", phy);
3085 if (rc) {
3086 dev_err(dev, "irq init: could not request "
3087 "sata interrupt %d, rc=%d\n",
3088 irq, rc);
3089 return -ENOENT;
3090 }
3091 }
31a9cfa6 3092
d3b688d3
XC
3093 for (i = 0; i < HISI_SAS_FATAL_INT_NR; i++) {
3094 int idx = i;
3095
3096 irq = irq_map[idx + 81];
3097 if (!irq) {
3098 dev_err(dev, "irq init: fail map fatal interrupt %d\n",
3099 idx);
3100 return -ENOENT;
3101 }
3102
3103 rc = devm_request_irq(dev, irq, fatal_interrupts[i], 0,
3104 DRV_NAME " fatal", hisi_hba);
3105 if (rc) {
3106 dev_err(dev,
3107 "irq init: could not request fatal interrupt %d, rc=%d\n",
3108 irq, rc);
3109 return -ENOENT;
3110 }
3111 }
3112
31a9cfa6
JG
3113 for (i = 0; i < hisi_hba->queue_count; i++) {
3114 int idx = i + 96; /* First cq interrupt is irq96 */
d177c408
JG
3115 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
3116 struct tasklet_struct *t = &cq->tasklet;
31a9cfa6
JG
3117
3118 irq = irq_map[idx];
3119 if (!irq) {
3120 dev_err(dev,
3121 "irq init: could not map cq interrupt %d\n",
3122 idx);
3123 return -ENOENT;
3124 }
3125 rc = devm_request_irq(dev, irq, cq_interrupt_v2_hw, 0,
3126 DRV_NAME " cq", &hisi_hba->cq[i]);
3127 if (rc) {
3128 dev_err(dev,
3129 "irq init: could not request cq interrupt %d, rc=%d\n",
3130 irq, rc);
3131 return -ENOENT;
3132 }
d177c408 3133 tasklet_init(t, cq_tasklet_v2_hw, (unsigned long)cq);
31a9cfa6
JG
3134 }
3135
7911e66f
JG
3136 return 0;
3137}
3138
94eac9e1
JG
3139static int hisi_sas_v2_init(struct hisi_hba *hisi_hba)
3140{
3141 int rc;
3142
3143 rc = hw_init_v2_hw(hisi_hba);
3144 if (rc)
3145 return rc;
3146
7911e66f
JG
3147 rc = interrupt_init_v2_hw(hisi_hba);
3148 if (rc)
3149 return rc;
3150
94eac9e1
JG
3151 return 0;
3152}
3153
06ec0fb9
XC
3154static void interrupt_disable_v2_hw(struct hisi_hba *hisi_hba)
3155{
3156 struct platform_device *pdev = hisi_hba->pdev;
3157 int i;
3158
3159 for (i = 0; i < hisi_hba->queue_count; i++)
3160 hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK + 0x4 * i, 0x1);
3161
3162 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0xffffffff);
3163 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0xffffffff);
3164 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0xffffffff);
3165 hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xffffffff);
3166
3167 for (i = 0; i < hisi_hba->n_phy; i++) {
3168 hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xffffffff);
3169 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0xffffffff);
3170 }
3171
3172 for (i = 0; i < 128; i++)
3173 synchronize_irq(platform_get_irq(pdev, i));
3174}
3175
3176static int soft_reset_v2_hw(struct hisi_hba *hisi_hba)
3177{
3178 struct device *dev = &hisi_hba->pdev->dev;
3179 u32 old_state, state;
3180 int rc, cnt;
3181 int phy_no;
3182
3183 old_state = hisi_sas_read32(hisi_hba, PHY_STATE);
3184
3185 interrupt_disable_v2_hw(hisi_hba);
3186 hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0x0);
3187
3188 stop_phys_v2_hw(hisi_hba);
3189
3190 mdelay(10);
3191
3192 hisi_sas_write32(hisi_hba, AXI_MASTER_CFG_BASE + AM_CTRL_GLOBAL, 0x1);
3193
3194 /* wait until bus idle */
3195 cnt = 0;
3196 while (1) {
3197 u32 status = hisi_sas_read32_relaxed(hisi_hba,
3198 AXI_MASTER_CFG_BASE + AM_CURR_TRANS_RETURN);
3199
3200 if (status == 0x3)
3201 break;
3202
3203 udelay(10);
3204 if (cnt++ > 10) {
3205 dev_info(dev, "wait axi bus state to idle timeout!\n");
3206 return -1;
3207 }
3208 }
3209
3210 hisi_sas_init_mem(hisi_hba);
3211
3212 rc = hw_init_v2_hw(hisi_hba);
3213 if (rc)
3214 return rc;
3215
3216 /* Re-enable the PHYs */
3217 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
3218 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
3219 struct asd_sas_phy *sas_phy = &phy->sas_phy;
3220
3221 if (sas_phy->enabled)
3222 start_phy_v2_hw(hisi_hba, phy_no);
3223 }
3224
3225 /* Wait for the PHYs to come up and read the PHY state */
3226 msleep(1000);
3227
3228 state = hisi_sas_read32(hisi_hba, PHY_STATE);
3229
3230 hisi_sas_rescan_topology(hisi_hba, old_state, state);
3231
3232 return 0;
3233}
3234
3417ba8a 3235static const struct hisi_sas_hw hisi_sas_v2_hw = {
94eac9e1 3236 .hw_init = hisi_sas_v2_init,
85b2c3c0 3237 .setup_itct = setup_itct_v2_hw,
330fa7f3 3238 .slot_index_alloc = slot_index_alloc_quirk_v2_hw,
b2bdaf2b 3239 .alloc_dev = alloc_dev_quirk_v2_hw,
7911e66f 3240 .sl_notify = sl_notify_v2_hw,
5473c060 3241 .get_wideport_bitmap = get_wideport_bitmap_v2_hw,
85b2c3c0 3242 .free_device = free_device_v2_hw,
c2d89392 3243 .prep_smp = prep_smp_v2_hw,
8c36e31d 3244 .prep_ssp = prep_ssp_v2_hw,
6f2ff1a1 3245 .prep_stp = prep_ata_v2_hw,
a3e665d9 3246 .prep_abort = prep_abort_v2_hw,
8c36e31d
JG
3247 .get_free_slot = get_free_slot_v2_hw,
3248 .start_delivery = start_delivery_v2_hw,
31a9cfa6 3249 .slot_complete = slot_complete_v2_hw,
396b8044 3250 .phys_init = phys_init_v2_hw,
63fb11b8
JG
3251 .phy_enable = enable_phy_v2_hw,
3252 .phy_disable = disable_phy_v2_hw,
3253 .phy_hard_reset = phy_hard_reset_v2_hw,
2ae75787
XC
3254 .phy_set_linkrate = phy_set_linkrate_v2_hw,
3255 .phy_get_max_linkrate = phy_get_max_linkrate_v2_hw,
94eac9e1
JG
3256 .max_command_entries = HISI_SAS_COMMAND_ENTRIES_V2_HW,
3257 .complete_hdr_size = sizeof(struct hisi_sas_complete_v2_hdr),
06ec0fb9 3258 .soft_reset = soft_reset_v2_hw,
3417ba8a
JG
3259};
3260
3261static int hisi_sas_v2_probe(struct platform_device *pdev)
3262{
26f3ba96
JG
3263 /*
3264 * Check if we should defer the probe before we probe the
3265 * upper layer, as it's hard to defer later on.
3266 */
3267 int ret = platform_get_irq(pdev, 0);
3268
3269 if (ret < 0) {
3270 if (ret != -EPROBE_DEFER)
3271 dev_err(&pdev->dev, "cannot obtain irq\n");
3272 return ret;
3273 }
3274
3417ba8a
JG
3275 return hisi_sas_probe(pdev, &hisi_sas_v2_hw);
3276}
3277
3278static int hisi_sas_v2_remove(struct platform_device *pdev)
3279{
f2f89c32
XC
3280 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
3281 struct hisi_hba *hisi_hba = sha->lldd_ha;
3282
3283 if (timer_pending(&hisi_hba->timer))
3284 del_timer(&hisi_hba->timer);
3285
3417ba8a
JG
3286 return hisi_sas_remove(pdev);
3287}
3288
3289static const struct of_device_id sas_v2_of_match[] = {
3290 { .compatible = "hisilicon,hip06-sas-v2",},
039ae102 3291 { .compatible = "hisilicon,hip07-sas-v2",},
3417ba8a
JG
3292 {},
3293};
3294MODULE_DEVICE_TABLE(of, sas_v2_of_match);
3295
50408712
JG
3296static const struct acpi_device_id sas_v2_acpi_match[] = {
3297 { "HISI0162", 0 },
3298 { }
3299};
3300
3301MODULE_DEVICE_TABLE(acpi, sas_v2_acpi_match);
3302
3417ba8a
JG
3303static struct platform_driver hisi_sas_v2_driver = {
3304 .probe = hisi_sas_v2_probe,
3305 .remove = hisi_sas_v2_remove,
3306 .driver = {
3307 .name = DRV_NAME,
3308 .of_match_table = sas_v2_of_match,
50408712 3309 .acpi_match_table = ACPI_PTR(sas_v2_acpi_match),
3417ba8a
JG
3310 },
3311};
3312
3313module_platform_driver(hisi_sas_v2_driver);
3314
3315MODULE_LICENSE("GPL");
3316MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
3317MODULE_DESCRIPTION("HISILICON SAS controller v2 hw driver");
3318MODULE_ALIAS("platform:" DRV_NAME);