1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Huawei HiNIC PCI Express Linux driver
4  * Copyright(c) 2017 Huawei Technologies Co., Ltd
5  */
6 
7 #ifndef HINIC_HW_IF_H
8 #define HINIC_HW_IF_H
9 
10 #include <linux/pci.h>
11 #include <linux/io.h>
12 #include <linux/types.h>
13 #include <asm/byteorder.h>
14 
15 #define HINIC_DMA_ATTR_ST_SHIFT                                 0
16 #define HINIC_DMA_ATTR_AT_SHIFT                                 8
17 #define HINIC_DMA_ATTR_PH_SHIFT                                 10
18 #define HINIC_DMA_ATTR_NO_SNOOPING_SHIFT                        12
19 #define HINIC_DMA_ATTR_TPH_EN_SHIFT                             13
20 
21 #define HINIC_DMA_ATTR_ST_MASK                                  0xFF
22 #define HINIC_DMA_ATTR_AT_MASK                                  0x3
23 #define HINIC_DMA_ATTR_PH_MASK                                  0x3
24 #define HINIC_DMA_ATTR_NO_SNOOPING_MASK                         0x1
25 #define HINIC_DMA_ATTR_TPH_EN_MASK                              0x1
26 
27 #define HINIC_DMA_ATTR_SET(val, member)                         \
28 	(((u32)(val) & HINIC_DMA_ATTR_##member##_MASK) <<       \
29 	 HINIC_DMA_ATTR_##member##_SHIFT)
30 
31 #define HINIC_DMA_ATTR_CLEAR(val, member)                       \
32 	((val) & (~(HINIC_DMA_ATTR_##member##_MASK              \
33 	 << HINIC_DMA_ATTR_##member##_SHIFT)))
34 
35 #define HINIC_FA0_FUNC_IDX_SHIFT                                0
36 #define HINIC_FA0_PF_IDX_SHIFT                                  10
37 #define HINIC_FA0_PCI_INTF_IDX_SHIFT                            14
38 #define HINIC_FA0_VF_IN_PF_SHIFT				16
39 /* reserved members - off 16 */
40 #define HINIC_FA0_FUNC_TYPE_SHIFT                               24
41 
42 #define HINIC_FA0_FUNC_IDX_MASK                                 0x3FF
43 #define HINIC_FA0_PF_IDX_MASK                                   0xF
44 #define HINIC_FA0_PCI_INTF_IDX_MASK                             0x3
45 #define HINIC_FA0_FUNC_TYPE_MASK                                0x1
46 #define HINIC_FA0_VF_IN_PF_MASK					0xFF
47 
48 #define HINIC_FA0_GET(val, member)                              \
49 	(((val) >> HINIC_FA0_##member##_SHIFT) & HINIC_FA0_##member##_MASK)
50 
51 #define HINIC_FA1_AEQS_PER_FUNC_SHIFT                           8
52 /* reserved members - off 10 */
53 #define HINIC_FA1_CEQS_PER_FUNC_SHIFT                           12
54 /* reserved members - off 15 */
55 #define HINIC_FA1_IRQS_PER_FUNC_SHIFT                           20
56 #define HINIC_FA1_DMA_ATTR_PER_FUNC_SHIFT                       24
57 /* reserved members - off 27 */
58 #define HINIC_FA1_MGMT_INIT_STATUS_SHIFT			30
59 #define HINIC_FA1_PF_INIT_STATUS_SHIFT				31
60 
61 #define HINIC_FA1_AEQS_PER_FUNC_MASK                            0x3
62 #define HINIC_FA1_CEQS_PER_FUNC_MASK                            0x7
63 #define HINIC_FA1_IRQS_PER_FUNC_MASK                            0xF
64 #define HINIC_FA1_DMA_ATTR_PER_FUNC_MASK                        0x7
65 #define HINIC_FA1_MGMT_INIT_STATUS_MASK                         0x1
66 #define HINIC_FA1_PF_INIT_STATUS_MASK				0x1
67 
68 #define HINIC_FA1_GET(val, member)                              \
69 	(((val) >> HINIC_FA1_##member##_SHIFT) & HINIC_FA1_##member##_MASK)
70 
71 #define HINIC_FA2_GLOBAL_VF_ID_OF_PF_SHIFT	16
72 #define HINIC_FA2_GLOBAL_VF_ID_OF_PF_MASK	0x3FF
73 
74 #define HINIC_FA2_GET(val, member)				\
75 	(((val) >> HINIC_FA2_##member##_SHIFT) & HINIC_FA2_##member##_MASK)
76 
77 #define HINIC_FA4_OUTBOUND_STATE_SHIFT                          0
78 #define HINIC_FA4_DB_STATE_SHIFT                                1
79 
80 #define HINIC_FA4_OUTBOUND_STATE_MASK                           0x1
81 #define HINIC_FA4_DB_STATE_MASK                                 0x1
82 
83 #define HINIC_FA4_GET(val, member)                              \
84 	(((val) >> HINIC_FA4_##member##_SHIFT) & HINIC_FA4_##member##_MASK)
85 
86 #define HINIC_FA4_SET(val, member)                              \
87 	((((u32)val) & HINIC_FA4_##member##_MASK) << HINIC_FA4_##member##_SHIFT)
88 
89 #define HINIC_FA4_CLEAR(val, member)                            \
90 	((val) & (~(HINIC_FA4_##member##_MASK << HINIC_FA4_##member##_SHIFT)))
91 
92 #define HINIC_FA5_PF_ACTION_SHIFT                               0
93 #define HINIC_FA5_PF_ACTION_MASK                                0xFFFF
94 
95 #define HINIC_FA5_SET(val, member)                              \
96 	(((u32)(val) & HINIC_FA5_##member##_MASK) << HINIC_FA5_##member##_SHIFT)
97 
98 #define HINIC_FA5_CLEAR(val, member)                            \
99 	((val) & (~(HINIC_FA5_##member##_MASK << HINIC_FA5_##member##_SHIFT)))
100 
101 #define HINIC_PPF_ELECTION_IDX_SHIFT                            0
102 #define HINIC_PPF_ELECTION_IDX_MASK                             0x1F
103 
104 #define HINIC_PPF_ELECTION_SET(val, member)                     \
105 	(((u32)(val) & HINIC_PPF_ELECTION_##member##_MASK) <<   \
106 	 HINIC_PPF_ELECTION_##member##_SHIFT)
107 
108 #define HINIC_PPF_ELECTION_GET(val, member)                     \
109 	(((val) >> HINIC_PPF_ELECTION_##member##_SHIFT) &       \
110 	 HINIC_PPF_ELECTION_##member##_MASK)
111 
112 #define HINIC_PPF_ELECTION_CLEAR(val, member)                   \
113 	((val) & (~(HINIC_PPF_ELECTION_##member##_MASK          \
114 	 << HINIC_PPF_ELECTION_##member##_SHIFT)))
115 
116 #define HINIC_MSIX_PENDING_LIMIT_SHIFT                          0
117 #define HINIC_MSIX_COALESC_TIMER_SHIFT                          8
118 #define HINIC_MSIX_LLI_TIMER_SHIFT                              16
119 #define HINIC_MSIX_LLI_CREDIT_SHIFT                             24
120 #define HINIC_MSIX_RESEND_TIMER_SHIFT                           29
121 
122 #define HINIC_MSIX_PENDING_LIMIT_MASK                           0xFF
123 #define HINIC_MSIX_COALESC_TIMER_MASK                           0xFF
124 #define HINIC_MSIX_LLI_TIMER_MASK                               0xFF
125 #define HINIC_MSIX_LLI_CREDIT_MASK                              0x1F
126 #define HINIC_MSIX_RESEND_TIMER_MASK                            0x7
127 
128 #define HINIC_MSIX_ATTR_SET(val, member)                        \
129 	(((u32)(val) & HINIC_MSIX_##member##_MASK) <<           \
130 	 HINIC_MSIX_##member##_SHIFT)
131 
132 #define HINIC_MSIX_ATTR_GET(val, member)                        \
133 	(((val) >> HINIC_MSIX_##member##_SHIFT) &               \
134 	 HINIC_MSIX_##member##_MASK)
135 
136 #define HINIC_MSIX_CNT_RESEND_TIMER_SHIFT                       29
137 
138 #define HINIC_MSIX_CNT_RESEND_TIMER_MASK                        0x1
139 
140 #define HINIC_MSIX_CNT_SET(val, member)                         \
141 	(((u32)(val) & HINIC_MSIX_CNT_##member##_MASK) <<       \
142 	 HINIC_MSIX_CNT_##member##_SHIFT)
143 
144 #define HINIC_HWIF_NUM_AEQS(hwif)       ((hwif)->attr.num_aeqs)
145 #define HINIC_HWIF_NUM_CEQS(hwif)       ((hwif)->attr.num_ceqs)
146 #define HINIC_HWIF_NUM_IRQS(hwif)       ((hwif)->attr.num_irqs)
147 #define HINIC_HWIF_FUNC_IDX(hwif)       ((hwif)->attr.func_idx)
148 #define HINIC_HWIF_PCI_INTF(hwif)       ((hwif)->attr.pci_intf_idx)
149 #define HINIC_HWIF_PF_IDX(hwif)         ((hwif)->attr.pf_idx)
150 #define HINIC_HWIF_PPF_IDX(hwif)        ((hwif)->attr.ppf_idx)
151 
152 #define HINIC_FUNC_TYPE(hwif)           ((hwif)->attr.func_type)
153 #define HINIC_IS_VF(hwif)               (HINIC_FUNC_TYPE(hwif) == HINIC_VF)
154 #define HINIC_IS_PF(hwif)               (HINIC_FUNC_TYPE(hwif) == HINIC_PF)
155 #define HINIC_IS_PPF(hwif)              (HINIC_FUNC_TYPE(hwif) == HINIC_PPF)
156 
157 #define HINIC_PCI_CFG_REGS_BAR          0
158 #define HINIC_PCI_INTR_REGS_BAR         2
159 #define HINIC_PCI_DB_BAR                4
160 
161 #define HINIC_PCIE_ST_DISABLE           0
162 #define HINIC_PCIE_AT_DISABLE           0
163 #define HINIC_PCIE_PH_DISABLE           0
164 
165 #define HINIC_EQ_MSIX_PENDING_LIMIT_DEFAULT     0       /* Disabled */
166 #define HINIC_EQ_MSIX_COALESC_TIMER_DEFAULT     0xFF    /* max */
167 #define HINIC_EQ_MSIX_LLI_TIMER_DEFAULT         0       /* Disabled */
168 #define HINIC_EQ_MSIX_LLI_CREDIT_LIMIT_DEFAULT  0       /* Disabled */
169 #define HINIC_EQ_MSIX_RESEND_TIMER_DEFAULT      7       /* max */
170 
171 #define HINIC_PCI_MSIX_ENTRY_SIZE               16
172 #define HINIC_PCI_MSIX_ENTRY_VECTOR_CTRL        12
173 #define HINIC_PCI_MSIX_ENTRY_CTRL_MASKBIT       1
174 
175 enum hinic_pcie_nosnoop {
176 	HINIC_PCIE_SNOOP        = 0,
177 	HINIC_PCIE_NO_SNOOP     = 1,
178 };
179 
180 enum hinic_pcie_tph {
181 	HINIC_PCIE_TPH_DISABLE  = 0,
182 	HINIC_PCIE_TPH_ENABLE   = 1,
183 };
184 
185 enum hinic_func_type {
186 	HINIC_PF        = 0,
187 	HINIC_VF	    = 1,
188 	HINIC_PPF       = 2,
189 };
190 
191 enum hinic_mod_type {
192 	HINIC_MOD_COMM  = 0,    /* HW communication module */
193 	HINIC_MOD_L2NIC = 1,    /* L2NIC module */
194 	HINIC_MOD_CFGM  = 7,    /* Configuration module */
195 	HINIC_MOD_HILINK = 14,  /* Hilink module */
196 	HINIC_MOD_MAX   = 15
197 };
198 
199 enum hinic_node_id {
200 	HINIC_NODE_ID_MGMT = 21,
201 };
202 
203 enum hinic_pf_action {
204 	HINIC_PF_MGMT_INIT = 0x0,
205 
206 	HINIC_PF_MGMT_ACTIVE = 0x11,
207 };
208 
209 enum hinic_outbound_state {
210 	HINIC_OUTBOUND_ENABLE  = 0,
211 	HINIC_OUTBOUND_DISABLE = 1,
212 };
213 
214 enum hinic_db_state {
215 	HINIC_DB_ENABLE  = 0,
216 	HINIC_DB_DISABLE = 1,
217 };
218 
219 enum hinic_msix_state {
220 	HINIC_MSIX_ENABLE,
221 	HINIC_MSIX_DISABLE,
222 };
223 
224 struct hinic_func_attr {
225 	u16                     func_idx;
226 	u8                      pf_idx;
227 	u8                      pci_intf_idx;
228 
229 	enum hinic_func_type    func_type;
230 
231 	u8                      ppf_idx;
232 
233 	u16                     num_irqs;
234 	u8                      num_aeqs;
235 	u8                      num_ceqs;
236 
237 	u8                      num_dma_attr;
238 
239 	u16						global_vf_id_of_pf;
240 };
241 
242 struct hinic_hwif {
243 	struct pci_dev          *pdev;
244 	void __iomem            *cfg_regs_bar;
245 	void __iomem		*intr_regs_base;
246 
247 	struct hinic_func_attr  attr;
248 };
249 
250 static inline u32 hinic_hwif_read_reg(struct hinic_hwif *hwif, u32 reg)
251 {
252 	return be32_to_cpu(readl(hwif->cfg_regs_bar + reg));
253 }
254 
255 static inline void hinic_hwif_write_reg(struct hinic_hwif *hwif, u32 reg,
256 					u32 val)
257 {
258 	writel(cpu_to_be32(val), hwif->cfg_regs_bar + reg);
259 }
260 
261 int hinic_msix_attr_set(struct hinic_hwif *hwif, u16 msix_index,
262 			u8 pending_limit, u8 coalesc_timer,
263 			u8 lli_timer_cfg, u8 lli_credit_limit,
264 			u8 resend_timer);
265 
266 int hinic_msix_attr_get(struct hinic_hwif *hwif, u16 msix_index,
267 			u8 *pending_limit, u8 *coalesc_timer_cfg,
268 			u8 *lli_timer, u8 *lli_credit_limit,
269 			u8 *resend_timer);
270 
271 void hinic_set_msix_state(struct hinic_hwif *hwif, u16 msix_idx,
272 			  enum hinic_msix_state flag);
273 
274 int hinic_msix_attr_cnt_clear(struct hinic_hwif *hwif, u16 msix_index);
275 
276 void hinic_set_pf_action(struct hinic_hwif *hwif, enum hinic_pf_action action);
277 
278 enum hinic_outbound_state hinic_outbound_state_get(struct hinic_hwif *hwif);
279 
280 void hinic_outbound_state_set(struct hinic_hwif *hwif,
281 			      enum hinic_outbound_state outbound_state);
282 
283 enum hinic_db_state hinic_db_state_get(struct hinic_hwif *hwif);
284 
285 void hinic_db_state_set(struct hinic_hwif *hwif,
286 			enum hinic_db_state db_state);
287 
288 u16 hinic_glb_pf_vf_offset(struct hinic_hwif *hwif);
289 
290 u16 hinic_global_func_id_hw(struct hinic_hwif *hwif);
291 
292 u16 hinic_pf_id_of_vf_hw(struct hinic_hwif *hwif);
293 
294 int hinic_init_hwif(struct hinic_hwif *hwif, struct pci_dev *pdev);
295 
296 void hinic_free_hwif(struct hinic_hwif *hwif);
297 
298 #endif
299