1 // SPDX-License-Identifier: GPL-2.0-only
3 * Huawei HiNIC PCI Express Linux driver
4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
8 #include <linux/device.h>
9 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/bitops.h>
13 #include <linux/delay.h>
15 #include "hinic_hw_csr.h"
16 #include "hinic_hw_if.h"
18 #define PCIE_ATTR_ENTRY 0
20 #define VALID_MSIX_IDX(attr, msix_index) ((msix_index) < (attr)->num_irqs)
22 #define WAIT_HWIF_READY_TIMEOUT 10000
24 #define HINIC_SELFTEST_RESULT 0x883C
27 * hinic_msix_attr_set - set message attribute for msix entry
28 * @hwif: the HW interface of a pci function device
29 * @msix_index: msix_index
30 * @pending_limit: the maximum pending interrupt events (unit 8)
31 * @coalesc_timer: coalesc period for interrupt (unit 8 us)
32 * @lli_timer: replenishing period for low latency credit (unit 8 us)
33 * @lli_credit_limit: maximum credits for low latency msix messages (unit 8)
34 * @resend_timer: maximum wait for resending msix (unit coalesc period)
36 * Return 0 - Success, negative - Failure
38 int hinic_msix_attr_set(struct hinic_hwif
*hwif
, u16 msix_index
,
39 u8 pending_limit
, u8 coalesc_timer
,
40 u8 lli_timer
, u8 lli_credit_limit
,
45 if (!VALID_MSIX_IDX(&hwif
->attr
, msix_index
))
48 msix_ctrl
= HINIC_MSIX_ATTR_SET(pending_limit
, PENDING_LIMIT
) |
49 HINIC_MSIX_ATTR_SET(coalesc_timer
, COALESC_TIMER
) |
50 HINIC_MSIX_ATTR_SET(lli_timer
, LLI_TIMER
) |
51 HINIC_MSIX_ATTR_SET(lli_credit_limit
, LLI_CREDIT
) |
52 HINIC_MSIX_ATTR_SET(resend_timer
, RESEND_TIMER
);
54 addr
= HINIC_CSR_MSIX_CTRL_ADDR(msix_index
);
56 hinic_hwif_write_reg(hwif
, addr
, msix_ctrl
);
61 * hinic_msix_attr_get - get message attribute of msix entry
62 * @hwif: the HW interface of a pci function device
63 * @msix_index: msix_index
64 * @pending_limit: the maximum pending interrupt events (unit 8)
65 * @coalesc_timer: coalesc period for interrupt (unit 8 us)
66 * @lli_timer: replenishing period for low latency credit (unit 8 us)
67 * @lli_credit_limit: maximum credits for low latency msix messages (unit 8)
68 * @resend_timer: maximum wait for resending msix (unit coalesc period)
70 * Return 0 - Success, negative - Failure
72 int hinic_msix_attr_get(struct hinic_hwif
*hwif
, u16 msix_index
,
73 u8
*pending_limit
, u8
*coalesc_timer
,
74 u8
*lli_timer
, u8
*lli_credit_limit
,
79 if (!VALID_MSIX_IDX(&hwif
->attr
, msix_index
))
82 addr
= HINIC_CSR_MSIX_CTRL_ADDR(msix_index
);
83 val
= hinic_hwif_read_reg(hwif
, addr
);
85 *pending_limit
= HINIC_MSIX_ATTR_GET(val
, PENDING_LIMIT
);
86 *coalesc_timer
= HINIC_MSIX_ATTR_GET(val
, COALESC_TIMER
);
87 *lli_timer
= HINIC_MSIX_ATTR_GET(val
, LLI_TIMER
);
88 *lli_credit_limit
= HINIC_MSIX_ATTR_GET(val
, LLI_CREDIT
);
89 *resend_timer
= HINIC_MSIX_ATTR_GET(val
, RESEND_TIMER
);
94 * hinic_msix_attr_cnt_clear - clear message attribute counters for msix entry
95 * @hwif: the HW interface of a pci function device
96 * @msix_index: msix_index
98 * Return 0 - Success, negative - Failure
100 int hinic_msix_attr_cnt_clear(struct hinic_hwif
*hwif
, u16 msix_index
)
104 if (!VALID_MSIX_IDX(&hwif
->attr
, msix_index
))
107 msix_ctrl
= HINIC_MSIX_CNT_SET(1, RESEND_TIMER
);
108 addr
= HINIC_CSR_MSIX_CNT_ADDR(msix_index
);
110 hinic_hwif_write_reg(hwif
, addr
, msix_ctrl
);
115 * hinic_set_pf_action - set action on pf channel
116 * @hwif: the HW interface of a pci function device
117 * @action: action on pf channel
119 * Return 0 - Success, negative - Failure
121 void hinic_set_pf_action(struct hinic_hwif
*hwif
, enum hinic_pf_action action
)
125 if (HINIC_IS_VF(hwif
))
128 attr5
= hinic_hwif_read_reg(hwif
, HINIC_CSR_FUNC_ATTR5_ADDR
);
129 attr5
= HINIC_FA5_CLEAR(attr5
, PF_ACTION
);
130 attr5
|= HINIC_FA5_SET(action
, PF_ACTION
);
132 hinic_hwif_write_reg(hwif
, HINIC_CSR_FUNC_ATTR5_ADDR
, attr5
);
135 enum hinic_outbound_state
hinic_outbound_state_get(struct hinic_hwif
*hwif
)
137 u32 attr4
= hinic_hwif_read_reg(hwif
, HINIC_CSR_FUNC_ATTR4_ADDR
);
139 return HINIC_FA4_GET(attr4
, OUTBOUND_STATE
);
142 void hinic_outbound_state_set(struct hinic_hwif
*hwif
,
143 enum hinic_outbound_state outbound_state
)
145 u32 attr4
= hinic_hwif_read_reg(hwif
, HINIC_CSR_FUNC_ATTR4_ADDR
);
147 attr4
= HINIC_FA4_CLEAR(attr4
, OUTBOUND_STATE
);
148 attr4
|= HINIC_FA4_SET(outbound_state
, OUTBOUND_STATE
);
150 hinic_hwif_write_reg(hwif
, HINIC_CSR_FUNC_ATTR4_ADDR
, attr4
);
153 enum hinic_db_state
hinic_db_state_get(struct hinic_hwif
*hwif
)
155 u32 attr4
= hinic_hwif_read_reg(hwif
, HINIC_CSR_FUNC_ATTR4_ADDR
);
157 return HINIC_FA4_GET(attr4
, DB_STATE
);
160 void hinic_db_state_set(struct hinic_hwif
*hwif
,
161 enum hinic_db_state db_state
)
163 u32 attr4
= hinic_hwif_read_reg(hwif
, HINIC_CSR_FUNC_ATTR4_ADDR
);
165 attr4
= HINIC_FA4_CLEAR(attr4
, DB_STATE
);
166 attr4
|= HINIC_FA4_SET(db_state
, DB_STATE
);
168 hinic_hwif_write_reg(hwif
, HINIC_CSR_FUNC_ATTR4_ADDR
, attr4
);
171 void hinic_set_msix_state(struct hinic_hwif
*hwif
, u16 msix_idx
,
172 enum hinic_msix_state flag
)
174 u32 offset
= msix_idx
* HINIC_PCI_MSIX_ENTRY_SIZE
+
175 HINIC_PCI_MSIX_ENTRY_VECTOR_CTRL
;
178 mask_bits
= readl(hwif
->intr_regs_base
+ offset
);
179 mask_bits
&= ~HINIC_PCI_MSIX_ENTRY_CTRL_MASKBIT
;
182 mask_bits
|= HINIC_PCI_MSIX_ENTRY_CTRL_MASKBIT
;
184 writel(mask_bits
, hwif
->intr_regs_base
+ offset
);
188 * hwif_ready - test if the HW is ready for use
189 * @hwif: the HW interface of a pci function device
191 * Return 0 - Success, negative - Failure
193 static int hwif_ready(struct hinic_hwif
*hwif
)
197 addr
= HINIC_CSR_FUNC_ATTR1_ADDR
;
198 attr1
= hinic_hwif_read_reg(hwif
, addr
);
200 if (!HINIC_FA1_GET(attr1
, MGMT_INIT_STATUS
))
203 if (HINIC_IS_VF(hwif
)) {
204 if (!HINIC_FA1_GET(attr1
, PF_INIT_STATUS
))
211 static int wait_hwif_ready(struct hinic_hwif
*hwif
)
213 unsigned long timeout
= 0;
216 if (!hwif_ready(hwif
))
219 usleep_range(999, 1000);
221 } while (timeout
<= WAIT_HWIF_READY_TIMEOUT
);
223 dev_err(&hwif
->pdev
->dev
, "Wait for hwif timeout\n");
229 * set_hwif_attr - set the attributes in the relevant members in hwif
230 * @hwif: the HW interface of a pci function device
231 * @attr0: the first attribute that was read from the hw
232 * @attr1: the second attribute that was read from the hw
233 * @attr2: the third attribute that was read from the hw
235 static void set_hwif_attr(struct hinic_hwif
*hwif
, u32 attr0
, u32 attr1
,
238 hwif
->attr
.func_idx
= HINIC_FA0_GET(attr0
, FUNC_IDX
);
239 hwif
->attr
.pf_idx
= HINIC_FA0_GET(attr0
, PF_IDX
);
240 hwif
->attr
.pci_intf_idx
= HINIC_FA0_GET(attr0
, PCI_INTF_IDX
);
241 hwif
->attr
.func_type
= HINIC_FA0_GET(attr0
, FUNC_TYPE
);
243 hwif
->attr
.num_aeqs
= BIT(HINIC_FA1_GET(attr1
, AEQS_PER_FUNC
));
244 hwif
->attr
.num_ceqs
= BIT(HINIC_FA1_GET(attr1
, CEQS_PER_FUNC
));
245 hwif
->attr
.num_irqs
= BIT(HINIC_FA1_GET(attr1
, IRQS_PER_FUNC
));
246 hwif
->attr
.num_dma_attr
= BIT(HINIC_FA1_GET(attr1
, DMA_ATTR_PER_FUNC
));
247 hwif
->attr
.global_vf_id_of_pf
= HINIC_FA2_GET(attr2
,
252 * read_hwif_attr - read the attributes and set members in hwif
253 * @hwif: the HW interface of a pci function device
255 static void read_hwif_attr(struct hinic_hwif
*hwif
)
257 u32 addr
, attr0
, attr1
, attr2
;
259 addr
= HINIC_CSR_FUNC_ATTR0_ADDR
;
260 attr0
= hinic_hwif_read_reg(hwif
, addr
);
262 addr
= HINIC_CSR_FUNC_ATTR1_ADDR
;
263 attr1
= hinic_hwif_read_reg(hwif
, addr
);
265 addr
= HINIC_CSR_FUNC_ATTR2_ADDR
;
266 attr2
= hinic_hwif_read_reg(hwif
, addr
);
268 set_hwif_attr(hwif
, attr0
, attr1
, attr2
);
272 * set_ppf - try to set hwif as ppf and set the type of hwif in this case
273 * @hwif: the HW interface of a pci function device
275 static void set_ppf(struct hinic_hwif
*hwif
)
277 struct hinic_func_attr
*attr
= &hwif
->attr
;
278 u32 addr
, val
, ppf_election
;
280 /* Read Modify Write */
281 addr
= HINIC_CSR_PPF_ELECTION_ADDR(HINIC_HWIF_PCI_INTF(hwif
));
283 val
= hinic_hwif_read_reg(hwif
, addr
);
284 val
= HINIC_PPF_ELECTION_CLEAR(val
, IDX
);
286 ppf_election
= HINIC_PPF_ELECTION_SET(HINIC_HWIF_FUNC_IDX(hwif
), IDX
);
289 hinic_hwif_write_reg(hwif
, addr
, val
);
292 val
= hinic_hwif_read_reg(hwif
, addr
);
294 attr
->ppf_idx
= HINIC_PPF_ELECTION_GET(val
, IDX
);
295 if (attr
->ppf_idx
== HINIC_HWIF_FUNC_IDX(hwif
))
296 attr
->func_type
= HINIC_PPF
;
300 * set_dma_attr - set the dma attributes in the HW
301 * @hwif: the HW interface of a pci function device
302 * @entry_idx: the entry index in the dma table
303 * @st: PCIE TLP steering tag
304 * @at: PCIE TLP AT field
305 * @ph: PCIE TLP Processing Hint field
306 * @no_snooping: PCIE TLP No snooping
307 * @tph_en: PCIE TLP Processing Hint Enable
309 static void set_dma_attr(struct hinic_hwif
*hwif
, u32 entry_idx
,
311 enum hinic_pcie_nosnoop no_snooping
,
312 enum hinic_pcie_tph tph_en
)
314 u32 addr
, val
, dma_attr_entry
;
316 /* Read Modify Write */
317 addr
= HINIC_CSR_DMA_ATTR_ADDR(entry_idx
);
319 val
= hinic_hwif_read_reg(hwif
, addr
);
320 val
= HINIC_DMA_ATTR_CLEAR(val
, ST
) &
321 HINIC_DMA_ATTR_CLEAR(val
, AT
) &
322 HINIC_DMA_ATTR_CLEAR(val
, PH
) &
323 HINIC_DMA_ATTR_CLEAR(val
, NO_SNOOPING
) &
324 HINIC_DMA_ATTR_CLEAR(val
, TPH_EN
);
326 dma_attr_entry
= HINIC_DMA_ATTR_SET(st
, ST
) |
327 HINIC_DMA_ATTR_SET(at
, AT
) |
328 HINIC_DMA_ATTR_SET(ph
, PH
) |
329 HINIC_DMA_ATTR_SET(no_snooping
, NO_SNOOPING
) |
330 HINIC_DMA_ATTR_SET(tph_en
, TPH_EN
);
332 val
|= dma_attr_entry
;
333 hinic_hwif_write_reg(hwif
, addr
, val
);
337 * dma_attr_table_init - initialize the the default dma attributes
338 * @hwif: the HW interface of a pci function device
340 static void dma_attr_init(struct hinic_hwif
*hwif
)
342 set_dma_attr(hwif
, PCIE_ATTR_ENTRY
, HINIC_PCIE_ST_DISABLE
,
343 HINIC_PCIE_AT_DISABLE
, HINIC_PCIE_PH_DISABLE
,
344 HINIC_PCIE_SNOOP
, HINIC_PCIE_TPH_DISABLE
);
347 u16
hinic_glb_pf_vf_offset(struct hinic_hwif
*hwif
)
352 return hwif
->attr
.global_vf_id_of_pf
;
355 u16
hinic_global_func_id_hw(struct hinic_hwif
*hwif
)
359 addr
= HINIC_CSR_FUNC_ATTR0_ADDR
;
360 attr0
= hinic_hwif_read_reg(hwif
, addr
);
362 return HINIC_FA0_GET(attr0
, FUNC_IDX
);
365 u16
hinic_pf_id_of_vf_hw(struct hinic_hwif
*hwif
)
369 addr
= HINIC_CSR_FUNC_ATTR0_ADDR
;
370 attr0
= hinic_hwif_read_reg(hwif
, addr
);
372 return HINIC_FA0_GET(attr0
, PF_IDX
);
375 static void __print_selftest_reg(struct hinic_hwif
*hwif
)
377 u32 addr
, attr0
, attr1
;
379 addr
= HINIC_CSR_FUNC_ATTR1_ADDR
;
380 attr1
= hinic_hwif_read_reg(hwif
, addr
);
382 if (attr1
== HINIC_PCIE_LINK_DOWN
) {
383 dev_err(&hwif
->pdev
->dev
, "PCIE is link down\n");
387 addr
= HINIC_CSR_FUNC_ATTR0_ADDR
;
388 attr0
= hinic_hwif_read_reg(hwif
, addr
);
389 if (HINIC_FA0_GET(attr0
, FUNC_TYPE
) != HINIC_VF
&&
390 !HINIC_FA0_GET(attr0
, PCI_INTF_IDX
))
391 dev_err(&hwif
->pdev
->dev
, "Selftest reg: 0x%08x\n",
392 hinic_hwif_read_reg(hwif
, HINIC_SELFTEST_RESULT
));
396 * hinic_init_hwif - initialize the hw interface
397 * @hwif: the HW interface of a pci function device
398 * @pdev: the pci device for acessing PCI resources
400 * Return 0 - Success, negative - Failure
402 int hinic_init_hwif(struct hinic_hwif
*hwif
, struct pci_dev
*pdev
)
408 hwif
->cfg_regs_bar
= pci_ioremap_bar(pdev
, HINIC_PCI_CFG_REGS_BAR
);
409 if (!hwif
->cfg_regs_bar
) {
410 dev_err(&pdev
->dev
, "Failed to map configuration regs\n");
414 hwif
->intr_regs_base
= pci_ioremap_bar(pdev
, HINIC_PCI_INTR_REGS_BAR
);
415 if (!hwif
->intr_regs_base
) {
416 dev_err(&pdev
->dev
, "Failed to map configuration regs\n");
418 goto err_map_intr_bar
;
421 err
= wait_hwif_ready(hwif
);
423 dev_err(&pdev
->dev
, "HW interface is not ready\n");
424 __print_selftest_reg(hwif
);
428 read_hwif_attr(hwif
);
430 if (HINIC_IS_PF(hwif
))
433 /* No transactionss before DMA is initialized */
438 iounmap(hwif
->intr_regs_base
);
441 iounmap(hwif
->cfg_regs_bar
);
447 * hinic_free_hwif - free the HW interface
448 * @hwif: the HW interface of a pci function device
450 void hinic_free_hwif(struct hinic_hwif
*hwif
)
452 iounmap(hwif
->intr_regs_base
);
453 iounmap(hwif
->cfg_regs_bar
);