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