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