1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Huawei HiNIC PCI Express Linux driver
4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
10 #include <linux/pci.h>
12 #include <linux/types.h>
13 #include <asm/byteorder.h>
15 #define HINIC_PCIE_LINK_DOWN 0xFFFFFFFF
17 #define HINIC_DMA_ATTR_ST_SHIFT 0
18 #define HINIC_DMA_ATTR_AT_SHIFT 8
19 #define HINIC_DMA_ATTR_PH_SHIFT 10
20 #define HINIC_DMA_ATTR_NO_SNOOPING_SHIFT 12
21 #define HINIC_DMA_ATTR_TPH_EN_SHIFT 13
23 #define HINIC_DMA_ATTR_ST_MASK 0xFF
24 #define HINIC_DMA_ATTR_AT_MASK 0x3
25 #define HINIC_DMA_ATTR_PH_MASK 0x3
26 #define HINIC_DMA_ATTR_NO_SNOOPING_MASK 0x1
27 #define HINIC_DMA_ATTR_TPH_EN_MASK 0x1
29 #define HINIC_DMA_ATTR_SET(val, member) \
30 (((u32)(val) & HINIC_DMA_ATTR_##member##_MASK) << \
31 HINIC_DMA_ATTR_##member##_SHIFT)
33 #define HINIC_DMA_ATTR_CLEAR(val, member) \
34 ((val) & (~(HINIC_DMA_ATTR_##member##_MASK \
35 << HINIC_DMA_ATTR_##member##_SHIFT)))
37 #define HINIC_FA0_FUNC_IDX_SHIFT 0
38 #define HINIC_FA0_PF_IDX_SHIFT 10
39 #define HINIC_FA0_PCI_INTF_IDX_SHIFT 14
40 #define HINIC_FA0_VF_IN_PF_SHIFT 16
41 /* reserved members - off 16 */
42 #define HINIC_FA0_FUNC_TYPE_SHIFT 24
44 #define HINIC_FA0_FUNC_IDX_MASK 0x3FF
45 #define HINIC_FA0_PF_IDX_MASK 0xF
46 #define HINIC_FA0_PCI_INTF_IDX_MASK 0x3
47 #define HINIC_FA0_FUNC_TYPE_MASK 0x1
48 #define HINIC_FA0_VF_IN_PF_MASK 0xFF
50 #define HINIC_FA0_GET(val, member) \
51 (((val) >> HINIC_FA0_##member##_SHIFT) & HINIC_FA0_##member##_MASK)
53 #define HINIC_FA1_AEQS_PER_FUNC_SHIFT 8
54 /* reserved members - off 10 */
55 #define HINIC_FA1_CEQS_PER_FUNC_SHIFT 12
56 /* reserved members - off 15 */
57 #define HINIC_FA1_IRQS_PER_FUNC_SHIFT 20
58 #define HINIC_FA1_DMA_ATTR_PER_FUNC_SHIFT 24
59 /* reserved members - off 27 */
60 #define HINIC_FA1_MGMT_INIT_STATUS_SHIFT 30
61 #define HINIC_FA1_PF_INIT_STATUS_SHIFT 31
63 #define HINIC_FA1_AEQS_PER_FUNC_MASK 0x3
64 #define HINIC_FA1_CEQS_PER_FUNC_MASK 0x7
65 #define HINIC_FA1_IRQS_PER_FUNC_MASK 0xF
66 #define HINIC_FA1_DMA_ATTR_PER_FUNC_MASK 0x7
67 #define HINIC_FA1_MGMT_INIT_STATUS_MASK 0x1
68 #define HINIC_FA1_PF_INIT_STATUS_MASK 0x1
70 #define HINIC_FA1_GET(val, member) \
71 (((val) >> HINIC_FA1_##member##_SHIFT) & HINIC_FA1_##member##_MASK)
73 #define HINIC_FA2_GLOBAL_VF_ID_OF_PF_SHIFT 16
74 #define HINIC_FA2_GLOBAL_VF_ID_OF_PF_MASK 0x3FF
76 #define HINIC_FA2_GET(val, member) \
77 (((val) >> HINIC_FA2_##member##_SHIFT) & HINIC_FA2_##member##_MASK)
79 #define HINIC_FA4_OUTBOUND_STATE_SHIFT 0
80 #define HINIC_FA4_DB_STATE_SHIFT 1
82 #define HINIC_FA4_OUTBOUND_STATE_MASK 0x1
83 #define HINIC_FA4_DB_STATE_MASK 0x1
85 #define HINIC_FA4_GET(val, member) \
86 (((val) >> HINIC_FA4_##member##_SHIFT) & HINIC_FA4_##member##_MASK)
88 #define HINIC_FA4_SET(val, member) \
89 ((((u32)val) & HINIC_FA4_##member##_MASK) << HINIC_FA4_##member##_SHIFT)
91 #define HINIC_FA4_CLEAR(val, member) \
92 ((val) & (~(HINIC_FA4_##member##_MASK << HINIC_FA4_##member##_SHIFT)))
94 #define HINIC_FA5_PF_ACTION_SHIFT 0
95 #define HINIC_FA5_PF_ACTION_MASK 0xFFFF
97 #define HINIC_FA5_SET(val, member) \
98 (((u32)(val) & HINIC_FA5_##member##_MASK) << HINIC_FA5_##member##_SHIFT)
100 #define HINIC_FA5_CLEAR(val, member) \
101 ((val) & (~(HINIC_FA5_##member##_MASK << HINIC_FA5_##member##_SHIFT)))
103 #define HINIC_PPF_ELECTION_IDX_SHIFT 0
104 #define HINIC_PPF_ELECTION_IDX_MASK 0x1F
106 #define HINIC_PPF_ELECTION_SET(val, member) \
107 (((u32)(val) & HINIC_PPF_ELECTION_##member##_MASK) << \
108 HINIC_PPF_ELECTION_##member##_SHIFT)
110 #define HINIC_PPF_ELECTION_GET(val, member) \
111 (((val) >> HINIC_PPF_ELECTION_##member##_SHIFT) & \
112 HINIC_PPF_ELECTION_##member##_MASK)
114 #define HINIC_PPF_ELECTION_CLEAR(val, member) \
115 ((val) & (~(HINIC_PPF_ELECTION_##member##_MASK \
116 << HINIC_PPF_ELECTION_##member##_SHIFT)))
118 #define HINIC_MSIX_PENDING_LIMIT_SHIFT 0
119 #define HINIC_MSIX_COALESC_TIMER_SHIFT 8
120 #define HINIC_MSIX_LLI_TIMER_SHIFT 16
121 #define HINIC_MSIX_LLI_CREDIT_SHIFT 24
122 #define HINIC_MSIX_RESEND_TIMER_SHIFT 29
124 #define HINIC_MSIX_PENDING_LIMIT_MASK 0xFF
125 #define HINIC_MSIX_COALESC_TIMER_MASK 0xFF
126 #define HINIC_MSIX_LLI_TIMER_MASK 0xFF
127 #define HINIC_MSIX_LLI_CREDIT_MASK 0x1F
128 #define HINIC_MSIX_RESEND_TIMER_MASK 0x7
130 #define HINIC_MSIX_ATTR_SET(val, member) \
131 (((u32)(val) & HINIC_MSIX_##member##_MASK) << \
132 HINIC_MSIX_##member##_SHIFT)
134 #define HINIC_MSIX_CNT_RESEND_TIMER_SHIFT 29
136 #define HINIC_MSIX_CNT_RESEND_TIMER_MASK 0x1
138 #define HINIC_MSIX_CNT_SET(val, member) \
139 (((u32)(val) & HINIC_MSIX_CNT_##member##_MASK) << \
140 HINIC_MSIX_CNT_##member##_SHIFT)
142 #define HINIC_HWIF_NUM_AEQS(hwif) ((hwif)->attr.num_aeqs)
143 #define HINIC_HWIF_NUM_CEQS(hwif) ((hwif)->attr.num_ceqs)
144 #define HINIC_HWIF_NUM_IRQS(hwif) ((hwif)->attr.num_irqs)
145 #define HINIC_HWIF_FUNC_IDX(hwif) ((hwif)->attr.func_idx)
146 #define HINIC_HWIF_PCI_INTF(hwif) ((hwif)->attr.pci_intf_idx)
147 #define HINIC_HWIF_PF_IDX(hwif) ((hwif)->attr.pf_idx)
148 #define HINIC_HWIF_PPF_IDX(hwif) ((hwif)->attr.ppf_idx)
150 #define HINIC_FUNC_TYPE(hwif) ((hwif)->attr.func_type)
151 #define HINIC_IS_VF(hwif) (HINIC_FUNC_TYPE(hwif) == HINIC_VF)
152 #define HINIC_IS_PF(hwif) (HINIC_FUNC_TYPE(hwif) == HINIC_PF)
153 #define HINIC_IS_PPF(hwif) (HINIC_FUNC_TYPE(hwif) == HINIC_PPF)
155 #define HINIC_PCI_CFG_REGS_BAR 0
156 #define HINIC_PCI_INTR_REGS_BAR 2
157 #define HINIC_PCI_DB_BAR 4
159 #define HINIC_PCIE_ST_DISABLE 0
160 #define HINIC_PCIE_AT_DISABLE 0
161 #define HINIC_PCIE_PH_DISABLE 0
163 #define HINIC_EQ_MSIX_PENDING_LIMIT_DEFAULT 0 /* Disabled */
164 #define HINIC_EQ_MSIX_COALESC_TIMER_DEFAULT 0xFF /* max */
165 #define HINIC_EQ_MSIX_LLI_TIMER_DEFAULT 0 /* Disabled */
166 #define HINIC_EQ_MSIX_LLI_CREDIT_LIMIT_DEFAULT 0 /* Disabled */
167 #define HINIC_EQ_MSIX_RESEND_TIMER_DEFAULT 7 /* max */
169 #define HINIC_PCI_MSIX_ENTRY_SIZE 16
170 #define HINIC_PCI_MSIX_ENTRY_VECTOR_CTRL 12
171 #define HINIC_PCI_MSIX_ENTRY_CTRL_MASKBIT 1
173 enum hinic_pcie_nosnoop {
174 HINIC_PCIE_SNOOP = 0,
175 HINIC_PCIE_NO_SNOOP = 1,
178 enum hinic_pcie_tph {
179 HINIC_PCIE_TPH_DISABLE = 0,
180 HINIC_PCIE_TPH_ENABLE = 1,
183 enum hinic_func_type {
189 enum hinic_mod_type {
190 HINIC_MOD_COMM = 0, /* HW communication module */
191 HINIC_MOD_L2NIC = 1, /* L2NIC module */
192 HINIC_MOD_CFGM = 7, /* Configuration module */
193 HINIC_MOD_HILINK = 14, /* Hilink module */
198 HINIC_NODE_ID_MGMT = 21,
201 enum hinic_pf_action {
202 HINIC_PF_MGMT_INIT = 0x0,
204 HINIC_PF_MGMT_ACTIVE = 0x11,
207 enum hinic_outbound_state {
208 HINIC_OUTBOUND_ENABLE = 0,
209 HINIC_OUTBOUND_DISABLE = 1,
212 enum hinic_db_state {
214 HINIC_DB_DISABLE = 1,
217 enum hinic_msix_state {
222 struct hinic_func_attr {
227 enum hinic_func_type func_type;
237 u16 global_vf_id_of_pf;
241 struct pci_dev *pdev;
242 void __iomem *cfg_regs_bar;
243 void __iomem *intr_regs_base;
245 struct hinic_func_attr attr;
248 static inline u32 hinic_hwif_read_reg(struct hinic_hwif *hwif, u32 reg)
250 u32 out = readl(hwif->cfg_regs_bar + reg);
252 return be32_to_cpu(*(__be32 *)&out);
255 static inline void hinic_hwif_write_reg(struct hinic_hwif *hwif, u32 reg,
258 __be32 in = cpu_to_be32(val);
260 writel(*(u32 *)&in, hwif->cfg_regs_bar + reg);
263 int hinic_msix_attr_set(struct hinic_hwif *hwif, u16 msix_index,
264 u8 pending_limit, u8 coalesc_timer,
265 u8 lli_timer_cfg, u8 lli_credit_limit,
268 void hinic_set_msix_state(struct hinic_hwif *hwif, u16 msix_idx,
269 enum hinic_msix_state flag);
271 int hinic_msix_attr_cnt_clear(struct hinic_hwif *hwif, u16 msix_index);
273 void hinic_set_pf_action(struct hinic_hwif *hwif, enum hinic_pf_action action);
275 enum hinic_outbound_state hinic_outbound_state_get(struct hinic_hwif *hwif);
277 void hinic_outbound_state_set(struct hinic_hwif *hwif,
278 enum hinic_outbound_state outbound_state);
280 enum hinic_db_state hinic_db_state_get(struct hinic_hwif *hwif);
282 void hinic_db_state_set(struct hinic_hwif *hwif,
283 enum hinic_db_state db_state);
285 u16 hinic_glb_pf_vf_offset(struct hinic_hwif *hwif);
287 u16 hinic_global_func_id_hw(struct hinic_hwif *hwif);
289 u16 hinic_pf_id_of_vf_hw(struct hinic_hwif *hwif);
291 int hinic_init_hwif(struct hinic_hwif *hwif, struct pci_dev *pdev);
293 void hinic_free_hwif(struct hinic_hwif *hwif);