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