1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2016-2019 Netronome Systems, Inc. */
4 #include <linux/bitops.h>
10 #define ccm_warn(app, msg...) nn_dp_warn(&(app)->ctrl->dp, msg)
12 #define NFP_CCM_TAG_ALLOC_SPAN (U16_MAX / 4)
14 static bool nfp_ccm_all_tags_busy(struct nfp_ccm
*ccm
)
18 used_tags
= ccm
->tag_alloc_next
- ccm
->tag_alloc_last
;
20 return used_tags
> NFP_CCM_TAG_ALLOC_SPAN
;
23 static int nfp_ccm_alloc_tag(struct nfp_ccm
*ccm
)
25 /* CCM is for FW communication which is request-reply. To make sure
26 * we don't reuse the message ID too early after timeout - limit the
27 * number of requests in flight.
29 if (unlikely(nfp_ccm_all_tags_busy(ccm
))) {
30 ccm_warn(ccm
->app
, "all FW request contexts busy!\n");
34 WARN_ON(__test_and_set_bit(ccm
->tag_alloc_next
, ccm
->tag_allocator
));
35 return ccm
->tag_alloc_next
++;
38 static void nfp_ccm_free_tag(struct nfp_ccm
*ccm
, u16 tag
)
40 WARN_ON(!__test_and_clear_bit(tag
, ccm
->tag_allocator
));
42 while (!test_bit(ccm
->tag_alloc_last
, ccm
->tag_allocator
) &&
43 ccm
->tag_alloc_last
!= ccm
->tag_alloc_next
)
44 ccm
->tag_alloc_last
++;
47 static struct sk_buff
*__nfp_ccm_reply(struct nfp_ccm
*ccm
, u16 tag
)
52 skb_queue_walk(&ccm
->replies
, skb
) {
53 msg_tag
= nfp_ccm_get_tag(skb
);
55 nfp_ccm_free_tag(ccm
, tag
);
56 __skb_unlink(skb
, &ccm
->replies
);
64 static struct sk_buff
*
65 nfp_ccm_reply(struct nfp_ccm
*ccm
, struct nfp_app
*app
, u16 tag
)
69 nfp_ctrl_lock(app
->ctrl
);
70 skb
= __nfp_ccm_reply(ccm
, tag
);
71 nfp_ctrl_unlock(app
->ctrl
);
76 static struct sk_buff
*
77 nfp_ccm_reply_drop_tag(struct nfp_ccm
*ccm
, struct nfp_app
*app
, u16 tag
)
81 nfp_ctrl_lock(app
->ctrl
);
82 skb
= __nfp_ccm_reply(ccm
, tag
);
84 nfp_ccm_free_tag(ccm
, tag
);
85 nfp_ctrl_unlock(app
->ctrl
);
90 static struct sk_buff
*
91 nfp_ccm_wait_reply(struct nfp_ccm
*ccm
, struct nfp_app
*app
,
92 enum nfp_ccm_type type
, int tag
)
97 for (i
= 0; i
< 50; i
++) {
99 skb
= nfp_ccm_reply(ccm
, app
, tag
);
104 err
= wait_event_interruptible_timeout(ccm
->wq
,
105 skb
= nfp_ccm_reply(ccm
, app
,
107 msecs_to_jiffies(5000));
108 /* We didn't get a response - try last time and atomically drop
109 * the tag even if no response is matched.
112 skb
= nfp_ccm_reply_drop_tag(ccm
, app
, tag
);
114 ccm_warn(app
, "%s waiting for response to 0x%02x: %d\n",
115 err
== ERESTARTSYS
? "interrupted" : "error",
120 ccm_warn(app
, "timeout waiting for response to 0x%02x\n", type
);
121 return ERR_PTR(-ETIMEDOUT
);
128 nfp_ccm_communicate(struct nfp_ccm
*ccm
, struct sk_buff
*skb
,
129 enum nfp_ccm_type type
, unsigned int reply_size
)
131 struct nfp_app
*app
= ccm
->app
;
132 struct nfp_ccm_hdr
*hdr
;
135 nfp_ctrl_lock(app
->ctrl
);
136 tag
= nfp_ccm_alloc_tag(ccm
);
138 nfp_ctrl_unlock(app
->ctrl
);
139 dev_kfree_skb_any(skb
);
143 hdr
= (void *)skb
->data
;
144 hdr
->ver
= NFP_CCM_ABI_VERSION
;
146 hdr
->tag
= cpu_to_be16(tag
);
148 __nfp_app_ctrl_tx(app
, skb
);
150 nfp_ctrl_unlock(app
->ctrl
);
152 skb
= nfp_ccm_wait_reply(ccm
, app
, type
, tag
);
156 reply_type
= nfp_ccm_get_type(skb
);
157 if (reply_type
!= __NFP_CCM_REPLY(type
)) {
158 ccm_warn(app
, "cmsg drop - wrong type 0x%02x != 0x%02lx!\n",
159 reply_type
, __NFP_CCM_REPLY(type
));
162 /* 0 reply_size means caller will do the validation */
163 if (reply_size
&& skb
->len
!= reply_size
) {
164 ccm_warn(app
, "cmsg drop - type 0x%02x wrong size %d != %d!\n",
165 type
, skb
->len
, reply_size
);
171 dev_kfree_skb_any(skb
);
172 return ERR_PTR(-EIO
);
175 void nfp_ccm_rx(struct nfp_ccm
*ccm
, struct sk_buff
*skb
)
177 struct nfp_app
*app
= ccm
->app
;
180 if (unlikely(skb
->len
< sizeof(struct nfp_ccm_hdr
))) {
181 ccm_warn(app
, "cmsg drop - too short %d!\n", skb
->len
);
185 nfp_ctrl_lock(app
->ctrl
);
187 tag
= nfp_ccm_get_tag(skb
);
188 if (unlikely(!test_bit(tag
, ccm
->tag_allocator
))) {
189 ccm_warn(app
, "cmsg drop - no one is waiting for tag %u!\n",
194 __skb_queue_tail(&ccm
->replies
, skb
);
195 wake_up_interruptible_all(&ccm
->wq
);
197 nfp_ctrl_unlock(app
->ctrl
);
201 nfp_ctrl_unlock(app
->ctrl
);
203 dev_kfree_skb_any(skb
);
206 int nfp_ccm_init(struct nfp_ccm
*ccm
, struct nfp_app
*app
)
209 skb_queue_head_init(&ccm
->replies
);
210 init_waitqueue_head(&ccm
->wq
);
214 void nfp_ccm_clean(struct nfp_ccm
*ccm
)
216 WARN_ON(!skb_queue_empty(&ccm
->replies
));