1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2019 Netronome Systems, Inc. */
3 
4 #ifndef _NFP_DP_NFDK_H_
5 #define _NFP_DP_NFDK_H_
6 
7 #include <linux/bitops.h>
8 #include <linux/types.h>
9 
10 #define NFDK_TX_DESC_PER_SIMPLE_PKT	2
11 
12 #define NFDK_TX_MAX_DATA_PER_HEAD	SZ_4K
13 #define NFDK_TX_MAX_DATA_PER_DESC	SZ_16K
14 #define NFDK_TX_DESC_BLOCK_SZ		256
15 #define NFDK_TX_DESC_BLOCK_CNT		(NFDK_TX_DESC_BLOCK_SZ /	\
16 					 sizeof(struct nfp_nfdk_tx_desc))
17 #define NFDK_TX_DESC_STOP_CNT		(NFDK_TX_DESC_BLOCK_CNT *	\
18 					 NFDK_TX_DESC_PER_SIMPLE_PKT)
19 #define NFDK_TX_MAX_DATA_PER_BLOCK	SZ_64K
20 #define NFDK_TX_DESC_GATHER_MAX		17
21 
22 /* TX descriptor format */
23 
24 #define NFDK_DESC_TX_MSS_MASK		GENMASK(13, 0)
25 
26 #define NFDK_DESC_TX_CHAIN_META		BIT(3)
27 #define NFDK_DESC_TX_ENCAP		BIT(2)
28 #define NFDK_DESC_TX_L4_CSUM		BIT(1)
29 #define NFDK_DESC_TX_L3_CSUM		BIT(0)
30 
31 #define NFDK_DESC_TX_DMA_LEN_HEAD	GENMASK(11, 0)
32 #define NFDK_DESC_TX_TYPE_HEAD		GENMASK(15, 12)
33 #define NFDK_DESC_TX_DMA_LEN		GENMASK(13, 0)
34 #define NFDK_DESC_TX_TYPE_NOP		0
35 #define NFDK_DESC_TX_TYPE_GATHER	1
36 #define NFDK_DESC_TX_TYPE_TSO		2
37 #define NFDK_DESC_TX_TYPE_SIMPLE	8
38 #define NFDK_DESC_TX_EOP		BIT(14)
39 
40 #define NFDK_META_LEN			GENMASK(7, 0)
41 #define NFDK_META_FIELDS		GENMASK(31, 8)
42 
43 #define D_BLOCK_CPL(idx)		(NFDK_TX_DESC_BLOCK_CNT -	\
44 					 (idx) % NFDK_TX_DESC_BLOCK_CNT)
45 
46 struct nfp_nfdk_tx_desc {
47 	union {
48 		struct {
49 			u8 dma_addr_hi;  /* High bits of host buf address */
50 			u8 padding;  /* Must be zero */
51 			__le16 dma_len_type; /* Length to DMA for this desc */
52 			__le32 dma_addr_lo;  /* Low 32bit of host buf addr */
53 		};
54 
55 		struct {
56 			__le16 mss;	/* MSS to be used for LSO */
57 			u8 lso_hdrlen;  /* LSO, TCP payload offset */
58 			u8 lso_totsegs; /* LSO, total segments */
59 			u8 l3_offset;   /* L3 header offset */
60 			u8 l4_offset;   /* L4 header offset */
61 			__le16 lso_meta_res; /* Rsvd bits in TSO metadata */
62 		};
63 
64 		struct {
65 			u8 flags;	/* TX Flags, see @NFDK_DESC_TX_* */
66 			u8 reserved[7];	/* meta byte placeholder */
67 		};
68 
69 		__le32 vals[2];
70 		__le64 raw;
71 	};
72 };
73 
74 /* The device don't make use of the 2 or 3 least significant bits of the address
75  * due to alignment constraints. The driver can make use of those bits to carry
76  * information about the buffer before giving it to the device.
77  *
78  * NOTE: The driver must clear the lower bits before handing the buffer to the
79  * device.
80  *
81  * - NFDK_TX_BUF_INFO_SOP - Start of a packet
82  *   Mark the buffer as a start of a packet. This is used in the XDP TX process
83  *   to stash virtual and DMA address so that they can be recycled when the TX
84  *   operation is completed.
85  */
86 #define NFDK_TX_BUF_PTR(val) ((val) & ~(sizeof(void *) - 1))
87 #define NFDK_TX_BUF_INFO(val) ((val) & (sizeof(void *) - 1))
88 #define NFDK_TX_BUF_INFO_SOP BIT(0)
89 
90 struct nfp_nfdk_tx_buf {
91 	union {
92 		/* First slot */
93 		union {
94 			struct sk_buff *skb;
95 			void *frag;
96 			unsigned long val;
97 		};
98 
99 		/* 1 + nr_frags next slots */
100 		dma_addr_t dma_addr;
101 
102 		/* TSO (optional) */
103 		struct {
104 			u32 pkt_cnt;
105 			u32 real_len;
106 		};
107 
108 		u64 raw;
109 	};
110 };
111 
112 static inline int nfp_nfdk_headlen_to_segs(unsigned int headlen)
113 {
114 	/* First descriptor fits less data, so adjust for that */
115 	return DIV_ROUND_UP(headlen +
116 			    NFDK_TX_MAX_DATA_PER_DESC -
117 			    NFDK_TX_MAX_DATA_PER_HEAD,
118 			    NFDK_TX_MAX_DATA_PER_DESC);
119 }
120 
121 int nfp_nfdk_poll(struct napi_struct *napi, int budget);
122 netdev_tx_t nfp_nfdk_tx(struct sk_buff *skb, struct net_device *netdev);
123 bool
124 nfp_nfdk_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
125 		     struct sk_buff *skb, bool old);
126 void nfp_nfdk_ctrl_poll(struct tasklet_struct *t);
127 void nfp_nfdk_rx_ring_fill_freelist(struct nfp_net_dp *dp,
128 				    struct nfp_net_rx_ring *rx_ring);
129 #endif
130