1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2016-2019 Netronome Systems, Inc. */
3 
4 #ifndef NFP_CCM_H
5 #define NFP_CCM_H 1
6 
7 #include <linux/bitmap.h>
8 #include <linux/skbuff.h>
9 #include <linux/wait.h>
10 
11 struct nfp_app;
12 struct nfp_net;
13 
14 /* Firmware ABI */
15 
16 enum nfp_ccm_type {
17 	NFP_CCM_TYPE_BPF_MAP_ALLOC	= 1,
18 	NFP_CCM_TYPE_BPF_MAP_FREE	= 2,
19 	NFP_CCM_TYPE_BPF_MAP_LOOKUP	= 3,
20 	NFP_CCM_TYPE_BPF_MAP_UPDATE	= 4,
21 	NFP_CCM_TYPE_BPF_MAP_DELETE	= 5,
22 	NFP_CCM_TYPE_BPF_MAP_GETNEXT	= 6,
23 	NFP_CCM_TYPE_BPF_MAP_GETFIRST	= 7,
24 	NFP_CCM_TYPE_BPF_BPF_EVENT	= 8,
25 	NFP_CCM_TYPE_CRYPTO_RESET	= 9,
26 	NFP_CCM_TYPE_CRYPTO_ADD		= 10,
27 	NFP_CCM_TYPE_CRYPTO_DEL		= 11,
28 	NFP_CCM_TYPE_CRYPTO_UPDATE	= 12,
29 	__NFP_CCM_TYPE_MAX,
30 };
31 
32 #define NFP_CCM_ABI_VERSION		1
33 
34 #define NFP_CCM_TYPE_REPLY_BIT		7
35 #define __NFP_CCM_REPLY(req)		(BIT(NFP_CCM_TYPE_REPLY_BIT) | (req))
36 
37 struct nfp_ccm_hdr {
38 	union {
39 		struct {
40 			u8 type;
41 			u8 ver;
42 			__be16 tag;
43 		};
44 		__be32 raw;
45 	};
46 };
47 
48 static inline u8 nfp_ccm_get_type(struct sk_buff *skb)
49 {
50 	struct nfp_ccm_hdr *hdr;
51 
52 	hdr = (struct nfp_ccm_hdr *)skb->data;
53 
54 	return hdr->type;
55 }
56 
57 static inline __be16 __nfp_ccm_get_tag(struct sk_buff *skb)
58 {
59 	struct nfp_ccm_hdr *hdr;
60 
61 	hdr = (struct nfp_ccm_hdr *)skb->data;
62 
63 	return hdr->tag;
64 }
65 
66 static inline unsigned int nfp_ccm_get_tag(struct sk_buff *skb)
67 {
68 	return be16_to_cpu(__nfp_ccm_get_tag(skb));
69 }
70 
71 #define NFP_NET_MBOX_TLV_TYPE		GENMASK(31, 16)
72 #define NFP_NET_MBOX_TLV_LEN		GENMASK(15, 0)
73 
74 enum nfp_ccm_mbox_tlv_type {
75 	NFP_NET_MBOX_TLV_TYPE_UNKNOWN	= 0,
76 	NFP_NET_MBOX_TLV_TYPE_END	= 1,
77 	NFP_NET_MBOX_TLV_TYPE_MSG	= 2,
78 	NFP_NET_MBOX_TLV_TYPE_MSG_NOSUP	= 3,
79 	NFP_NET_MBOX_TLV_TYPE_RESV	= 4,
80 };
81 
82 /* Implementation */
83 
84 /**
85  * struct nfp_ccm - common control message handling
86  * @app:		APP handle
87  *
88  * @tag_allocator:	bitmap of control message tags in use
89  * @tag_alloc_next:	next tag bit to allocate
90  * @tag_alloc_last:	next tag bit to be freed
91  *
92  * @replies:		received cmsg replies waiting to be consumed
93  * @wq:			work queue for waiting for cmsg replies
94  */
95 struct nfp_ccm {
96 	struct nfp_app *app;
97 
98 	DECLARE_BITMAP(tag_allocator, U16_MAX + 1);
99 	u16 tag_alloc_next;
100 	u16 tag_alloc_last;
101 
102 	struct sk_buff_head replies;
103 	wait_queue_head_t wq;
104 };
105 
106 int nfp_ccm_init(struct nfp_ccm *ccm, struct nfp_app *app);
107 void nfp_ccm_clean(struct nfp_ccm *ccm);
108 void nfp_ccm_rx(struct nfp_ccm *ccm, struct sk_buff *skb);
109 struct sk_buff *
110 nfp_ccm_communicate(struct nfp_ccm *ccm, struct sk_buff *skb,
111 		    enum nfp_ccm_type type, unsigned int reply_size);
112 
113 int nfp_ccm_mbox_alloc(struct nfp_net *nn);
114 void nfp_ccm_mbox_free(struct nfp_net *nn);
115 int nfp_ccm_mbox_init(struct nfp_net *nn);
116 void nfp_ccm_mbox_clean(struct nfp_net *nn);
117 bool nfp_ccm_mbox_fits(struct nfp_net *nn, unsigned int size);
118 struct sk_buff *
119 nfp_ccm_mbox_msg_alloc(struct nfp_net *nn, unsigned int req_size,
120 		       unsigned int reply_size, gfp_t flags);
121 int __nfp_ccm_mbox_communicate(struct nfp_net *nn, struct sk_buff *skb,
122 			       enum nfp_ccm_type type,
123 			       unsigned int reply_size,
124 			       unsigned int max_reply_size, bool critical);
125 int nfp_ccm_mbox_communicate(struct nfp_net *nn, struct sk_buff *skb,
126 			     enum nfp_ccm_type type,
127 			     unsigned int reply_size,
128 			     unsigned int max_reply_size);
129 int nfp_ccm_mbox_post(struct nfp_net *nn, struct sk_buff *skb,
130 		      enum nfp_ccm_type type, unsigned int max_reply_size);
131 #endif
132