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