drm: bridge: adv7511: remove s32 format from i2s capabilities
[drm/drm-misc.git] / drivers / net / ethernet / microsoft / mana / mana_bpf.c
blob23b1521c0df96e7fd595daad8b49fa9be935090c
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2021, Microsoft Corporation. */
4 #include <linux/inetdevice.h>
5 #include <linux/etherdevice.h>
6 #include <linux/mm.h>
7 #include <linux/bpf.h>
8 #include <linux/bpf_trace.h>
9 #include <net/xdp.h>
11 #include <net/mana/mana.h>
13 void mana_xdp_tx(struct sk_buff *skb, struct net_device *ndev)
15 u16 txq_idx = skb_get_queue_mapping(skb);
16 struct netdev_queue *ndevtxq;
17 int rc;
19 __skb_push(skb, ETH_HLEN);
21 ndevtxq = netdev_get_tx_queue(ndev, txq_idx);
22 __netif_tx_lock(ndevtxq, smp_processor_id());
24 rc = mana_start_xmit(skb, ndev);
26 __netif_tx_unlock(ndevtxq);
28 if (dev_xmit_complete(rc))
29 return;
31 dev_kfree_skb_any(skb);
32 ndev->stats.tx_dropped++;
35 static int mana_xdp_xmit_fm(struct net_device *ndev, struct xdp_frame *frame,
36 u16 q_idx)
38 struct sk_buff *skb;
40 skb = xdp_build_skb_from_frame(frame, ndev);
41 if (unlikely(!skb))
42 return -ENOMEM;
44 skb_set_queue_mapping(skb, q_idx);
46 mana_xdp_tx(skb, ndev);
48 return 0;
51 int mana_xdp_xmit(struct net_device *ndev, int n, struct xdp_frame **frames,
52 u32 flags)
54 struct mana_port_context *apc = netdev_priv(ndev);
55 struct mana_stats_tx *tx_stats;
56 int i, count = 0;
57 u16 q_idx;
59 if (unlikely(!apc->port_is_up))
60 return 0;
62 q_idx = smp_processor_id() % ndev->real_num_tx_queues;
64 for (i = 0; i < n; i++) {
65 if (mana_xdp_xmit_fm(ndev, frames[i], q_idx))
66 break;
68 count++;
71 tx_stats = &apc->tx_qp[q_idx].txq.stats;
73 u64_stats_update_begin(&tx_stats->syncp);
74 tx_stats->xdp_xmit += count;
75 u64_stats_update_end(&tx_stats->syncp);
77 return count;
80 u32 mana_run_xdp(struct net_device *ndev, struct mana_rxq *rxq,
81 struct xdp_buff *xdp, void *buf_va, uint pkt_len)
83 struct mana_stats_rx *rx_stats;
84 struct bpf_prog *prog;
85 u32 act = XDP_PASS;
87 rcu_read_lock();
88 prog = rcu_dereference(rxq->bpf_prog);
90 if (!prog)
91 goto out;
93 xdp_init_buff(xdp, PAGE_SIZE, &rxq->xdp_rxq);
94 xdp_prepare_buff(xdp, buf_va, XDP_PACKET_HEADROOM, pkt_len, false);
96 act = bpf_prog_run_xdp(prog, xdp);
98 rx_stats = &rxq->stats;
100 switch (act) {
101 case XDP_PASS:
102 case XDP_TX:
103 case XDP_DROP:
104 break;
106 case XDP_REDIRECT:
107 rxq->xdp_rc = xdp_do_redirect(ndev, xdp, prog);
108 if (!rxq->xdp_rc) {
109 rxq->xdp_flush = true;
111 u64_stats_update_begin(&rx_stats->syncp);
112 rx_stats->packets++;
113 rx_stats->bytes += pkt_len;
114 rx_stats->xdp_redirect++;
115 u64_stats_update_end(&rx_stats->syncp);
117 break;
120 fallthrough;
122 case XDP_ABORTED:
123 trace_xdp_exception(ndev, prog, act);
124 break;
126 default:
127 bpf_warn_invalid_xdp_action(ndev, prog, act);
130 out:
131 rcu_read_unlock();
133 return act;
136 struct bpf_prog *mana_xdp_get(struct mana_port_context *apc)
138 ASSERT_RTNL();
140 return apc->bpf_prog;
143 static struct bpf_prog *mana_chn_xdp_get(struct mana_port_context *apc)
145 return rtnl_dereference(apc->rxqs[0]->bpf_prog);
148 /* Set xdp program on channels */
149 void mana_chn_setxdp(struct mana_port_context *apc, struct bpf_prog *prog)
151 struct bpf_prog *old_prog = mana_chn_xdp_get(apc);
152 unsigned int num_queues = apc->num_queues;
153 int i;
155 ASSERT_RTNL();
157 if (old_prog == prog)
158 return;
160 if (prog)
161 bpf_prog_add(prog, num_queues);
163 for (i = 0; i < num_queues; i++)
164 rcu_assign_pointer(apc->rxqs[i]->bpf_prog, prog);
166 if (old_prog)
167 for (i = 0; i < num_queues; i++)
168 bpf_prog_put(old_prog);
171 static int mana_xdp_set(struct net_device *ndev, struct bpf_prog *prog,
172 struct netlink_ext_ack *extack)
174 struct mana_port_context *apc = netdev_priv(ndev);
175 struct bpf_prog *old_prog;
176 struct gdma_context *gc;
178 gc = apc->ac->gdma_dev->gdma_context;
180 old_prog = mana_xdp_get(apc);
182 if (!old_prog && !prog)
183 return 0;
185 if (prog && ndev->mtu > MANA_XDP_MTU_MAX) {
186 netdev_err(ndev, "XDP: mtu:%u too large, mtu_max:%lu\n",
187 ndev->mtu, MANA_XDP_MTU_MAX);
188 NL_SET_ERR_MSG_MOD(extack, "XDP: mtu too large");
190 return -EOPNOTSUPP;
193 /* One refcnt of the prog is hold by the caller already, so
194 * don't increase refcnt for this one.
196 apc->bpf_prog = prog;
198 if (old_prog)
199 bpf_prog_put(old_prog);
201 if (apc->port_is_up)
202 mana_chn_setxdp(apc, prog);
204 if (prog)
205 ndev->max_mtu = MANA_XDP_MTU_MAX;
206 else
207 ndev->max_mtu = gc->adapter_mtu - ETH_HLEN;
209 return 0;
212 int mana_bpf(struct net_device *ndev, struct netdev_bpf *bpf)
214 struct netlink_ext_ack *extack = bpf->extack;
215 int ret;
217 switch (bpf->command) {
218 case XDP_SETUP_PROG:
219 return mana_xdp_set(ndev, bpf->prog, extack);
221 default:
222 return -EOPNOTSUPP;
225 return ret;