Merge branch 'for-4.18/alps' into for-linus
[linux-2.6-microblaze.git] / drivers / scsi / hisi_sas / hisi_sas_v1_hw.c
1 /*
2  * Copyright (c) 2015 Linaro Ltd.
3  * Copyright (c) 2015 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_v1_hw"
14
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 BROKEN_MSG_ADDR_LO              0x18
22 #define 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 PHY_CONN_RATE                   0x30
28 #define HGC_TRANS_TASK_CNT_LIMIT        0x38
29 #define AXI_AHB_CLK_CFG                 0x3c
30 #define HGC_SAS_TXFAIL_RETRY_CTRL       0x84
31 #define HGC_GET_ITV_TIME                0x90
32 #define DEVICE_MSG_WORK_MODE            0x94
33 #define I_T_NEXUS_LOSS_TIME             0xa0
34 #define BUS_INACTIVE_LIMIT_TIME         0xa8
35 #define REJECT_TO_OPEN_LIMIT_TIME       0xac
36 #define CFG_AGING_TIME                  0xbc
37 #define CFG_AGING_TIME_ITCT_REL_OFF     0
38 #define CFG_AGING_TIME_ITCT_REL_MSK     (0x1 << CFG_AGING_TIME_ITCT_REL_OFF)
39 #define HGC_DFX_CFG2                    0xc0
40 #define FIS_LIST_BADDR_L                0xc4
41 #define CFG_1US_TIMER_TRSH              0xcc
42 #define CFG_SAS_CONFIG                  0xd4
43 #define HGC_IOST_ECC_ADDR               0x140
44 #define HGC_IOST_ECC_ADDR_BAD_OFF       16
45 #define HGC_IOST_ECC_ADDR_BAD_MSK       (0x3ff << HGC_IOST_ECC_ADDR_BAD_OFF)
46 #define HGC_DQ_ECC_ADDR                 0x144
47 #define HGC_DQ_ECC_ADDR_BAD_OFF         16
48 #define HGC_DQ_ECC_ADDR_BAD_MSK         (0xfff << HGC_DQ_ECC_ADDR_BAD_OFF)
49 #define HGC_INVLD_DQE_INFO              0x148
50 #define HGC_INVLD_DQE_INFO_DQ_OFF       0
51 #define HGC_INVLD_DQE_INFO_DQ_MSK       (0xffff << HGC_INVLD_DQE_INFO_DQ_OFF)
52 #define HGC_INVLD_DQE_INFO_TYPE_OFF     16
53 #define HGC_INVLD_DQE_INFO_TYPE_MSK     (0x1 << HGC_INVLD_DQE_INFO_TYPE_OFF)
54 #define HGC_INVLD_DQE_INFO_FORCE_OFF    17
55 #define HGC_INVLD_DQE_INFO_FORCE_MSK    (0x1 << HGC_INVLD_DQE_INFO_FORCE_OFF)
56 #define HGC_INVLD_DQE_INFO_PHY_OFF      18
57 #define HGC_INVLD_DQE_INFO_PHY_MSK      (0x1 << HGC_INVLD_DQE_INFO_PHY_OFF)
58 #define HGC_INVLD_DQE_INFO_ABORT_OFF    19
59 #define HGC_INVLD_DQE_INFO_ABORT_MSK    (0x1 << HGC_INVLD_DQE_INFO_ABORT_OFF)
60 #define HGC_INVLD_DQE_INFO_IPTT_OF_OFF  20
61 #define HGC_INVLD_DQE_INFO_IPTT_OF_MSK  (0x1 << HGC_INVLD_DQE_INFO_IPTT_OF_OFF)
62 #define HGC_INVLD_DQE_INFO_SSP_ERR_OFF  21
63 #define HGC_INVLD_DQE_INFO_SSP_ERR_MSK  (0x1 << HGC_INVLD_DQE_INFO_SSP_ERR_OFF)
64 #define HGC_INVLD_DQE_INFO_OFL_OFF      22
65 #define HGC_INVLD_DQE_INFO_OFL_MSK      (0x1 << HGC_INVLD_DQE_INFO_OFL_OFF)
66 #define HGC_ITCT_ECC_ADDR               0x150
67 #define HGC_ITCT_ECC_ADDR_BAD_OFF       16
68 #define HGC_ITCT_ECC_ADDR_BAD_MSK       (0x3ff << HGC_ITCT_ECC_ADDR_BAD_OFF)
69 #define HGC_AXI_FIFO_ERR_INFO           0x154
70 #define INT_COAL_EN                     0x1bc
71 #define OQ_INT_COAL_TIME                0x1c0
72 #define OQ_INT_COAL_CNT                 0x1c4
73 #define ENT_INT_COAL_TIME               0x1c8
74 #define ENT_INT_COAL_CNT                0x1cc
75 #define OQ_INT_SRC                      0x1d0
76 #define OQ_INT_SRC_MSK                  0x1d4
77 #define ENT_INT_SRC1                    0x1d8
78 #define ENT_INT_SRC2                    0x1dc
79 #define ENT_INT_SRC2_DQ_CFG_ERR_OFF     25
80 #define ENT_INT_SRC2_DQ_CFG_ERR_MSK     (0x1 << ENT_INT_SRC2_DQ_CFG_ERR_OFF)
81 #define ENT_INT_SRC2_CQ_CFG_ERR_OFF     27
82 #define ENT_INT_SRC2_CQ_CFG_ERR_MSK     (0x1 << ENT_INT_SRC2_CQ_CFG_ERR_OFF)
83 #define ENT_INT_SRC2_AXI_WRONG_INT_OFF  28
84 #define ENT_INT_SRC2_AXI_WRONG_INT_MSK  (0x1 << ENT_INT_SRC2_AXI_WRONG_INT_OFF)
85 #define ENT_INT_SRC2_AXI_OVERLF_INT_OFF 29
86 #define ENT_INT_SRC2_AXI_OVERLF_INT_MSK (0x1 << ENT_INT_SRC2_AXI_OVERLF_INT_OFF)
87 #define ENT_INT_SRC_MSK1                0x1e0
88 #define ENT_INT_SRC_MSK2                0x1e4
89 #define SAS_ECC_INTR                    0x1e8
90 #define SAS_ECC_INTR_DQ_ECC1B_OFF       0
91 #define SAS_ECC_INTR_DQ_ECC1B_MSK       (0x1 << SAS_ECC_INTR_DQ_ECC1B_OFF)
92 #define SAS_ECC_INTR_DQ_ECCBAD_OFF      1
93 #define SAS_ECC_INTR_DQ_ECCBAD_MSK      (0x1 << SAS_ECC_INTR_DQ_ECCBAD_OFF)
94 #define SAS_ECC_INTR_IOST_ECC1B_OFF     2
95 #define SAS_ECC_INTR_IOST_ECC1B_MSK     (0x1 << SAS_ECC_INTR_IOST_ECC1B_OFF)
96 #define SAS_ECC_INTR_IOST_ECCBAD_OFF    3
97 #define SAS_ECC_INTR_IOST_ECCBAD_MSK    (0x1 << SAS_ECC_INTR_IOST_ECCBAD_OFF)
98 #define SAS_ECC_INTR_ITCT_ECC1B_OFF     4
99 #define SAS_ECC_INTR_ITCT_ECC1B_MSK     (0x1 << SAS_ECC_INTR_ITCT_ECC1B_OFF)
100 #define SAS_ECC_INTR_ITCT_ECCBAD_OFF    5
101 #define SAS_ECC_INTR_ITCT_ECCBAD_MSK    (0x1 << SAS_ECC_INTR_ITCT_ECCBAD_OFF)
102 #define SAS_ECC_INTR_MSK                0x1ec
103 #define HGC_ERR_STAT_EN                 0x238
104 #define DLVRY_Q_0_BASE_ADDR_LO          0x260
105 #define DLVRY_Q_0_BASE_ADDR_HI          0x264
106 #define DLVRY_Q_0_DEPTH                 0x268
107 #define DLVRY_Q_0_WR_PTR                0x26c
108 #define DLVRY_Q_0_RD_PTR                0x270
109 #define COMPL_Q_0_BASE_ADDR_LO          0x4e0
110 #define COMPL_Q_0_BASE_ADDR_HI          0x4e4
111 #define COMPL_Q_0_DEPTH                 0x4e8
112 #define COMPL_Q_0_WR_PTR                0x4ec
113 #define COMPL_Q_0_RD_PTR                0x4f0
114 #define HGC_ECC_ERR                     0x7d0
115
116 /* phy registers need init */
117 #define PORT_BASE                       (0x800)
118
119 #define PHY_CFG                         (PORT_BASE + 0x0)
120 #define PHY_CFG_ENA_OFF                 0
121 #define PHY_CFG_ENA_MSK                 (0x1 << PHY_CFG_ENA_OFF)
122 #define PHY_CFG_DC_OPT_OFF              2
123 #define PHY_CFG_DC_OPT_MSK              (0x1 << PHY_CFG_DC_OPT_OFF)
124 #define PROG_PHY_LINK_RATE              (PORT_BASE + 0xc)
125 #define PROG_PHY_LINK_RATE_MAX_OFF      0
126 #define PROG_PHY_LINK_RATE_MAX_MSK      (0xf << PROG_PHY_LINK_RATE_MAX_OFF)
127 #define PROG_PHY_LINK_RATE_MIN_OFF      4
128 #define PROG_PHY_LINK_RATE_MIN_MSK      (0xf << PROG_PHY_LINK_RATE_MIN_OFF)
129 #define PROG_PHY_LINK_RATE_OOB_OFF      8
130 #define PROG_PHY_LINK_RATE_OOB_MSK      (0xf << PROG_PHY_LINK_RATE_OOB_OFF)
131 #define PHY_CTRL                        (PORT_BASE + 0x14)
132 #define PHY_CTRL_RESET_OFF              0
133 #define PHY_CTRL_RESET_MSK              (0x1 << PHY_CTRL_RESET_OFF)
134 #define PHY_RATE_NEGO                   (PORT_BASE + 0x30)
135 #define PHY_PCN                         (PORT_BASE + 0x44)
136 #define SL_TOUT_CFG                     (PORT_BASE + 0x8c)
137 #define SL_CONTROL                      (PORT_BASE + 0x94)
138 #define SL_CONTROL_NOTIFY_EN_OFF        0
139 #define SL_CONTROL_NOTIFY_EN_MSK        (0x1 << SL_CONTROL_NOTIFY_EN_OFF)
140 #define TX_ID_DWORD0                    (PORT_BASE + 0x9c)
141 #define TX_ID_DWORD1                    (PORT_BASE + 0xa0)
142 #define TX_ID_DWORD2                    (PORT_BASE + 0xa4)
143 #define TX_ID_DWORD3                    (PORT_BASE + 0xa8)
144 #define TX_ID_DWORD4                    (PORT_BASE + 0xaC)
145 #define TX_ID_DWORD5                    (PORT_BASE + 0xb0)
146 #define TX_ID_DWORD6                    (PORT_BASE + 0xb4)
147 #define RX_IDAF_DWORD0                  (PORT_BASE + 0xc4)
148 #define RX_IDAF_DWORD1                  (PORT_BASE + 0xc8)
149 #define RX_IDAF_DWORD2                  (PORT_BASE + 0xcc)
150 #define RX_IDAF_DWORD3                  (PORT_BASE + 0xd0)
151 #define RX_IDAF_DWORD4                  (PORT_BASE + 0xd4)
152 #define RX_IDAF_DWORD5                  (PORT_BASE + 0xd8)
153 #define RX_IDAF_DWORD6                  (PORT_BASE + 0xdc)
154 #define RXOP_CHECK_CFG_H                (PORT_BASE + 0xfc)
155 #define DONE_RECEIVED_TIME              (PORT_BASE + 0x12c)
156 #define CON_CFG_DRIVER                  (PORT_BASE + 0x130)
157 #define PHY_CONFIG2                     (PORT_BASE + 0x1a8)
158 #define PHY_CONFIG2_FORCE_TXDEEMPH_OFF  3
159 #define PHY_CONFIG2_FORCE_TXDEEMPH_MSK  (0x1 << PHY_CONFIG2_FORCE_TXDEEMPH_OFF)
160 #define PHY_CONFIG2_TX_TRAIN_COMP_OFF   24
161 #define PHY_CONFIG2_TX_TRAIN_COMP_MSK   (0x1 << PHY_CONFIG2_TX_TRAIN_COMP_OFF)
162 #define CHL_INT0                        (PORT_BASE + 0x1b0)
163 #define CHL_INT0_PHYCTRL_NOTRDY_OFF     0
164 #define CHL_INT0_PHYCTRL_NOTRDY_MSK     (0x1 << CHL_INT0_PHYCTRL_NOTRDY_OFF)
165 #define CHL_INT0_SN_FAIL_NGR_OFF        2
166 #define CHL_INT0_SN_FAIL_NGR_MSK        (0x1 << CHL_INT0_SN_FAIL_NGR_OFF)
167 #define CHL_INT0_DWS_LOST_OFF           4
168 #define CHL_INT0_DWS_LOST_MSK           (0x1 << CHL_INT0_DWS_LOST_OFF)
169 #define CHL_INT0_SL_IDAF_FAIL_OFF       10
170 #define CHL_INT0_SL_IDAF_FAIL_MSK       (0x1 << CHL_INT0_SL_IDAF_FAIL_OFF)
171 #define CHL_INT0_ID_TIMEOUT_OFF         11
172 #define CHL_INT0_ID_TIMEOUT_MSK         (0x1 << CHL_INT0_ID_TIMEOUT_OFF)
173 #define CHL_INT0_SL_OPAF_FAIL_OFF       12
174 #define CHL_INT0_SL_OPAF_FAIL_MSK       (0x1 << CHL_INT0_SL_OPAF_FAIL_OFF)
175 #define CHL_INT0_SL_PS_FAIL_OFF         21
176 #define CHL_INT0_SL_PS_FAIL_MSK         (0x1 << CHL_INT0_SL_PS_FAIL_OFF)
177 #define CHL_INT1                        (PORT_BASE + 0x1b4)
178 #define CHL_INT2                        (PORT_BASE + 0x1b8)
179 #define CHL_INT2_SL_RX_BC_ACK_OFF       2
180 #define CHL_INT2_SL_RX_BC_ACK_MSK       (0x1 << CHL_INT2_SL_RX_BC_ACK_OFF)
181 #define CHL_INT2_SL_PHY_ENA_OFF         6
182 #define CHL_INT2_SL_PHY_ENA_MSK         (0x1 << CHL_INT2_SL_PHY_ENA_OFF)
183 #define CHL_INT0_MSK                    (PORT_BASE + 0x1bc)
184 #define CHL_INT0_MSK_PHYCTRL_NOTRDY_OFF 0
185 #define CHL_INT0_MSK_PHYCTRL_NOTRDY_MSK (0x1 << CHL_INT0_MSK_PHYCTRL_NOTRDY_OFF)
186 #define CHL_INT1_MSK                    (PORT_BASE + 0x1c0)
187 #define CHL_INT2_MSK                    (PORT_BASE + 0x1c4)
188 #define CHL_INT_COAL_EN                 (PORT_BASE + 0x1d0)
189 #define DMA_TX_STATUS                   (PORT_BASE + 0x2d0)
190 #define DMA_TX_STATUS_BUSY_OFF          0
191 #define DMA_TX_STATUS_BUSY_MSK          (0x1 << DMA_TX_STATUS_BUSY_OFF)
192 #define DMA_RX_STATUS                   (PORT_BASE + 0x2e8)
193 #define DMA_RX_STATUS_BUSY_OFF          0
194 #define DMA_RX_STATUS_BUSY_MSK          (0x1 << DMA_RX_STATUS_BUSY_OFF)
195
196 #define AXI_CFG                         0x5100
197 #define RESET_VALUE                     0x7ffff
198
199 /* HW dma structures */
200 /* Delivery queue header */
201 /* dw0 */
202 #define CMD_HDR_RESP_REPORT_OFF         5
203 #define CMD_HDR_RESP_REPORT_MSK         0x20
204 #define CMD_HDR_TLR_CTRL_OFF            6
205 #define CMD_HDR_TLR_CTRL_MSK            0xc0
206 #define CMD_HDR_PORT_OFF                17
207 #define CMD_HDR_PORT_MSK                0xe0000
208 #define CMD_HDR_PRIORITY_OFF            27
209 #define CMD_HDR_PRIORITY_MSK            0x8000000
210 #define CMD_HDR_MODE_OFF                28
211 #define CMD_HDR_MODE_MSK                0x10000000
212 #define CMD_HDR_CMD_OFF                 29
213 #define CMD_HDR_CMD_MSK                 0xe0000000
214 /* dw1 */
215 #define CMD_HDR_VERIFY_DTL_OFF          10
216 #define CMD_HDR_VERIFY_DTL_MSK          0x400
217 #define CMD_HDR_SSP_FRAME_TYPE_OFF      13
218 #define CMD_HDR_SSP_FRAME_TYPE_MSK      0xe000
219 #define CMD_HDR_DEVICE_ID_OFF           16
220 #define CMD_HDR_DEVICE_ID_MSK           0xffff0000
221 /* dw2 */
222 #define CMD_HDR_CFL_OFF                 0
223 #define CMD_HDR_CFL_MSK                 0x1ff
224 #define CMD_HDR_MRFL_OFF                15
225 #define CMD_HDR_MRFL_MSK                0xff8000
226 #define CMD_HDR_FIRST_BURST_OFF         25
227 #define CMD_HDR_FIRST_BURST_MSK         0x2000000
228 /* dw3 */
229 #define CMD_HDR_IPTT_OFF                0
230 #define CMD_HDR_IPTT_MSK                0xffff
231 /* dw6 */
232 #define CMD_HDR_DATA_SGL_LEN_OFF        16
233 #define CMD_HDR_DATA_SGL_LEN_MSK        0xffff0000
234
235 /* Completion header */
236 #define CMPLT_HDR_IPTT_OFF              0
237 #define CMPLT_HDR_IPTT_MSK              (0xffff << CMPLT_HDR_IPTT_OFF)
238 #define CMPLT_HDR_CMD_CMPLT_OFF         17
239 #define CMPLT_HDR_CMD_CMPLT_MSK         (0x1 << CMPLT_HDR_CMD_CMPLT_OFF)
240 #define CMPLT_HDR_ERR_RCRD_XFRD_OFF     18
241 #define CMPLT_HDR_ERR_RCRD_XFRD_MSK     (0x1 << CMPLT_HDR_ERR_RCRD_XFRD_OFF)
242 #define CMPLT_HDR_RSPNS_XFRD_OFF        19
243 #define CMPLT_HDR_RSPNS_XFRD_MSK        (0x1 << CMPLT_HDR_RSPNS_XFRD_OFF)
244 #define CMPLT_HDR_IO_CFG_ERR_OFF        27
245 #define CMPLT_HDR_IO_CFG_ERR_MSK        (0x1 << CMPLT_HDR_IO_CFG_ERR_OFF)
246
247 /* ITCT header */
248 /* qw0 */
249 #define ITCT_HDR_DEV_TYPE_OFF           0
250 #define ITCT_HDR_DEV_TYPE_MSK           (0x3ULL << ITCT_HDR_DEV_TYPE_OFF)
251 #define ITCT_HDR_VALID_OFF              2
252 #define ITCT_HDR_VALID_MSK              (0x1ULL << ITCT_HDR_VALID_OFF)
253 #define ITCT_HDR_AWT_CONTROL_OFF        4
254 #define ITCT_HDR_AWT_CONTROL_MSK        (0x1ULL << ITCT_HDR_AWT_CONTROL_OFF)
255 #define ITCT_HDR_MAX_CONN_RATE_OFF      5
256 #define ITCT_HDR_MAX_CONN_RATE_MSK      (0xfULL << ITCT_HDR_MAX_CONN_RATE_OFF)
257 #define ITCT_HDR_VALID_LINK_NUM_OFF     9
258 #define ITCT_HDR_VALID_LINK_NUM_MSK     (0xfULL << ITCT_HDR_VALID_LINK_NUM_OFF)
259 #define ITCT_HDR_PORT_ID_OFF            13
260 #define ITCT_HDR_PORT_ID_MSK            (0x7ULL << ITCT_HDR_PORT_ID_OFF)
261 #define ITCT_HDR_SMP_TIMEOUT_OFF        16
262 #define ITCT_HDR_SMP_TIMEOUT_MSK        (0xffffULL << ITCT_HDR_SMP_TIMEOUT_OFF)
263 /* qw1 */
264 #define ITCT_HDR_MAX_SAS_ADDR_OFF       0
265 #define ITCT_HDR_MAX_SAS_ADDR_MSK       (0xffffffffffffffff << \
266                                         ITCT_HDR_MAX_SAS_ADDR_OFF)
267 /* qw2 */
268 #define ITCT_HDR_IT_NEXUS_LOSS_TL_OFF   0
269 #define ITCT_HDR_IT_NEXUS_LOSS_TL_MSK   (0xffffULL << \
270                                         ITCT_HDR_IT_NEXUS_LOSS_TL_OFF)
271 #define ITCT_HDR_BUS_INACTIVE_TL_OFF    16
272 #define ITCT_HDR_BUS_INACTIVE_TL_MSK    (0xffffULL << \
273                                         ITCT_HDR_BUS_INACTIVE_TL_OFF)
274 #define ITCT_HDR_MAX_CONN_TL_OFF        32
275 #define ITCT_HDR_MAX_CONN_TL_MSK        (0xffffULL << \
276                                         ITCT_HDR_MAX_CONN_TL_OFF)
277 #define ITCT_HDR_REJ_OPEN_TL_OFF        48
278 #define ITCT_HDR_REJ_OPEN_TL_MSK        (0xffffULL << \
279                                         ITCT_HDR_REJ_OPEN_TL_OFF)
280
281 /* Err record header */
282 #define ERR_HDR_DMA_TX_ERR_TYPE_OFF     0
283 #define ERR_HDR_DMA_TX_ERR_TYPE_MSK     (0xffff << ERR_HDR_DMA_TX_ERR_TYPE_OFF)
284 #define ERR_HDR_DMA_RX_ERR_TYPE_OFF     16
285 #define ERR_HDR_DMA_RX_ERR_TYPE_MSK     (0xffff << ERR_HDR_DMA_RX_ERR_TYPE_OFF)
286
287 struct hisi_sas_complete_v1_hdr {
288         __le32 data;
289 };
290
291 struct hisi_sas_err_record_v1 {
292         /* dw0 */
293         __le32 dma_err_type;
294
295         /* dw1 */
296         __le32 trans_tx_fail_type;
297
298         /* dw2 */
299         __le32 trans_rx_fail_type;
300
301         /* dw3 */
302         u32 rsvd;
303 };
304
305 enum {
306         HISI_SAS_PHY_BCAST_ACK = 0,
307         HISI_SAS_PHY_SL_PHY_ENABLED,
308         HISI_SAS_PHY_INT_ABNORMAL,
309         HISI_SAS_PHY_INT_NR
310 };
311
312 enum {
313         DMA_TX_ERR_BASE = 0x0,
314         DMA_RX_ERR_BASE = 0x100,
315         TRANS_TX_FAIL_BASE = 0x200,
316         TRANS_RX_FAIL_BASE = 0x300,
317
318         /* dma tx */
319         DMA_TX_DIF_CRC_ERR = DMA_TX_ERR_BASE, /* 0x0 */
320         DMA_TX_DIF_APP_ERR, /* 0x1 */
321         DMA_TX_DIF_RPP_ERR, /* 0x2 */
322         DMA_TX_AXI_BUS_ERR, /* 0x3 */
323         DMA_TX_DATA_SGL_OVERFLOW_ERR, /* 0x4 */
324         DMA_TX_DIF_SGL_OVERFLOW_ERR, /* 0x5 */
325         DMA_TX_UNEXP_XFER_RDY_ERR, /* 0x6 */
326         DMA_TX_XFER_RDY_OFFSET_ERR, /* 0x7 */
327         DMA_TX_DATA_UNDERFLOW_ERR, /* 0x8 */
328         DMA_TX_XFER_RDY_LENGTH_OVERFLOW_ERR, /* 0x9 */
329
330         /* dma rx */
331         DMA_RX_BUFFER_ECC_ERR = DMA_RX_ERR_BASE, /* 0x100 */
332         DMA_RX_DIF_CRC_ERR, /* 0x101 */
333         DMA_RX_DIF_APP_ERR, /* 0x102 */
334         DMA_RX_DIF_RPP_ERR, /* 0x103 */
335         DMA_RX_RESP_BUFFER_OVERFLOW_ERR, /* 0x104 */
336         DMA_RX_AXI_BUS_ERR, /* 0x105 */
337         DMA_RX_DATA_SGL_OVERFLOW_ERR, /* 0x106 */
338         DMA_RX_DIF_SGL_OVERFLOW_ERR, /* 0x107 */
339         DMA_RX_DATA_OFFSET_ERR, /* 0x108 */
340         DMA_RX_UNEXP_RX_DATA_ERR, /* 0x109 */
341         DMA_RX_DATA_OVERFLOW_ERR, /* 0x10a */
342         DMA_RX_DATA_UNDERFLOW_ERR, /* 0x10b */
343         DMA_RX_UNEXP_RETRANS_RESP_ERR, /* 0x10c */
344
345         /* trans tx */
346         TRANS_TX_RSVD0_ERR = TRANS_TX_FAIL_BASE, /* 0x200 */
347         TRANS_TX_PHY_NOT_ENABLE_ERR, /* 0x201 */
348         TRANS_TX_OPEN_REJCT_WRONG_DEST_ERR, /* 0x202 */
349         TRANS_TX_OPEN_REJCT_ZONE_VIOLATION_ERR, /* 0x203 */
350         TRANS_TX_OPEN_REJCT_BY_OTHER_ERR, /* 0x204 */
351         TRANS_TX_RSVD1_ERR, /* 0x205 */
352         TRANS_TX_OPEN_REJCT_AIP_TIMEOUT_ERR, /* 0x206 */
353         TRANS_TX_OPEN_REJCT_STP_BUSY_ERR, /* 0x207 */
354         TRANS_TX_OPEN_REJCT_PROTOCOL_NOT_SUPPORT_ERR, /* 0x208 */
355         TRANS_TX_OPEN_REJCT_RATE_NOT_SUPPORT_ERR, /* 0x209 */
356         TRANS_TX_OPEN_REJCT_BAD_DEST_ERR, /* 0x20a */
357         TRANS_TX_OPEN_BREAK_RECEIVE_ERR, /* 0x20b */
358         TRANS_TX_LOW_PHY_POWER_ERR, /* 0x20c */
359         TRANS_TX_OPEN_REJCT_PATHWAY_BLOCKED_ERR, /* 0x20d */
360         TRANS_TX_OPEN_TIMEOUT_ERR, /* 0x20e */
361         TRANS_TX_OPEN_REJCT_NO_DEST_ERR, /* 0x20f */
362         TRANS_TX_OPEN_RETRY_ERR, /* 0x210 */
363         TRANS_TX_RSVD2_ERR, /* 0x211 */
364         TRANS_TX_BREAK_TIMEOUT_ERR, /* 0x212 */
365         TRANS_TX_BREAK_REQUEST_ERR, /* 0x213 */
366         TRANS_TX_BREAK_RECEIVE_ERR, /* 0x214 */
367         TRANS_TX_CLOSE_TIMEOUT_ERR, /* 0x215 */
368         TRANS_TX_CLOSE_NORMAL_ERR, /* 0x216 */
369         TRANS_TX_CLOSE_PHYRESET_ERR, /* 0x217 */
370         TRANS_TX_WITH_CLOSE_DWS_TIMEOUT_ERR, /* 0x218 */
371         TRANS_TX_WITH_CLOSE_COMINIT_ERR, /* 0x219 */
372         TRANS_TX_NAK_RECEIVE_ERR, /* 0x21a */
373         TRANS_TX_ACK_NAK_TIMEOUT_ERR, /* 0x21b */
374         TRANS_TX_CREDIT_TIMEOUT_ERR, /* 0x21c */
375         TRANS_TX_IPTT_CONFLICT_ERR, /* 0x21d */
376         TRANS_TX_TXFRM_TYPE_ERR, /* 0x21e */
377         TRANS_TX_TXSMP_LENGTH_ERR, /* 0x21f */
378
379         /* trans rx */
380         TRANS_RX_FRAME_CRC_ERR = TRANS_RX_FAIL_BASE, /* 0x300 */
381         TRANS_RX_FRAME_DONE_ERR, /* 0x301 */
382         TRANS_RX_FRAME_ERRPRM_ERR, /* 0x302 */
383         TRANS_RX_FRAME_NO_CREDIT_ERR, /* 0x303 */
384         TRANS_RX_RSVD0_ERR, /* 0x304 */
385         TRANS_RX_FRAME_OVERRUN_ERR, /* 0x305 */
386         TRANS_RX_FRAME_NO_EOF_ERR, /* 0x306 */
387         TRANS_RX_LINK_BUF_OVERRUN_ERR, /* 0x307 */
388         TRANS_RX_BREAK_TIMEOUT_ERR, /* 0x308 */
389         TRANS_RX_BREAK_REQUEST_ERR, /* 0x309 */
390         TRANS_RX_BREAK_RECEIVE_ERR, /* 0x30a */
391         TRANS_RX_CLOSE_TIMEOUT_ERR, /* 0x30b */
392         TRANS_RX_CLOSE_NORMAL_ERR, /* 0x30c */
393         TRANS_RX_CLOSE_PHYRESET_ERR, /* 0x30d */
394         TRANS_RX_WITH_CLOSE_DWS_TIMEOUT_ERR, /* 0x30e */
395         TRANS_RX_WITH_CLOSE_COMINIT_ERR, /* 0x30f */
396         TRANS_RX_DATA_LENGTH0_ERR, /* 0x310 */
397         TRANS_RX_BAD_HASH_ERR, /* 0x311 */
398         TRANS_RX_XRDY_ZERO_ERR, /* 0x312 */
399         TRANS_RX_SSP_FRAME_LEN_ERR, /* 0x313 */
400         TRANS_RX_TRANS_RX_RSVD1_ERR, /* 0x314 */
401         TRANS_RX_NO_BALANCE_ERR, /* 0x315 */
402         TRANS_RX_TRANS_RX_RSVD2_ERR, /* 0x316 */
403         TRANS_RX_TRANS_RX_RSVD3_ERR, /* 0x317 */
404         TRANS_RX_BAD_FRAME_TYPE_ERR, /* 0x318 */
405         TRANS_RX_SMP_FRAME_LEN_ERR, /* 0x319 */
406         TRANS_RX_SMP_RESP_TIMEOUT_ERR, /* 0x31a */
407 };
408
409 #define HISI_SAS_COMMAND_ENTRIES_V1_HW 8192
410
411 #define HISI_SAS_PHY_MAX_INT_NR (HISI_SAS_PHY_INT_NR * HISI_SAS_MAX_PHYS)
412 #define HISI_SAS_CQ_MAX_INT_NR (HISI_SAS_MAX_QUEUES)
413 #define HISI_SAS_FATAL_INT_NR (2)
414
415 #define HISI_SAS_MAX_INT_NR \
416         (HISI_SAS_PHY_MAX_INT_NR + HISI_SAS_CQ_MAX_INT_NR +\
417         HISI_SAS_FATAL_INT_NR)
418
419 static u32 hisi_sas_read32(struct hisi_hba *hisi_hba, u32 off)
420 {
421         void __iomem *regs = hisi_hba->regs + off;
422
423         return readl(regs);
424 }
425
426 static u32 hisi_sas_read32_relaxed(struct hisi_hba *hisi_hba, u32 off)
427 {
428         void __iomem *regs = hisi_hba->regs + off;
429
430         return readl_relaxed(regs);
431 }
432
433 static void hisi_sas_write32(struct hisi_hba *hisi_hba,
434                                     u32 off, u32 val)
435 {
436         void __iomem *regs = hisi_hba->regs + off;
437
438         writel(val, regs);
439 }
440
441 static void hisi_sas_phy_write32(struct hisi_hba *hisi_hba,
442                                         int phy_no, u32 off, u32 val)
443 {
444         void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
445
446         writel(val, regs);
447 }
448
449 static u32 hisi_sas_phy_read32(struct hisi_hba *hisi_hba,
450                                       int phy_no, u32 off)
451 {
452         void __iomem *regs = hisi_hba->regs + (0x400 * phy_no) + off;
453
454         return readl(regs);
455 }
456
457 static void config_phy_opt_mode_v1_hw(struct hisi_hba *hisi_hba, int phy_no)
458 {
459         u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
460
461         cfg &= ~PHY_CFG_DC_OPT_MSK;
462         cfg |= 1 << PHY_CFG_DC_OPT_OFF;
463         hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
464 }
465
466 static void config_tx_tfe_autoneg_v1_hw(struct hisi_hba *hisi_hba, int phy_no)
467 {
468         u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CONFIG2);
469
470         cfg &= ~PHY_CONFIG2_FORCE_TXDEEMPH_MSK;
471         hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CONFIG2, cfg);
472 }
473
474 static void config_id_frame_v1_hw(struct hisi_hba *hisi_hba, int phy_no)
475 {
476         struct sas_identify_frame identify_frame;
477         u32 *identify_buffer;
478
479         memset(&identify_frame, 0, sizeof(identify_frame));
480         identify_frame.dev_type = SAS_END_DEVICE;
481         identify_frame.frame_type = 0;
482         identify_frame._un1 = 1;
483         identify_frame.initiator_bits = SAS_PROTOCOL_ALL;
484         identify_frame.target_bits = SAS_PROTOCOL_NONE;
485         memcpy(&identify_frame._un4_11[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
486         memcpy(&identify_frame.sas_addr[0], hisi_hba->sas_addr, SAS_ADDR_SIZE);
487         identify_frame.phy_id = phy_no;
488         identify_buffer = (u32 *)(&identify_frame);
489
490         hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD0,
491                         __swab32(identify_buffer[0]));
492         hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD1,
493                         __swab32(identify_buffer[1]));
494         hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD2,
495                         __swab32(identify_buffer[2]));
496         hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD3,
497                         __swab32(identify_buffer[3]));
498         hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD4,
499                         __swab32(identify_buffer[4]));
500         hisi_sas_phy_write32(hisi_hba, phy_no, TX_ID_DWORD5,
501                         __swab32(identify_buffer[5]));
502 }
503
504 static void setup_itct_v1_hw(struct hisi_hba *hisi_hba,
505                              struct hisi_sas_device *sas_dev)
506 {
507         struct domain_device *device = sas_dev->sas_device;
508         struct device *dev = hisi_hba->dev;
509         u64 qw0, device_id = sas_dev->device_id;
510         struct hisi_sas_itct *itct = &hisi_hba->itct[device_id];
511         struct asd_sas_port *sas_port = device->port;
512         struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
513
514         memset(itct, 0, sizeof(*itct));
515
516         /* qw0 */
517         qw0 = 0;
518         switch (sas_dev->dev_type) {
519         case SAS_END_DEVICE:
520         case SAS_EDGE_EXPANDER_DEVICE:
521         case SAS_FANOUT_EXPANDER_DEVICE:
522                 qw0 = HISI_SAS_DEV_TYPE_SSP << ITCT_HDR_DEV_TYPE_OFF;
523                 break;
524         default:
525                 dev_warn(dev, "setup itct: unsupported dev type (%d)\n",
526                          sas_dev->dev_type);
527         }
528
529         qw0 |= ((1 << ITCT_HDR_VALID_OFF) |
530                 (1 << ITCT_HDR_AWT_CONTROL_OFF) |
531                 (device->max_linkrate << ITCT_HDR_MAX_CONN_RATE_OFF) |
532                 (1 << ITCT_HDR_VALID_LINK_NUM_OFF) |
533                 (port->id << ITCT_HDR_PORT_ID_OFF));
534         itct->qw0 = cpu_to_le64(qw0);
535
536         /* qw1 */
537         memcpy(&itct->sas_addr, device->sas_addr, SAS_ADDR_SIZE);
538         itct->sas_addr = __swab64(itct->sas_addr);
539
540         /* qw2 */
541         itct->qw2 = cpu_to_le64((500ULL << ITCT_HDR_IT_NEXUS_LOSS_TL_OFF) |
542                                 (0xff00ULL << ITCT_HDR_BUS_INACTIVE_TL_OFF) |
543                                 (0xff00ULL << ITCT_HDR_MAX_CONN_TL_OFF) |
544                                 (0xff00ULL << ITCT_HDR_REJ_OPEN_TL_OFF));
545 }
546
547 static void clear_itct_v1_hw(struct hisi_hba *hisi_hba,
548                               struct hisi_sas_device *sas_dev)
549 {
550         u64 dev_id = sas_dev->device_id;
551         struct hisi_sas_itct *itct = &hisi_hba->itct[dev_id];
552         u64 qw0;
553         u32 reg_val = hisi_sas_read32(hisi_hba, CFG_AGING_TIME);
554
555         reg_val |= CFG_AGING_TIME_ITCT_REL_MSK;
556         hisi_sas_write32(hisi_hba, CFG_AGING_TIME, reg_val);
557
558         /* free itct */
559         udelay(1);
560         reg_val = hisi_sas_read32(hisi_hba, CFG_AGING_TIME);
561         reg_val &= ~CFG_AGING_TIME_ITCT_REL_MSK;
562         hisi_sas_write32(hisi_hba, CFG_AGING_TIME, reg_val);
563
564         qw0 = cpu_to_le64(itct->qw0);
565         qw0 &= ~ITCT_HDR_VALID_MSK;
566         itct->qw0 = cpu_to_le64(qw0);
567 }
568
569 static int reset_hw_v1_hw(struct hisi_hba *hisi_hba)
570 {
571         int i;
572         unsigned long end_time;
573         u32 val;
574         struct device *dev = hisi_hba->dev;
575
576         for (i = 0; i < hisi_hba->n_phy; i++) {
577                 u32 phy_ctrl = hisi_sas_phy_read32(hisi_hba, i, PHY_CTRL);
578
579                 phy_ctrl |= PHY_CTRL_RESET_MSK;
580                 hisi_sas_phy_write32(hisi_hba, i, PHY_CTRL, phy_ctrl);
581         }
582         msleep(1); /* It is safe to wait for 50us */
583
584         /* Ensure DMA tx & rx idle */
585         for (i = 0; i < hisi_hba->n_phy; i++) {
586                 u32 dma_tx_status, dma_rx_status;
587
588                 end_time = jiffies + msecs_to_jiffies(1000);
589
590                 while (1) {
591                         dma_tx_status = hisi_sas_phy_read32(hisi_hba, i,
592                                                             DMA_TX_STATUS);
593                         dma_rx_status = hisi_sas_phy_read32(hisi_hba, i,
594                                                             DMA_RX_STATUS);
595
596                         if (!(dma_tx_status & DMA_TX_STATUS_BUSY_MSK) &&
597                                 !(dma_rx_status & DMA_RX_STATUS_BUSY_MSK))
598                                 break;
599
600                         msleep(20);
601                         if (time_after(jiffies, end_time))
602                                 return -EIO;
603                 }
604         }
605
606         /* Ensure axi bus idle */
607         end_time = jiffies + msecs_to_jiffies(1000);
608         while (1) {
609                 u32 axi_status =
610                         hisi_sas_read32(hisi_hba, AXI_CFG);
611
612                 if (axi_status == 0)
613                         break;
614
615                 msleep(20);
616                 if (time_after(jiffies, end_time))
617                         return -EIO;
618         }
619
620         if (ACPI_HANDLE(dev)) {
621                 acpi_status s;
622
623                 s = acpi_evaluate_object(ACPI_HANDLE(dev), "_RST", NULL, NULL);
624                 if (ACPI_FAILURE(s)) {
625                         dev_err(dev, "Reset failed\n");
626                         return -EIO;
627                 }
628         } else if (hisi_hba->ctrl) {
629                 /* Apply reset and disable clock */
630                 /* clk disable reg is offset by +4 bytes from clk enable reg */
631                 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg,
632                              RESET_VALUE);
633                 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg + 4,
634                              RESET_VALUE);
635                 msleep(1);
636                 regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg, &val);
637                 if (RESET_VALUE != (val & RESET_VALUE)) {
638                         dev_err(dev, "Reset failed\n");
639                         return -EIO;
640                 }
641
642                 /* De-reset and enable clock */
643                 /* deassert rst reg is offset by +4 bytes from assert reg */
644                 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_reset_reg + 4,
645                              RESET_VALUE);
646                 regmap_write(hisi_hba->ctrl, hisi_hba->ctrl_clock_ena_reg,
647                              RESET_VALUE);
648                 msleep(1);
649                 regmap_read(hisi_hba->ctrl, hisi_hba->ctrl_reset_sts_reg, &val);
650                 if (val & RESET_VALUE) {
651                         dev_err(dev, "De-reset failed\n");
652                         return -EIO;
653                 }
654         } else {
655                 dev_warn(dev, "no reset method\n");
656                 return -EINVAL;
657         }
658
659         return 0;
660 }
661
662 static void init_reg_v1_hw(struct hisi_hba *hisi_hba)
663 {
664         int i;
665
666         /* Global registers init*/
667         hisi_sas_write32(hisi_hba, DLVRY_QUEUE_ENABLE,
668                          (u32)((1ULL << hisi_hba->queue_count) - 1));
669         hisi_sas_write32(hisi_hba, HGC_TRANS_TASK_CNT_LIMIT, 0x11);
670         hisi_sas_write32(hisi_hba, DEVICE_MSG_WORK_MODE, 0x1);
671         hisi_sas_write32(hisi_hba, HGC_SAS_TXFAIL_RETRY_CTRL, 0x1ff);
672         hisi_sas_write32(hisi_hba, HGC_ERR_STAT_EN, 0x401);
673         hisi_sas_write32(hisi_hba, CFG_1US_TIMER_TRSH, 0x64);
674         hisi_sas_write32(hisi_hba, HGC_GET_ITV_TIME, 0x1);
675         hisi_sas_write32(hisi_hba, I_T_NEXUS_LOSS_TIME, 0x64);
676         hisi_sas_write32(hisi_hba, BUS_INACTIVE_LIMIT_TIME, 0x2710);
677         hisi_sas_write32(hisi_hba, REJECT_TO_OPEN_LIMIT_TIME, 0x1);
678         hisi_sas_write32(hisi_hba, CFG_AGING_TIME, 0x7a12);
679         hisi_sas_write32(hisi_hba, HGC_DFX_CFG2, 0x9c40);
680         hisi_sas_write32(hisi_hba, FIS_LIST_BADDR_L, 0x2);
681         hisi_sas_write32(hisi_hba, INT_COAL_EN, 0xc);
682         hisi_sas_write32(hisi_hba, OQ_INT_COAL_TIME, 0x186a0);
683         hisi_sas_write32(hisi_hba, OQ_INT_COAL_CNT, 1);
684         hisi_sas_write32(hisi_hba, ENT_INT_COAL_TIME, 0x1);
685         hisi_sas_write32(hisi_hba, ENT_INT_COAL_CNT, 0x1);
686         hisi_sas_write32(hisi_hba, OQ_INT_SRC, 0xffffffff);
687         hisi_sas_write32(hisi_hba, OQ_INT_SRC_MSK, 0);
688         hisi_sas_write32(hisi_hba, ENT_INT_SRC1, 0xffffffff);
689         hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK1, 0);
690         hisi_sas_write32(hisi_hba, ENT_INT_SRC2, 0xffffffff);
691         hisi_sas_write32(hisi_hba, ENT_INT_SRC_MSK2, 0);
692         hisi_sas_write32(hisi_hba, SAS_ECC_INTR_MSK, 0);
693         hisi_sas_write32(hisi_hba, AXI_AHB_CLK_CFG, 0x2);
694         hisi_sas_write32(hisi_hba, CFG_SAS_CONFIG, 0x22000000);
695
696         for (i = 0; i < hisi_hba->n_phy; i++) {
697                 hisi_sas_phy_write32(hisi_hba, i, PROG_PHY_LINK_RATE, 0x88a);
698                 hisi_sas_phy_write32(hisi_hba, i, PHY_CONFIG2, 0x7c080);
699                 hisi_sas_phy_write32(hisi_hba, i, PHY_RATE_NEGO, 0x415ee00);
700                 hisi_sas_phy_write32(hisi_hba, i, PHY_PCN, 0x80a80000);
701                 hisi_sas_phy_write32(hisi_hba, i, SL_TOUT_CFG, 0x7d7d7d7d);
702                 hisi_sas_phy_write32(hisi_hba, i, DONE_RECEIVED_TIME, 0x0);
703                 hisi_sas_phy_write32(hisi_hba, i, RXOP_CHECK_CFG_H, 0x1000);
704                 hisi_sas_phy_write32(hisi_hba, i, DONE_RECEIVED_TIME, 0);
705                 hisi_sas_phy_write32(hisi_hba, i, CON_CFG_DRIVER, 0x13f0a);
706                 hisi_sas_phy_write32(hisi_hba, i, CHL_INT_COAL_EN, 3);
707                 hisi_sas_phy_write32(hisi_hba, i, DONE_RECEIVED_TIME, 8);
708         }
709
710         for (i = 0; i < hisi_hba->queue_count; i++) {
711                 /* Delivery queue */
712                 hisi_sas_write32(hisi_hba,
713                                  DLVRY_Q_0_BASE_ADDR_HI + (i * 0x14),
714                                  upper_32_bits(hisi_hba->cmd_hdr_dma[i]));
715
716                 hisi_sas_write32(hisi_hba,
717                                  DLVRY_Q_0_BASE_ADDR_LO + (i * 0x14),
718                                  lower_32_bits(hisi_hba->cmd_hdr_dma[i]));
719
720                 hisi_sas_write32(hisi_hba,
721                                  DLVRY_Q_0_DEPTH + (i * 0x14),
722                                  HISI_SAS_QUEUE_SLOTS);
723
724                 /* Completion queue */
725                 hisi_sas_write32(hisi_hba,
726                                  COMPL_Q_0_BASE_ADDR_HI + (i * 0x14),
727                                  upper_32_bits(hisi_hba->complete_hdr_dma[i]));
728
729                 hisi_sas_write32(hisi_hba,
730                                  COMPL_Q_0_BASE_ADDR_LO + (i * 0x14),
731                                  lower_32_bits(hisi_hba->complete_hdr_dma[i]));
732
733                 hisi_sas_write32(hisi_hba, COMPL_Q_0_DEPTH + (i * 0x14),
734                                  HISI_SAS_QUEUE_SLOTS);
735         }
736
737         /* itct */
738         hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_LO,
739                          lower_32_bits(hisi_hba->itct_dma));
740
741         hisi_sas_write32(hisi_hba, ITCT_BASE_ADDR_HI,
742                          upper_32_bits(hisi_hba->itct_dma));
743
744         /* iost */
745         hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_LO,
746                          lower_32_bits(hisi_hba->iost_dma));
747
748         hisi_sas_write32(hisi_hba, IOST_BASE_ADDR_HI,
749                          upper_32_bits(hisi_hba->iost_dma));
750
751         /* breakpoint */
752         hisi_sas_write32(hisi_hba, BROKEN_MSG_ADDR_LO,
753                          lower_32_bits(hisi_hba->breakpoint_dma));
754
755         hisi_sas_write32(hisi_hba, BROKEN_MSG_ADDR_HI,
756                          upper_32_bits(hisi_hba->breakpoint_dma));
757 }
758
759 static int hw_init_v1_hw(struct hisi_hba *hisi_hba)
760 {
761         struct device *dev = hisi_hba->dev;
762         int rc;
763
764         rc = reset_hw_v1_hw(hisi_hba);
765         if (rc) {
766                 dev_err(dev, "hisi_sas_reset_hw failed, rc=%d", rc);
767                 return rc;
768         }
769
770         msleep(100);
771         init_reg_v1_hw(hisi_hba);
772
773         return 0;
774 }
775
776 static void enable_phy_v1_hw(struct hisi_hba *hisi_hba, int phy_no)
777 {
778         u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
779
780         cfg |= PHY_CFG_ENA_MSK;
781         hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
782 }
783
784 static void disable_phy_v1_hw(struct hisi_hba *hisi_hba, int phy_no)
785 {
786         u32 cfg = hisi_sas_phy_read32(hisi_hba, phy_no, PHY_CFG);
787
788         cfg &= ~PHY_CFG_ENA_MSK;
789         hisi_sas_phy_write32(hisi_hba, phy_no, PHY_CFG, cfg);
790 }
791
792 static void start_phy_v1_hw(struct hisi_hba *hisi_hba, int phy_no)
793 {
794         config_id_frame_v1_hw(hisi_hba, phy_no);
795         config_phy_opt_mode_v1_hw(hisi_hba, phy_no);
796         config_tx_tfe_autoneg_v1_hw(hisi_hba, phy_no);
797         enable_phy_v1_hw(hisi_hba, phy_no);
798 }
799
800 static void stop_phy_v1_hw(struct hisi_hba *hisi_hba, int phy_no)
801 {
802         disable_phy_v1_hw(hisi_hba, phy_no);
803 }
804
805 static void phy_hard_reset_v1_hw(struct hisi_hba *hisi_hba, int phy_no)
806 {
807         stop_phy_v1_hw(hisi_hba, phy_no);
808         msleep(100);
809         start_phy_v1_hw(hisi_hba, phy_no);
810 }
811
812 static void start_phys_v1_hw(struct timer_list *t)
813 {
814         struct hisi_hba *hisi_hba = from_timer(hisi_hba, t, timer);
815         int i;
816
817         for (i = 0; i < hisi_hba->n_phy; i++) {
818                 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0x12a);
819                 start_phy_v1_hw(hisi_hba, i);
820         }
821 }
822
823 static void phys_init_v1_hw(struct hisi_hba *hisi_hba)
824 {
825         int i;
826         struct timer_list *timer = &hisi_hba->timer;
827
828         for (i = 0; i < hisi_hba->n_phy; i++) {
829                 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0x6a);
830                 hisi_sas_phy_read32(hisi_hba, i, CHL_INT2_MSK);
831         }
832
833         timer_setup(timer, start_phys_v1_hw, 0);
834         mod_timer(timer, jiffies + HZ);
835 }
836
837 static void sl_notify_v1_hw(struct hisi_hba *hisi_hba, int phy_no)
838 {
839         u32 sl_control;
840
841         sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
842         sl_control |= SL_CONTROL_NOTIFY_EN_MSK;
843         hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
844         msleep(1);
845         sl_control = hisi_sas_phy_read32(hisi_hba, phy_no, SL_CONTROL);
846         sl_control &= ~SL_CONTROL_NOTIFY_EN_MSK;
847         hisi_sas_phy_write32(hisi_hba, phy_no, SL_CONTROL, sl_control);
848 }
849
850 static enum sas_linkrate phy_get_max_linkrate_v1_hw(void)
851 {
852         return SAS_LINK_RATE_6_0_GBPS;
853 }
854
855 static void phy_set_linkrate_v1_hw(struct hisi_hba *hisi_hba, int phy_no,
856                 struct sas_phy_linkrates *r)
857 {
858         u32 prog_phy_link_rate =
859                 hisi_sas_phy_read32(hisi_hba, phy_no, PROG_PHY_LINK_RATE);
860         struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
861         struct asd_sas_phy *sas_phy = &phy->sas_phy;
862         int i;
863         enum sas_linkrate min, max;
864         u32 rate_mask = 0;
865
866         if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
867                 max = sas_phy->phy->maximum_linkrate;
868                 min = r->minimum_linkrate;
869         } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
870                 max = r->maximum_linkrate;
871                 min = sas_phy->phy->minimum_linkrate;
872         } else
873                 return;
874
875         sas_phy->phy->maximum_linkrate = max;
876         sas_phy->phy->minimum_linkrate = min;
877
878         max -= SAS_LINK_RATE_1_5_GBPS;
879
880         for (i = 0; i <= max; i++)
881                 rate_mask |= 1 << (i * 2);
882
883         prog_phy_link_rate &= ~0xff;
884         prog_phy_link_rate |= rate_mask;
885
886         disable_phy_v1_hw(hisi_hba, phy_no);
887         msleep(100);
888         hisi_sas_phy_write32(hisi_hba, phy_no, PROG_PHY_LINK_RATE,
889                         prog_phy_link_rate);
890         start_phy_v1_hw(hisi_hba, phy_no);
891 }
892
893 static int get_wideport_bitmap_v1_hw(struct hisi_hba *hisi_hba, int port_id)
894 {
895         int i, bitmap = 0;
896         u32 phy_port_num_ma = hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA);
897
898         for (i = 0; i < hisi_hba->n_phy; i++)
899                 if (((phy_port_num_ma >> (i * 4)) & 0xf) == port_id)
900                         bitmap |= 1 << i;
901
902         return bitmap;
903 }
904
905 /*
906  * The callpath to this function and upto writing the write
907  * queue pointer should be safe from interruption.
908  */
909 static int
910 get_free_slot_v1_hw(struct hisi_hba *hisi_hba, struct hisi_sas_dq *dq)
911 {
912         struct device *dev = hisi_hba->dev;
913         int queue = dq->id;
914         u32 r, w;
915
916         w = dq->wr_point;
917         r = hisi_sas_read32_relaxed(hisi_hba,
918                                 DLVRY_Q_0_RD_PTR + (queue * 0x14));
919         if (r == (w+1) % HISI_SAS_QUEUE_SLOTS) {
920                 dev_warn(dev, "could not find free slot\n");
921                 return -EAGAIN;
922         }
923
924         return 0;
925 }
926
927 static void start_delivery_v1_hw(struct hisi_sas_dq *dq)
928 {
929         struct hisi_hba *hisi_hba = dq->hisi_hba;
930         int dlvry_queue = dq->slot_prep->dlvry_queue;
931         int dlvry_queue_slot = dq->slot_prep->dlvry_queue_slot;
932
933         dq->wr_point = ++dlvry_queue_slot % HISI_SAS_QUEUE_SLOTS;
934         hisi_sas_write32(hisi_hba, DLVRY_Q_0_WR_PTR + (dlvry_queue * 0x14),
935                          dq->wr_point);
936 }
937
938 static int prep_prd_sge_v1_hw(struct hisi_hba *hisi_hba,
939                               struct hisi_sas_slot *slot,
940                               struct hisi_sas_cmd_hdr *hdr,
941                               struct scatterlist *scatter,
942                               int n_elem)
943 {
944         struct hisi_sas_sge_page *sge_page = hisi_sas_sge_addr_mem(slot);
945         struct device *dev = hisi_hba->dev;
946         struct scatterlist *sg;
947         int i;
948
949         if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
950                 dev_err(dev, "prd err: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
951                         n_elem);
952                 return -EINVAL;
953         }
954
955         for_each_sg(scatter, sg, n_elem, i) {
956                 struct hisi_sas_sge *entry = &sge_page->sge[i];
957
958                 entry->addr = cpu_to_le64(sg_dma_address(sg));
959                 entry->page_ctrl_0 = entry->page_ctrl_1 = 0;
960                 entry->data_len = cpu_to_le32(sg_dma_len(sg));
961                 entry->data_off = 0;
962         }
963
964         hdr->prd_table_addr = cpu_to_le64(hisi_sas_sge_addr_dma(slot));
965
966         hdr->sg_len = cpu_to_le32(n_elem << CMD_HDR_DATA_SGL_LEN_OFF);
967
968         return 0;
969 }
970
971 static int prep_smp_v1_hw(struct hisi_hba *hisi_hba,
972                           struct hisi_sas_slot *slot)
973 {
974         struct sas_task *task = slot->task;
975         struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
976         struct domain_device *device = task->dev;
977         struct device *dev = hisi_hba->dev;
978         struct hisi_sas_port *port = slot->port;
979         struct scatterlist *sg_req, *sg_resp;
980         struct hisi_sas_device *sas_dev = device->lldd_dev;
981         dma_addr_t req_dma_addr;
982         unsigned int req_len, resp_len;
983         int elem, rc;
984
985         /*
986         * DMA-map SMP request, response buffers
987         */
988         /* req */
989         sg_req = &task->smp_task.smp_req;
990         elem = dma_map_sg(dev, sg_req, 1, DMA_TO_DEVICE);
991         if (!elem)
992                 return -ENOMEM;
993         req_len = sg_dma_len(sg_req);
994         req_dma_addr = sg_dma_address(sg_req);
995
996         /* resp */
997         sg_resp = &task->smp_task.smp_resp;
998         elem = dma_map_sg(dev, sg_resp, 1, DMA_FROM_DEVICE);
999         if (!elem) {
1000                 rc = -ENOMEM;
1001                 goto err_out_req;
1002         }
1003         resp_len = sg_dma_len(sg_resp);
1004         if ((req_len & 0x3) || (resp_len & 0x3)) {
1005                 rc = -EINVAL;
1006                 goto err_out_resp;
1007         }
1008
1009         /* create header */
1010         /* dw0 */
1011         hdr->dw0 = cpu_to_le32((port->id << CMD_HDR_PORT_OFF) |
1012                                (1 << CMD_HDR_PRIORITY_OFF) | /* high pri */
1013                                (1 << CMD_HDR_MODE_OFF) | /* ini mode */
1014                                (2 << CMD_HDR_CMD_OFF)); /* smp */
1015
1016         /* map itct entry */
1017         hdr->dw1 = cpu_to_le32(sas_dev->device_id << CMD_HDR_DEVICE_ID_OFF);
1018
1019         /* dw2 */
1020         hdr->dw2 = cpu_to_le32((((req_len-4)/4) << CMD_HDR_CFL_OFF) |
1021                                (HISI_SAS_MAX_SMP_RESP_SZ/4 <<
1022                                CMD_HDR_MRFL_OFF));
1023
1024         hdr->transfer_tags = cpu_to_le32(slot->idx << CMD_HDR_IPTT_OFF);
1025
1026         hdr->cmd_table_addr = cpu_to_le64(req_dma_addr);
1027         hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
1028
1029         return 0;
1030
1031 err_out_resp:
1032         dma_unmap_sg(dev, &slot->task->smp_task.smp_resp, 1,
1033                      DMA_FROM_DEVICE);
1034 err_out_req:
1035         dma_unmap_sg(dev, &slot->task->smp_task.smp_req, 1,
1036                      DMA_TO_DEVICE);
1037         return rc;
1038 }
1039
1040 static int prep_ssp_v1_hw(struct hisi_hba *hisi_hba,
1041                           struct hisi_sas_slot *slot, int is_tmf,
1042                           struct hisi_sas_tmf_task *tmf)
1043 {
1044         struct sas_task *task = slot->task;
1045         struct hisi_sas_cmd_hdr *hdr = slot->cmd_hdr;
1046         struct domain_device *device = task->dev;
1047         struct hisi_sas_device *sas_dev = device->lldd_dev;
1048         struct hisi_sas_port *port = slot->port;
1049         struct sas_ssp_task *ssp_task = &task->ssp_task;
1050         struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
1051         int has_data = 0, rc, priority = is_tmf;
1052         u8 *buf_cmd, fburst = 0;
1053         u32 dw1, dw2;
1054
1055         /* create header */
1056         hdr->dw0 = cpu_to_le32((1 << CMD_HDR_RESP_REPORT_OFF) |
1057                                (0x2 << CMD_HDR_TLR_CTRL_OFF) |
1058                                (port->id << CMD_HDR_PORT_OFF) |
1059                                (priority << CMD_HDR_PRIORITY_OFF) |
1060                                (1 << CMD_HDR_MODE_OFF) | /* ini mode */
1061                                (1 << CMD_HDR_CMD_OFF)); /* ssp */
1062
1063         dw1 = 1 << CMD_HDR_VERIFY_DTL_OFF;
1064
1065         if (is_tmf) {
1066                 dw1 |= 3 << CMD_HDR_SSP_FRAME_TYPE_OFF;
1067         } else {
1068                 switch (scsi_cmnd->sc_data_direction) {
1069                 case DMA_TO_DEVICE:
1070                         dw1 |= 2 << CMD_HDR_SSP_FRAME_TYPE_OFF;
1071                         has_data = 1;
1072                         break;
1073                 case DMA_FROM_DEVICE:
1074                         dw1 |= 1 << CMD_HDR_SSP_FRAME_TYPE_OFF;
1075                         has_data = 1;
1076                         break;
1077                 default:
1078                         dw1 |= 0 << CMD_HDR_SSP_FRAME_TYPE_OFF;
1079                 }
1080         }
1081
1082         /* map itct entry */
1083         dw1 |= sas_dev->device_id << CMD_HDR_DEVICE_ID_OFF;
1084         hdr->dw1 = cpu_to_le32(dw1);
1085
1086         if (is_tmf) {
1087                 dw2 = ((sizeof(struct ssp_tmf_iu) +
1088                         sizeof(struct ssp_frame_hdr)+3)/4) <<
1089                         CMD_HDR_CFL_OFF;
1090         } else {
1091                 dw2 = ((sizeof(struct ssp_command_iu) +
1092                         sizeof(struct ssp_frame_hdr)+3)/4) <<
1093                         CMD_HDR_CFL_OFF;
1094         }
1095
1096         dw2 |= (HISI_SAS_MAX_SSP_RESP_SZ/4) << CMD_HDR_MRFL_OFF;
1097
1098         hdr->transfer_tags = cpu_to_le32(slot->idx << CMD_HDR_IPTT_OFF);
1099
1100         if (has_data) {
1101                 rc = prep_prd_sge_v1_hw(hisi_hba, slot, hdr, task->scatter,
1102                                         slot->n_elem);
1103                 if (rc)
1104                         return rc;
1105         }
1106
1107         hdr->data_transfer_len = cpu_to_le32(task->total_xfer_len);
1108         hdr->cmd_table_addr = cpu_to_le64(hisi_sas_cmd_hdr_addr_dma(slot));
1109         hdr->sts_buffer_addr = cpu_to_le64(hisi_sas_status_buf_addr_dma(slot));
1110
1111         buf_cmd = hisi_sas_cmd_hdr_addr_mem(slot) +
1112                 sizeof(struct ssp_frame_hdr);
1113         if (task->ssp_task.enable_first_burst) {
1114                 fburst = (1 << 7);
1115                 dw2 |= 1 << CMD_HDR_FIRST_BURST_OFF;
1116         }
1117         hdr->dw2 = cpu_to_le32(dw2);
1118
1119         memcpy(buf_cmd, &task->ssp_task.LUN, 8);
1120         if (!is_tmf) {
1121                 buf_cmd[9] = fburst | task->ssp_task.task_attr |
1122                                 (task->ssp_task.task_prio << 3);
1123                 memcpy(buf_cmd + 12, task->ssp_task.cmd->cmnd,
1124                                 task->ssp_task.cmd->cmd_len);
1125         } else {
1126                 buf_cmd[10] = tmf->tmf;
1127                 switch (tmf->tmf) {
1128                 case TMF_ABORT_TASK:
1129                 case TMF_QUERY_TASK:
1130                         buf_cmd[12] =
1131                                 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
1132                         buf_cmd[13] =
1133                                 tmf->tag_of_task_to_be_managed & 0xff;
1134                         break;
1135                 default:
1136                         break;
1137                 }
1138         }
1139
1140         return 0;
1141 }
1142
1143 /* by default, task resp is complete */
1144 static void slot_err_v1_hw(struct hisi_hba *hisi_hba,
1145                            struct sas_task *task,
1146                            struct hisi_sas_slot *slot)
1147 {
1148         struct task_status_struct *ts = &task->task_status;
1149         struct hisi_sas_err_record_v1 *err_record =
1150                         hisi_sas_status_buf_addr_mem(slot);
1151         struct device *dev = hisi_hba->dev;
1152
1153         switch (task->task_proto) {
1154         case SAS_PROTOCOL_SSP:
1155         {
1156                 int error = -1;
1157                 u32 dma_err_type = cpu_to_le32(err_record->dma_err_type);
1158                 u32 dma_tx_err_type = ((dma_err_type &
1159                                         ERR_HDR_DMA_TX_ERR_TYPE_MSK)) >>
1160                                         ERR_HDR_DMA_TX_ERR_TYPE_OFF;
1161                 u32 dma_rx_err_type = ((dma_err_type &
1162                                         ERR_HDR_DMA_RX_ERR_TYPE_MSK)) >>
1163                                         ERR_HDR_DMA_RX_ERR_TYPE_OFF;
1164                 u32 trans_tx_fail_type =
1165                                 cpu_to_le32(err_record->trans_tx_fail_type);
1166                 u32 trans_rx_fail_type =
1167                                 cpu_to_le32(err_record->trans_rx_fail_type);
1168
1169                 if (dma_tx_err_type) {
1170                         /* dma tx err */
1171                         error = ffs(dma_tx_err_type)
1172                                 - 1 + DMA_TX_ERR_BASE;
1173                 } else if (dma_rx_err_type) {
1174                         /* dma rx err */
1175                         error = ffs(dma_rx_err_type)
1176                                 - 1 + DMA_RX_ERR_BASE;
1177                 } else if (trans_tx_fail_type) {
1178                         /* trans tx err */
1179                         error = ffs(trans_tx_fail_type)
1180                                 - 1 + TRANS_TX_FAIL_BASE;
1181                 } else if (trans_rx_fail_type) {
1182                         /* trans rx err */
1183                         error = ffs(trans_rx_fail_type)
1184                                 - 1 + TRANS_RX_FAIL_BASE;
1185                 }
1186
1187                 switch (error) {
1188                 case DMA_TX_DATA_UNDERFLOW_ERR:
1189                 case DMA_RX_DATA_UNDERFLOW_ERR:
1190                 {
1191                         ts->residual = 0;
1192                         ts->stat = SAS_DATA_UNDERRUN;
1193                         break;
1194                 }
1195                 case DMA_TX_DATA_SGL_OVERFLOW_ERR:
1196                 case DMA_TX_DIF_SGL_OVERFLOW_ERR:
1197                 case DMA_TX_XFER_RDY_LENGTH_OVERFLOW_ERR:
1198                 case DMA_RX_DATA_OVERFLOW_ERR:
1199                 case TRANS_RX_FRAME_OVERRUN_ERR:
1200                 case TRANS_RX_LINK_BUF_OVERRUN_ERR:
1201                 {
1202                         ts->stat = SAS_DATA_OVERRUN;
1203                         ts->residual = 0;
1204                         break;
1205                 }
1206                 case TRANS_TX_PHY_NOT_ENABLE_ERR:
1207                 {
1208                         ts->stat = SAS_PHY_DOWN;
1209                         break;
1210                 }
1211                 case TRANS_TX_OPEN_REJCT_WRONG_DEST_ERR:
1212                 case TRANS_TX_OPEN_REJCT_ZONE_VIOLATION_ERR:
1213                 case TRANS_TX_OPEN_REJCT_BY_OTHER_ERR:
1214                 case TRANS_TX_OPEN_REJCT_AIP_TIMEOUT_ERR:
1215                 case TRANS_TX_OPEN_REJCT_STP_BUSY_ERR:
1216                 case TRANS_TX_OPEN_REJCT_PROTOCOL_NOT_SUPPORT_ERR:
1217                 case TRANS_TX_OPEN_REJCT_RATE_NOT_SUPPORT_ERR:
1218                 case TRANS_TX_OPEN_REJCT_BAD_DEST_ERR:
1219                 case TRANS_TX_OPEN_BREAK_RECEIVE_ERR:
1220                 case TRANS_TX_OPEN_REJCT_PATHWAY_BLOCKED_ERR:
1221                 case TRANS_TX_OPEN_REJCT_NO_DEST_ERR:
1222                 case TRANS_TX_OPEN_RETRY_ERR:
1223                 {
1224                         ts->stat = SAS_OPEN_REJECT;
1225                         ts->open_rej_reason = SAS_OREJ_UNKNOWN;
1226                         break;
1227                 }
1228                 case TRANS_TX_OPEN_TIMEOUT_ERR:
1229                 {
1230                         ts->stat = SAS_OPEN_TO;
1231                         break;
1232                 }
1233                 case TRANS_TX_NAK_RECEIVE_ERR:
1234                 case TRANS_TX_ACK_NAK_TIMEOUT_ERR:
1235                 {
1236                         ts->stat = SAS_NAK_R_ERR;
1237                         break;
1238                 }
1239                 case TRANS_TX_CREDIT_TIMEOUT_ERR:
1240                 case TRANS_TX_CLOSE_NORMAL_ERR:
1241                 {
1242                         /* This will request a retry */
1243                         ts->stat = SAS_QUEUE_FULL;
1244                         slot->abort = 1;
1245                         break;
1246                 }
1247                 default:
1248                 {
1249                         ts->stat = SAM_STAT_CHECK_CONDITION;
1250                         break;
1251                 }
1252                 }
1253         }
1254                 break;
1255         case SAS_PROTOCOL_SMP:
1256                 ts->stat = SAM_STAT_CHECK_CONDITION;
1257                 break;
1258
1259         case SAS_PROTOCOL_SATA:
1260         case SAS_PROTOCOL_STP:
1261         case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
1262         {
1263                 dev_err(dev, "slot err: SATA/STP not supported");
1264         }
1265                 break;
1266         default:
1267                 break;
1268         }
1269
1270 }
1271
1272 static int slot_complete_v1_hw(struct hisi_hba *hisi_hba,
1273                                struct hisi_sas_slot *slot)
1274 {
1275         struct sas_task *task = slot->task;
1276         struct hisi_sas_device *sas_dev;
1277         struct device *dev = hisi_hba->dev;
1278         struct task_status_struct *ts;
1279         struct domain_device *device;
1280         enum exec_status sts;
1281         struct hisi_sas_complete_v1_hdr *complete_queue =
1282                         hisi_hba->complete_hdr[slot->cmplt_queue];
1283         struct hisi_sas_complete_v1_hdr *complete_hdr;
1284         unsigned long flags;
1285         u32 cmplt_hdr_data;
1286
1287         complete_hdr = &complete_queue[slot->cmplt_queue_slot];
1288         cmplt_hdr_data = le32_to_cpu(complete_hdr->data);
1289
1290         if (unlikely(!task || !task->lldd_task || !task->dev))
1291                 return -EINVAL;
1292
1293         ts = &task->task_status;
1294         device = task->dev;
1295         sas_dev = device->lldd_dev;
1296
1297         spin_lock_irqsave(&task->task_state_lock, flags);
1298         task->task_state_flags &=
1299                 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1300         task->task_state_flags |= SAS_TASK_STATE_DONE;
1301         spin_unlock_irqrestore(&task->task_state_lock, flags);
1302
1303         memset(ts, 0, sizeof(*ts));
1304         ts->resp = SAS_TASK_COMPLETE;
1305
1306         if (unlikely(!sas_dev)) {
1307                 dev_dbg(dev, "slot complete: port has no device\n");
1308                 ts->stat = SAS_PHY_DOWN;
1309                 goto out;
1310         }
1311
1312         if (cmplt_hdr_data & CMPLT_HDR_IO_CFG_ERR_MSK) {
1313                 u32 info_reg = hisi_sas_read32(hisi_hba, HGC_INVLD_DQE_INFO);
1314
1315                 if (info_reg & HGC_INVLD_DQE_INFO_DQ_MSK)
1316                         dev_err(dev, "slot complete: [%d:%d] has dq IPTT err",
1317                                 slot->cmplt_queue, slot->cmplt_queue_slot);
1318
1319                 if (info_reg & HGC_INVLD_DQE_INFO_TYPE_MSK)
1320                         dev_err(dev, "slot complete: [%d:%d] has dq type err",
1321                                 slot->cmplt_queue, slot->cmplt_queue_slot);
1322
1323                 if (info_reg & HGC_INVLD_DQE_INFO_FORCE_MSK)
1324                         dev_err(dev, "slot complete: [%d:%d] has dq force phy err",
1325                                 slot->cmplt_queue, slot->cmplt_queue_slot);
1326
1327                 if (info_reg & HGC_INVLD_DQE_INFO_PHY_MSK)
1328                         dev_err(dev, "slot complete: [%d:%d] has dq phy id err",
1329                                 slot->cmplt_queue, slot->cmplt_queue_slot);
1330
1331                 if (info_reg & HGC_INVLD_DQE_INFO_ABORT_MSK)
1332                         dev_err(dev, "slot complete: [%d:%d] has dq abort flag err",
1333                                 slot->cmplt_queue, slot->cmplt_queue_slot);
1334
1335                 if (info_reg & HGC_INVLD_DQE_INFO_IPTT_OF_MSK)
1336                         dev_err(dev, "slot complete: [%d:%d] has dq IPTT or ICT err",
1337                                 slot->cmplt_queue, slot->cmplt_queue_slot);
1338
1339                 if (info_reg & HGC_INVLD_DQE_INFO_SSP_ERR_MSK)
1340                         dev_err(dev, "slot complete: [%d:%d] has dq SSP frame type err",
1341                                 slot->cmplt_queue, slot->cmplt_queue_slot);
1342
1343                 if (info_reg & HGC_INVLD_DQE_INFO_OFL_MSK)
1344                         dev_err(dev, "slot complete: [%d:%d] has dq order frame len err",
1345                                 slot->cmplt_queue, slot->cmplt_queue_slot);
1346
1347                 ts->stat = SAS_OPEN_REJECT;
1348                 ts->open_rej_reason = SAS_OREJ_UNKNOWN;
1349                 goto out;
1350         }
1351
1352         if (cmplt_hdr_data & CMPLT_HDR_ERR_RCRD_XFRD_MSK &&
1353                 !(cmplt_hdr_data & CMPLT_HDR_RSPNS_XFRD_MSK)) {
1354
1355                 slot_err_v1_hw(hisi_hba, task, slot);
1356                 if (unlikely(slot->abort)) {
1357                         queue_work(hisi_hba->wq, &slot->abort_slot);
1358                         /* immediately return and do not complete */
1359                         return ts->stat;
1360                 }
1361                 goto out;
1362         }
1363
1364         switch (task->task_proto) {
1365         case SAS_PROTOCOL_SSP:
1366         {
1367                 struct hisi_sas_status_buffer *status_buffer =
1368                                 hisi_sas_status_buf_addr_mem(slot);
1369                 struct ssp_response_iu *iu = (struct ssp_response_iu *)
1370                                 &status_buffer->iu[0];
1371
1372                 sas_ssp_task_response(dev, task, iu);
1373                 break;
1374         }
1375         case SAS_PROTOCOL_SMP:
1376         {
1377                 void *to;
1378                 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
1379
1380                 ts->stat = SAM_STAT_GOOD;
1381                 to = kmap_atomic(sg_page(sg_resp));
1382
1383                 dma_unmap_sg(dev, &task->smp_task.smp_resp, 1,
1384                              DMA_FROM_DEVICE);
1385                 dma_unmap_sg(dev, &task->smp_task.smp_req, 1,
1386                              DMA_TO_DEVICE);
1387                 memcpy(to + sg_resp->offset,
1388                        hisi_sas_status_buf_addr_mem(slot) +
1389                        sizeof(struct hisi_sas_err_record),
1390                        sg_dma_len(sg_resp));
1391                 kunmap_atomic(to);
1392                 break;
1393         }
1394         case SAS_PROTOCOL_SATA:
1395         case SAS_PROTOCOL_STP:
1396         case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
1397                 dev_err(dev, "slot complete: SATA/STP not supported");
1398                 break;
1399
1400         default:
1401                 ts->stat = SAM_STAT_CHECK_CONDITION;
1402                 break;
1403         }
1404
1405         if (!slot->port->port_attached) {
1406                 dev_err(dev, "slot complete: port %d has removed\n",
1407                         slot->port->sas_port.id);
1408                 ts->stat = SAS_PHY_DOWN;
1409         }
1410
1411 out:
1412         hisi_sas_slot_task_free(hisi_hba, task, slot);
1413         sts = ts->stat;
1414
1415         if (task->task_done)
1416                 task->task_done(task);
1417
1418         return sts;
1419 }
1420
1421 /* Interrupts */
1422 static irqreturn_t int_phyup_v1_hw(int irq_no, void *p)
1423 {
1424         struct hisi_sas_phy *phy = p;
1425         struct hisi_hba *hisi_hba = phy->hisi_hba;
1426         struct device *dev = hisi_hba->dev;
1427         struct asd_sas_phy *sas_phy = &phy->sas_phy;
1428         int i, phy_no = sas_phy->id;
1429         u32 irq_value, context, port_id, link_rate;
1430         u32 *frame_rcvd = (u32 *)sas_phy->frame_rcvd;
1431         struct sas_identify_frame *id = (struct sas_identify_frame *)frame_rcvd;
1432         irqreturn_t res = IRQ_HANDLED;
1433
1434         irq_value = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT2);
1435         if (!(irq_value & CHL_INT2_SL_PHY_ENA_MSK)) {
1436                 dev_dbg(dev, "phyup: irq_value = %x not set enable bit\n",
1437                         irq_value);
1438                 res = IRQ_NONE;
1439                 goto end;
1440         }
1441
1442         context = hisi_sas_read32(hisi_hba, PHY_CONTEXT);
1443         if (context & 1 << phy_no) {
1444                 dev_err(dev, "phyup: phy%d SATA attached equipment\n",
1445                         phy_no);
1446                 goto end;
1447         }
1448
1449         port_id = (hisi_sas_read32(hisi_hba, PHY_PORT_NUM_MA) >> (4 * phy_no))
1450                   & 0xf;
1451         if (port_id == 0xf) {
1452                 dev_err(dev, "phyup: phy%d invalid portid\n", phy_no);
1453                 res = IRQ_NONE;
1454                 goto end;
1455         }
1456
1457         for (i = 0; i < 6; i++) {
1458                 u32 idaf = hisi_sas_phy_read32(hisi_hba, phy_no,
1459                                         RX_IDAF_DWORD0 + (i * 4));
1460                 frame_rcvd[i] = __swab32(idaf);
1461         }
1462
1463         /* Get the linkrate */
1464         link_rate = hisi_sas_read32(hisi_hba, PHY_CONN_RATE);
1465         link_rate = (link_rate >> (phy_no * 4)) & 0xf;
1466         sas_phy->linkrate = link_rate;
1467         sas_phy->oob_mode = SAS_OOB_MODE;
1468         memcpy(sas_phy->attached_sas_addr,
1469                 &id->sas_addr, SAS_ADDR_SIZE);
1470         dev_info(dev, "phyup: phy%d link_rate=%d\n",
1471                  phy_no, link_rate);
1472         phy->port_id = port_id;
1473         phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1474         phy->phy_type |= PORT_TYPE_SAS;
1475         phy->phy_attached = 1;
1476         phy->identify.device_type = id->dev_type;
1477         phy->frame_rcvd_size =  sizeof(struct sas_identify_frame);
1478         if (phy->identify.device_type == SAS_END_DEVICE)
1479                 phy->identify.target_port_protocols =
1480                         SAS_PROTOCOL_SSP;
1481         else if (phy->identify.device_type != SAS_PHY_UNUSED)
1482                 phy->identify.target_port_protocols =
1483                         SAS_PROTOCOL_SMP;
1484         hisi_sas_notify_phy_event(phy, HISI_PHYE_PHY_UP);
1485
1486 end:
1487         hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2,
1488                              CHL_INT2_SL_PHY_ENA_MSK);
1489
1490         if (irq_value & CHL_INT2_SL_PHY_ENA_MSK) {
1491                 u32 chl_int0 = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT0);
1492
1493                 chl_int0 &= ~CHL_INT0_PHYCTRL_NOTRDY_MSK;
1494                 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0, chl_int0);
1495                 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0_MSK, 0x3ce3ee);
1496         }
1497
1498         return res;
1499 }
1500
1501 static irqreturn_t int_bcast_v1_hw(int irq, void *p)
1502 {
1503         struct hisi_sas_phy *phy = p;
1504         struct hisi_hba *hisi_hba = phy->hisi_hba;
1505         struct asd_sas_phy *sas_phy = &phy->sas_phy;
1506         struct sas_ha_struct *sha = &hisi_hba->sha;
1507         struct device *dev = hisi_hba->dev;
1508         int phy_no = sas_phy->id;
1509         u32 irq_value;
1510         irqreturn_t res = IRQ_HANDLED;
1511
1512         irq_value = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT2);
1513
1514         if (!(irq_value & CHL_INT2_SL_RX_BC_ACK_MSK)) {
1515                 dev_err(dev, "bcast: irq_value = %x not set enable bit",
1516                         irq_value);
1517                 res = IRQ_NONE;
1518                 goto end;
1519         }
1520
1521         sha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
1522
1523 end:
1524         hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT2,
1525                              CHL_INT2_SL_RX_BC_ACK_MSK);
1526
1527         return res;
1528 }
1529
1530 static irqreturn_t int_abnormal_v1_hw(int irq, void *p)
1531 {
1532         struct hisi_sas_phy *phy = p;
1533         struct hisi_hba *hisi_hba = phy->hisi_hba;
1534         struct device *dev = hisi_hba->dev;
1535         struct asd_sas_phy *sas_phy = &phy->sas_phy;
1536         u32 irq_value, irq_mask_old;
1537         int phy_no = sas_phy->id;
1538
1539         /* mask_int0 */
1540         irq_mask_old = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT0_MSK);
1541         hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0_MSK, 0x3fffff);
1542
1543         /* read int0 */
1544         irq_value = hisi_sas_phy_read32(hisi_hba, phy_no, CHL_INT0);
1545
1546         if (irq_value & CHL_INT0_PHYCTRL_NOTRDY_MSK) {
1547                 u32 phy_state = hisi_sas_read32(hisi_hba, PHY_STATE);
1548
1549                 hisi_sas_phy_down(hisi_hba, phy_no,
1550                                   (phy_state & 1 << phy_no) ? 1 : 0);
1551         }
1552
1553         if (irq_value & CHL_INT0_ID_TIMEOUT_MSK)
1554                 dev_dbg(dev, "abnormal: ID_TIMEOUT phy%d identify timeout\n",
1555                         phy_no);
1556
1557         if (irq_value & CHL_INT0_DWS_LOST_MSK)
1558                 dev_dbg(dev, "abnormal: DWS_LOST phy%d dws lost\n", phy_no);
1559
1560         if (irq_value & CHL_INT0_SN_FAIL_NGR_MSK)
1561                 dev_dbg(dev, "abnormal: SN_FAIL_NGR phy%d sn fail ngr\n",
1562                         phy_no);
1563
1564         if (irq_value & CHL_INT0_SL_IDAF_FAIL_MSK ||
1565                 irq_value & CHL_INT0_SL_OPAF_FAIL_MSK)
1566                 dev_dbg(dev, "abnormal: SL_ID/OPAF_FAIL phy%d check adr frm err\n",
1567                         phy_no);
1568
1569         if (irq_value & CHL_INT0_SL_PS_FAIL_OFF)
1570                 dev_dbg(dev, "abnormal: SL_PS_FAIL phy%d fail\n", phy_no);
1571
1572         /* write to zero */
1573         hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0, irq_value);
1574
1575         if (irq_value & CHL_INT0_PHYCTRL_NOTRDY_MSK)
1576                 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0_MSK,
1577                                 0x3fffff & ~CHL_INT0_MSK_PHYCTRL_NOTRDY_MSK);
1578         else
1579                 hisi_sas_phy_write32(hisi_hba, phy_no, CHL_INT0_MSK,
1580                                 irq_mask_old);
1581
1582         return IRQ_HANDLED;
1583 }
1584
1585 static irqreturn_t cq_interrupt_v1_hw(int irq, void *p)
1586 {
1587         struct hisi_sas_cq *cq = p;
1588         struct hisi_hba *hisi_hba = cq->hisi_hba;
1589         struct hisi_sas_slot *slot;
1590         int queue = cq->id;
1591         struct hisi_sas_complete_v1_hdr *complete_queue =
1592                         (struct hisi_sas_complete_v1_hdr *)
1593                         hisi_hba->complete_hdr[queue];
1594         u32 irq_value, rd_point = cq->rd_point, wr_point;
1595
1596         spin_lock(&hisi_hba->lock);
1597         irq_value = hisi_sas_read32(hisi_hba, OQ_INT_SRC);
1598
1599         hisi_sas_write32(hisi_hba, OQ_INT_SRC, 1 << queue);
1600         wr_point = hisi_sas_read32(hisi_hba,
1601                         COMPL_Q_0_WR_PTR + (0x14 * queue));
1602
1603         while (rd_point != wr_point) {
1604                 struct hisi_sas_complete_v1_hdr *complete_hdr;
1605                 int idx;
1606                 u32 cmplt_hdr_data;
1607
1608                 complete_hdr = &complete_queue[rd_point];
1609                 cmplt_hdr_data = cpu_to_le32(complete_hdr->data);
1610                 idx = (cmplt_hdr_data & CMPLT_HDR_IPTT_MSK) >>
1611                       CMPLT_HDR_IPTT_OFF;
1612                 slot = &hisi_hba->slot_info[idx];
1613
1614                 /* The completion queue and queue slot index are not
1615                  * necessarily the same as the delivery queue and
1616                  * queue slot index.
1617                  */
1618                 slot->cmplt_queue_slot = rd_point;
1619                 slot->cmplt_queue = queue;
1620                 slot_complete_v1_hw(hisi_hba, slot);
1621
1622                 if (++rd_point >= HISI_SAS_QUEUE_SLOTS)
1623                         rd_point = 0;
1624         }
1625
1626         /* update rd_point */
1627         cq->rd_point = rd_point;
1628         hisi_sas_write32(hisi_hba, COMPL_Q_0_RD_PTR + (0x14 * queue), rd_point);
1629         spin_unlock(&hisi_hba->lock);
1630
1631         return IRQ_HANDLED;
1632 }
1633
1634 static irqreturn_t fatal_ecc_int_v1_hw(int irq, void *p)
1635 {
1636         struct hisi_hba *hisi_hba = p;
1637         struct device *dev = hisi_hba->dev;
1638         u32 ecc_int = hisi_sas_read32(hisi_hba, SAS_ECC_INTR);
1639
1640         if (ecc_int & SAS_ECC_INTR_DQ_ECC1B_MSK) {
1641                 u32 ecc_err = hisi_sas_read32(hisi_hba, HGC_ECC_ERR);
1642
1643                 panic("%s: Fatal DQ 1b ECC interrupt (0x%x)\n",
1644                       dev_name(dev), ecc_err);
1645         }
1646
1647         if (ecc_int & SAS_ECC_INTR_DQ_ECCBAD_MSK) {
1648                 u32 addr = (hisi_sas_read32(hisi_hba, HGC_DQ_ECC_ADDR) &
1649                                 HGC_DQ_ECC_ADDR_BAD_MSK) >>
1650                                 HGC_DQ_ECC_ADDR_BAD_OFF;
1651
1652                 panic("%s: Fatal DQ RAM ECC interrupt @ 0x%08x\n",
1653                       dev_name(dev), addr);
1654         }
1655
1656         if (ecc_int & SAS_ECC_INTR_IOST_ECC1B_MSK) {
1657                 u32 ecc_err = hisi_sas_read32(hisi_hba, HGC_ECC_ERR);
1658
1659                 panic("%s: Fatal IOST 1b ECC interrupt (0x%x)\n",
1660                       dev_name(dev), ecc_err);
1661         }
1662
1663         if (ecc_int & SAS_ECC_INTR_IOST_ECCBAD_MSK) {
1664                 u32 addr = (hisi_sas_read32(hisi_hba, HGC_IOST_ECC_ADDR) &
1665                                 HGC_IOST_ECC_ADDR_BAD_MSK) >>
1666                                 HGC_IOST_ECC_ADDR_BAD_OFF;
1667
1668                 panic("%s: Fatal IOST RAM ECC interrupt @ 0x%08x\n",
1669                       dev_name(dev), addr);
1670         }
1671
1672         if (ecc_int & SAS_ECC_INTR_ITCT_ECCBAD_MSK) {
1673                 u32 addr = (hisi_sas_read32(hisi_hba, HGC_ITCT_ECC_ADDR) &
1674                                 HGC_ITCT_ECC_ADDR_BAD_MSK) >>
1675                                 HGC_ITCT_ECC_ADDR_BAD_OFF;
1676
1677                 panic("%s: Fatal TCT RAM ECC interrupt @ 0x%08x\n",
1678                       dev_name(dev), addr);
1679         }
1680
1681         if (ecc_int & SAS_ECC_INTR_ITCT_ECC1B_MSK) {
1682                 u32 ecc_err = hisi_sas_read32(hisi_hba, HGC_ECC_ERR);
1683
1684                 panic("%s: Fatal ITCT 1b ECC interrupt (0x%x)\n",
1685                       dev_name(dev), ecc_err);
1686         }
1687
1688         hisi_sas_write32(hisi_hba, SAS_ECC_INTR, ecc_int | 0x3f);
1689
1690         return IRQ_HANDLED;
1691 }
1692
1693 static irqreturn_t fatal_axi_int_v1_hw(int irq, void *p)
1694 {
1695         struct hisi_hba *hisi_hba = p;
1696         struct device *dev = hisi_hba->dev;
1697         u32 axi_int = hisi_sas_read32(hisi_hba, ENT_INT_SRC2);
1698         u32 axi_info = hisi_sas_read32(hisi_hba, HGC_AXI_FIFO_ERR_INFO);
1699
1700         if (axi_int & ENT_INT_SRC2_DQ_CFG_ERR_MSK)
1701                 panic("%s: Fatal DQ_CFG_ERR interrupt (0x%x)\n",
1702                       dev_name(dev), axi_info);
1703
1704         if (axi_int & ENT_INT_SRC2_CQ_CFG_ERR_MSK)
1705                 panic("%s: Fatal CQ_CFG_ERR interrupt (0x%x)\n",
1706                       dev_name(dev), axi_info);
1707
1708         if (axi_int & ENT_INT_SRC2_AXI_WRONG_INT_MSK)
1709                 panic("%s: Fatal AXI_WRONG_INT interrupt (0x%x)\n",
1710                       dev_name(dev), axi_info);
1711
1712         if (axi_int & ENT_INT_SRC2_AXI_OVERLF_INT_MSK)
1713                 panic("%s: Fatal AXI_OVERLF_INT incorrect interrupt (0x%x)\n",
1714                       dev_name(dev), axi_info);
1715
1716         hisi_sas_write32(hisi_hba, ENT_INT_SRC2, axi_int | 0x30000000);
1717
1718         return IRQ_HANDLED;
1719 }
1720
1721 static irq_handler_t phy_interrupts[HISI_SAS_PHY_INT_NR] = {
1722         int_bcast_v1_hw,
1723         int_phyup_v1_hw,
1724         int_abnormal_v1_hw
1725 };
1726
1727 static irq_handler_t fatal_interrupts[HISI_SAS_MAX_QUEUES] = {
1728         fatal_ecc_int_v1_hw,
1729         fatal_axi_int_v1_hw
1730 };
1731
1732 static int interrupt_init_v1_hw(struct hisi_hba *hisi_hba)
1733 {
1734         struct platform_device *pdev = hisi_hba->platform_dev;
1735         struct device *dev = &pdev->dev;
1736         int i, j, irq, rc, idx;
1737
1738         for (i = 0; i < hisi_hba->n_phy; i++) {
1739                 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
1740
1741                 idx = i * HISI_SAS_PHY_INT_NR;
1742                 for (j = 0; j < HISI_SAS_PHY_INT_NR; j++, idx++) {
1743                         irq = platform_get_irq(pdev, idx);
1744                         if (!irq) {
1745                                 dev_err(dev,
1746                                         "irq init: fail map phy interrupt %d\n",
1747                                         idx);
1748                                 return -ENOENT;
1749                         }
1750
1751                         rc = devm_request_irq(dev, irq, phy_interrupts[j], 0,
1752                                               DRV_NAME " phy", phy);
1753                         if (rc) {
1754                                 dev_err(dev, "irq init: could not request "
1755                                         "phy interrupt %d, rc=%d\n",
1756                                         irq, rc);
1757                                 return -ENOENT;
1758                         }
1759                 }
1760         }
1761
1762         idx = hisi_hba->n_phy * HISI_SAS_PHY_INT_NR;
1763         for (i = 0; i < hisi_hba->queue_count; i++, idx++) {
1764                 irq = platform_get_irq(pdev, idx);
1765                 if (!irq) {
1766                         dev_err(dev, "irq init: could not map cq interrupt %d\n",
1767                                 idx);
1768                         return -ENOENT;
1769                 }
1770
1771                 rc = devm_request_irq(dev, irq, cq_interrupt_v1_hw, 0,
1772                                       DRV_NAME " cq", &hisi_hba->cq[i]);
1773                 if (rc) {
1774                         dev_err(dev, "irq init: could not request cq interrupt %d, rc=%d\n",
1775                                 irq, rc);
1776                         return -ENOENT;
1777                 }
1778         }
1779
1780         idx = (hisi_hba->n_phy * HISI_SAS_PHY_INT_NR) + hisi_hba->queue_count;
1781         for (i = 0; i < HISI_SAS_FATAL_INT_NR; i++, idx++) {
1782                 irq = platform_get_irq(pdev, idx);
1783                 if (!irq) {
1784                         dev_err(dev, "irq init: could not map fatal interrupt %d\n",
1785                                 idx);
1786                         return -ENOENT;
1787                 }
1788
1789                 rc = devm_request_irq(dev, irq, fatal_interrupts[i], 0,
1790                                       DRV_NAME " fatal", hisi_hba);
1791                 if (rc) {
1792                         dev_err(dev,
1793                                 "irq init: could not request fatal interrupt %d, rc=%d\n",
1794                                 irq, rc);
1795                         return -ENOENT;
1796                 }
1797         }
1798
1799         return 0;
1800 }
1801
1802 static int interrupt_openall_v1_hw(struct hisi_hba *hisi_hba)
1803 {
1804         int i;
1805         u32 val;
1806
1807         for (i = 0; i < hisi_hba->n_phy; i++) {
1808                 /* Clear interrupt status */
1809                 val = hisi_sas_phy_read32(hisi_hba, i, CHL_INT0);
1810                 hisi_sas_phy_write32(hisi_hba, i, CHL_INT0, val);
1811                 val = hisi_sas_phy_read32(hisi_hba, i, CHL_INT1);
1812                 hisi_sas_phy_write32(hisi_hba, i, CHL_INT1, val);
1813                 val = hisi_sas_phy_read32(hisi_hba, i, CHL_INT2);
1814                 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2, val);
1815
1816                 /* Unmask interrupt */
1817                 hisi_sas_phy_write32(hisi_hba, i, CHL_INT0_MSK, 0x3ce3ee);
1818                 hisi_sas_phy_write32(hisi_hba, i, CHL_INT1_MSK, 0x17fff);
1819                 hisi_sas_phy_write32(hisi_hba, i, CHL_INT2_MSK, 0x8000012a);
1820
1821                 /* bypass chip bug mask abnormal intr */
1822                 hisi_sas_phy_write32(hisi_hba, i, CHL_INT0_MSK,
1823                                 0x3fffff & ~CHL_INT0_MSK_PHYCTRL_NOTRDY_MSK);
1824         }
1825
1826         return 0;
1827 }
1828
1829 static int hisi_sas_v1_init(struct hisi_hba *hisi_hba)
1830 {
1831         int rc;
1832
1833         rc = hw_init_v1_hw(hisi_hba);
1834         if (rc)
1835                 return rc;
1836
1837         rc = interrupt_init_v1_hw(hisi_hba);
1838         if (rc)
1839                 return rc;
1840
1841         rc = interrupt_openall_v1_hw(hisi_hba);
1842         if (rc)
1843                 return rc;
1844
1845         return 0;
1846 }
1847
1848 static const struct hisi_sas_hw hisi_sas_v1_hw = {
1849         .hw_init = hisi_sas_v1_init,
1850         .setup_itct = setup_itct_v1_hw,
1851         .sl_notify = sl_notify_v1_hw,
1852         .clear_itct = clear_itct_v1_hw,
1853         .prep_smp = prep_smp_v1_hw,
1854         .prep_ssp = prep_ssp_v1_hw,
1855         .get_free_slot = get_free_slot_v1_hw,
1856         .start_delivery = start_delivery_v1_hw,
1857         .slot_complete = slot_complete_v1_hw,
1858         .phys_init = phys_init_v1_hw,
1859         .phy_start = start_phy_v1_hw,
1860         .phy_disable = disable_phy_v1_hw,
1861         .phy_hard_reset = phy_hard_reset_v1_hw,
1862         .phy_set_linkrate = phy_set_linkrate_v1_hw,
1863         .phy_get_max_linkrate = phy_get_max_linkrate_v1_hw,
1864         .get_wideport_bitmap = get_wideport_bitmap_v1_hw,
1865         .max_command_entries = HISI_SAS_COMMAND_ENTRIES_V1_HW,
1866         .complete_hdr_size = sizeof(struct hisi_sas_complete_v1_hdr),
1867 };
1868
1869 static int hisi_sas_v1_probe(struct platform_device *pdev)
1870 {
1871         return hisi_sas_probe(pdev, &hisi_sas_v1_hw);
1872 }
1873
1874 static int hisi_sas_v1_remove(struct platform_device *pdev)
1875 {
1876         return hisi_sas_remove(pdev);
1877 }
1878
1879 static const struct of_device_id sas_v1_of_match[] = {
1880         { .compatible = "hisilicon,hip05-sas-v1",},
1881         {},
1882 };
1883 MODULE_DEVICE_TABLE(of, sas_v1_of_match);
1884
1885 static const struct acpi_device_id sas_v1_acpi_match[] = {
1886         { "HISI0161", 0 },
1887         { }
1888 };
1889
1890 MODULE_DEVICE_TABLE(acpi, sas_v1_acpi_match);
1891
1892 static struct platform_driver hisi_sas_v1_driver = {
1893         .probe = hisi_sas_v1_probe,
1894         .remove = hisi_sas_v1_remove,
1895         .driver = {
1896                 .name = DRV_NAME,
1897                 .of_match_table = sas_v1_of_match,
1898                 .acpi_match_table = ACPI_PTR(sas_v1_acpi_match),
1899         },
1900 };
1901
1902 module_platform_driver(hisi_sas_v1_driver);
1903
1904 MODULE_LICENSE("GPL");
1905 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1906 MODULE_DESCRIPTION("HISILICON SAS controller v1 hw driver");
1907 MODULE_ALIAS("platform:" DRV_NAME);