1*17fcb3dcSFan Gong /* SPDX-License-Identifier: GPL-2.0 */
2*17fcb3dcSFan Gong /* Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved. */
3*17fcb3dcSFan Gong
4*17fcb3dcSFan Gong #ifndef _HINIC3_NIC_IO_H_
5*17fcb3dcSFan Gong #define _HINIC3_NIC_IO_H_
6*17fcb3dcSFan Gong
7*17fcb3dcSFan Gong #include <linux/bitfield.h>
8*17fcb3dcSFan Gong
9*17fcb3dcSFan Gong #include "hinic3_wq.h"
10*17fcb3dcSFan Gong
11*17fcb3dcSFan Gong struct hinic3_nic_dev;
12*17fcb3dcSFan Gong
13*17fcb3dcSFan Gong #define HINIC3_SQ_WQEBB_SHIFT 4
14*17fcb3dcSFan Gong #define HINIC3_RQ_WQEBB_SHIFT 3
15*17fcb3dcSFan Gong #define HINIC3_SQ_WQEBB_SIZE BIT(HINIC3_SQ_WQEBB_SHIFT)
16*17fcb3dcSFan Gong
17*17fcb3dcSFan Gong /* ******************** RQ_CTRL ******************** */
18*17fcb3dcSFan Gong enum hinic3_rq_wqe_type {
19*17fcb3dcSFan Gong HINIC3_NORMAL_RQ_WQE = 1,
20*17fcb3dcSFan Gong };
21*17fcb3dcSFan Gong
22*17fcb3dcSFan Gong /* ******************** SQ_CTRL ******************** */
23*17fcb3dcSFan Gong #define HINIC3_TX_MSS_DEFAULT 0x3E00
24*17fcb3dcSFan Gong #define HINIC3_TX_MSS_MIN 0x50
25*17fcb3dcSFan Gong #define HINIC3_MAX_SQ_SGE 18
26*17fcb3dcSFan Gong
27*17fcb3dcSFan Gong struct hinic3_io_queue {
28*17fcb3dcSFan Gong struct hinic3_wq wq;
29*17fcb3dcSFan Gong u8 owner;
30*17fcb3dcSFan Gong u16 q_id;
31*17fcb3dcSFan Gong u16 msix_entry_idx;
32*17fcb3dcSFan Gong u8 __iomem *db_addr;
33*17fcb3dcSFan Gong u16 *cons_idx_addr;
34*17fcb3dcSFan Gong } ____cacheline_aligned;
35*17fcb3dcSFan Gong
hinic3_get_sq_local_ci(const struct hinic3_io_queue * sq)36*17fcb3dcSFan Gong static inline u16 hinic3_get_sq_local_ci(const struct hinic3_io_queue *sq)
37*17fcb3dcSFan Gong {
38*17fcb3dcSFan Gong const struct hinic3_wq *wq = &sq->wq;
39*17fcb3dcSFan Gong
40*17fcb3dcSFan Gong return wq->cons_idx & wq->idx_mask;
41*17fcb3dcSFan Gong }
42*17fcb3dcSFan Gong
hinic3_get_sq_local_pi(const struct hinic3_io_queue * sq)43*17fcb3dcSFan Gong static inline u16 hinic3_get_sq_local_pi(const struct hinic3_io_queue *sq)
44*17fcb3dcSFan Gong {
45*17fcb3dcSFan Gong const struct hinic3_wq *wq = &sq->wq;
46*17fcb3dcSFan Gong
47*17fcb3dcSFan Gong return wq->prod_idx & wq->idx_mask;
48*17fcb3dcSFan Gong }
49*17fcb3dcSFan Gong
hinic3_get_sq_hw_ci(const struct hinic3_io_queue * sq)50*17fcb3dcSFan Gong static inline u16 hinic3_get_sq_hw_ci(const struct hinic3_io_queue *sq)
51*17fcb3dcSFan Gong {
52*17fcb3dcSFan Gong const struct hinic3_wq *wq = &sq->wq;
53*17fcb3dcSFan Gong
54*17fcb3dcSFan Gong return READ_ONCE(*sq->cons_idx_addr) & wq->idx_mask;
55*17fcb3dcSFan Gong }
56*17fcb3dcSFan Gong
57*17fcb3dcSFan Gong /* ******************** DB INFO ******************** */
58*17fcb3dcSFan Gong #define DB_INFO_QID_MASK GENMASK(12, 0)
59*17fcb3dcSFan Gong #define DB_INFO_CFLAG_MASK BIT(23)
60*17fcb3dcSFan Gong #define DB_INFO_COS_MASK GENMASK(26, 24)
61*17fcb3dcSFan Gong #define DB_INFO_TYPE_MASK GENMASK(31, 27)
62*17fcb3dcSFan Gong #define DB_INFO_SET(val, member) \
63*17fcb3dcSFan Gong FIELD_PREP(DB_INFO_##member##_MASK, val)
64*17fcb3dcSFan Gong
65*17fcb3dcSFan Gong #define DB_PI_LOW_MASK 0xFFU
66*17fcb3dcSFan Gong #define DB_PI_HIGH_MASK 0xFFU
67*17fcb3dcSFan Gong #define DB_PI_HI_SHIFT 8
68*17fcb3dcSFan Gong #define DB_PI_LOW(pi) ((pi) & DB_PI_LOW_MASK)
69*17fcb3dcSFan Gong #define DB_PI_HIGH(pi) (((pi) >> DB_PI_HI_SHIFT) & DB_PI_HIGH_MASK)
70*17fcb3dcSFan Gong #define DB_ADDR(q, pi) ((u64 __iomem *)((q)->db_addr) + DB_PI_LOW(pi))
71*17fcb3dcSFan Gong #define DB_SRC_TYPE 1
72*17fcb3dcSFan Gong
73*17fcb3dcSFan Gong /* CFLAG_DATA_PATH */
74*17fcb3dcSFan Gong #define DB_CFLAG_DP_SQ 0
75*17fcb3dcSFan Gong #define DB_CFLAG_DP_RQ 1
76*17fcb3dcSFan Gong
77*17fcb3dcSFan Gong struct hinic3_nic_db {
78*17fcb3dcSFan Gong u32 db_info;
79*17fcb3dcSFan Gong u32 pi_hi;
80*17fcb3dcSFan Gong };
81*17fcb3dcSFan Gong
hinic3_write_db(struct hinic3_io_queue * queue,int cos,u8 cflag,u16 pi)82*17fcb3dcSFan Gong static inline void hinic3_write_db(struct hinic3_io_queue *queue, int cos,
83*17fcb3dcSFan Gong u8 cflag, u16 pi)
84*17fcb3dcSFan Gong {
85*17fcb3dcSFan Gong struct hinic3_nic_db db;
86*17fcb3dcSFan Gong
87*17fcb3dcSFan Gong db.db_info = DB_INFO_SET(DB_SRC_TYPE, TYPE) |
88*17fcb3dcSFan Gong DB_INFO_SET(cflag, CFLAG) |
89*17fcb3dcSFan Gong DB_INFO_SET(cos, COS) |
90*17fcb3dcSFan Gong DB_INFO_SET(queue->q_id, QID);
91*17fcb3dcSFan Gong db.pi_hi = DB_PI_HIGH(pi);
92*17fcb3dcSFan Gong
93*17fcb3dcSFan Gong writeq(*((u64 *)&db), DB_ADDR(queue, pi));
94*17fcb3dcSFan Gong }
95*17fcb3dcSFan Gong
96*17fcb3dcSFan Gong struct hinic3_nic_io {
97*17fcb3dcSFan Gong struct hinic3_io_queue *sq;
98*17fcb3dcSFan Gong struct hinic3_io_queue *rq;
99*17fcb3dcSFan Gong
100*17fcb3dcSFan Gong u16 num_qps;
101*17fcb3dcSFan Gong u16 max_qps;
102*17fcb3dcSFan Gong
103*17fcb3dcSFan Gong /* Base address for consumer index of all tx queues. Each queue is
104*17fcb3dcSFan Gong * given a full cache line to hold its consumer index. HW updates
105*17fcb3dcSFan Gong * current consumer index as it consumes tx WQEs.
106*17fcb3dcSFan Gong */
107*17fcb3dcSFan Gong void *ci_vaddr_base;
108*17fcb3dcSFan Gong dma_addr_t ci_dma_base;
109*17fcb3dcSFan Gong
110*17fcb3dcSFan Gong u8 __iomem *sqs_db_addr;
111*17fcb3dcSFan Gong u8 __iomem *rqs_db_addr;
112*17fcb3dcSFan Gong
113*17fcb3dcSFan Gong u16 rx_buf_len;
114*17fcb3dcSFan Gong u64 feature_cap;
115*17fcb3dcSFan Gong };
116*17fcb3dcSFan Gong
117*17fcb3dcSFan Gong int hinic3_init_nic_io(struct hinic3_nic_dev *nic_dev);
118*17fcb3dcSFan Gong void hinic3_free_nic_io(struct hinic3_nic_dev *nic_dev);
119*17fcb3dcSFan Gong
120*17fcb3dcSFan Gong #endif
121