]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/scsi/hisi_sas/hisi_sas_v2_hw.c
hisi_sas: add slot_index_alloc_quirk_v2_hw()
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / hisi_sas / hisi_sas_v2_hw.c
CommitLineData
3417ba8a
JG
1/*
2 * Copyright (c) 2016 Linaro Ltd.
3 * Copyright (c) 2016 Hisilicon Limited.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 */
11
12#include "hisi_sas.h"
13#define DRV_NAME "hisi_sas_v2_hw"
14
45c901b8
JG
15/* global registers need init*/
16#define DLVRY_QUEUE_ENABLE 0x0
17#define IOST_BASE_ADDR_LO 0x8
18#define IOST_BASE_ADDR_HI 0xc
19#define ITCT_BASE_ADDR_LO 0x10
20#define ITCT_BASE_ADDR_HI 0x14
21#define IO_BROKEN_MSG_ADDR_LO 0x18
22#define IO_BROKEN_MSG_ADDR_HI 0x1c
23#define PHY_CONTEXT 0x20
24#define PHY_STATE 0x24
25#define PHY_PORT_NUM_MA 0x28
26#define PORT_STATE 0x2c
27#define PORT_STATE_PHY8_PORT_NUM_OFF 16
28#define PORT_STATE_PHY8_PORT_NUM_MSK (0xf << PORT_STATE_PHY8_PORT_NUM_OFF)
29#define PORT_STATE_PHY8_CONN_RATE_OFF 20
30#define PORT_STATE_PHY8_CONN_RATE_MSK (0xf << PORT_STATE_PHY8_CONN_RATE_OFF)
31#define PHY_CONN_RATE 0x30
32#define HGC_TRANS_TASK_CNT_LIMIT 0x38
33#define AXI_AHB_CLK_CFG 0x3c
34#define ITCT_CLR 0x44
35#define ITCT_CLR_EN_OFF 16
36#define ITCT_CLR_EN_MSK (0x1 << ITCT_CLR_EN_OFF)
37#define ITCT_DEV_OFF 0
38#define ITCT_DEV_MSK (0x7ff << ITCT_DEV_OFF)
39#define AXI_USER1 0x48
40#define AXI_USER2 0x4c
41#define IO_SATA_BROKEN_MSG_ADDR_LO 0x58
42#define IO_SATA_BROKEN_MSG_ADDR_HI 0x5c
43#define SATA_INITI_D2H_STORE_ADDR_LO 0x60
44#define SATA_INITI_D2H_STORE_ADDR_HI 0x64
45#define HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL 0x84
46#define HGC_SAS_TXFAIL_RETRY_CTRL 0x88
47#define HGC_GET_ITV_TIME 0x90
48#define DEVICE_MSG_WORK_MODE 0x94
49#define OPENA_WT_CONTI_TIME 0x9c
50#define I_T_NEXUS_LOSS_TIME 0xa0
51#define MAX_CON_TIME_LIMIT_TIME 0xa4
52#define BUS_INACTIVE_LIMIT_TIME 0xa8
53#define REJECT_TO_OPEN_LIMIT_TIME 0xac
54#define CFG_AGING_TIME 0xbc
55#define HGC_DFX_CFG2 0xc0
56#define HGC_IOMB_PROC1_STATUS 0x104
57#define CFG_1US_TIMER_TRSH 0xcc
58#define HGC_INVLD_DQE_INFO 0x148
59#define HGC_INVLD_DQE_INFO_FB_CH0_OFF 9
60#define HGC_INVLD_DQE_INFO_FB_CH0_MSK (0x1 << HGC_INVLD_DQE_INFO_FB_CH0_OFF)
61#define HGC_INVLD_DQE_INFO_FB_CH3_OFF 18
62#define INT_COAL_EN 0x19c
63#define OQ_INT_COAL_TIME 0x1a0
64#define OQ_INT_COAL_CNT 0x1a4
65#define ENT_INT_COAL_TIME 0x1a8
66#define ENT_INT_COAL_CNT 0x1ac
67#define OQ_INT_SRC 0x1b0
68#define OQ_INT_SRC_MSK 0x1b4
69#define ENT_INT_SRC1 0x1b8
70#define ENT_INT_SRC1_D2H_FIS_CH0_OFF 0
71#define ENT_INT_SRC1_D2H_FIS_CH0_MSK (0x1 << ENT_INT_SRC1_D2H_FIS_CH0_OFF)
72#define ENT_INT_SRC1_D2H_FIS_CH1_OFF 8
73#define ENT_INT_SRC1_D2H_FIS_CH1_MSK (0x1 << ENT_INT_SRC1_D2H_FIS_CH1_OFF)
74#define ENT_INT_SRC2 0x1bc
75#define ENT_INT_SRC3 0x1c0
76#define ENT_INT_SRC3_ITC_INT_OFF 15
77#define ENT_INT_SRC3_ITC_INT_MSK (0x1 << ENT_INT_SRC3_ITC_INT_OFF)
78#define ENT_INT_SRC_MSK1 0x1c4
79#define ENT_INT_SRC_MSK2 0x1c8
80#define ENT_INT_SRC_MSK3 0x1cc
81#define ENT_INT_SRC_MSK3_ENT95_MSK_OFF 31
82#define ENT_INT_SRC_MSK3_ENT95_MSK_MSK (0x1 << ENT_INT_SRC_MSK3_ENT95_MSK_OFF)
83#define SAS_ECC_INTR_MSK 0x1ec
84#define HGC_ERR_STAT_EN 0x238
85#define DLVRY_Q_0_BASE_ADDR_LO 0x260
86#define DLVRY_Q_0_BASE_ADDR_HI 0x264
87#define DLVRY_Q_0_DEPTH 0x268
88#define DLVRY_Q_0_WR_PTR 0x26c
89#define DLVRY_Q_0_RD_PTR 0x270
90#define HYPER_STREAM_ID_EN_CFG 0xc80
91#define OQ0_INT_SRC_MSK 0xc90
92#define COMPL_Q_0_BASE_ADDR_LO 0x4e0
93#define COMPL_Q_0_BASE_ADDR_HI 0x4e4
94#define COMPL_Q_0_DEPTH 0x4e8
95#define COMPL_Q_0_WR_PTR 0x4ec
96#define COMPL_Q_0_RD_PTR 0x4f0
97
98/* phy registers need init */
99#define PORT_BASE (0x2000)
100
101#define PHY_CFG (PORT_BASE + 0x0)
102#define HARD_PHY_LINKRATE (PORT_BASE + 0x4)
103#define PHY_CFG_ENA_OFF 0
104#define PHY_CFG_ENA_MSK (0x1 << PHY_CFG_ENA_OFF)
105#define PHY_CFG_DC_OPT_OFF 2
106#define PHY_CFG_DC_OPT_MSK (0x1 << PHY_CFG_DC_OPT_OFF)
107#define PROG_PHY_LINK_RATE (PORT_BASE + 0x8)
108#define PROG_PHY_LINK_RATE_MAX_OFF 0
109#define PROG_PHY_LINK_RATE_MAX_MSK (0xff << PROG_PHY_LINK_RATE_MAX_OFF)
110#define PHY_CTRL (PORT_BASE + 0x14)
111#define PHY_CTRL_RESET_OFF 0
112#define PHY_CTRL_RESET_MSK (0x1 << PHY_CTRL_RESET_OFF)
113#define SAS_PHY_CTRL (PORT_BASE + 0x20)
114#define SL_CFG (PORT_BASE + 0x84)
115#define PHY_PCN (PORT_BASE + 0x44)
116#define SL_TOUT_CFG (PORT_BASE + 0x8c)
117#define SL_CONTROL (PORT_BASE + 0x94)
118#define SL_CONTROL_NOTIFY_EN_OFF 0
119#define SL_CONTROL_NOTIFY_EN_MSK (0x1 << SL_CONTROL_NOTIFY_EN_OFF)
120#define TX_ID_DWORD0 (PORT_BASE + 0x9c)
121#define TX_ID_DWORD1 (PORT_BASE + 0xa0)
122#define TX_ID_DWORD2 (PORT_BASE + 0xa4)
123#define TX_ID_DWORD3 (PORT_BASE + 0xa8)
124#define TX_ID_DWORD4 (PORT_BASE + 0xaC)
125#define TX_ID_DWORD5 (PORT_BASE + 0xb0)
126#define TX_ID_DWORD6 (PORT_BASE + 0xb4)
127#define RX_IDAF_DWORD0 (PORT_BASE + 0xc4)
128#define RX_IDAF_DWORD1 (PORT_BASE + 0xc8)
129#define RX_IDAF_DWORD2 (PORT_BASE + 0xcc)
130#define RX_IDAF_DWORD3 (PORT_BASE + 0xd0)
131#define RX_IDAF_DWORD4 (PORT_BASE + 0xd4)
132#define RX_IDAF_DWORD5 (PORT_BASE + 0xd8)
133#define RX_IDAF_DWORD6 (PORT_BASE + 0xdc)
134#define RXOP_CHECK_CFG_H (PORT_BASE + 0xfc)
135#define DONE_RECEIVED_TIME (PORT_BASE + 0x11c)
136#define CHL_INT0 (PORT_BASE + 0x1b4)
137#define CHL_INT0_HOTPLUG_TOUT_OFF 0
138#define CHL_INT0_HOTPLUG_TOUT_MSK (0x1 << CHL_INT0_HOTPLUG_TOUT_OFF)
139#define CHL_INT0_SL_RX_BCST_ACK_OFF 1
140#define CHL_INT0_SL_RX_BCST_ACK_MSK (0x1 << CHL_INT0_SL_RX_BCST_ACK_OFF)
141#define CHL_INT0_SL_PHY_ENABLE_OFF 2
142#define CHL_INT0_SL_PHY_ENABLE_MSK (0x1 << CHL_INT0_SL_PHY_ENABLE_OFF)
143#define CHL_INT0_NOT_RDY_OFF 4
144#define CHL_INT0_NOT_RDY_MSK (0x1 << CHL_INT0_NOT_RDY_OFF)
145#define CHL_INT0_PHY_RDY_OFF 5
146#define CHL_INT0_PHY_RDY_MSK (0x1 << CHL_INT0_PHY_RDY_OFF)
147#define CHL_INT1 (PORT_BASE + 0x1b8)
148#define CHL_INT1_DMAC_TX_ECC_ERR_OFF 15
149#define CHL_INT1_DMAC_TX_ECC_ERR_MSK (0x1 << CHL_INT1_DMAC_TX_ECC_ERR_OFF)
150#define CHL_INT1_DMAC_RX_ECC_ERR_OFF 17
151#define CHL_INT1_DMAC_RX_ECC_ERR_MSK (0x1 << CHL_INT1_DMAC_RX_ECC_ERR_OFF)
152#define CHL_INT2 (PORT_BASE + 0x1bc)
153#define CHL_INT0_MSK (PORT_BASE + 0x1c0)
154#define CHL_INT1_MSK (PORT_BASE + 0x1c4)
155#define CHL_INT2_MSK (PORT_BASE + 0x1c8)
156#define CHL_INT_COAL_EN (PORT_BASE + 0x1d0)
157#define PHY_CTRL_RDY_MSK (PORT_BASE + 0x2b0)
158#define PHYCTRL_NOT_RDY_MSK (PORT_BASE + 0x2b4)
159#define PHYCTRL_DWS_RESET_MSK (PORT_BASE + 0x2b8)
160#define PHYCTRL_PHY_ENA_MSK (PORT_BASE + 0x2bc)
161#define SL_RX_BCAST_CHK_MSK (PORT_BASE + 0x2c0)
162#define PHYCTRL_OOB_RESTART_MSK (PORT_BASE + 0x2c4)
163#define DMA_TX_STATUS (PORT_BASE + 0x2d0)
164#define DMA_TX_STATUS_BUSY_OFF 0
165#define DMA_TX_STATUS_BUSY_MSK (0x1 << DMA_TX_STATUS_BUSY_OFF)
166#define DMA_RX_STATUS (PORT_BASE + 0x2e8)
167#define DMA_RX_STATUS_BUSY_OFF 0
168#define DMA_RX_STATUS_BUSY_MSK (0x1 << DMA_RX_STATUS_BUSY_OFF)
169
170#define AXI_CFG (0x5100)
171#define AM_CFG_MAX_TRANS (0x5010)
172#define AM_CFG_SINGLE_PORT_MAX_TRANS (0x5014)
173
174/* HW dma structures */
175/* Delivery queue header */
176/* dw0 */
177#define CMD_HDR_RESP_REPORT_OFF 5
178#define CMD_HDR_RESP_REPORT_MSK (0x1 << CMD_HDR_RESP_REPORT_OFF)
179#define CMD_HDR_TLR_CTRL_OFF 6
180#define CMD_HDR_TLR_CTRL_MSK (0x3 << CMD_HDR_TLR_CTRL_OFF)
181#define CMD_HDR_PORT_OFF 18
182#define CMD_HDR_PORT_MSK (0xf << CMD_HDR_PORT_OFF)
183#define CMD_HDR_PRIORITY_OFF 27
184#define CMD_HDR_PRIORITY_MSK (0x1 << CMD_HDR_PRIORITY_OFF)
185#define CMD_HDR_CMD_OFF 29
186#define CMD_HDR_CMD_MSK (0x7 << CMD_HDR_CMD_OFF)
187/* dw1 */
188#define CMD_HDR_DIR_OFF 5
189#define CMD_HDR_DIR_MSK (0x3 << CMD_HDR_DIR_OFF)
190#define CMD_HDR_RESET_OFF 7
191#define CMD_HDR_RESET_MSK (0x1 << CMD_HDR_RESET_OFF)
192#define CMD_HDR_VDTL_OFF 10
193#define CMD_HDR_VDTL_MSK (0x1 << CMD_HDR_VDTL_OFF)
194#define CMD_HDR_FRAME_TYPE_OFF 11
195#define CMD_HDR_FRAME_TYPE_MSK (0x1f << CMD_HDR_FRAME_TYPE_OFF)
196#define CMD_HDR_DEV_ID_OFF 16
197#define CMD_HDR_DEV_ID_MSK (0xffff << CMD_HDR_DEV_ID_OFF)
198/* dw2 */
199#define CMD_HDR_CFL_OFF 0
200#define CMD_HDR_CFL_MSK (0x1ff << CMD_HDR_CFL_OFF)
201#define CMD_HDR_NCQ_TAG_OFF 10
202#define CMD_HDR_NCQ_TAG_MSK (0x1f << CMD_HDR_NCQ_TAG_OFF)
203#define CMD_HDR_MRFL_OFF 15
204#define CMD_HDR_MRFL_MSK (0x1ff << CMD_HDR_MRFL_OFF)
205#define CMD_HDR_SG_MOD_OFF 24
206#define CMD_HDR_SG_MOD_MSK (0x3 << CMD_HDR_SG_MOD_OFF)
207#define CMD_HDR_FIRST_BURST_OFF 26
208#define CMD_HDR_FIRST_BURST_MSK (0x1 << CMD_HDR_SG_MOD_OFF)
209/* dw3 */
210#define CMD_HDR_IPTT_OFF 0
211#define CMD_HDR_IPTT_MSK (0xffff << CMD_HDR_IPTT_OFF)
212/* dw6 */
213#define CMD_HDR_DIF_SGL_LEN_OFF 0
214#define CMD_HDR_DIF_SGL_LEN_MSK (0xffff << CMD_HDR_DIF_SGL_LEN_OFF)
215#define CMD_HDR_DATA_SGL_LEN_OFF 16
216#define CMD_HDR_DATA_SGL_LEN_MSK (0xffff << CMD_HDR_DATA_SGL_LEN_OFF)
217
218/* Completion header */
219/* dw0 */
220#define CMPLT_HDR_RSPNS_XFRD_OFF 10
221#define CMPLT_HDR_RSPNS_XFRD_MSK (0x1 << CMPLT_HDR_RSPNS_XFRD_OFF)
222#define CMPLT_HDR_ERX_OFF 12
223#define CMPLT_HDR_ERX_MSK (0x1 << CMPLT_HDR_ERX_OFF)
224/* dw1 */
225#define CMPLT_HDR_IPTT_OFF 0
226#define CMPLT_HDR_IPTT_MSK (0xffff << CMPLT_HDR_IPTT_OFF)
227#define CMPLT_HDR_DEV_ID_OFF 16
228#define CMPLT_HDR_DEV_ID_MSK (0xffff << CMPLT_HDR_DEV_ID_OFF)
229
230/* ITCT header */
231/* qw0 */
232#define ITCT_HDR_DEV_TYPE_OFF 0
233#define ITCT_HDR_DEV_TYPE_MSK (0x3 << ITCT_HDR_DEV_TYPE_OFF)
234#define ITCT_HDR_VALID_OFF 2
235#define ITCT_HDR_VALID_MSK (0x1 << ITCT_HDR_VALID_OFF)
236#define ITCT_HDR_MCR_OFF 5
237#define ITCT_HDR_MCR_MSK (0xf << ITCT_HDR_MCR_OFF)
238#define ITCT_HDR_VLN_OFF 9
239#define ITCT_HDR_VLN_MSK (0xf << ITCT_HDR_VLN_OFF)
240#define ITCT_HDR_PORT_ID_OFF 28
241#define ITCT_HDR_PORT_ID_MSK (0xf << ITCT_HDR_PORT_ID_OFF)
242/* qw2 */
243#define ITCT_HDR_INLT_OFF 0
244#define ITCT_HDR_INLT_MSK (0xffffULL << ITCT_HDR_INLT_OFF)
245#define ITCT_HDR_BITLT_OFF 16
246#define ITCT_HDR_BITLT_MSK (0xffffULL << ITCT_HDR_BITLT_OFF)
247#define ITCT_HDR_MCTLT_OFF 32
248#define ITCT_HDR_MCTLT_MSK (0xffffULL << ITCT_HDR_MCTLT_OFF)
249#define ITCT_HDR_RTOLT_OFF 48
250#define ITCT_HDR_RTOLT_MSK (0xffffULL << ITCT_HDR_RTOLT_OFF)
251
94eac9e1
JG
252struct hisi_sas_complete_v2_hdr {
253 __le32 dw0;
254 __le32 dw1;
255 __le32 act;
256 __le32 dw3;
257};
258
e8fed0e9
JG
259struct hisi_sas_err_record_v2 {
260 /* dw0 */
261 __le32 trans_tx_fail_type;
262
263 /* dw1 */
264 __le32 trans_rx_fail_type;
265
266 /* dw2 */
267 __le16 dma_tx_err_type;
268 __le16 sipc_rx_err_type;
269
270 /* dw3 */
271 __le32 dma_rx_err_type;
272};
273
7911e66f
JG
274enum {
275 HISI_SAS_PHY_PHY_UPDOWN,
d3bf3d84 276 HISI_SAS_PHY_CHNL_INT,
7911e66f
JG
277 HISI_SAS_PHY_INT_NR
278};
279
e8fed0e9
JG
280enum {
281 TRANS_TX_FAIL_BASE = 0x0, /* dw0 */
282 TRANS_RX_FAIL_BASE = 0x100, /* dw1 */
283 DMA_TX_ERR_BASE = 0x200, /* dw2 bit 15-0 */
284 SIPC_RX_ERR_BASE = 0x300, /* dw2 bit 31-16*/
285 DMA_RX_ERR_BASE = 0x400, /* dw3 */
286
287 /* trans tx*/
288 TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS = TRANS_TX_FAIL_BASE, /* 0x0 */
289 TRANS_TX_ERR_PHY_NOT_ENABLE, /* 0x1 */
290 TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION, /* 0x2 */
291 TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION, /* 0x3 */
292 TRANS_TX_OPEN_CNX_ERR_BY_OTHER, /* 0x4 */
293 RESERVED0, /* 0x5 */
294 TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT, /* 0x6 */
295 TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY, /* 0x7 */
296 TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED, /* 0x8 */
297 TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED, /* 0x9 */
298 TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION, /* 0xa */
299 TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD, /* 0xb */
300 TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER, /* 0xc */
301 TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED, /* 0xd */
302 TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT, /* 0xe */
303 TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION, /* 0xf */
304 TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED, /* 0x10 */
305 TRANS_TX_ERR_FRAME_TXED, /* 0x11 */
306 TRANS_TX_ERR_WITH_BREAK_TIMEOUT, /* 0x12 */
307 TRANS_TX_ERR_WITH_BREAK_REQUEST, /* 0x13 */
308 TRANS_TX_ERR_WITH_BREAK_RECEVIED, /* 0x14 */
309 TRANS_TX_ERR_WITH_CLOSE_TIMEOUT, /* 0x15 */
310 TRANS_TX_ERR_WITH_CLOSE_NORMAL, /* 0x16 for ssp*/
311 TRANS_TX_ERR_WITH_CLOSE_PHYDISALE, /* 0x17 */
312 TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT, /* 0x18 */
313 TRANS_TX_ERR_WITH_CLOSE_COMINIT, /* 0x19 */
314 TRANS_TX_ERR_WITH_NAK_RECEVIED, /* 0x1a for ssp*/
315 TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT, /* 0x1b for ssp*/
316 /*IO_TX_ERR_WITH_R_ERR_RECEVIED, [> 0x1b for sata/stp<] */
317 TRANS_TX_ERR_WITH_CREDIT_TIMEOUT, /* 0x1c for ssp */
318 /*IO_RX_ERR_WITH_SATA_DEVICE_LOST 0x1c for sata/stp */
319 TRANS_TX_ERR_WITH_IPTT_CONFLICT, /* 0x1d for ssp/smp */
320 TRANS_TX_ERR_WITH_OPEN_BY_DES_OR_OTHERS, /* 0x1e */
321 /*IO_TX_ERR_WITH_SYNC_RXD, [> 0x1e <] for sata/stp */
322 TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT, /* 0x1f for sata/stp */
323
324 /* trans rx */
325 TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR = TRANS_RX_FAIL_BASE, /* 0x100 */
326 TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR, /* 0x101 for sata/stp */
327 TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM, /* 0x102 for ssp/smp */
328 /*IO_ERR_WITH_RXFIS_8B10B_CODE_ERR, [> 0x102 <] for sata/stp */
329 TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR, /* 0x103 for sata/stp */
330 TRANS_RX_ERR_WITH_RXFIS_CRC_ERR, /* 0x104 for sata/stp */
331 TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN, /* 0x105 for smp */
332 /*IO_ERR_WITH_RXFIS_TX SYNCP, [> 0x105 <] for sata/stp */
333 TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP, /* 0x106 for sata/stp*/
334 TRANS_RX_ERR_WITH_LINK_BUF_OVERRUN, /* 0x107 */
335 TRANS_RX_ERR_WITH_BREAK_TIMEOUT, /* 0x108 */
336 TRANS_RX_ERR_WITH_BREAK_REQUEST, /* 0x109 */
337 TRANS_RX_ERR_WITH_BREAK_RECEVIED, /* 0x10a */
338 RESERVED1, /* 0x10b */
339 TRANS_RX_ERR_WITH_CLOSE_NORMAL, /* 0x10c */
340 TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE, /* 0x10d */
341 TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT, /* 0x10e */
342 TRANS_RX_ERR_WITH_CLOSE_COMINIT, /* 0x10f */
343 TRANS_RX_ERR_WITH_DATA_LEN0, /* 0x110 for ssp/smp */
344 TRANS_RX_ERR_WITH_BAD_HASH, /* 0x111 for ssp */
345 /*IO_RX_ERR_WITH_FIS_TOO_SHORT, [> 0x111 <] for sata/stp */
346 TRANS_RX_XRDY_WLEN_ZERO_ERR, /* 0x112 for ssp*/
347 /*IO_RX_ERR_WITH_FIS_TOO_LONG, [> 0x112 <] for sata/stp */
348 TRANS_RX_SSP_FRM_LEN_ERR, /* 0x113 for ssp */
349 /*IO_RX_ERR_WITH_SATA_DEVICE_LOST, [> 0x113 <] for sata */
350 RESERVED2, /* 0x114 */
351 RESERVED3, /* 0x115 */
352 RESERVED4, /* 0x116 */
353 RESERVED5, /* 0x117 */
354 TRANS_RX_ERR_WITH_BAD_FRM_TYPE, /* 0x118 */
355 TRANS_RX_SMP_FRM_LEN_ERR, /* 0x119 */
356 TRANS_RX_SMP_RESP_TIMEOUT_ERR, /* 0x11a */
357 RESERVED6, /* 0x11b */
358 RESERVED7, /* 0x11c */
359 RESERVED8, /* 0x11d */
360 RESERVED9, /* 0x11e */
361 TRANS_RX_R_ERR, /* 0x11f */
362
363 /* dma tx */
364 DMA_TX_DIF_CRC_ERR = DMA_TX_ERR_BASE, /* 0x200 */
365 DMA_TX_DIF_APP_ERR, /* 0x201 */
366 DMA_TX_DIF_RPP_ERR, /* 0x202 */
367 DMA_TX_DATA_SGL_OVERFLOW, /* 0x203 */
368 DMA_TX_DIF_SGL_OVERFLOW, /* 0x204 */
369 DMA_TX_UNEXP_XFER_ERR, /* 0x205 */
370 DMA_TX_UNEXP_RETRANS_ERR, /* 0x206 */
371 DMA_TX_XFER_LEN_OVERFLOW, /* 0x207 */
372 DMA_TX_XFER_OFFSET_ERR, /* 0x208 */
373 DMA_TX_RAM_ECC_ERR, /* 0x209 */
374 DMA_TX_DIF_LEN_ALIGN_ERR, /* 0x20a */
375
376 /* sipc rx */
377 SIPC_RX_FIS_STATUS_ERR_BIT_VLD = SIPC_RX_ERR_BASE, /* 0x300 */
378 SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR, /* 0x301 */
379 SIPC_RX_FIS_STATUS_BSY_BIT_ERR, /* 0x302 */
380 SIPC_RX_WRSETUP_LEN_ODD_ERR, /* 0x303 */
381 SIPC_RX_WRSETUP_LEN_ZERO_ERR, /* 0x304 */
382 SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR, /* 0x305 */
383 SIPC_RX_NCQ_WRSETUP_OFFSET_ERR, /* 0x306 */
384 SIPC_RX_NCQ_WRSETUP_AUTO_ACTIVE_ERR, /* 0x307 */
385 SIPC_RX_SATA_UNEXP_FIS_ERR, /* 0x308 */
386 SIPC_RX_WRSETUP_ESTATUS_ERR, /* 0x309 */
387 SIPC_RX_DATA_UNDERFLOW_ERR, /* 0x30a */
388
389 /* dma rx */
390 DMA_RX_DIF_CRC_ERR = DMA_RX_ERR_BASE, /* 0x400 */
391 DMA_RX_DIF_APP_ERR, /* 0x401 */
392 DMA_RX_DIF_RPP_ERR, /* 0x402 */
393 DMA_RX_DATA_SGL_OVERFLOW, /* 0x403 */
394 DMA_RX_DIF_SGL_OVERFLOW, /* 0x404 */
395 DMA_RX_DATA_LEN_OVERFLOW, /* 0x405 */
396 DMA_RX_DATA_LEN_UNDERFLOW, /* 0x406 */
397 DMA_RX_DATA_OFFSET_ERR, /* 0x407 */
398 RESERVED10, /* 0x408 */
399 DMA_RX_SATA_FRAME_TYPE_ERR, /* 0x409 */
400 DMA_RX_RESP_BUF_OVERFLOW, /* 0x40a */
401 DMA_RX_UNEXP_RETRANS_RESP_ERR, /* 0x40b */
402 DMA_RX_UNEXP_NORM_RESP_ERR, /* 0x40c */
403 DMA_RX_UNEXP_RDFRAME_ERR, /* 0x40d */
404 DMA_RX_PIO_DATA_LEN_ERR, /* 0x40e */
405 DMA_RX_RDSETUP_STATUS_ERR, /* 0x40f */
406 DMA_RX_RDSETUP_STATUS_DRQ_ERR, /* 0x410 */
407 DMA_RX_RDSETUP_STATUS_BSY_ERR, /* 0x411 */
408 DMA_RX_RDSETUP_LEN_ODD_ERR, /* 0x412 */
409 DMA_RX_RDSETUP_LEN_ZERO_ERR, /* 0x413 */
410 DMA_RX_RDSETUP_LEN_OVER_ERR, /* 0x414 */
411 DMA_RX_RDSETUP_OFFSET_ERR, /* 0x415 */
412 DMA_RX_RDSETUP_ACTIVE_ERR, /* 0x416 */
413 DMA_RX_RDSETUP_ESTATUS_ERR, /* 0x417 */
414 DMA_RX_RAM_ECC_ERR, /* 0x418 */
415 DMA_RX_UNKNOWN_FRM_ERR, /* 0x419 */
416};
417
94eac9e1
JG
418#define HISI_SAS_COMMAND_ENTRIES_V2_HW 4096
419
8c36e31d
JG
420#define DIR_NO_DATA 0
421#define DIR_TO_INI 1
422#define DIR_TO_DEVICE 2
423#define DIR_RESERVED 3
424
6f2ff1a1
JG
425#define SATA_PROTOCOL_NONDATA 0x1
426#define SATA_PROTOCOL_PIO 0x2
427#define SATA_PROTOCOL_DMA 0x4
428#define SATA_PROTOCOL_FPDMA 0x8
429#define SATA_PROTOCOL_ATAPI 0x10
430
94eac9e1
JG
431static u32 hisi_sas_read32(struct hisi_hba *hisi_hba, u32 off)
432{
433 void __iomem *regs = hisi_hba->regs + off;
434
435 return readl(regs);
436}
437
8c36e31d
JG
438static u32 hisi_sas_read32_relaxed(struct hisi_hba *hisi_hba, u32 off)
439{
440 void __iomem *regs = hisi_hba->regs + off;
441
442 return readl_relaxed(regs);
443}
444
94eac9e1
JG
445static void hisi_sas_write32(struct hisi_hba *hisi_hba, u32 off, u32 val)
446{
447 void __iomem *regs = hisi_hba->regs + off;
448
449 writel(val, regs);
450}
451
452static void hisi_sas_phy_write32(struct hisi_hba *hisi_hba, int phy_no,
453 u32 off, u32 val)
454{
455 void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
456
457 writel(val, regs);
458}
459
460static u32 hisi_sas_phy_read32(struct hisi_hba *hisi_hba,
461 int phy_no, u32 off)
462{
463 void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
464
465 return readl(regs);
466}
467
330fa7f3
JG
468/* This function needs to be protected from pre-emption. */
469static int
470slot_index_alloc_quirk_v2_hw(struct hisi_hba *hisi_hba, int *slot_idx,
471 struct domain_device *device)
472{
473 unsigned int index = 0;
474 void *bitmap = hisi_hba->slot_index_tags;
475 int sata_dev = dev_is_sata(device);
476
477 while (1) {
478 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
479 index);
480 if (index >= hisi_hba->slot_index_count)
481 return -SAS_QUEUE_FULL;
482 /*
483 * SAS IPTT bit0 should be 1
484 */
485 if (sata_dev || (index & 1))
486 break;
487 index++;
488 }
489
490 set_bit(index, bitmap);
491 *slot_idx = index;
492 return 0;
493}
494
29a20428
JG
495static void config_phy_opt_mode_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
496{
497 u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
498
499 cfg &= ~PHY_CFG_DC_OPT_MSK;
500 cfg |= 1 << PHY_CFG_DC_OPT_OFF;
501 hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
502}
503
806bb768
JG
504static void config_id_frame_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
505{
506 struct sas_identify_frame identify_frame;
507 u32 *identify_buffer;
508
509 memset(&identify_frame, 0, sizeof(identify_frame));
510 identify_frame.dev_type = SAS_END_DEVICE;
511 identify_frame.frame_type = 0;
512 identify_frame._un1 = 1;
513 identify_frame.initiator_bits = SAS_PROTOCOL_ALL;
514 identify_frame.target_bits = SAS_PROTOCOL_NONE;
515 memcpy(&identify_frame._un4_11[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
516 memcpy(&identify_frame.sas_addr[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
517 identify_frame.phy_id = phy_no;
518 identify_buffer = (u32 *)(&identify_frame);
519
520 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD0,
521 __swab32(identify_buffer[0]));
522 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD1,
523 identify_buffer[2]);
524 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD2,
525 identify_buffer[1]);
526 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD3,
527 identify_buffer[4]);
528 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD4,
529 identify_buffer[3]);
530 hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD5,
531 __swab32(identify_buffer[5]));
532}
533
534static void init_id_frame_v2_hw(struct hisi_hba *hisi_hba)
535{
536 int i;
537
538 for (i = 0; i < hisi_hba->n_phy; i++)
539 config_id_frame_v2_hw(hisi_hba, i);
540}
541
85b2c3c0
JG
542static void setup_itct_v2_hw(struct hisi_hba *hisi_hba,
543 struct hisi_sas_device *sas_dev)
544{
545 struct domain_device *device = sas_dev->sas_device;
546 struct device *dev = &hisi_hba->pdev->dev;
547 u64 qw0, device_id = sas_dev->device_id;
548 struct hisi_sas_itct *itct = &hisi_hba->itct[device_id];
549 struct domain_device *parent_dev = device->parent;
550 struct hisi_sas_port *port = device->port->lldd_port;
551
552 memset(itct, 0, sizeof(*itct));
553
554 /* qw0 */
555 qw0 = 0;
556 switch (sas_dev->dev_type) {
557 case SAS_END_DEVICE:
558 case SAS_EDGE_EXPANDER_DEVICE:
559 case SAS_FANOUT_EXPANDER_DEVICE:
560 qw0 = HISI_SAS_DEV_TYPE_SSP << ITCT_HDR_DEV_TYPE_OFF;
561 break;
562 case SAS_SATA_DEV:
563 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type))
564 qw0 = HISI_SAS_DEV_TYPE_STP << ITCT_HDR_DEV_TYPE_OFF;
565 else
566 qw0 = HISI_SAS_DEV_TYPE_SATA << ITCT_HDR_DEV_TYPE_OFF;
567 break;
568 default:
569 dev_warn(dev, "setup itct: unsupported dev type (%d)\n",
570 sas_dev->dev_type);
571 }
572
573 qw0 |= ((1 << ITCT_HDR_VALID_OFF) |
75249268 574 (device->linkrate << ITCT_HDR_MCR_OFF) |
85b2c3c0
JG
575 (1 << ITCT_HDR_VLN_OFF) |
576 (port->id << ITCT_HDR_PORT_ID_OFF));
577 itct->qw0 = cpu_to_le64(qw0);
578
579 /* qw1 */
580 memcpy(&itct->sas_addr, device->sas_addr, SAS_ADDR_SIZE);
581 itct->sas_addr = __swab64(itct->sas_addr);
582
583 /* qw2 */
f76a0b49
JG
584 if (!dev_is_sata(device))
585 itct->qw2 = cpu_to_le64((500ULL << ITCT_HDR_INLT_OFF) |
586 (0x1ULL << ITCT_HDR_BITLT_OFF) |
587 (0x32ULL << ITCT_HDR_MCTLT_OFF) |
588 (0x1ULL << ITCT_HDR_RTOLT_OFF));
85b2c3c0
JG
589}
590
591static void free_device_v2_hw(struct hisi_hba *hisi_hba,
592 struct hisi_sas_device *sas_dev)
593{
594 u64 qw0, dev_id = sas_dev->device_id;
595 struct device *dev = &hisi_hba->pdev->dev;
596 struct hisi_sas_itct *itct = &hisi_hba->itct[dev_id];
597 u32 reg_val = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
598 int i;
599
600 /* clear the itct interrupt state */
601 if (ENT_INT_SRC3_ITC_INT_MSK & reg_val)
602 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
603 ENT_INT_SRC3_ITC_INT_MSK);
604
605 /* clear the itct int*/
606 for (i = 0; i < 2; i++) {
607 /* clear the itct table*/
608 reg_val = hisi_sas_read32(hisi_hba, ITCT_CLR);
609 reg_val |= ITCT_CLR_EN_MSK | (dev_id & ITCT_DEV_MSK);
610 hisi_sas_write32(hisi_hba, ITCT_CLR, reg_val);
611
612 udelay(10);
613 reg_val = hisi_sas_read32(hisi_hba, ENT_INT_SRC3);
614 if (ENT_INT_SRC3_ITC_INT_MSK & reg_val) {
615 dev_dbg(dev, "got clear ITCT done interrupt\n");
616
617 /* invalid the itct state*/
618 qw0 = cpu_to_le64(itct->qw0);
619 qw0 &= ~(1 << ITCT_HDR_VALID_OFF);
620 hisi_sas_write32(hisi_hba, ENT_INT_SRC3,
621 ENT_INT_SRC3_ITC_INT_MSK);
622 hisi_hba->devices[dev_id].dev_type = SAS_PHY_UNUSED;
623 hisi_hba->devices[dev_id].dev_status = HISI_SAS_DEV_NORMAL;
624
625 /* clear the itct */
626 hisi_sas_write32(hisi_hba, ITCT_CLR, 0);
627 dev_dbg(dev, "clear ITCT ok\n");
628 break;
629 }
630 }
631}
632
94eac9e1
JG
633static int reset_hw_v2_hw(struct hisi_hba *hisi_hba)
634{
635 int i, reset_val;
636 u32 val;
637 unsigned long end_time;
638 struct device *dev = &hisi_hba->pdev->dev;
639
640 /* The mask needs to be set depending on the number of phys */
641 if (hisi_hba->n_phy == 9)
642 reset_val = 0x1fffff;
643 else
644 reset_val = 0x7ffff;
645
646 /* Disable all of the DQ */
647 for (i = 0; i < HISI_SAS_MAX_QUEUES; i++)
648 hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE, 0);
649
650 /* Disable all of the PHYs */
651 for (i = 0; i < hisi_hba->n_phy; i++) {
652 u32 phy_cfg = hisi_sas_phy_read32(hisi_hba, i, PHY_CFG);
653
654 phy_cfg &= ~PHY_CTRL_RESET_MSK;
655 hisi_sas_phy_write32(hisi_hba, i, PHY_CFG, phy_cfg);
656 }
657 udelay(50);
658
659 /* Ensure DMA tx & rx idle */
660 for (i = 0; i < hisi_hba->n_phy; i++) {
661 u32 dma_tx_status, dma_rx_status;
662
663 end_time = jiffies + msecs_to_jiffies(1000);
664
665 while (1) {
666 dma_tx_status = hisi_sas_phy_read32(hisi_hba, i,
667 DMA_TX_STATUS);
668 dma_rx_status = hisi_sas_phy_read32(hisi_hba, i,
669 DMA_RX_STATUS);
670
671 if (!(dma_tx_status & DMA_TX_STATUS_BUSY_MSK) &&
672 !(dma_rx_status & DMA_RX_STATUS_BUSY_MSK))
673 break;
674
675 msleep(20);
676 if (time_after(jiffies, end_time))
677 return -EIO;
678 }
679 }
680
681 /* Ensure axi bus idle */
682 end_time = jiffies + msecs_to_jiffies(1000);
683 while (1) {
684 u32 axi_status =
685 hisi_sas_read32(hisi_hba, AXI_CFG);
686
687 if (axi_status == 0)
688 break;
689
690 msleep(20);
691 if (time_after(jiffies, end_time))
692 return -EIO;
693 }
694
695 /* reset and disable clock*/
696 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg,
697 reset_val);
698 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg + 4,
699 reset_val);
700 msleep(1);
701 regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg, &val);
702 if (reset_val != (val & reset_val)) {
703 dev_err(dev, "SAS reset fail.\n");
704 return -EIO;
705 }
706
707 /* De-reset and enable clock*/
708 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg + 4,
709 reset_val);
710 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg,
711 reset_val);
712 msleep(1);
713 regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg,
714 &val);
715 if (val & reset_val) {
716 dev_err(dev, "SAS de-reset fail.\n");
717 return -EIO;
718 }
719
720 return 0;
721}
722
723static void init_reg_v2_hw(struct hisi_hba *hisi_hba)
724{
725 struct device *dev = &hisi_hba->pdev->dev;
726 struct device_node *np = dev->of_node;
727 int i;
728
729 /* Global registers init */
730
731 /* Deal with am-max-transmissions quirk */
732 if (of_get_property(np, "hip06-sas-v2-quirk-amt", NULL)) {
733 hisi_sas_write32(hisi_hba, AM_CFG_MAX_TRANS, 0x2020);
734 hisi_sas_write32(hisi_hba, AM_CFG_SINGLE_PORT_MAX_TRANS,
735 0x2020);
736 } /* Else, use defaults -> do nothing */
737
738 hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE,
739 (u32)((1ULL << hisi_hba->queue_count) - 1));
740 hisi_sas_write32(hisi_hba, AXI_USER1, 0xc0000000);
741 hisi_sas_write32(hisi_hba, AXI_USER2, 0x10000);
742 hisi_sas_write32(hisi_hba, HGC_SAS_TXFAIL_RETRY_CTRL, 0x108);
743 hisi_sas_write32(hisi_hba, HGC_SAS_TX_OPEN_FAIL_RETRY_CTRL, 0x7FF);
744 hisi_sas_write32(hisi_hba, OPENA_WT_CONTI_TIME, 0x1);
745 hisi_sas_write32(hisi_hba, I_T_NEXUS_LOSS_TIME, 0x1F4);
f76a0b49 746 hisi_sas_write32(hisi_hba, MAX_CON_TIME_LIMIT_TIME, 0x32);
94eac9e1
JG
747 hisi_sas_write32(hisi_hba, BUS_INACTIVE_LIMIT_TIME, 0x1);
748 hisi_sas_write32(hisi_hba, CFG_AGING_TIME, 0x1);
749 hisi_sas_write32(hisi_hba, HGC_ERR_STAT_EN, 0x1);
750 hisi_sas_write32(hisi_hba, HGC_GET_ITV_TIME, 0x1);
751 hisi_sas_write32(hisi_hba, INT_COAL_EN, 0x1);
752 hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x1);
753 hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 0x1);
754 hisi_sas_write32(hisi_hba, ENT_INT_COAL_TIME, 0x1);
755 hisi_sas_write32(hisi_hba, ENT_INT_COAL_CNT, 0x1);
756 hisi_sas_write32(hisi_hba, OQ_INT_SRC, 0x0);
757 hisi_sas_write32(hisi_hba, ENT_INT_SRC1, 0xffffffff);
758 hisi_sas_write32(hisi_hba, ENT_INT_SRC2, 0xffffffff);
759 hisi_sas_write32(hisi_hba, ENT_INT_SRC3, 0xffffffff);
760 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0x7efefefe);
761 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0x7efefefe);
762 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, 0x7ffffffe);
763 hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0xfffff3c0);
764 for (i = 0; i < hisi_hba->queue_count; i++)
765 hisi_sas_write32(hisi_hba, OQ0_INT_SRC_MSK+0x4*i, 0);
766
767 hisi_sas_write32(hisi_hba, AXI_AHB_CLK_CFG, 1);
768 hisi_sas_write32(hisi_hba, HYPER_STREAM_ID_EN_CFG, 1);
769
770 for (i = 0; i < hisi_hba->n_phy; i++) {
771 hisi_sas_phy_write32(hisi_hba, i, PROG_PHY_LINK_RATE, 0x855);
772 hisi_sas_phy_write32(hisi_hba, i, SAS_PHY_CTRL, 0x30b9908);
773 hisi_sas_phy_write32(hisi_hba, i, SL_TOUT_CFG, 0x7d7d7d7d);
774 hisi_sas_phy_write32(hisi_hba, i, DONE_RECEIVED_TIME, 0x10);
775 hisi_sas_phy_write32(hisi_hba, i, CHL_INT0, 0xffffffff);
776 hisi_sas_phy_write32(hisi_hba, i, CHL_INT1, 0xffffffff);
777 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2, 0xffffffff);
778 hisi_sas_phy_write32(hisi_hba, i, RXOP_CHECK_CFG_H, 0x1000);
779 hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0xffffffff);
780 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0x8ffffbff);
781 hisi_sas_phy_write32(hisi_hba, i, SL_CFG, 0x23f801fc);
782 hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL_RDY_MSK, 0x0);
783 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_NOT_RDY_MSK, 0x0);
784 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_DWS_RESET_MSK, 0x0);
785 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_PHY_ENA_MSK, 0x0);
786 hisi_sas_phy_write32(hisi_hba, i, SL_RX_BCAST_CHK_MSK, 0x0);
787 hisi_sas_phy_write32(hisi_hba, i, CHL_INT_COAL_EN, 0x0);
788 hisi_sas_phy_write32(hisi_hba, i, PHYCTRL_OOB_RESTART_MSK, 0x0);
789 hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL, 0x199B694);
790 }
791
792 for (i = 0; i < hisi_hba->queue_count; i++) {
793 /* Delivery queue */
794 hisi_sas_write32(hisi_hba,
795 DLVRY_Q_0_BASE_ADDR_HI + (i * 0x14),
796 upper_32_bits(hisi_hba->cmd_hdr_dma[i]));
797
798 hisi_sas_write32(hisi_hba, DLVRY_Q_0_BASE_ADDR_LO + (i * 0x14),
799 lower_32_bits(hisi_hba->cmd_hdr_dma[i]));
800
801 hisi_sas_write32(hisi_hba, DLVRY_Q_0_DEPTH + (i * 0x14),
802 HISI_SAS_QUEUE_SLOTS);
803
804 /* Completion queue */
805 hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_HI + (i * 0x14),
806 upper_32_bits(hisi_hba->complete_hdr_dma[i]));
807
808 hisi_sas_write32(hisi_hba, COMPL_Q_0_BASE_ADDR_LO + (i * 0x14),
809 lower_32_bits(hisi_hba->complete_hdr_dma[i]));
810
811 hisi_sas_write32(hisi_hba, COMPL_Q_0_DEPTH + (i * 0x14),
812 HISI_SAS_QUEUE_SLOTS);
813 }
814
815 /* itct */
816 hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_LO,
817 lower_32_bits(hisi_hba->itct_dma));
818
819 hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_HI,
820 upper_32_bits(hisi_hba->itct_dma));
821
822 /* iost */
823 hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_LO,
824 lower_32_bits(hisi_hba->iost_dma));
825
826 hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_HI,
827 upper_32_bits(hisi_hba->iost_dma));
828
829 /* breakpoint */
830 hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_LO,
831 lower_32_bits(hisi_hba->breakpoint_dma));
832
833 hisi_sas_write32(hisi_hba, IO_BROKEN_MSG_ADDR_HI,
834 upper_32_bits(hisi_hba->breakpoint_dma));
835
836 /* SATA broken msg */
837 hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_LO,
838 lower_32_bits(hisi_hba->sata_breakpoint_dma));
839
840 hisi_sas_write32(hisi_hba, IO_SATA_BROKEN_MSG_ADDR_HI,
841 upper_32_bits(hisi_hba->sata_breakpoint_dma));
842
843 /* SATA initial fis */
844 hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_LO,
845 lower_32_bits(hisi_hba->initial_fis_dma));
846
847 hisi_sas_write32(hisi_hba, SATA_INITI_D2H_STORE_ADDR_HI,
848 upper_32_bits(hisi_hba->initial_fis_dma));
849}
850
851static int hw_init_v2_hw(struct hisi_hba *hisi_hba)
852{
853 struct device *dev = &hisi_hba->pdev->dev;
854 int rc;
855
856 rc = reset_hw_v2_hw(hisi_hba);
857 if (rc) {
858 dev_err(dev, "hisi_sas_reset_hw failed, rc=%d", rc);
859 return rc;
860 }
861
862 msleep(100);
863 init_reg_v2_hw(hisi_hba);
864
806bb768
JG
865 init_id_frame_v2_hw(hisi_hba);
866
94eac9e1
JG
867 return 0;
868}
869
29a20428
JG
870static void enable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
871{
872 u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
873
874 cfg |= PHY_CFG_ENA_MSK;
875 hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
876}
877
63fb11b8
JG
878static void disable_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
879{
880 u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
881
882 cfg &= ~PHY_CFG_ENA_MSK;
883 hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
884}
885
29a20428
JG
886static void start_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
887{
888 config_id_frame_v2_hw(hisi_hba, phy_no);
889 config_phy_opt_mode_v2_hw(hisi_hba, phy_no);
890 enable_phy_v2_hw(hisi_hba, phy_no);
891}
892
63fb11b8
JG
893static void stop_phy_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
894{
895 disable_phy_v2_hw(hisi_hba, phy_no);
896}
897
898static void phy_hard_reset_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
899{
900 stop_phy_v2_hw(hisi_hba, phy_no);
901 msleep(100);
902 start_phy_v2_hw(hisi_hba, phy_no);
903}
904
29a20428
JG
905static void start_phys_v2_hw(unsigned long data)
906{
907 struct hisi_hba *hisi_hba = (struct hisi_hba *)data;
908 int i;
909
910 for (i = 0; i < hisi_hba->n_phy; i++)
911 start_phy_v2_hw(hisi_hba, i);
912}
913
914static void phys_init_v2_hw(struct hisi_hba *hisi_hba)
915{
916 int i;
917 struct timer_list *timer = &hisi_hba->timer;
918
919 for (i = 0; i < hisi_hba->n_phy; i++) {
920 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0x6a);
921 hisi_sas_phy_read32(hisi_hba, i, CHL_INT2_MSK);
922 }
923
924 setup_timer(timer, start_phys_v2_hw, (unsigned long)hisi_hba);
925 mod_timer(timer, jiffies + HZ);
926}
927
7911e66f
JG
928static void sl_notify_v2_hw(struct hisi_hba *hisi_hba, int phy_no)
929{
930 u32 sl_control;
931
932 sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
933 sl_control |= SL_CONTROL_NOTIFY_EN_MSK;
934 hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
935 msleep(1);
936 sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
937 sl_control &= ~SL_CONTROL_NOTIFY_EN_MSK;
938 hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
939}
940
5473c060
JG
941static int get_wideport_bitmap_v2_hw(struct hisi_hba *hisi_hba, int port_id)
942{
943 int i, bitmap = 0;
944 u32 phy_port_num_ma = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
945 u32 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
946
947 for (i = 0; i < (hisi_hba->n_phy < 9 ? hisi_hba->n_phy : 8); i++)
948 if (phy_state & 1 << i)
949 if (((phy_port_num_ma >> (i * 4)) & 0xf) == port_id)
950 bitmap |= 1 << i;
951
952 if (hisi_hba->n_phy == 9) {
953 u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
954
955 if (phy_state & 1 << 8)
956 if (((port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
957 PORT_STATE_PHY8_PORT_NUM_OFF) == port_id)
958 bitmap |= 1 << 9;
959 }
960
961 return bitmap;
962}
963
8c36e31d
JG
964/**
965 * This function allocates across all queues to load balance.
966 * Slots are allocated from queues in a round-robin fashion.
967 *
968 * The callpath to this function and upto writing the write
969 * queue pointer should be safe from interruption.
970 */
971static int get_free_slot_v2_hw(struct hisi_hba *hisi_hba, int *q, int *s)
972{
973 struct device *dev = &hisi_hba->pdev->dev;
974 u32 r, w;
975 int queue = hisi_hba->queue;
976
977 while (1) {
978 w = hisi_sas_read32_relaxed(hisi_hba,
979 DLVRY_Q_0_WR_PTR + (queue * 0x14));
980 r = hisi_sas_read32_relaxed(hisi_hba,
981 DLVRY_Q_0_RD_PTR + (queue * 0x14));
982 if (r == (w+1) % HISI_SAS_QUEUE_SLOTS) {
983 queue = (queue + 1) % hisi_hba->queue_count;
984 if (queue == hisi_hba->queue) {
985 dev_warn(dev, "could not find free slot\n");
986 return -EAGAIN;
987 }
988 continue;
989 }
990 break;
991 }
992 hisi_hba->queue = (queue + 1) % hisi_hba->queue_count;
993 *q = queue;
994 *s = w;
995 return 0;
996}
997
998static void start_delivery_v2_hw(struct hisi_hba *hisi_hba)
999{
1000 int dlvry_queue = hisi_hba->slot_prep->dlvry_queue;
1001 int dlvry_queue_slot = hisi_hba->slot_prep->dlvry_queue_slot;
1002
1003 hisi_sas_write32(hisi_hba, DLVRY_Q_0_WR_PTR + (dlvry_queue * 0x14),
1004 ++dlvry_queue_slot % HISI_SAS_QUEUE_SLOTS);
1005}
1006
1007static int prep_prd_sge_v2_hw(struct hisi_hba *hisi_hba,
1008 struct hisi_sas_slot *slot,
1009 struct hisi_sas_cmd_hdr *hdr,
1010 struct scatterlist *scatter,
1011 int n_elem)
1012{
1013 struct device *dev = &hisi_hba->pdev->dev;
1014 struct scatterlist *sg;
1015 int i;
1016
1017 if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
1018 dev_err(dev, "prd err: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
1019 n_elem);
1020 return -EINVAL;
1021 }
1022
1023 slot->sge_page = dma_pool_alloc(hisi_hba->sge_page_pool, GFP_ATOMIC,
1024 &slot->sge_page_dma);
1025 if (!slot->sge_page)
1026 return -ENOMEM;
1027
1028 for_each_sg(scatter, sg, n_elem, i) {
1029 struct hisi_sas_sge *entry = &slot->sge_page->sge[i];
1030
1031 entry->addr = cpu_to_le64(sg_dma_address(sg));
1032 entry->page_ctrl_0 = entry->page_ctrl_1 = 0;
1033 entry->data_len = cpu_to_le32(sg_dma_len(sg));
1034 entry->data_off = 0;
1035 }
1036
1037 hdr->prd_table_addr = cpu_to_le64(slot->sge_page_dma);
1038
1039 hdr->sg_len = cpu_to_le32(n_elem << CMD_HDR_DATA_SGL_LEN_OFF);
1040
1041 return 0;
1042}
1043
c2d89392
JG
1044static int prep_smp_v2_hw(struct hisi_hba *hisi_hba,
1045 struct hisi_sas_slot *slot)
1046{
1047 struct sas_task *task = slot->task;
1048 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1049 struct domain_device *device = task->dev;
1050 struct device *dev = &hisi_hba->pdev->dev;
1051 struct hisi_sas_port *port = slot->port;
1052 struct scatterlist *sg_req, *sg_resp;
1053 struct hisi_sas_device *sas_dev = device->lldd_dev;
1054 dma_addr_t req_dma_addr;
1055 unsigned int req_len, resp_len;
1056 int elem, rc;
1057
1058 /*
1059 * DMA-map SMP request, response buffers
1060 */
1061 /* req */
1062 sg_req = &task->smp_task.smp_req;
1063 elem = dma_map_sg(dev, sg_req, 1, DMA_TO_DEVICE);
1064 if (!elem)
1065 return -ENOMEM;
1066 req_len = sg_dma_len(sg_req);
1067 req_dma_addr = sg_dma_address(sg_req);
1068
1069 /* resp */
1070 sg_resp = &task->smp_task.smp_resp;
1071 elem = dma_map_sg(dev, sg_resp, 1, DMA_FROM_DEVICE);
1072 if (!elem) {
1073 rc = -ENOMEM;
1074 goto err_out_req;
1075 }
1076 resp_len = sg_dma_len(sg_resp);
1077 if ((req_len & 0x3) || (resp_len & 0x3)) {
1078 rc = -EINVAL;
1079 goto err_out_resp;
1080 }
1081
1082 /* create header */
1083 /* dw0 */
1084 hdr->dw0 = cpu_to_le32((port->id << CMD_HDR_PORT_OFF) |
1085 (1 << CMD_HDR_PRIORITY_OFF) | /* high pri */
1086 (2 << CMD_HDR_CMD_OFF)); /* smp */
1087
1088 /* map itct entry */
1089 hdr->dw1 = cpu_to_le32((sas_dev->device_id << CMD_HDR_DEV_ID_OFF) |
1090 (1 << CMD_HDR_FRAME_TYPE_OFF) |
1091 (DIR_NO_DATA << CMD_HDR_DIR_OFF));
1092
1093 /* dw2 */
1094 hdr->dw2 = cpu_to_le32((((req_len - 4) / 4) << CMD_HDR_CFL_OFF) |
1095 (HISI_SAS_MAX_SMP_RESP_SZ / 4 <<
1096 CMD_HDR_MRFL_OFF));
1097
1098 hdr->transfer_tags = cpu_to_le32(slot->idx << CMD_HDR_IPTT_OFF);
1099
1100 hdr->cmd_table_addr = cpu_to_le64(req_dma_addr);
1101 hdr->sts_buffer_addr = cpu_to_le64(slot->status_buffer_dma);
1102
1103 return 0;
1104
1105err_out_resp:
1106 dma_unmap_sg(dev, &slot->task->smp_task.smp_resp, 1,
1107 DMA_FROM_DEVICE);
1108err_out_req:
1109 dma_unmap_sg(dev, &slot->task->smp_task.smp_req, 1,
1110 DMA_TO_DEVICE);
1111 return rc;
1112}
1113
8c36e31d
JG
1114static int prep_ssp_v2_hw(struct hisi_hba *hisi_hba,
1115 struct hisi_sas_slot *slot, int is_tmf,
1116 struct hisi_sas_tmf_task *tmf)
1117{
1118 struct sas_task *task = slot->task;
1119 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1120 struct domain_device *device = task->dev;
1121 struct hisi_sas_device *sas_dev = device->lldd_dev;
1122 struct hisi_sas_port *port = slot->port;
1123 struct sas_ssp_task *ssp_task = &task->ssp_task;
1124 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
1125 int has_data = 0, rc, priority = is_tmf;
1126 u8 *buf_cmd;
1127 u32 dw1 = 0, dw2 = 0;
1128
1129 hdr->dw0 = cpu_to_le32((1 << CMD_HDR_RESP_REPORT_OFF) |
1130 (2 << CMD_HDR_TLR_CTRL_OFF) |
1131 (port->id << CMD_HDR_PORT_OFF) |
1132 (priority << CMD_HDR_PRIORITY_OFF) |
1133 (1 << CMD_HDR_CMD_OFF)); /* ssp */
1134
1135 dw1 = 1 << CMD_HDR_VDTL_OFF;
1136 if (is_tmf) {
1137 dw1 |= 2 << CMD_HDR_FRAME_TYPE_OFF;
1138 dw1 |= DIR_NO_DATA << CMD_HDR_DIR_OFF;
1139 } else {
1140 dw1 |= 1 << CMD_HDR_FRAME_TYPE_OFF;
1141 switch (scsi_cmnd->sc_data_direction) {
1142 case DMA_TO_DEVICE:
1143 has_data = 1;
1144 dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
1145 break;
1146 case DMA_FROM_DEVICE:
1147 has_data = 1;
1148 dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
1149 break;
1150 default:
1151 dw1 &= ~CMD_HDR_DIR_MSK;
1152 }
1153 }
1154
1155 /* map itct entry */
1156 dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
1157 hdr->dw1 = cpu_to_le32(dw1);
1158
1159 dw2 = (((sizeof(struct ssp_command_iu) + sizeof(struct ssp_frame_hdr)
1160 + 3) / 4) << CMD_HDR_CFL_OFF) |
1161 ((HISI_SAS_MAX_SSP_RESP_SZ / 4) << CMD_HDR_MRFL_OFF) |
1162 (2 << CMD_HDR_SG_MOD_OFF);
1163 hdr->dw2 = cpu_to_le32(dw2);
1164
1165 hdr->transfer_tags = cpu_to_le32(slot->idx);
1166
1167 if (has_data) {
1168 rc = prep_prd_sge_v2_hw(hisi_hba, slot, hdr, task->scatter,
1169 slot->n_elem);
1170 if (rc)
1171 return rc;
1172 }
1173
1174 hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
1175 hdr->cmd_table_addr = cpu_to_le64(slot->command_table_dma);
1176 hdr->sts_buffer_addr = cpu_to_le64(slot->status_buffer_dma);
1177
1178 buf_cmd = slot->command_table + sizeof(struct ssp_frame_hdr);
1179
1180 memcpy(buf_cmd, &task->ssp_task.LUN, 8);
1181 if (!is_tmf) {
1182 buf_cmd[9] = task->ssp_task.task_attr |
1183 (task->ssp_task.task_prio << 3);
1184 memcpy(buf_cmd + 12, task->ssp_task.cmd->cmnd,
1185 task->ssp_task.cmd->cmd_len);
1186 } else {
1187 buf_cmd[10] = tmf->tmf;
1188 switch (tmf->tmf) {
1189 case TMF_ABORT_TASK:
1190 case TMF_QUERY_TASK:
1191 buf_cmd[12] =
1192 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
1193 buf_cmd[13] =
1194 tmf->tag_of_task_to_be_managed & 0xff;
1195 break;
1196 default:
1197 break;
1198 }
1199 }
1200
1201 return 0;
1202}
1203
6f2ff1a1
JG
1204static void sata_done_v2_hw(struct hisi_hba *hisi_hba, struct sas_task *task,
1205 struct hisi_sas_slot *slot)
1206{
1207 struct task_status_struct *ts = &task->task_status;
1208 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
1209 struct dev_to_host_fis *d2h = slot->status_buffer +
1210 sizeof(struct hisi_sas_err_record);
1211
1212 resp->frame_len = sizeof(struct dev_to_host_fis);
1213 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
1214
1215 ts->buf_valid_size = sizeof(*resp);
1216}
e8fed0e9
JG
1217
1218/* by default, task resp is complete */
1219static void slot_err_v2_hw(struct hisi_hba *hisi_hba,
1220 struct sas_task *task,
1221 struct hisi_sas_slot *slot)
1222{
1223 struct task_status_struct *ts = &task->task_status;
1224 struct hisi_sas_err_record_v2 *err_record = slot->status_buffer;
1225 u32 trans_tx_fail_type = cpu_to_le32(err_record->trans_tx_fail_type);
1226 u32 trans_rx_fail_type = cpu_to_le32(err_record->trans_rx_fail_type);
1227 u16 dma_tx_err_type = cpu_to_le16(err_record->dma_tx_err_type);
1228 u16 sipc_rx_err_type = cpu_to_le16(err_record->sipc_rx_err_type);
1229 u32 dma_rx_err_type = cpu_to_le32(err_record->dma_rx_err_type);
1230 int error = -1;
1231
1232 if (dma_rx_err_type) {
1233 error = ffs(dma_rx_err_type)
1234 - 1 + DMA_RX_ERR_BASE;
1235 } else if (sipc_rx_err_type) {
1236 error = ffs(sipc_rx_err_type)
1237 - 1 + SIPC_RX_ERR_BASE;
1238 } else if (dma_tx_err_type) {
1239 error = ffs(dma_tx_err_type)
1240 - 1 + DMA_TX_ERR_BASE;
1241 } else if (trans_rx_fail_type) {
1242 error = ffs(trans_rx_fail_type)
1243 - 1 + TRANS_RX_FAIL_BASE;
1244 } else if (trans_tx_fail_type) {
1245 error = ffs(trans_tx_fail_type)
1246 - 1 + TRANS_TX_FAIL_BASE;
1247 }
1248
1249 switch (task->task_proto) {
1250 case SAS_PROTOCOL_SSP:
1251 {
1252 switch (error) {
1253 case TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION:
1254 {
1255 ts->stat = SAS_OPEN_REJECT;
1256 ts->open_rej_reason = SAS_OREJ_NO_DEST;
1257 break;
1258 }
1259 case TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED:
1260 {
1261 ts->stat = SAS_OPEN_REJECT;
1262 ts->open_rej_reason = SAS_OREJ_PATH_BLOCKED;
1263 break;
1264 }
1265 case TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED:
1266 {
1267 ts->stat = SAS_OPEN_REJECT;
1268 ts->open_rej_reason = SAS_OREJ_EPROTO;
1269 break;
1270 }
1271 case TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED:
1272 {
1273 ts->stat = SAS_OPEN_REJECT;
1274 ts->open_rej_reason = SAS_OREJ_CONN_RATE;
1275 break;
1276 }
1277 case TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION:
1278 {
1279 ts->stat = SAS_OPEN_REJECT;
1280 ts->open_rej_reason = SAS_OREJ_BAD_DEST;
1281 break;
1282 }
1283 case TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD:
1284 {
1285 ts->stat = SAS_OPEN_REJECT;
1286 ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
1287 break;
1288 }
1289 case TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION:
1290 {
1291 ts->stat = SAS_OPEN_REJECT;
1292 ts->open_rej_reason = SAS_OREJ_WRONG_DEST;
1293 break;
1294 }
1295 case TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION:
1296 {
1297 ts->stat = SAS_OPEN_REJECT;
1298 ts->open_rej_reason = SAS_OREJ_UNKNOWN;
1299 break;
1300 }
1301 case TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER:
1302 {
1303 /* not sure */
1304 ts->stat = SAS_DEV_NO_RESPONSE;
1305 break;
1306 }
1307 case TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE:
1308 {
1309 ts->stat = SAS_PHY_DOWN;
1310 break;
1311 }
1312 case TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT:
1313 {
1314 ts->stat = SAS_OPEN_TO;
1315 break;
1316 }
1317 case DMA_RX_DATA_LEN_OVERFLOW:
1318 {
1319 ts->stat = SAS_DATA_OVERRUN;
1320 ts->residual = 0;
1321 break;
1322 }
1323 case DMA_RX_DATA_LEN_UNDERFLOW:
1324 case SIPC_RX_DATA_UNDERFLOW_ERR:
1325 {
1326 ts->residual = trans_tx_fail_type;
1327 ts->stat = SAS_DATA_UNDERRUN;
1328 break;
1329 }
9c8ee657
JG
1330 case TRANS_TX_ERR_FRAME_TXED:
1331 {
1332 /* This will request a retry */
1333 ts->stat = SAS_QUEUE_FULL;
1334 slot->abort = 1;
1335 break;
1336 }
e8fed0e9
JG
1337 case TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS:
1338 case TRANS_TX_ERR_PHY_NOT_ENABLE:
1339 case TRANS_TX_OPEN_CNX_ERR_BY_OTHER:
1340 case TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT:
1341 case TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED:
1342 case TRANS_TX_ERR_WITH_BREAK_TIMEOUT:
1343 case TRANS_TX_ERR_WITH_BREAK_REQUEST:
1344 case TRANS_TX_ERR_WITH_BREAK_RECEVIED:
1345 case TRANS_TX_ERR_WITH_CLOSE_TIMEOUT:
1346 case TRANS_TX_ERR_WITH_CLOSE_NORMAL:
1347 case TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT:
1348 case TRANS_TX_ERR_WITH_CLOSE_COMINIT:
1349 case TRANS_TX_ERR_WITH_NAK_RECEVIED:
1350 case TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT:
1351 case TRANS_TX_ERR_WITH_IPTT_CONFLICT:
1352 case TRANS_TX_ERR_WITH_CREDIT_TIMEOUT:
1353 case TRANS_RX_ERR_WITH_RXFRAME_CRC_ERR:
1354 case TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR:
1355 case TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM:
1356 case TRANS_RX_ERR_WITH_BREAK_TIMEOUT:
1357 case TRANS_RX_ERR_WITH_BREAK_REQUEST:
1358 case TRANS_RX_ERR_WITH_BREAK_RECEVIED:
1359 case TRANS_RX_ERR_WITH_CLOSE_NORMAL:
1360 case TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT:
1361 case TRANS_RX_ERR_WITH_CLOSE_COMINIT:
1362 case TRANS_RX_ERR_WITH_DATA_LEN0:
1363 case TRANS_RX_ERR_WITH_BAD_HASH:
1364 case TRANS_RX_XRDY_WLEN_ZERO_ERR:
1365 case TRANS_RX_SSP_FRM_LEN_ERR:
1366 case TRANS_RX_ERR_WITH_BAD_FRM_TYPE:
1367 case DMA_TX_UNEXP_XFER_ERR:
1368 case DMA_TX_UNEXP_RETRANS_ERR:
1369 case DMA_TX_XFER_LEN_OVERFLOW:
1370 case DMA_TX_XFER_OFFSET_ERR:
1371 case DMA_RX_DATA_OFFSET_ERR:
1372 case DMA_RX_UNEXP_NORM_RESP_ERR:
1373 case DMA_RX_UNEXP_RDFRAME_ERR:
1374 case DMA_RX_UNKNOWN_FRM_ERR:
1375 {
1376 ts->stat = SAS_OPEN_REJECT;
1377 ts->open_rej_reason = SAS_OREJ_UNKNOWN;
1378 break;
1379 }
1380 default:
1381 break;
1382 }
1383 }
1384 break;
1385 case SAS_PROTOCOL_SMP:
1386 ts->stat = SAM_STAT_CHECK_CONDITION;
1387 break;
1388
1389 case SAS_PROTOCOL_SATA:
1390 case SAS_PROTOCOL_STP:
1391 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
1392 {
1393 switch (error) {
1394 case TRANS_TX_OPEN_CNX_ERR_LOW_PHY_POWER:
1395 case TRANS_TX_OPEN_CNX_ERR_PATHWAY_BLOCKED:
1396 case TRANS_TX_OPEN_CNX_ERR_NO_DESTINATION:
1397 {
1398 ts->resp = SAS_TASK_UNDELIVERED;
1399 ts->stat = SAS_DEV_NO_RESPONSE;
1400 break;
1401 }
1402 case TRANS_TX_OPEN_CNX_ERR_PROTOCOL_NOT_SUPPORTED:
1403 case TRANS_TX_OPEN_CNX_ERR_CONNECTION_RATE_NOT_SUPPORTED:
1404 case TRANS_TX_OPEN_CNX_ERR_BAD_DESTINATION:
1405 case TRANS_TX_OPEN_CNX_ERR_BREAK_RCVD:
1406 case TRANS_TX_OPEN_CNX_ERR_WRONG_DESTINATION:
1407 case TRANS_TX_OPEN_CNX_ERR_ZONE_VIOLATION:
1408 case TRANS_TX_OPEN_CNX_ERR_STP_RESOURCES_BUSY:
1409 {
1410 ts->stat = SAS_OPEN_REJECT;
1411 break;
1412 }
1413 case TRANS_TX_OPEN_CNX_ERR_OPEN_TIMEOUT:
1414 {
1415 ts->stat = SAS_OPEN_TO;
1416 break;
1417 }
1418 case DMA_RX_DATA_LEN_OVERFLOW:
1419 {
1420 ts->stat = SAS_DATA_OVERRUN;
1421 break;
1422 }
1423 case TRANS_TX_OPEN_FAIL_WITH_IT_NEXUS_LOSS:
1424 case TRANS_TX_ERR_PHY_NOT_ENABLE:
1425 case TRANS_TX_OPEN_CNX_ERR_BY_OTHER:
1426 case TRANS_TX_OPEN_CNX_ERR_AIP_TIMEOUT:
1427 case TRANS_TX_OPEN_RETRY_ERR_THRESHOLD_REACHED:
1428 case TRANS_TX_ERR_WITH_BREAK_TIMEOUT:
1429 case TRANS_TX_ERR_WITH_BREAK_REQUEST:
1430 case TRANS_TX_ERR_WITH_BREAK_RECEVIED:
1431 case TRANS_TX_ERR_WITH_CLOSE_TIMEOUT:
1432 case TRANS_TX_ERR_WITH_CLOSE_NORMAL:
1433 case TRANS_TX_ERR_WITH_CLOSE_DWS_TIMEOUT:
1434 case TRANS_TX_ERR_WITH_CLOSE_COMINIT:
1435 case TRANS_TX_ERR_WITH_NAK_RECEVIED:
1436 case TRANS_TX_ERR_WITH_ACK_NAK_TIMEOUT:
1437 case TRANS_TX_ERR_WITH_CREDIT_TIMEOUT:
1438 case TRANS_TX_ERR_WITH_WAIT_RECV_TIMEOUT:
1439 case TRANS_RX_ERR_WITH_RXFIS_8B10B_DISP_ERR:
1440 case TRANS_RX_ERR_WITH_RXFRAME_HAVE_ERRPRM:
1441 case TRANS_RX_ERR_WITH_RXFIS_DECODE_ERROR:
1442 case TRANS_RX_ERR_WITH_RXFIS_CRC_ERR:
1443 case TRANS_RX_ERR_WITH_RXFRAME_LENGTH_OVERRUN:
1444 case TRANS_RX_ERR_WITH_RXFIS_RX_SYNCP:
1445 case TRANS_RX_ERR_WITH_CLOSE_NORMAL:
1446 case TRANS_RX_ERR_WITH_CLOSE_PHY_DISABLE:
1447 case TRANS_RX_ERR_WITH_CLOSE_DWS_TIMEOUT:
1448 case TRANS_RX_ERR_WITH_CLOSE_COMINIT:
1449 case TRANS_RX_ERR_WITH_DATA_LEN0:
1450 case TRANS_RX_ERR_WITH_BAD_HASH:
1451 case TRANS_RX_XRDY_WLEN_ZERO_ERR:
1452 case TRANS_RX_SSP_FRM_LEN_ERR:
1453 case SIPC_RX_FIS_STATUS_ERR_BIT_VLD:
1454 case SIPC_RX_PIO_WRSETUP_STATUS_DRQ_ERR:
1455 case SIPC_RX_FIS_STATUS_BSY_BIT_ERR:
1456 case SIPC_RX_WRSETUP_LEN_ODD_ERR:
1457 case SIPC_RX_WRSETUP_LEN_ZERO_ERR:
1458 case SIPC_RX_WRDATA_LEN_NOT_MATCH_ERR:
1459 case SIPC_RX_SATA_UNEXP_FIS_ERR:
1460 case DMA_RX_SATA_FRAME_TYPE_ERR:
1461 case DMA_RX_UNEXP_RDFRAME_ERR:
1462 case DMA_RX_PIO_DATA_LEN_ERR:
1463 case DMA_RX_RDSETUP_STATUS_ERR:
1464 case DMA_RX_RDSETUP_STATUS_DRQ_ERR:
1465 case DMA_RX_RDSETUP_STATUS_BSY_ERR:
1466 case DMA_RX_RDSETUP_LEN_ODD_ERR:
1467 case DMA_RX_RDSETUP_LEN_ZERO_ERR:
1468 case DMA_RX_RDSETUP_LEN_OVER_ERR:
1469 case DMA_RX_RDSETUP_OFFSET_ERR:
1470 case DMA_RX_RDSETUP_ACTIVE_ERR:
1471 case DMA_RX_RDSETUP_ESTATUS_ERR:
1472 case DMA_RX_UNKNOWN_FRM_ERR:
1473 {
1474 ts->stat = SAS_OPEN_REJECT;
1475 break;
1476 }
1477 default:
1478 {
1479 ts->stat = SAS_PROTO_RESPONSE;
1480 break;
1481 }
1482 }
1483 sata_done_v2_hw(hisi_hba, task, slot);
1484 }
1485 break;
1486 default:
1487 break;
1488 }
1489}
1490
31a9cfa6
JG
1491static int
1492slot_complete_v2_hw(struct hisi_hba *hisi_hba, struct hisi_sas_slot *slot,
1493 int abort)
1494{
1495 struct sas_task *task = slot->task;
1496 struct hisi_sas_device *sas_dev;
1497 struct device *dev = &hisi_hba->pdev->dev;
1498 struct task_status_struct *ts;
1499 struct domain_device *device;
1500 enum exec_status sts;
1501 struct hisi_sas_complete_v2_hdr *complete_queue =
1502 hisi_hba->complete_hdr[slot->cmplt_queue];
1503 struct hisi_sas_complete_v2_hdr *complete_hdr =
1504 &complete_queue[slot->cmplt_queue_slot];
1505
1506 if (unlikely(!task || !task->lldd_task || !task->dev))
1507 return -EINVAL;
1508
1509 ts = &task->task_status;
1510 device = task->dev;
1511 sas_dev = device->lldd_dev;
1512
1513 task->task_state_flags &=
1514 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1515 task->task_state_flags |= SAS_TASK_STATE_DONE;
1516
1517 memset(ts, 0, sizeof(*ts));
1518 ts->resp = SAS_TASK_COMPLETE;
1519
1520 if (unlikely(!sas_dev || abort)) {
1521 if (!sas_dev)
1522 dev_dbg(dev, "slot complete: port has not device\n");
1523 ts->stat = SAS_PHY_DOWN;
1524 goto out;
1525 }
1526
1527 if ((complete_hdr->dw0 & CMPLT_HDR_ERX_MSK) &&
1528 (!(complete_hdr->dw0 & CMPLT_HDR_RSPNS_XFRD_MSK))) {
31a9cfa6 1529
e8fed0e9 1530 slot_err_v2_hw(hisi_hba, task, slot);
9c8ee657
JG
1531 if (unlikely(slot->abort)) {
1532 queue_work(hisi_hba->wq, &slot->abort_slot);
1533 /* immediately return and do not complete */
1534 return ts->stat;
1535 }
31a9cfa6
JG
1536 goto out;
1537 }
1538
1539 switch (task->task_proto) {
1540 case SAS_PROTOCOL_SSP:
1541 {
1542 struct ssp_response_iu *iu = slot->status_buffer +
1543 sizeof(struct hisi_sas_err_record);
1544
1545 sas_ssp_task_response(dev, task, iu);
1546 break;
1547 }
1548 case SAS_PROTOCOL_SMP:
1549 {
1550 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
1551 void *to;
1552
1553 ts->stat = SAM_STAT_GOOD;
1554 to = kmap_atomic(sg_page(sg_resp));
1555
1556 dma_unmap_sg(dev, &task->smp_task.smp_resp, 1,
1557 DMA_FROM_DEVICE);
1558 dma_unmap_sg(dev, &task->smp_task.smp_req, 1,
1559 DMA_TO_DEVICE);
1560 memcpy(to + sg_resp->offset,
1561 slot->status_buffer +
1562 sizeof(struct hisi_sas_err_record),
1563 sg_dma_len(sg_resp));
1564 kunmap_atomic(to);
1565 break;
1566 }
1567 case SAS_PROTOCOL_SATA:
1568 case SAS_PROTOCOL_STP:
1569 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
6f2ff1a1
JG
1570 {
1571 ts->stat = SAM_STAT_GOOD;
1572 sata_done_v2_hw(hisi_hba, task, slot);
1573 break;
1574 }
31a9cfa6
JG
1575 default:
1576 ts->stat = SAM_STAT_CHECK_CONDITION;
1577 break;
1578 }
1579
1580 if (!slot->port->port_attached) {
1581 dev_err(dev, "slot complete: port %d has removed\n",
1582 slot->port->sas_port.id);
1583 ts->stat = SAS_PHY_DOWN;
1584 }
1585
1586out:
1587 if (sas_dev && sas_dev->running_req)
1588 sas_dev->running_req--;
1589
1590 hisi_sas_slot_task_free(hisi_hba, task, slot);
1591 sts = ts->stat;
1592
1593 if (task->task_done)
1594 task->task_done(task);
1595
1596 return sts;
1597}
1598
6f2ff1a1
JG
1599static u8 get_ata_protocol(u8 cmd, int direction)
1600{
1601 switch (cmd) {
1602 case ATA_CMD_FPDMA_WRITE:
1603 case ATA_CMD_FPDMA_READ:
1604 return SATA_PROTOCOL_FPDMA;
1605
1606 case ATA_CMD_ID_ATA:
1607 case ATA_CMD_PMP_READ:
1608 case ATA_CMD_READ_LOG_EXT:
1609 case ATA_CMD_PIO_READ:
1610 case ATA_CMD_PIO_READ_EXT:
1611 case ATA_CMD_PMP_WRITE:
1612 case ATA_CMD_WRITE_LOG_EXT:
1613 case ATA_CMD_PIO_WRITE:
1614 case ATA_CMD_PIO_WRITE_EXT:
1615 return SATA_PROTOCOL_PIO;
1616
1617 case ATA_CMD_READ:
1618 case ATA_CMD_READ_EXT:
1619 case ATA_CMD_READ_LOG_DMA_EXT:
1620 case ATA_CMD_WRITE:
1621 case ATA_CMD_WRITE_EXT:
1622 case ATA_CMD_WRITE_QUEUED:
1623 case ATA_CMD_WRITE_LOG_DMA_EXT:
1624 return SATA_PROTOCOL_DMA;
1625
1626 case ATA_CMD_DOWNLOAD_MICRO:
1627 case ATA_CMD_DEV_RESET:
1628 case ATA_CMD_CHK_POWER:
1629 case ATA_CMD_FLUSH:
1630 case ATA_CMD_FLUSH_EXT:
1631 case ATA_CMD_VERIFY:
1632 case ATA_CMD_VERIFY_EXT:
1633 case ATA_CMD_SET_FEATURES:
1634 case ATA_CMD_STANDBY:
1635 case ATA_CMD_STANDBYNOW1:
1636 return SATA_PROTOCOL_NONDATA;
1637 default:
1638 if (direction == DMA_NONE)
1639 return SATA_PROTOCOL_NONDATA;
1640 return SATA_PROTOCOL_PIO;
1641 }
1642}
1643
1644static int get_ncq_tag_v2_hw(struct sas_task *task, u32 *tag)
1645{
1646 struct ata_queued_cmd *qc = task->uldd_task;
1647
1648 if (qc) {
1649 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
1650 qc->tf.command == ATA_CMD_FPDMA_READ) {
1651 *tag = qc->tag;
1652 return 1;
1653 }
1654 }
1655 return 0;
1656}
1657
1658static int prep_ata_v2_hw(struct hisi_hba *hisi_hba,
1659 struct hisi_sas_slot *slot)
1660{
1661 struct sas_task *task = slot->task;
1662 struct domain_device *device = task->dev;
1663 struct domain_device *parent_dev = device->parent;
1664 struct hisi_sas_device *sas_dev = device->lldd_dev;
1665 struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1666 struct hisi_sas_port *port = device->port->lldd_port;
1667 u8 *buf_cmd;
1668 int has_data = 0, rc = 0, hdr_tag = 0;
1669 u32 dw1 = 0, dw2 = 0;
1670
1671 /* create header */
1672 /* dw0 */
1673 hdr->dw0 = cpu_to_le32(port->id << CMD_HDR_PORT_OFF);
1674 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type))
1675 hdr->dw0 |= cpu_to_le32(3 << CMD_HDR_CMD_OFF);
1676 else
1677 hdr->dw0 |= cpu_to_le32(4 << CMD_HDR_CMD_OFF);
1678
1679 /* dw1 */
1680 switch (task->data_dir) {
1681 case DMA_TO_DEVICE:
1682 has_data = 1;
1683 dw1 |= DIR_TO_DEVICE << CMD_HDR_DIR_OFF;
1684 break;
1685 case DMA_FROM_DEVICE:
1686 has_data = 1;
1687 dw1 |= DIR_TO_INI << CMD_HDR_DIR_OFF;
1688 break;
1689 default:
1690 dw1 &= ~CMD_HDR_DIR_MSK;
1691 }
1692
1693 if (0 == task->ata_task.fis.command)
1694 dw1 |= 1 << CMD_HDR_RESET_OFF;
1695
1696 dw1 |= (get_ata_protocol(task->ata_task.fis.command, task->data_dir))
1697 << CMD_HDR_FRAME_TYPE_OFF;
1698 dw1 |= sas_dev->device_id << CMD_HDR_DEV_ID_OFF;
1699 hdr->dw1 = cpu_to_le32(dw1);
1700
1701 /* dw2 */
1702 if (task->ata_task.use_ncq && get_ncq_tag_v2_hw(task, &hdr_tag)) {
1703 task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
1704 dw2 |= hdr_tag << CMD_HDR_NCQ_TAG_OFF;
1705 }
1706
1707 dw2 |= (HISI_SAS_MAX_STP_RESP_SZ / 4) << CMD_HDR_CFL_OFF |
1708 2 << CMD_HDR_SG_MOD_OFF;
1709 hdr->dw2 = cpu_to_le32(dw2);
1710
1711 /* dw3 */
1712 hdr->transfer_tags = cpu_to_le32(slot->idx);
1713
1714 if (has_data) {
1715 rc = prep_prd_sge_v2_hw(hisi_hba, slot, hdr, task->scatter,
1716 slot->n_elem);
1717 if (rc)
1718 return rc;
1719 }
1720
1721
1722 hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
1723 hdr->cmd_table_addr = cpu_to_le64(slot->command_table_dma);
1724 hdr->sts_buffer_addr = cpu_to_le64(slot->status_buffer_dma);
1725
1726 buf_cmd = slot->command_table;
1727
1728 if (likely(!task->ata_task.device_control_reg_update))
1729 task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
1730 /* fill in command FIS */
1731 memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
1732
1733 return 0;
1734}
1735
7911e66f
JG
1736static int phy_up_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
1737{
1738 int i, res = 0;
1739 u32 context, port_id, link_rate, hard_phy_linkrate;
1740 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1741 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1742 struct device *dev = &hisi_hba->pdev->dev;
1743 u32 *frame_rcvd = (u32 *)sas_phy->frame_rcvd;
1744 struct sas_identify_frame *id = (struct sas_identify_frame *)frame_rcvd;
1745
1746 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 1);
1747
1748 /* Check for SATA dev */
1749 context = hisi_sas_read32(hisi_hba, PHY_CONTEXT);
1750 if (context & (1 << phy_no))
1751 goto end;
1752
1753 if (phy_no == 8) {
1754 u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
1755
1756 port_id = (port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
1757 PORT_STATE_PHY8_PORT_NUM_OFF;
1758 link_rate = (port_state & PORT_STATE_PHY8_CONN_RATE_MSK) >>
1759 PORT_STATE_PHY8_CONN_RATE_OFF;
1760 } else {
1761 port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
1762 port_id = (port_id >> (4 * phy_no)) & 0xf;
1763 link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
1764 link_rate = (link_rate >> (phy_no * 4)) & 0xf;
1765 }
1766
1767 if (port_id == 0xf) {
1768 dev_err(dev, "phyup: phy%d invalid portid\n", phy_no);
1769 res = IRQ_NONE;
1770 goto end;
1771 }
1772
1773 for (i = 0; i < 6; i++) {
1774 u32 idaf = hisi_sas_phy_read32(hisi_hba, phy_no,
1775 RX_IDAF_DWORD0 + (i * 4));
1776 frame_rcvd[i] = __swab32(idaf);
1777 }
1778
1779 /* Get the linkrates */
1780 link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
1781 link_rate = (link_rate >> (phy_no * 4)) & 0xf;
1782 sas_phy->linkrate = link_rate;
1783 hard_phy_linkrate = hisi_sas_phy_read32(hisi_hba, phy_no,
1784 HARD_PHY_LINKRATE);
1785 phy->maximum_linkrate = hard_phy_linkrate & 0xf;
1786 phy->minimum_linkrate = (hard_phy_linkrate >> 4) & 0xf;
1787
1788 sas_phy->oob_mode = SAS_OOB_MODE;
1789 memcpy(sas_phy->attached_sas_addr, &id->sas_addr, SAS_ADDR_SIZE);
1790 dev_info(dev, "phyup: phy%d link_rate=%d\n", phy_no, link_rate);
1791 phy->port_id = port_id;
1792 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1793 phy->phy_type |= PORT_TYPE_SAS;
1794 phy->phy_attached = 1;
1795 phy->identify.device_type = id->dev_type;
1796 phy->frame_rcvd_size = sizeof(struct sas_identify_frame);
1797 if (phy->identify.device_type == SAS_END_DEVICE)
1798 phy->identify.target_port_protocols =
1799 SAS_PROTOCOL_SSP;
1800 else if (phy->identify.device_type != SAS_PHY_UNUSED)
1801 phy->identify.target_port_protocols =
1802 SAS_PROTOCOL_SMP;
1803 queue_work(hisi_hba->wq, &phy->phyup_ws);
1804
1805end:
1806 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
1807 CHL_INT0_SL_PHY_ENABLE_MSK);
1808 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_PHY_ENA_MSK, 0);
1809
1810 return res;
1811}
1812
5473c060
JG
1813static int phy_down_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
1814{
1815 int res = 0;
1816 u32 phy_cfg, phy_state;
1817
1818 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 1);
1819
1820 phy_cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
1821
1822 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1823
1824 hisi_sas_phy_down(hisi_hba, phy_no, (phy_state & 1 << phy_no) ? 1 : 0);
1825
1826 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0, CHL_INT0_NOT_RDY_MSK);
1827 hisi_sas_phy_write32(hisi_hba, phy_no, PHYCTRL_NOT_RDY_MSK, 0);
1828
1829 return res;
1830}
1831
7911e66f
JG
1832static irqreturn_t int_phy_updown_v2_hw(int irq_no, void *p)
1833{
1834 struct hisi_hba *hisi_hba = p;
1835 u32 irq_msk;
1836 int phy_no = 0;
1837 irqreturn_t res = IRQ_HANDLED;
1838
1839 irq_msk = (hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO)
1840 >> HGC_INVLD_DQE_INFO_FB_CH0_OFF) & 0x1ff;
1841 while (irq_msk) {
1842 if (irq_msk & 1) {
1843 u32 irq_value = hisi_sas_phy_read32(hisi_hba, phy_no,
1844 CHL_INT0);
1845
1846 if (irq_value & CHL_INT0_SL_PHY_ENABLE_MSK)
1847 /* phy up */
1848 if (phy_up_v2_hw(phy_no, hisi_hba)) {
1849 res = IRQ_NONE;
1850 goto end;
1851 }
1852
5473c060
JG
1853 if (irq_value & CHL_INT0_NOT_RDY_MSK)
1854 /* phy down */
1855 if (phy_down_v2_hw(phy_no, hisi_hba)) {
1856 res = IRQ_NONE;
1857 goto end;
1858 }
7911e66f
JG
1859 }
1860 irq_msk >>= 1;
1861 phy_no++;
1862 }
1863
1864end:
1865 return res;
1866}
1867
d3bf3d84
JG
1868static void phy_bcast_v2_hw(int phy_no, struct hisi_hba *hisi_hba)
1869{
1870 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1871 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1872 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1873 unsigned long flags;
1874
1875 hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 1);
1876
1877 spin_lock_irqsave(&hisi_hba->lock, flags);
1878 sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
1879 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1880
1881 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0,
1882 CHL_INT0_SL_RX_BCST_ACK_MSK);
1883 hisi_sas_phy_write32(hisi_hba, phy_no, SL_RX_BCAST_CHK_MSK, 0);
1884}
1885
1886static irqreturn_t int_chnl_int_v2_hw(int irq_no, void *p)
1887{
1888 struct hisi_hba *hisi_hba = p;
1889 struct device *dev = &hisi_hba->pdev->dev;
1890 u32 ent_msk, ent_tmp, irq_msk;
1891 int phy_no = 0;
1892
1893 ent_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK3);
1894 ent_tmp = ent_msk;
1895 ent_msk |= ENT_INT_SRC_MSK3_ENT95_MSK_MSK;
1896 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, ent_msk);
1897
1898 irq_msk = (hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO) >>
1899 HGC_INVLD_DQE_INFO_FB_CH3_OFF) & 0x1ff;
1900
1901 while (irq_msk) {
1902 if (irq_msk & (1 << phy_no)) {
1903 u32 irq_value0 = hisi_sas_phy_read32(hisi_hba, phy_no,
1904 CHL_INT0);
1905 u32 irq_value1 = hisi_sas_phy_read32(hisi_hba, phy_no,
1906 CHL_INT1);
1907 u32 irq_value2 = hisi_sas_phy_read32(hisi_hba, phy_no,
1908 CHL_INT2);
1909
1910 if (irq_value1) {
1911 if (irq_value1 & (CHL_INT1_DMAC_RX_ECC_ERR_MSK |
1912 CHL_INT1_DMAC_TX_ECC_ERR_MSK))
1913 panic("%s: DMAC RX/TX ecc bad error! (0x%x)",
1914 dev_name(dev), irq_value1);
1915
1916 hisi_sas_phy_write32(hisi_hba, phy_no,
1917 CHL_INT1, irq_value1);
1918 }
1919
1920 if (irq_value2)
1921 hisi_sas_phy_write32(hisi_hba, phy_no,
1922 CHL_INT2, irq_value2);
1923
1924
1925 if (irq_value0) {
1926 if (irq_value0 & CHL_INT0_SL_RX_BCST_ACK_MSK)
1927 phy_bcast_v2_hw(phy_no, hisi_hba);
1928
1929 hisi_sas_phy_write32(hisi_hba, phy_no,
1930 CHL_INT0, irq_value0
1931 & (~CHL_INT0_HOTPLUG_TOUT_MSK)
1932 & (~CHL_INT0_SL_PHY_ENABLE_MSK)
1933 & (~CHL_INT0_NOT_RDY_MSK));
1934 }
1935 }
1936 irq_msk &= ~(1 << phy_no);
1937 phy_no++;
1938 }
1939
1940 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK3, ent_tmp);
1941
1942 return IRQ_HANDLED;
1943}
1944
31a9cfa6
JG
1945static irqreturn_t cq_interrupt_v2_hw(int irq_no, void *p)
1946{
1947 struct hisi_sas_cq *cq = p;
1948 struct hisi_hba *hisi_hba = cq->hisi_hba;
1949 struct hisi_sas_slot *slot;
1950 struct hisi_sas_itct *itct;
1951 struct hisi_sas_complete_v2_hdr *complete_queue;
1952 u32 irq_value, rd_point, wr_point, dev_id;
1953 int queue = cq->id;
1954
1955 complete_queue = hisi_hba->complete_hdr[queue];
1956 irq_value = hisi_sas_read32(hisi_hba, OQ_INT_SRC);
1957
1958 hisi_sas_write32(hisi_hba, OQ_INT_SRC, 1 << queue);
1959
1960 rd_point = hisi_sas_read32(hisi_hba, COMPL_Q_0_RD_PTR +
1961 (0x14 * queue));
1962 wr_point = hisi_sas_read32(hisi_hba, COMPL_Q_0_WR_PTR +
1963 (0x14 * queue));
1964
1965 while (rd_point != wr_point) {
1966 struct hisi_sas_complete_v2_hdr *complete_hdr;
1967 int iptt;
1968
1969 complete_hdr = &complete_queue[rd_point];
1970
1971 /* Check for NCQ completion */
1972 if (complete_hdr->act) {
1973 u32 act_tmp = complete_hdr->act;
1974 int ncq_tag_count = ffs(act_tmp);
1975
1976 dev_id = (complete_hdr->dw1 & CMPLT_HDR_DEV_ID_MSK) >>
1977 CMPLT_HDR_DEV_ID_OFF;
1978 itct = &hisi_hba->itct[dev_id];
1979
1980 /* The NCQ tags are held in the itct header */
1981 while (ncq_tag_count) {
1982 __le64 *ncq_tag = &itct->qw4_15[0];
1983
1984 ncq_tag_count -= 1;
1985 iptt = (ncq_tag[ncq_tag_count / 5]
1986 >> (ncq_tag_count % 5) * 12) & 0xfff;
1987
1988 slot = &hisi_hba->slot_info[iptt];
1989 slot->cmplt_queue_slot = rd_point;
1990 slot->cmplt_queue = queue;
1991 slot_complete_v2_hw(hisi_hba, slot, 0);
1992
1993 act_tmp &= ~(1 << ncq_tag_count);
1994 ncq_tag_count = ffs(act_tmp);
1995 }
1996 } else {
1997 iptt = (complete_hdr->dw1) & CMPLT_HDR_IPTT_MSK;
1998 slot = &hisi_hba->slot_info[iptt];
1999 slot->cmplt_queue_slot = rd_point;
2000 slot->cmplt_queue = queue;
2001 slot_complete_v2_hw(hisi_hba, slot, 0);
2002 }
2003
2004 if (++rd_point >= HISI_SAS_QUEUE_SLOTS)
2005 rd_point = 0;
2006 }
2007
2008 /* update rd_point */
2009 hisi_sas_write32(hisi_hba, COMPL_Q_0_RD_PTR + (0x14 * queue), rd_point);
2010 return IRQ_HANDLED;
2011}
2012
d43f9cdb
JG
2013static irqreturn_t sata_int_v2_hw(int irq_no, void *p)
2014{
2015 struct hisi_sas_phy *phy = p;
2016 struct hisi_hba *hisi_hba = phy->hisi_hba;
2017 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2018 struct device *dev = &hisi_hba->pdev->dev;
2019 struct hisi_sas_initial_fis *initial_fis;
2020 struct dev_to_host_fis *fis;
2021 u32 ent_tmp, ent_msk, ent_int, port_id, link_rate, hard_phy_linkrate;
2022 irqreturn_t res = IRQ_HANDLED;
2023 u8 attached_sas_addr[SAS_ADDR_SIZE] = {0};
11826e5d 2024 int phy_no, offset;
d43f9cdb
JG
2025
2026 phy_no = sas_phy->id;
2027 initial_fis = &hisi_hba->initial_fis[phy_no];
2028 fis = &initial_fis->fis;
2029
11826e5d
JG
2030 offset = 4 * (phy_no / 4);
2031 ent_msk = hisi_sas_read32(hisi_hba, ENT_INT_SRC_MSK1 + offset);
2032 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1 + offset,
2033 ent_msk | 1 << ((phy_no % 4) * 8));
d43f9cdb 2034
11826e5d
JG
2035 ent_int = hisi_sas_read32(hisi_hba, ENT_INT_SRC1 + offset);
2036 ent_tmp = ent_int & (1 << (ENT_INT_SRC1_D2H_FIS_CH1_OFF *
2037 (phy_no % 4)));
d43f9cdb
JG
2038 ent_int >>= ENT_INT_SRC1_D2H_FIS_CH1_OFF * (phy_no % 4);
2039 if ((ent_int & ENT_INT_SRC1_D2H_FIS_CH0_MSK) == 0) {
2040 dev_warn(dev, "sata int: phy%d did not receive FIS\n", phy_no);
d43f9cdb
JG
2041 res = IRQ_NONE;
2042 goto end;
2043 }
2044
2045 if (unlikely(phy_no == 8)) {
2046 u32 port_state = hisi_sas_read32(hisi_hba, PORT_STATE);
2047
2048 port_id = (port_state & PORT_STATE_PHY8_PORT_NUM_MSK) >>
2049 PORT_STATE_PHY8_PORT_NUM_OFF;
2050 link_rate = (port_state & PORT_STATE_PHY8_CONN_RATE_MSK) >>
2051 PORT_STATE_PHY8_CONN_RATE_OFF;
2052 } else {
2053 port_id = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
2054 port_id = (port_id >> (4 * phy_no)) & 0xf;
2055 link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
2056 link_rate = (link_rate >> (phy_no * 4)) & 0xf;
2057 }
2058
2059 if (port_id == 0xf) {
2060 dev_err(dev, "sata int: phy%d invalid portid\n", phy_no);
2061 res = IRQ_NONE;
2062 goto end;
2063 }
2064
2065 sas_phy->linkrate = link_rate;
2066 hard_phy_linkrate = hisi_sas_phy_read32(hisi_hba, phy_no,
2067 HARD_PHY_LINKRATE);
2068 phy->maximum_linkrate = hard_phy_linkrate & 0xf;
2069 phy->minimum_linkrate = (hard_phy_linkrate >> 4) & 0xf;
2070
2071 sas_phy->oob_mode = SATA_OOB_MODE;
2072 /* Make up some unique SAS address */
2073 attached_sas_addr[0] = 0x50;
2074 attached_sas_addr[7] = phy_no;
2075 memcpy(sas_phy->attached_sas_addr, attached_sas_addr, SAS_ADDR_SIZE);
2076 memcpy(sas_phy->frame_rcvd, fis, sizeof(struct dev_to_host_fis));
2077 dev_info(dev, "sata int phyup: phy%d link_rate=%d\n", phy_no, link_rate);
2078 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
2079 phy->port_id = port_id;
2080 phy->phy_type |= PORT_TYPE_SATA;
2081 phy->phy_attached = 1;
2082 phy->identify.device_type = SAS_SATA_DEV;
2083 phy->frame_rcvd_size = sizeof(struct dev_to_host_fis);
2084 phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
2085 queue_work(hisi_hba->wq, &phy->phyup_ws);
2086
2087end:
11826e5d
JG
2088 hisi_sas_write32(hisi_hba, ENT_INT_SRC1 + offset, ent_tmp);
2089 hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1 + offset, ent_msk);
d43f9cdb
JG
2090
2091 return res;
2092}
2093
7911e66f
JG
2094static irq_handler_t phy_interrupts[HISI_SAS_PHY_INT_NR] = {
2095 int_phy_updown_v2_hw,
d3bf3d84 2096 int_chnl_int_v2_hw,
7911e66f
JG
2097};
2098
2099/**
2100 * There is a limitation in the hip06 chipset that we need
2101 * to map in all mbigen interrupts, even if they are not used.
2102 */
2103static int interrupt_init_v2_hw(struct hisi_hba *hisi_hba)
2104{
2105 struct platform_device *pdev = hisi_hba->pdev;
2106 struct device *dev = &pdev->dev;
2107 int i, irq, rc, irq_map[128];
2108
2109
2110 for (i = 0; i < 128; i++)
2111 irq_map[i] = platform_get_irq(pdev, i);
2112
2113 for (i = 0; i < HISI_SAS_PHY_INT_NR; i++) {
2114 int idx = i;
2115
2116 irq = irq_map[idx + 1]; /* Phy up/down is irq1 */
2117 if (!irq) {
2118 dev_err(dev, "irq init: fail map phy interrupt %d\n",
2119 idx);
2120 return -ENOENT;
2121 }
2122
2123 rc = devm_request_irq(dev, irq, phy_interrupts[i], 0,
2124 DRV_NAME " phy", hisi_hba);
2125 if (rc) {
2126 dev_err(dev, "irq init: could not request "
2127 "phy interrupt %d, rc=%d\n",
2128 irq, rc);
2129 return -ENOENT;
2130 }
2131 }
2132
d43f9cdb
JG
2133 for (i = 0; i < hisi_hba->n_phy; i++) {
2134 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2135 int idx = i + 72; /* First SATA interrupt is irq72 */
2136
2137 irq = irq_map[idx];
2138 if (!irq) {
2139 dev_err(dev, "irq init: fail map phy interrupt %d\n",
2140 idx);
2141 return -ENOENT;
2142 }
2143
2144 rc = devm_request_irq(dev, irq, sata_int_v2_hw, 0,
2145 DRV_NAME " sata", phy);
2146 if (rc) {
2147 dev_err(dev, "irq init: could not request "
2148 "sata interrupt %d, rc=%d\n",
2149 irq, rc);
2150 return -ENOENT;
2151 }
2152 }
31a9cfa6
JG
2153
2154 for (i = 0; i < hisi_hba->queue_count; i++) {
2155 int idx = i + 96; /* First cq interrupt is irq96 */
2156
2157 irq = irq_map[idx];
2158 if (!irq) {
2159 dev_err(dev,
2160 "irq init: could not map cq interrupt %d\n",
2161 idx);
2162 return -ENOENT;
2163 }
2164 rc = devm_request_irq(dev, irq, cq_interrupt_v2_hw, 0,
2165 DRV_NAME " cq", &hisi_hba->cq[i]);
2166 if (rc) {
2167 dev_err(dev,
2168 "irq init: could not request cq interrupt %d, rc=%d\n",
2169 irq, rc);
2170 return -ENOENT;
2171 }
2172 }
2173
7911e66f
JG
2174 return 0;
2175}
2176
94eac9e1
JG
2177static int hisi_sas_v2_init(struct hisi_hba *hisi_hba)
2178{
2179 int rc;
2180
2181 rc = hw_init_v2_hw(hisi_hba);
2182 if (rc)
2183 return rc;
2184
7911e66f
JG
2185 rc = interrupt_init_v2_hw(hisi_hba);
2186 if (rc)
2187 return rc;
2188
29a20428
JG
2189 phys_init_v2_hw(hisi_hba);
2190
94eac9e1
JG
2191 return 0;
2192}
2193
3417ba8a 2194static const struct hisi_sas_hw hisi_sas_v2_hw = {
94eac9e1 2195 .hw_init = hisi_sas_v2_init,
85b2c3c0 2196 .setup_itct = setup_itct_v2_hw,
330fa7f3 2197 .slot_index_alloc = slot_index_alloc_quirk_v2_hw,
7911e66f 2198 .sl_notify = sl_notify_v2_hw,
5473c060 2199 .get_wideport_bitmap = get_wideport_bitmap_v2_hw,
85b2c3c0 2200 .free_device = free_device_v2_hw,
c2d89392 2201 .prep_smp = prep_smp_v2_hw,
8c36e31d 2202 .prep_ssp = prep_ssp_v2_hw,
6f2ff1a1 2203 .prep_stp = prep_ata_v2_hw,
8c36e31d
JG
2204 .get_free_slot = get_free_slot_v2_hw,
2205 .start_delivery = start_delivery_v2_hw,
31a9cfa6 2206 .slot_complete = slot_complete_v2_hw,
63fb11b8
JG
2207 .phy_enable = enable_phy_v2_hw,
2208 .phy_disable = disable_phy_v2_hw,
2209 .phy_hard_reset = phy_hard_reset_v2_hw,
94eac9e1
JG
2210 .max_command_entries = HISI_SAS_COMMAND_ENTRIES_V2_HW,
2211 .complete_hdr_size = sizeof(struct hisi_sas_complete_v2_hdr),
3417ba8a
JG
2212};
2213
2214static int hisi_sas_v2_probe(struct platform_device *pdev)
2215{
2216 return hisi_sas_probe(pdev, &hisi_sas_v2_hw);
2217}
2218
2219static int hisi_sas_v2_remove(struct platform_device *pdev)
2220{
2221 return hisi_sas_remove(pdev);
2222}
2223
2224static const struct of_device_id sas_v2_of_match[] = {
2225 { .compatible = "hisilicon,hip06-sas-v2",},
2226 {},
2227};
2228MODULE_DEVICE_TABLE(of, sas_v2_of_match);
2229
2230static struct platform_driver hisi_sas_v2_driver = {
2231 .probe = hisi_sas_v2_probe,
2232 .remove = hisi_sas_v2_remove,
2233 .driver = {
2234 .name = DRV_NAME,
2235 .of_match_table = sas_v2_of_match,
2236 },
2237};
2238
2239module_platform_driver(hisi_sas_v2_driver);
2240
2241MODULE_LICENSE("GPL");
2242MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2243MODULE_DESCRIPTION("HISILICON SAS controller v2 hw driver");
2244MODULE_ALIAS("platform:" DRV_NAME);