1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright 2015-2021 Amazon.com, Inc. or its affiliates. All rights reserved.
4  */
5 
6 #include "ena_xdp.h"
7 
8 static int validate_xdp_req_id(struct ena_ring *tx_ring, u16 req_id)
9 {
10 	struct ena_tx_buffer *tx_info;
11 
12 	tx_info = &tx_ring->tx_buffer_info[req_id];
13 	if (likely(tx_info->xdpf))
14 		return 0;
15 
16 	return handle_invalid_req_id(tx_ring, req_id, tx_info, true);
17 }
18 
19 static int ena_xdp_tx_map_frame(struct ena_ring *tx_ring,
20 				struct ena_tx_buffer *tx_info,
21 				struct xdp_frame *xdpf,
22 				struct ena_com_tx_ctx *ena_tx_ctx)
23 {
24 	struct ena_adapter *adapter = tx_ring->adapter;
25 	struct ena_com_buf *ena_buf;
26 	int push_len = 0;
27 	dma_addr_t dma;
28 	void *data;
29 	u32 size;
30 
31 	tx_info->xdpf = xdpf;
32 	data = tx_info->xdpf->data;
33 	size = tx_info->xdpf->len;
34 
35 	if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
36 		/* Designate part of the packet for LLQ */
37 		push_len = min_t(u32, size, tx_ring->tx_max_header_size);
38 
39 		ena_tx_ctx->push_header = data;
40 
41 		size -= push_len;
42 		data += push_len;
43 	}
44 
45 	ena_tx_ctx->header_len = push_len;
46 
47 	if (size > 0) {
48 		dma = dma_map_single(tx_ring->dev,
49 				     data,
50 				     size,
51 				     DMA_TO_DEVICE);
52 		if (unlikely(dma_mapping_error(tx_ring->dev, dma)))
53 			goto error_report_dma_error;
54 
55 		tx_info->map_linear_data = 0;
56 
57 		ena_buf = tx_info->bufs;
58 		ena_buf->paddr = dma;
59 		ena_buf->len = size;
60 
61 		ena_tx_ctx->ena_bufs = ena_buf;
62 		ena_tx_ctx->num_bufs = tx_info->num_of_bufs = 1;
63 	}
64 
65 	return 0;
66 
67 error_report_dma_error:
68 	ena_increase_stat(&tx_ring->tx_stats.dma_mapping_err, 1,
69 			  &tx_ring->syncp);
70 	netif_warn(adapter, tx_queued, adapter->netdev, "Failed to map xdp buff\n");
71 
72 	return -EINVAL;
73 }
74 
75 int ena_xdp_xmit_frame(struct ena_ring *tx_ring,
76 		       struct ena_adapter *adapter,
77 		       struct xdp_frame *xdpf,
78 		       int flags)
79 {
80 	struct ena_com_tx_ctx ena_tx_ctx = {};
81 	struct ena_tx_buffer *tx_info;
82 	u16 next_to_use, req_id;
83 	int rc;
84 
85 	next_to_use = tx_ring->next_to_use;
86 	req_id = tx_ring->free_ids[next_to_use];
87 	tx_info = &tx_ring->tx_buffer_info[req_id];
88 	tx_info->num_of_bufs = 0;
89 
90 	rc = ena_xdp_tx_map_frame(tx_ring, tx_info, xdpf, &ena_tx_ctx);
91 	if (unlikely(rc))
92 		goto err;
93 
94 	ena_tx_ctx.req_id = req_id;
95 
96 	rc = ena_xmit_common(adapter,
97 			     tx_ring,
98 			     tx_info,
99 			     &ena_tx_ctx,
100 			     next_to_use,
101 			     xdpf->len);
102 	if (rc)
103 		goto error_unmap_dma;
104 
105 	/* trigger the dma engine. ena_ring_tx_doorbell()
106 	 * calls a memory barrier inside it.
107 	 */
108 	if (flags & XDP_XMIT_FLUSH)
109 		ena_ring_tx_doorbell(tx_ring);
110 
111 	return rc;
112 
113 error_unmap_dma:
114 	ena_unmap_tx_buff(tx_ring, tx_info);
115 err:
116 	tx_info->xdpf = NULL;
117 
118 	return rc;
119 }
120 
121 int ena_xdp_xmit(struct net_device *dev, int n,
122 		 struct xdp_frame **frames, u32 flags)
123 {
124 	struct ena_adapter *adapter = netdev_priv(dev);
125 	struct ena_ring *tx_ring;
126 	int qid, i, nxmit = 0;
127 
128 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
129 		return -EINVAL;
130 
131 	if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
132 		return -ENETDOWN;
133 
134 	/* We assume that all rings have the same XDP program */
135 	if (!READ_ONCE(adapter->rx_ring->xdp_bpf_prog))
136 		return -ENXIO;
137 
138 	qid = smp_processor_id() % adapter->xdp_num_queues;
139 	qid += adapter->xdp_first_ring;
140 	tx_ring = &adapter->tx_ring[qid];
141 
142 	/* Other CPU ids might try to send thorugh this queue */
143 	spin_lock(&tx_ring->xdp_tx_lock);
144 
145 	for (i = 0; i < n; i++) {
146 		if (ena_xdp_xmit_frame(tx_ring, adapter, frames[i], 0))
147 			break;
148 		nxmit++;
149 	}
150 
151 	/* Ring doorbell to make device aware of the packets */
152 	if (flags & XDP_XMIT_FLUSH)
153 		ena_ring_tx_doorbell(tx_ring);
154 
155 	spin_unlock(&tx_ring->xdp_tx_lock);
156 
157 	/* Return number of packets sent */
158 	return nxmit;
159 }
160 
161 static void ena_init_all_xdp_queues(struct ena_adapter *adapter)
162 {
163 	adapter->xdp_first_ring = adapter->num_io_queues;
164 	adapter->xdp_num_queues = adapter->num_io_queues;
165 
166 	ena_init_io_rings(adapter,
167 			  adapter->xdp_first_ring,
168 			  adapter->xdp_num_queues);
169 }
170 
171 int ena_setup_and_create_all_xdp_queues(struct ena_adapter *adapter)
172 {
173 	u32 xdp_first_ring = adapter->xdp_first_ring;
174 	u32 xdp_num_queues = adapter->xdp_num_queues;
175 	int rc = 0;
176 
177 	rc = ena_setup_tx_resources_in_range(adapter, xdp_first_ring, xdp_num_queues);
178 	if (rc)
179 		goto setup_err;
180 
181 	rc = ena_create_io_tx_queues_in_range(adapter, xdp_first_ring, xdp_num_queues);
182 	if (rc)
183 		goto create_err;
184 
185 	return 0;
186 
187 create_err:
188 	ena_free_all_io_tx_resources_in_range(adapter, xdp_first_ring, xdp_num_queues);
189 setup_err:
190 	return rc;
191 }
192 
193 /* Provides a way for both kernel and bpf-prog to know
194  * more about the RX-queue a given XDP frame arrived on.
195  */
196 static int ena_xdp_register_rxq_info(struct ena_ring *rx_ring)
197 {
198 	int rc;
199 
200 	rc = xdp_rxq_info_reg(&rx_ring->xdp_rxq, rx_ring->netdev, rx_ring->qid, 0);
201 
202 	if (rc) {
203 		netif_err(rx_ring->adapter, ifup, rx_ring->netdev,
204 			  "Failed to register xdp rx queue info. RX queue num %d rc: %d\n",
205 			  rx_ring->qid, rc);
206 		goto err;
207 	}
208 
209 	rc = xdp_rxq_info_reg_mem_model(&rx_ring->xdp_rxq, MEM_TYPE_PAGE_SHARED, NULL);
210 
211 	if (rc) {
212 		netif_err(rx_ring->adapter, ifup, rx_ring->netdev,
213 			  "Failed to register xdp rx queue info memory model. RX queue num %d rc: %d\n",
214 			  rx_ring->qid, rc);
215 		xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
216 	}
217 
218 err:
219 	return rc;
220 }
221 
222 static void ena_xdp_unregister_rxq_info(struct ena_ring *rx_ring)
223 {
224 	xdp_rxq_info_unreg_mem_model(&rx_ring->xdp_rxq);
225 	xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
226 }
227 
228 void ena_xdp_exchange_program_rx_in_range(struct ena_adapter *adapter,
229 					  struct bpf_prog *prog,
230 					  int first, int count)
231 {
232 	struct bpf_prog *old_bpf_prog;
233 	struct ena_ring *rx_ring;
234 	int i = 0;
235 
236 	for (i = first; i < count; i++) {
237 		rx_ring = &adapter->rx_ring[i];
238 		old_bpf_prog = xchg(&rx_ring->xdp_bpf_prog, prog);
239 
240 		if (!old_bpf_prog && prog) {
241 			ena_xdp_register_rxq_info(rx_ring);
242 			rx_ring->rx_headroom = XDP_PACKET_HEADROOM;
243 		} else if (old_bpf_prog && !prog) {
244 			ena_xdp_unregister_rxq_info(rx_ring);
245 			rx_ring->rx_headroom = NET_SKB_PAD;
246 		}
247 	}
248 }
249 
250 static void ena_xdp_exchange_program(struct ena_adapter *adapter,
251 				     struct bpf_prog *prog)
252 {
253 	struct bpf_prog *old_bpf_prog = xchg(&adapter->xdp_bpf_prog, prog);
254 
255 	ena_xdp_exchange_program_rx_in_range(adapter,
256 					     prog,
257 					     0,
258 					     adapter->num_io_queues);
259 
260 	if (old_bpf_prog)
261 		bpf_prog_put(old_bpf_prog);
262 }
263 
264 static int ena_destroy_and_free_all_xdp_queues(struct ena_adapter *adapter)
265 {
266 	bool was_up;
267 	int rc;
268 
269 	was_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
270 
271 	if (was_up)
272 		ena_down(adapter);
273 
274 	adapter->xdp_first_ring = 0;
275 	adapter->xdp_num_queues = 0;
276 	ena_xdp_exchange_program(adapter, NULL);
277 	if (was_up) {
278 		rc = ena_up(adapter);
279 		if (rc)
280 			return rc;
281 	}
282 	return 0;
283 }
284 
285 static int ena_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf)
286 {
287 	struct ena_adapter *adapter = netdev_priv(netdev);
288 	struct bpf_prog *prog = bpf->prog;
289 	struct bpf_prog *old_bpf_prog;
290 	int rc, prev_mtu;
291 	bool is_up;
292 
293 	is_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
294 	rc = ena_xdp_allowed(adapter);
295 	if (rc == ENA_XDP_ALLOWED) {
296 		old_bpf_prog = adapter->xdp_bpf_prog;
297 		if (prog) {
298 			if (!is_up) {
299 				ena_init_all_xdp_queues(adapter);
300 			} else if (!old_bpf_prog) {
301 				ena_down(adapter);
302 				ena_init_all_xdp_queues(adapter);
303 			}
304 			ena_xdp_exchange_program(adapter, prog);
305 
306 			if (is_up && !old_bpf_prog) {
307 				rc = ena_up(adapter);
308 				if (rc)
309 					return rc;
310 			}
311 			xdp_features_set_redirect_target(netdev, false);
312 		} else if (old_bpf_prog) {
313 			xdp_features_clear_redirect_target(netdev);
314 			rc = ena_destroy_and_free_all_xdp_queues(adapter);
315 			if (rc)
316 				return rc;
317 		}
318 
319 		prev_mtu = netdev->max_mtu;
320 		netdev->max_mtu = prog ? ENA_XDP_MAX_MTU : adapter->max_mtu;
321 
322 		if (!old_bpf_prog)
323 			netif_info(adapter, drv, adapter->netdev,
324 				   "XDP program is set, changing the max_mtu from %d to %d",
325 				   prev_mtu, netdev->max_mtu);
326 
327 	} else if (rc == ENA_XDP_CURRENT_MTU_TOO_LARGE) {
328 		netif_err(adapter, drv, adapter->netdev,
329 			  "Failed to set xdp program, the current MTU (%d) is larger than the maximum allowed MTU (%lu) while xdp is on",
330 			  netdev->mtu, ENA_XDP_MAX_MTU);
331 		NL_SET_ERR_MSG_MOD(bpf->extack,
332 				   "Failed to set xdp program, the current MTU is larger than the maximum allowed MTU. Check the dmesg for more info");
333 		return -EINVAL;
334 	} else if (rc == ENA_XDP_NO_ENOUGH_QUEUES) {
335 		netif_err(adapter, drv, adapter->netdev,
336 			  "Failed to set xdp program, the Rx/Tx channel count should be at most half of the maximum allowed channel count. The current queue count (%d), the maximal queue count (%d)\n",
337 			  adapter->num_io_queues, adapter->max_num_io_queues);
338 		NL_SET_ERR_MSG_MOD(bpf->extack,
339 				   "Failed to set xdp program, there is no enough space for allocating XDP queues, Check the dmesg for more info");
340 		return -EINVAL;
341 	}
342 
343 	return 0;
344 }
345 
346 /* This is the main xdp callback, it's used by the kernel to set/unset the xdp
347  * program as well as to query the current xdp program id.
348  */
349 int ena_xdp(struct net_device *netdev, struct netdev_bpf *bpf)
350 {
351 	switch (bpf->command) {
352 	case XDP_SETUP_PROG:
353 		return ena_xdp_set(netdev, bpf);
354 	default:
355 		return -EINVAL;
356 	}
357 	return 0;
358 }
359 
360 static int ena_clean_xdp_irq(struct ena_ring *tx_ring, u32 budget)
361 {
362 	u32 total_done = 0;
363 	u16 next_to_clean;
364 	int tx_pkts = 0;
365 	u16 req_id;
366 	int rc;
367 
368 	if (unlikely(!tx_ring))
369 		return 0;
370 	next_to_clean = tx_ring->next_to_clean;
371 
372 	while (tx_pkts < budget) {
373 		struct ena_tx_buffer *tx_info;
374 		struct xdp_frame *xdpf;
375 
376 		rc = ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq,
377 						&req_id);
378 		if (rc) {
379 			if (unlikely(rc == -EINVAL))
380 				handle_invalid_req_id(tx_ring, req_id, NULL, true);
381 			break;
382 		}
383 
384 		/* validate that the request id points to a valid xdp_frame */
385 		rc = validate_xdp_req_id(tx_ring, req_id);
386 		if (rc)
387 			break;
388 
389 		tx_info = &tx_ring->tx_buffer_info[req_id];
390 		xdpf = tx_info->xdpf;
391 
392 		tx_info->xdpf = NULL;
393 		tx_info->last_jiffies = 0;
394 		ena_unmap_tx_buff(tx_ring, tx_info);
395 
396 		netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
397 			  "tx_poll: q %d skb %p completed\n", tx_ring->qid,
398 			  xdpf);
399 
400 		tx_pkts++;
401 		total_done += tx_info->tx_descs;
402 
403 		xdp_return_frame(xdpf);
404 		tx_ring->free_ids[next_to_clean] = req_id;
405 		next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
406 						     tx_ring->ring_size);
407 	}
408 
409 	tx_ring->next_to_clean = next_to_clean;
410 	ena_com_comp_ack(tx_ring->ena_com_io_sq, total_done);
411 	ena_com_update_dev_comp_head(tx_ring->ena_com_io_cq);
412 
413 	netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
414 		  "tx_poll: q %d done. total pkts: %d\n",
415 		  tx_ring->qid, tx_pkts);
416 
417 	return tx_pkts;
418 }
419 
420 /* This is the XDP napi callback. XDP queues use a separate napi callback
421  * than Rx/Tx queues.
422  */
423 int ena_xdp_io_poll(struct napi_struct *napi, int budget)
424 {
425 	struct ena_napi *ena_napi = container_of(napi, struct ena_napi, napi);
426 	u32 xdp_work_done, xdp_budget;
427 	struct ena_ring *tx_ring;
428 	int napi_comp_call = 0;
429 	int ret;
430 
431 	tx_ring = ena_napi->tx_ring;
432 
433 	xdp_budget = budget;
434 
435 	if (!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
436 	    test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags)) {
437 		napi_complete_done(napi, 0);
438 		return 0;
439 	}
440 
441 	xdp_work_done = ena_clean_xdp_irq(tx_ring, xdp_budget);
442 
443 	/* If the device is about to reset or down, avoid unmask
444 	 * the interrupt and return 0 so NAPI won't reschedule
445 	 */
446 	if (unlikely(!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags))) {
447 		napi_complete_done(napi, 0);
448 		ret = 0;
449 	} else if (xdp_budget > xdp_work_done) {
450 		napi_comp_call = 1;
451 		if (napi_complete_done(napi, xdp_work_done))
452 			ena_unmask_interrupt(tx_ring, NULL);
453 		ena_update_ring_numa_node(tx_ring, NULL);
454 		ret = xdp_work_done;
455 	} else {
456 		ret = xdp_budget;
457 	}
458 
459 	u64_stats_update_begin(&tx_ring->syncp);
460 	tx_ring->tx_stats.napi_comp += napi_comp_call;
461 	tx_ring->tx_stats.tx_poll++;
462 	u64_stats_update_end(&tx_ring->syncp);
463 	tx_ring->tx_stats.last_napi_jiffies = jiffies;
464 
465 	return ret;
466 }
467