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