xref: /linux/drivers/net/ethernet/google/gve/gve_tx.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Google virtual Ethernet (gve) driver
3  *
4  * Copyright (C) 2015-2021 Google, Inc.
5  */
6 
7 #include "gve.h"
8 #include "gve_adminq.h"
9 #include "gve_utils.h"
10 #include <linux/ip.h>
11 #include <linux/tcp.h>
12 #include <linux/vmalloc.h>
13 #include <linux/skbuff.h>
14 #include <net/xdp_sock_drv.h>
15 
gve_tx_put_doorbell(struct gve_priv * priv,struct gve_queue_resources * q_resources,u32 val)16 static inline void gve_tx_put_doorbell(struct gve_priv *priv,
17 				       struct gve_queue_resources *q_resources,
18 				       u32 val)
19 {
20 	iowrite32be(val, &priv->db_bar2[be32_to_cpu(q_resources->db_index)]);
21 }
22 
gve_xdp_tx_flush(struct gve_priv * priv,u32 xdp_qid)23 void gve_xdp_tx_flush(struct gve_priv *priv, u32 xdp_qid)
24 {
25 	u32 tx_qid = gve_xdp_tx_queue_id(priv, xdp_qid);
26 	struct gve_tx_ring *tx = &priv->tx[tx_qid];
27 
28 	gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
29 }
30 
31 /* gvnic can only transmit from a Registered Segment.
32  * We copy skb payloads into the registered segment before writing Tx
33  * descriptors and ringing the Tx doorbell.
34  *
35  * gve_tx_fifo_* manages the Registered Segment as a FIFO - clients must
36  * free allocations in the order they were allocated.
37  */
38 
gve_tx_fifo_init(struct gve_priv * priv,struct gve_tx_fifo * fifo)39 static int gve_tx_fifo_init(struct gve_priv *priv, struct gve_tx_fifo *fifo)
40 {
41 	fifo->base = vmap(fifo->qpl->pages, fifo->qpl->num_entries, VM_MAP,
42 			  PAGE_KERNEL);
43 	if (unlikely(!fifo->base)) {
44 		netif_err(priv, drv, priv->dev, "Failed to vmap fifo, qpl_id = %d\n",
45 			  fifo->qpl->id);
46 		return -ENOMEM;
47 	}
48 
49 	fifo->size = fifo->qpl->num_entries * PAGE_SIZE;
50 	atomic_set(&fifo->available, fifo->size);
51 	fifo->head = 0;
52 	return 0;
53 }
54 
gve_tx_fifo_release(struct gve_priv * priv,struct gve_tx_fifo * fifo)55 static void gve_tx_fifo_release(struct gve_priv *priv, struct gve_tx_fifo *fifo)
56 {
57 	WARN(atomic_read(&fifo->available) != fifo->size,
58 	     "Releasing non-empty fifo");
59 
60 	vunmap(fifo->base);
61 }
62 
gve_tx_fifo_pad_alloc_one_frag(struct gve_tx_fifo * fifo,size_t bytes)63 static int gve_tx_fifo_pad_alloc_one_frag(struct gve_tx_fifo *fifo,
64 					  size_t bytes)
65 {
66 	return (fifo->head + bytes < fifo->size) ? 0 : fifo->size - fifo->head;
67 }
68 
gve_tx_fifo_can_alloc(struct gve_tx_fifo * fifo,size_t bytes)69 static bool gve_tx_fifo_can_alloc(struct gve_tx_fifo *fifo, size_t bytes)
70 {
71 	return (atomic_read(&fifo->available) <= bytes) ? false : true;
72 }
73 
74 /* gve_tx_alloc_fifo - Allocate fragment(s) from Tx FIFO
75  * @fifo: FIFO to allocate from
76  * @bytes: Allocation size
77  * @iov: Scatter-gather elements to fill with allocation fragment base/len
78  *
79  * Returns number of valid elements in iov[] or negative on error.
80  *
81  * Allocations from a given FIFO must be externally synchronized but concurrent
82  * allocation and frees are allowed.
83  */
gve_tx_alloc_fifo(struct gve_tx_fifo * fifo,size_t bytes,struct gve_tx_iovec iov[2])84 static int gve_tx_alloc_fifo(struct gve_tx_fifo *fifo, size_t bytes,
85 			     struct gve_tx_iovec iov[2])
86 {
87 	size_t overflow, padding;
88 	u32 aligned_head;
89 	int nfrags = 0;
90 
91 	if (!bytes)
92 		return 0;
93 
94 	/* This check happens before we know how much padding is needed to
95 	 * align to a cacheline boundary for the payload, but that is fine,
96 	 * because the FIFO head always start aligned, and the FIFO's boundaries
97 	 * are aligned, so if there is space for the data, there is space for
98 	 * the padding to the next alignment.
99 	 */
100 	WARN(!gve_tx_fifo_can_alloc(fifo, bytes),
101 	     "Reached %s when there's not enough space in the fifo", __func__);
102 
103 	nfrags++;
104 
105 	iov[0].iov_offset = fifo->head;
106 	iov[0].iov_len = bytes;
107 	fifo->head += bytes;
108 
109 	if (fifo->head > fifo->size) {
110 		/* If the allocation did not fit in the tail fragment of the
111 		 * FIFO, also use the head fragment.
112 		 */
113 		nfrags++;
114 		overflow = fifo->head - fifo->size;
115 		iov[0].iov_len -= overflow;
116 		iov[1].iov_offset = 0;	/* Start of fifo*/
117 		iov[1].iov_len = overflow;
118 
119 		fifo->head = overflow;
120 	}
121 
122 	/* Re-align to a cacheline boundary */
123 	aligned_head = L1_CACHE_ALIGN(fifo->head);
124 	padding = aligned_head - fifo->head;
125 	iov[nfrags - 1].iov_padding = padding;
126 	atomic_sub(bytes + padding, &fifo->available);
127 	fifo->head = aligned_head;
128 
129 	if (fifo->head == fifo->size)
130 		fifo->head = 0;
131 
132 	return nfrags;
133 }
134 
135 /* gve_tx_free_fifo - Return space to Tx FIFO
136  * @fifo: FIFO to return fragments to
137  * @bytes: Bytes to free
138  */
gve_tx_free_fifo(struct gve_tx_fifo * fifo,size_t bytes)139 static void gve_tx_free_fifo(struct gve_tx_fifo *fifo, size_t bytes)
140 {
141 	atomic_add(bytes, &fifo->available);
142 }
143 
gve_tx_clear_buffer_state(struct gve_tx_buffer_state * info)144 static size_t gve_tx_clear_buffer_state(struct gve_tx_buffer_state *info)
145 {
146 	size_t space_freed = 0;
147 	int i;
148 
149 	for (i = 0; i < ARRAY_SIZE(info->iov); i++) {
150 		space_freed += info->iov[i].iov_len + info->iov[i].iov_padding;
151 		info->iov[i].iov_len = 0;
152 		info->iov[i].iov_padding = 0;
153 	}
154 	return space_freed;
155 }
156 
gve_clean_xdp_done(struct gve_priv * priv,struct gve_tx_ring * tx,u32 to_do)157 static int gve_clean_xdp_done(struct gve_priv *priv, struct gve_tx_ring *tx,
158 			      u32 to_do)
159 {
160 	struct gve_tx_buffer_state *info;
161 	u64 pkts = 0, bytes = 0;
162 	size_t space_freed = 0;
163 	u32 xsk_complete = 0;
164 	u32 idx;
165 	int i;
166 
167 	for (i = 0; i < to_do; i++) {
168 		idx = tx->done & tx->mask;
169 		info = &tx->info[idx];
170 		tx->done++;
171 
172 		if (unlikely(!info->xdp.size))
173 			continue;
174 
175 		bytes += info->xdp.size;
176 		pkts++;
177 		xsk_complete += info->xdp.is_xsk;
178 
179 		info->xdp.size = 0;
180 		if (info->xdp_frame) {
181 			xdp_return_frame(info->xdp_frame);
182 			info->xdp_frame = NULL;
183 		}
184 		space_freed += gve_tx_clear_buffer_state(info);
185 	}
186 
187 	gve_tx_free_fifo(&tx->tx_fifo, space_freed);
188 	if (xsk_complete > 0 && tx->xsk_pool)
189 		xsk_tx_completed(tx->xsk_pool, xsk_complete);
190 	u64_stats_update_begin(&tx->statss);
191 	tx->bytes_done += bytes;
192 	tx->pkt_done += pkts;
193 	u64_stats_update_end(&tx->statss);
194 	return pkts;
195 }
196 
197 static int gve_clean_tx_done(struct gve_priv *priv, struct gve_tx_ring *tx,
198 			     u32 to_do, bool try_to_wake);
199 
gve_tx_stop_ring_gqi(struct gve_priv * priv,int idx)200 void gve_tx_stop_ring_gqi(struct gve_priv *priv, int idx)
201 {
202 	int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx);
203 	struct gve_tx_ring *tx = &priv->tx[idx];
204 
205 	if (!gve_tx_was_added_to_block(priv, idx))
206 		return;
207 
208 	gve_remove_napi(priv, ntfy_idx);
209 	if (tx->q_num < priv->tx_cfg.num_queues)
210 		gve_clean_tx_done(priv, tx, priv->tx_desc_cnt, false);
211 	else
212 		gve_clean_xdp_done(priv, tx, priv->tx_desc_cnt);
213 	netdev_tx_reset_queue(tx->netdev_txq);
214 	gve_tx_remove_from_block(priv, idx);
215 }
216 
gve_tx_free_ring_gqi(struct gve_priv * priv,struct gve_tx_ring * tx,struct gve_tx_alloc_rings_cfg * cfg)217 static void gve_tx_free_ring_gqi(struct gve_priv *priv, struct gve_tx_ring *tx,
218 				 struct gve_tx_alloc_rings_cfg *cfg)
219 {
220 	struct device *hdev = &priv->pdev->dev;
221 	int idx = tx->q_num;
222 	size_t bytes;
223 	u32 qpl_id;
224 	u32 slots;
225 
226 	slots = tx->mask + 1;
227 	dma_free_coherent(hdev, sizeof(*tx->q_resources),
228 			  tx->q_resources, tx->q_resources_bus);
229 	tx->q_resources = NULL;
230 
231 	if (tx->tx_fifo.qpl) {
232 		if (tx->tx_fifo.base)
233 			gve_tx_fifo_release(priv, &tx->tx_fifo);
234 
235 		qpl_id = gve_tx_qpl_id(priv, tx->q_num);
236 		gve_free_queue_page_list(priv, tx->tx_fifo.qpl, qpl_id);
237 		tx->tx_fifo.qpl = NULL;
238 	}
239 
240 	bytes = sizeof(*tx->desc) * slots;
241 	dma_free_coherent(hdev, bytes, tx->desc, tx->bus);
242 	tx->desc = NULL;
243 
244 	vfree(tx->info);
245 	tx->info = NULL;
246 
247 	netif_dbg(priv, drv, priv->dev, "freed tx queue %d\n", idx);
248 }
249 
gve_tx_start_ring_gqi(struct gve_priv * priv,int idx)250 void gve_tx_start_ring_gqi(struct gve_priv *priv, int idx)
251 {
252 	int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx);
253 	struct gve_tx_ring *tx = &priv->tx[idx];
254 
255 	gve_tx_add_to_block(priv, idx);
256 
257 	tx->netdev_txq = netdev_get_tx_queue(priv->dev, idx);
258 	gve_add_napi(priv, ntfy_idx, gve_napi_poll);
259 }
260 
gve_tx_alloc_ring_gqi(struct gve_priv * priv,struct gve_tx_alloc_rings_cfg * cfg,struct gve_tx_ring * tx,int idx)261 static int gve_tx_alloc_ring_gqi(struct gve_priv *priv,
262 				 struct gve_tx_alloc_rings_cfg *cfg,
263 				 struct gve_tx_ring *tx,
264 				 int idx)
265 {
266 	struct device *hdev = &priv->pdev->dev;
267 	int qpl_page_cnt;
268 	u32 qpl_id = 0;
269 	size_t bytes;
270 
271 	/* Make sure everything is zeroed to start */
272 	memset(tx, 0, sizeof(*tx));
273 	spin_lock_init(&tx->clean_lock);
274 	spin_lock_init(&tx->xdp_lock);
275 	tx->q_num = idx;
276 
277 	tx->mask = cfg->ring_size - 1;
278 
279 	/* alloc metadata */
280 	tx->info = vcalloc(cfg->ring_size, sizeof(*tx->info));
281 	if (!tx->info)
282 		return -ENOMEM;
283 
284 	/* alloc tx queue */
285 	bytes = sizeof(*tx->desc) * cfg->ring_size;
286 	tx->desc = dma_alloc_coherent(hdev, bytes, &tx->bus, GFP_KERNEL);
287 	if (!tx->desc)
288 		goto abort_with_info;
289 
290 	tx->raw_addressing = cfg->raw_addressing;
291 	tx->dev = hdev;
292 	if (!tx->raw_addressing) {
293 		qpl_id = gve_tx_qpl_id(priv, tx->q_num);
294 		qpl_page_cnt = priv->tx_pages_per_qpl;
295 
296 		tx->tx_fifo.qpl = gve_alloc_queue_page_list(priv, qpl_id,
297 							    qpl_page_cnt);
298 		if (!tx->tx_fifo.qpl)
299 			goto abort_with_desc;
300 
301 		/* map Tx FIFO */
302 		if (gve_tx_fifo_init(priv, &tx->tx_fifo))
303 			goto abort_with_qpl;
304 	}
305 
306 	tx->q_resources =
307 		dma_alloc_coherent(hdev,
308 				   sizeof(*tx->q_resources),
309 				   &tx->q_resources_bus,
310 				   GFP_KERNEL);
311 	if (!tx->q_resources)
312 		goto abort_with_fifo;
313 
314 	return 0;
315 
316 abort_with_fifo:
317 	if (!tx->raw_addressing)
318 		gve_tx_fifo_release(priv, &tx->tx_fifo);
319 abort_with_qpl:
320 	if (!tx->raw_addressing) {
321 		gve_free_queue_page_list(priv, tx->tx_fifo.qpl, qpl_id);
322 		tx->tx_fifo.qpl = NULL;
323 	}
324 abort_with_desc:
325 	dma_free_coherent(hdev, bytes, tx->desc, tx->bus);
326 	tx->desc = NULL;
327 abort_with_info:
328 	vfree(tx->info);
329 	tx->info = NULL;
330 	return -ENOMEM;
331 }
332 
gve_tx_alloc_rings_gqi(struct gve_priv * priv,struct gve_tx_alloc_rings_cfg * cfg)333 int gve_tx_alloc_rings_gqi(struct gve_priv *priv,
334 			   struct gve_tx_alloc_rings_cfg *cfg)
335 {
336 	struct gve_tx_ring *tx = cfg->tx;
337 	int total_queues;
338 	int err = 0;
339 	int i, j;
340 
341 	total_queues = cfg->qcfg->num_queues + cfg->num_xdp_rings;
342 	if (total_queues > cfg->qcfg->max_queues) {
343 		netif_err(priv, drv, priv->dev,
344 			  "Cannot alloc more than the max num of Tx rings\n");
345 		return -EINVAL;
346 	}
347 
348 	tx = kvzalloc_objs(struct gve_tx_ring, cfg->qcfg->max_queues);
349 	if (!tx)
350 		return -ENOMEM;
351 
352 	for (i = 0; i < total_queues; i++) {
353 		err = gve_tx_alloc_ring_gqi(priv, cfg, &tx[i], i);
354 		if (err) {
355 			netif_err(priv, drv, priv->dev,
356 				  "Failed to alloc tx ring=%d: err=%d\n",
357 				  i, err);
358 			goto cleanup;
359 		}
360 	}
361 
362 	cfg->tx = tx;
363 	return 0;
364 
365 cleanup:
366 	for (j = 0; j < i; j++)
367 		gve_tx_free_ring_gqi(priv, &tx[j], cfg);
368 	kvfree(tx);
369 	return err;
370 }
371 
gve_tx_free_rings_gqi(struct gve_priv * priv,struct gve_tx_alloc_rings_cfg * cfg)372 void gve_tx_free_rings_gqi(struct gve_priv *priv,
373 			   struct gve_tx_alloc_rings_cfg *cfg)
374 {
375 	struct gve_tx_ring *tx = cfg->tx;
376 	int i;
377 
378 	if (!tx)
379 		return;
380 
381 	for (i = 0; i < cfg->qcfg->num_queues + cfg->qcfg->num_xdp_queues; i++)
382 		gve_tx_free_ring_gqi(priv, &tx[i], cfg);
383 
384 	kvfree(tx);
385 	cfg->tx = NULL;
386 }
387 
388 /* gve_tx_avail - Calculates the number of slots available in the ring
389  * @tx: tx ring to check
390  *
391  * Returns the number of slots available
392  *
393  * The capacity of the queue is mask + 1. We don't need to reserve an entry.
394  **/
gve_tx_avail(struct gve_tx_ring * tx)395 static inline u32 gve_tx_avail(struct gve_tx_ring *tx)
396 {
397 	return tx->mask + 1 - (tx->req - tx->done);
398 }
399 
gve_skb_fifo_bytes_required(struct gve_tx_ring * tx,struct sk_buff * skb)400 static inline int gve_skb_fifo_bytes_required(struct gve_tx_ring *tx,
401 					      struct sk_buff *skb)
402 {
403 	int pad_bytes, align_hdr_pad;
404 	int bytes;
405 	int hlen;
406 
407 	hlen = skb_is_gso(skb) ? skb_checksum_start_offset(skb) + tcp_hdrlen(skb) :
408 				 min_t(int, GVE_GQ_TX_MIN_PKT_DESC_BYTES, skb->len);
409 
410 	pad_bytes = gve_tx_fifo_pad_alloc_one_frag(&tx->tx_fifo,
411 						   hlen);
412 	/* We need to take into account the header alignment padding. */
413 	align_hdr_pad = L1_CACHE_ALIGN(hlen) - hlen;
414 	bytes = align_hdr_pad + pad_bytes + skb->len;
415 
416 	return bytes;
417 }
418 
419 /* The most descriptors we could need is MAX_SKB_FRAGS + 4 :
420  * 1 for each skb frag
421  * 1 for the skb linear portion
422  * 1 for when tcp hdr needs to be in separate descriptor
423  * 1 if the payload wraps to the beginning of the FIFO
424  * 1 for metadata descriptor
425  */
426 #define MAX_TX_DESC_NEEDED	(MAX_SKB_FRAGS + 4)
gve_tx_unmap_buf(struct device * dev,struct gve_tx_buffer_state * info)427 static void gve_tx_unmap_buf(struct device *dev, struct gve_tx_buffer_state *info)
428 {
429 	if (info->skb) {
430 		dma_unmap_single(dev, dma_unmap_addr(info, dma),
431 				 dma_unmap_len(info, len),
432 				 DMA_TO_DEVICE);
433 		dma_unmap_len_set(info, len, 0);
434 	} else {
435 		dma_unmap_page(dev, dma_unmap_addr(info, dma),
436 			       dma_unmap_len(info, len),
437 			       DMA_TO_DEVICE);
438 		dma_unmap_len_set(info, len, 0);
439 	}
440 }
441 
442 /* Check if sufficient resources (descriptor ring space, FIFO space) are
443  * available to transmit the given number of bytes.
444  */
gve_can_tx(struct gve_tx_ring * tx,int bytes_required)445 static inline bool gve_can_tx(struct gve_tx_ring *tx, int bytes_required)
446 {
447 	bool can_alloc = true;
448 
449 	if (!tx->raw_addressing)
450 		can_alloc = gve_tx_fifo_can_alloc(&tx->tx_fifo, bytes_required);
451 
452 	return (gve_tx_avail(tx) >= MAX_TX_DESC_NEEDED && can_alloc);
453 }
454 
455 static_assert(NAPI_POLL_WEIGHT >= MAX_TX_DESC_NEEDED);
456 
457 /* Stops the queue if the skb cannot be transmitted. */
gve_maybe_stop_tx(struct gve_priv * priv,struct gve_tx_ring * tx,struct sk_buff * skb)458 static int gve_maybe_stop_tx(struct gve_priv *priv, struct gve_tx_ring *tx,
459 			     struct sk_buff *skb)
460 {
461 	int bytes_required = 0;
462 	u32 nic_done;
463 	u32 to_do;
464 	int ret;
465 
466 	if (!tx->raw_addressing)
467 		bytes_required = gve_skb_fifo_bytes_required(tx, skb);
468 
469 	if (likely(gve_can_tx(tx, bytes_required)))
470 		return 0;
471 
472 	ret = -EBUSY;
473 	spin_lock(&tx->clean_lock);
474 	nic_done = gve_tx_load_event_counter(priv, tx);
475 	to_do = nic_done - tx->done;
476 
477 	/* Only try to clean if there is hope for TX */
478 	if (to_do + gve_tx_avail(tx) >= MAX_TX_DESC_NEEDED) {
479 		if (to_do > 0) {
480 			to_do = min_t(u32, to_do, NAPI_POLL_WEIGHT);
481 			gve_clean_tx_done(priv, tx, to_do, false);
482 		}
483 		if (likely(gve_can_tx(tx, bytes_required)))
484 			ret = 0;
485 	}
486 	if (ret) {
487 		/* No space, so stop the queue */
488 		tx->stop_queue++;
489 		netif_tx_stop_queue(tx->netdev_txq);
490 	}
491 	spin_unlock(&tx->clean_lock);
492 
493 	return ret;
494 }
495 
gve_tx_fill_pkt_desc(union gve_tx_desc * pkt_desc,u16 csum_offset,u8 ip_summed,bool is_gso,int l4_hdr_offset,u32 desc_cnt,u16 hlen,u64 addr,u16 pkt_len)496 static void gve_tx_fill_pkt_desc(union gve_tx_desc *pkt_desc,
497 				 u16 csum_offset, u8 ip_summed, bool is_gso,
498 				 int l4_hdr_offset, u32 desc_cnt,
499 				 u16 hlen, u64 addr, u16 pkt_len)
500 {
501 	/* l4_hdr_offset and csum_offset are in units of 16-bit words */
502 	if (is_gso) {
503 		pkt_desc->pkt.type_flags = GVE_TXD_TSO | GVE_TXF_L4CSUM;
504 		pkt_desc->pkt.l4_csum_offset = csum_offset >> 1;
505 		pkt_desc->pkt.l4_hdr_offset = l4_hdr_offset >> 1;
506 	} else if (likely(ip_summed == CHECKSUM_PARTIAL)) {
507 		pkt_desc->pkt.type_flags = GVE_TXD_STD | GVE_TXF_L4CSUM;
508 		pkt_desc->pkt.l4_csum_offset = csum_offset >> 1;
509 		pkt_desc->pkt.l4_hdr_offset = l4_hdr_offset >> 1;
510 	} else {
511 		pkt_desc->pkt.type_flags = GVE_TXD_STD;
512 		pkt_desc->pkt.l4_csum_offset = 0;
513 		pkt_desc->pkt.l4_hdr_offset = 0;
514 	}
515 	pkt_desc->pkt.desc_cnt = desc_cnt;
516 	pkt_desc->pkt.len = cpu_to_be16(pkt_len);
517 	pkt_desc->pkt.seg_len = cpu_to_be16(hlen);
518 	pkt_desc->pkt.seg_addr = cpu_to_be64(addr);
519 }
520 
gve_tx_fill_mtd_desc(union gve_tx_desc * mtd_desc,struct sk_buff * skb)521 static void gve_tx_fill_mtd_desc(union gve_tx_desc *mtd_desc,
522 				 struct sk_buff *skb)
523 {
524 	BUILD_BUG_ON(sizeof(mtd_desc->mtd) != sizeof(mtd_desc->pkt));
525 
526 	mtd_desc->mtd.type_flags = GVE_TXD_MTD | GVE_MTD_SUBTYPE_PATH;
527 	mtd_desc->mtd.path_state = GVE_MTD_PATH_STATE_DEFAULT |
528 				   GVE_MTD_PATH_HASH_L4;
529 	mtd_desc->mtd.path_hash = cpu_to_be32(skb->hash);
530 	mtd_desc->mtd.reserved0 = 0;
531 	mtd_desc->mtd.reserved1 = 0;
532 }
533 
gve_tx_fill_seg_desc(union gve_tx_desc * seg_desc,u16 l3_offset,u16 gso_size,bool is_gso_v6,bool is_gso,u16 len,u64 addr)534 static void gve_tx_fill_seg_desc(union gve_tx_desc *seg_desc,
535 				 u16 l3_offset, u16 gso_size,
536 				 bool is_gso_v6, bool is_gso,
537 				 u16 len, u64 addr)
538 {
539 	seg_desc->seg.type_flags = GVE_TXD_SEG;
540 	if (is_gso) {
541 		if (is_gso_v6)
542 			seg_desc->seg.type_flags |= GVE_TXSF_IPV6;
543 		seg_desc->seg.l3_offset = l3_offset >> 1;
544 		seg_desc->seg.mss = cpu_to_be16(gso_size);
545 	}
546 	seg_desc->seg.seg_len = cpu_to_be16(len);
547 	seg_desc->seg.seg_addr = cpu_to_be64(addr);
548 }
549 
gve_dma_sync_for_device(struct device * dev,dma_addr_t * page_buses,u64 iov_offset,u64 iov_len)550 static void gve_dma_sync_for_device(struct device *dev, dma_addr_t *page_buses,
551 				    u64 iov_offset, u64 iov_len)
552 {
553 	u64 last_page = (iov_offset + iov_len - 1) / PAGE_SIZE;
554 	u64 first_page = iov_offset / PAGE_SIZE;
555 	u64 page;
556 
557 	for (page = first_page; page <= last_page; page++)
558 		dma_sync_single_for_device(dev, page_buses[page], PAGE_SIZE, DMA_TO_DEVICE);
559 }
560 
gve_tx_add_skb_copy(struct gve_priv * priv,struct gve_tx_ring * tx,struct sk_buff * skb)561 static int gve_tx_add_skb_copy(struct gve_priv *priv, struct gve_tx_ring *tx, struct sk_buff *skb)
562 {
563 	int pad_bytes, hlen, hdr_nfrags, payload_nfrags, l4_hdr_offset;
564 	union gve_tx_desc *pkt_desc, *seg_desc;
565 	struct gve_tx_buffer_state *info;
566 	int mtd_desc_nr = !!skb->l4_hash;
567 	bool is_gso = skb_is_gso(skb);
568 	u32 idx = tx->req & tx->mask;
569 	int payload_iov = 2;
570 	int copy_offset;
571 	u32 next_idx;
572 	int i;
573 
574 	info = &tx->info[idx];
575 	pkt_desc = &tx->desc[idx];
576 
577 	l4_hdr_offset = skb_checksum_start_offset(skb);
578 	/* If the skb is gso, then we want the tcp header alone in the first segment
579 	 * otherwise we want the minimum required by the gVNIC spec.
580 	 */
581 	hlen = is_gso ? l4_hdr_offset + tcp_hdrlen(skb) :
582 			min_t(int, GVE_GQ_TX_MIN_PKT_DESC_BYTES, skb->len);
583 
584 	info->skb =  skb;
585 	/* We don't want to split the header, so if necessary, pad to the end
586 	 * of the fifo and then put the header at the beginning of the fifo.
587 	 */
588 	pad_bytes = gve_tx_fifo_pad_alloc_one_frag(&tx->tx_fifo, hlen);
589 	hdr_nfrags = gve_tx_alloc_fifo(&tx->tx_fifo, hlen + pad_bytes,
590 				       &info->iov[0]);
591 	WARN(!hdr_nfrags, "hdr_nfrags should never be 0!");
592 	payload_nfrags = gve_tx_alloc_fifo(&tx->tx_fifo, skb->len - hlen,
593 					   &info->iov[payload_iov]);
594 
595 	gve_tx_fill_pkt_desc(pkt_desc, skb->csum_offset, skb->ip_summed,
596 			     is_gso, l4_hdr_offset,
597 			     1 + mtd_desc_nr + payload_nfrags, hlen,
598 			     info->iov[hdr_nfrags - 1].iov_offset, skb->len);
599 
600 	skb_copy_bits(skb, 0,
601 		      tx->tx_fifo.base + info->iov[hdr_nfrags - 1].iov_offset,
602 		      hlen);
603 	gve_dma_sync_for_device(&priv->pdev->dev, tx->tx_fifo.qpl->page_buses,
604 				info->iov[hdr_nfrags - 1].iov_offset,
605 				info->iov[hdr_nfrags - 1].iov_len);
606 	copy_offset = hlen;
607 
608 	if (mtd_desc_nr) {
609 		next_idx = (tx->req + 1) & tx->mask;
610 		gve_tx_fill_mtd_desc(&tx->desc[next_idx], skb);
611 	}
612 
613 	for (i = payload_iov; i < payload_nfrags + payload_iov; i++) {
614 		next_idx = (tx->req + 1 + mtd_desc_nr + i - payload_iov) & tx->mask;
615 		seg_desc = &tx->desc[next_idx];
616 
617 		gve_tx_fill_seg_desc(seg_desc, skb_network_offset(skb),
618 				     skb_shinfo(skb)->gso_size,
619 				     skb_is_gso_v6(skb), is_gso,
620 				     info->iov[i].iov_len,
621 				     info->iov[i].iov_offset);
622 
623 		skb_copy_bits(skb, copy_offset,
624 			      tx->tx_fifo.base + info->iov[i].iov_offset,
625 			      info->iov[i].iov_len);
626 		gve_dma_sync_for_device(&priv->pdev->dev, tx->tx_fifo.qpl->page_buses,
627 					info->iov[i].iov_offset,
628 					info->iov[i].iov_len);
629 		copy_offset += info->iov[i].iov_len;
630 	}
631 
632 	return 1 + mtd_desc_nr + payload_nfrags;
633 }
634 
gve_tx_add_skb_no_copy(struct gve_priv * priv,struct gve_tx_ring * tx,struct sk_buff * skb)635 static int gve_tx_add_skb_no_copy(struct gve_priv *priv, struct gve_tx_ring *tx,
636 				  struct sk_buff *skb)
637 {
638 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
639 	int hlen, num_descriptors, l4_hdr_offset;
640 	union gve_tx_desc *pkt_desc, *mtd_desc, *seg_desc;
641 	struct gve_tx_buffer_state *info;
642 	int mtd_desc_nr = !!skb->l4_hash;
643 	bool is_gso = skb_is_gso(skb);
644 	u32 idx = tx->req & tx->mask;
645 	u64 addr;
646 	u32 len;
647 	int i;
648 
649 	info = &tx->info[idx];
650 	pkt_desc = &tx->desc[idx];
651 
652 	l4_hdr_offset = skb_checksum_start_offset(skb);
653 	/* If the skb is gso, then we want only up to the tcp header in the first segment
654 	 * to efficiently replicate on each segment otherwise we want the linear portion
655 	 * of the skb (which will contain the checksum because skb->csum_start and
656 	 * skb->csum_offset are given relative to skb->head) in the first segment.
657 	 */
658 	hlen = is_gso ? l4_hdr_offset + tcp_hdrlen(skb) : skb_headlen(skb);
659 	len = skb_headlen(skb);
660 
661 	info->skb =  skb;
662 
663 	addr = dma_map_single(tx->dev, skb->data, len, DMA_TO_DEVICE);
664 	if (unlikely(dma_mapping_error(tx->dev, addr))) {
665 		tx->dma_mapping_error++;
666 		goto drop;
667 	}
668 	dma_unmap_len_set(info, len, len);
669 	dma_unmap_addr_set(info, dma, addr);
670 
671 	num_descriptors = 1 + shinfo->nr_frags;
672 	if (hlen < len)
673 		num_descriptors++;
674 	if (mtd_desc_nr)
675 		num_descriptors++;
676 
677 	gve_tx_fill_pkt_desc(pkt_desc, skb->csum_offset, skb->ip_summed,
678 			     is_gso, l4_hdr_offset,
679 			     num_descriptors, hlen, addr, skb->len);
680 
681 	if (mtd_desc_nr) {
682 		idx = (idx + 1) & tx->mask;
683 		mtd_desc = &tx->desc[idx];
684 		gve_tx_fill_mtd_desc(mtd_desc, skb);
685 	}
686 
687 	if (hlen < len) {
688 		/* For gso the rest of the linear portion of the skb needs to
689 		 * be in its own descriptor.
690 		 */
691 		len -= hlen;
692 		addr += hlen;
693 		idx = (idx + 1) & tx->mask;
694 		seg_desc = &tx->desc[idx];
695 		gve_tx_fill_seg_desc(seg_desc, skb_network_offset(skb),
696 				     skb_shinfo(skb)->gso_size,
697 				     skb_is_gso_v6(skb), is_gso, len, addr);
698 	}
699 
700 	for (i = 0; i < shinfo->nr_frags; i++) {
701 		const skb_frag_t *frag = &shinfo->frags[i];
702 
703 		idx = (idx + 1) & tx->mask;
704 		seg_desc = &tx->desc[idx];
705 		len = skb_frag_size(frag);
706 		addr = skb_frag_dma_map(tx->dev, frag, 0, len, DMA_TO_DEVICE);
707 		if (unlikely(dma_mapping_error(tx->dev, addr))) {
708 			tx->dma_mapping_error++;
709 			goto unmap_drop;
710 		}
711 		tx->info[idx].skb = NULL;
712 		dma_unmap_len_set(&tx->info[idx], len, len);
713 		dma_unmap_addr_set(&tx->info[idx], dma, addr);
714 
715 		gve_tx_fill_seg_desc(seg_desc, skb_network_offset(skb),
716 				     skb_shinfo(skb)->gso_size,
717 				     skb_is_gso_v6(skb), is_gso, len, addr);
718 	}
719 
720 	return num_descriptors;
721 
722 unmap_drop:
723 	i += num_descriptors - shinfo->nr_frags;
724 	while (i--) {
725 		/* Skip metadata descriptor, if set */
726 		if (i == 1 && mtd_desc_nr == 1)
727 			continue;
728 		idx--;
729 		gve_tx_unmap_buf(tx->dev, &tx->info[idx & tx->mask]);
730 	}
731 drop:
732 	u64_stats_update_begin(&tx->statss);
733 	tx->dropped_pkt++;
734 	u64_stats_update_end(&tx->statss);
735 	return 0;
736 }
737 
gve_tx(struct sk_buff * skb,struct net_device * dev)738 netdev_tx_t gve_tx(struct sk_buff *skb, struct net_device *dev)
739 {
740 	struct gve_priv *priv = netdev_priv(dev);
741 	struct gve_tx_ring *tx;
742 	int nsegs;
743 
744 	WARN(skb_get_queue_mapping(skb) >= priv->tx_cfg.num_queues,
745 	     "skb queue index out of range");
746 	tx = &priv->tx[skb_get_queue_mapping(skb)];
747 	if (unlikely(gve_maybe_stop_tx(priv, tx, skb))) {
748 		/* We need to ring the txq doorbell -- we have stopped the Tx
749 		 * queue for want of resources, but prior calls to gve_tx()
750 		 * may have added descriptors without ringing the doorbell.
751 		 */
752 
753 		gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
754 		return NETDEV_TX_BUSY;
755 	}
756 	if (tx->raw_addressing)
757 		nsegs = gve_tx_add_skb_no_copy(priv, tx, skb);
758 	else
759 		nsegs = gve_tx_add_skb_copy(priv, tx, skb);
760 
761 	/* If the packet is getting sent, we need to update the skb */
762 	if (nsegs) {
763 		netdev_tx_sent_queue(tx->netdev_txq, skb->len);
764 		skb_tx_timestamp(skb);
765 		tx->req += nsegs;
766 	} else {
767 		dev_kfree_skb_any(skb);
768 	}
769 
770 	if (!netif_xmit_stopped(tx->netdev_txq) && netdev_xmit_more())
771 		return NETDEV_TX_OK;
772 
773 	/* Give packets to NIC. Even if this packet failed to send the doorbell
774 	 * might need to be rung because of xmit_more.
775 	 */
776 	gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
777 	return NETDEV_TX_OK;
778 }
779 
gve_tx_fill_xdp(struct gve_priv * priv,struct gve_tx_ring * tx,void * data,int len,void * frame_p,bool is_xsk)780 static int gve_tx_fill_xdp(struct gve_priv *priv, struct gve_tx_ring *tx,
781 			   void *data, int len, void *frame_p, bool is_xsk)
782 {
783 	int pad, nfrags, ndescs, iovi, offset;
784 	struct gve_tx_buffer_state *info;
785 	u32 reqi = tx->req;
786 
787 	pad = gve_tx_fifo_pad_alloc_one_frag(&tx->tx_fifo, len);
788 	if (pad >= GVE_GQ_TX_MIN_PKT_DESC_BYTES)
789 		pad = 0;
790 	info = &tx->info[reqi & tx->mask];
791 	info->xdp_frame = frame_p;
792 	info->xdp.size = len;
793 	info->xdp.is_xsk = is_xsk;
794 
795 	nfrags = gve_tx_alloc_fifo(&tx->tx_fifo, pad + len,
796 				   &info->iov[0]);
797 	iovi = pad > 0;
798 	ndescs = nfrags - iovi;
799 	offset = 0;
800 
801 	while (iovi < nfrags) {
802 		if (!offset)
803 			gve_tx_fill_pkt_desc(&tx->desc[reqi & tx->mask], 0,
804 					     CHECKSUM_NONE, false, 0, ndescs,
805 					     info->iov[iovi].iov_len,
806 					     info->iov[iovi].iov_offset, len);
807 		else
808 			gve_tx_fill_seg_desc(&tx->desc[reqi & tx->mask],
809 					     0, 0, false, false,
810 					     info->iov[iovi].iov_len,
811 					     info->iov[iovi].iov_offset);
812 
813 		memcpy(tx->tx_fifo.base + info->iov[iovi].iov_offset,
814 		       data + offset, info->iov[iovi].iov_len);
815 		gve_dma_sync_for_device(&priv->pdev->dev,
816 					tx->tx_fifo.qpl->page_buses,
817 					info->iov[iovi].iov_offset,
818 					info->iov[iovi].iov_len);
819 		offset += info->iov[iovi].iov_len;
820 		iovi++;
821 		reqi++;
822 	}
823 
824 	return ndescs;
825 }
826 
gve_xdp_xmit_gqi(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)827 int gve_xdp_xmit_gqi(struct net_device *dev, int n, struct xdp_frame **frames,
828 		     u32 flags)
829 {
830 	struct gve_priv *priv = netdev_priv(dev);
831 	struct gve_tx_ring *tx;
832 	int i, err = 0, qid;
833 
834 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK) || !priv->xdp_prog)
835 		return -EINVAL;
836 
837 	if (!gve_get_napi_enabled(priv))
838 		return -ENETDOWN;
839 
840 	qid = gve_xdp_tx_queue_id(priv,
841 				  smp_processor_id() % priv->tx_cfg.num_xdp_queues);
842 
843 	tx = &priv->tx[qid];
844 
845 	spin_lock(&tx->xdp_lock);
846 	for (i = 0; i < n; i++) {
847 		err = gve_xdp_xmit_one(priv, tx, frames[i]->data,
848 				       frames[i]->len, frames[i]);
849 		if (err)
850 			break;
851 	}
852 
853 	if (flags & XDP_XMIT_FLUSH)
854 		gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
855 
856 	spin_unlock(&tx->xdp_lock);
857 
858 	u64_stats_update_begin(&tx->statss);
859 	tx->xdp_xmit += n;
860 	tx->xdp_xmit_errors += n - i;
861 	u64_stats_update_end(&tx->statss);
862 
863 	return i ? i : err;
864 }
865 
gve_xdp_xmit_one(struct gve_priv * priv,struct gve_tx_ring * tx,void * data,int len,void * frame_p)866 int gve_xdp_xmit_one(struct gve_priv *priv, struct gve_tx_ring *tx,
867 		     void *data, int len, void *frame_p)
868 {
869 	int nsegs;
870 
871 	if (!gve_can_tx(tx, len + GVE_GQ_TX_MIN_PKT_DESC_BYTES - 1))
872 		return -EBUSY;
873 
874 	nsegs = gve_tx_fill_xdp(priv, tx, data, len, frame_p, false);
875 	tx->req += nsegs;
876 
877 	return 0;
878 }
879 
880 #define GVE_TX_START_THRESH	4096
881 
gve_clean_tx_done(struct gve_priv * priv,struct gve_tx_ring * tx,u32 to_do,bool try_to_wake)882 static int gve_clean_tx_done(struct gve_priv *priv, struct gve_tx_ring *tx,
883 			     u32 to_do, bool try_to_wake)
884 {
885 	struct gve_tx_buffer_state *info;
886 	u64 pkts = 0, bytes = 0;
887 	size_t space_freed = 0;
888 	struct sk_buff *skb;
889 	u32 idx;
890 	int j;
891 
892 	for (j = 0; j < to_do; j++) {
893 		idx = tx->done & tx->mask;
894 		netif_info(priv, tx_done, priv->dev,
895 			   "[%d] %s: idx=%d (req=%u done=%u)\n",
896 			   tx->q_num, __func__, idx, tx->req, tx->done);
897 		info = &tx->info[idx];
898 		skb = info->skb;
899 
900 		/* Unmap the buffer */
901 		if (tx->raw_addressing)
902 			gve_tx_unmap_buf(tx->dev, info);
903 		tx->done++;
904 		/* Mark as free */
905 		if (skb) {
906 			info->skb = NULL;
907 			bytes += skb->len;
908 			pkts++;
909 			dev_consume_skb_any(skb);
910 			if (tx->raw_addressing)
911 				continue;
912 			space_freed += gve_tx_clear_buffer_state(info);
913 		}
914 	}
915 
916 	if (!tx->raw_addressing)
917 		gve_tx_free_fifo(&tx->tx_fifo, space_freed);
918 	u64_stats_update_begin(&tx->statss);
919 	tx->bytes_done += bytes;
920 	tx->pkt_done += pkts;
921 	u64_stats_update_end(&tx->statss);
922 	netdev_tx_completed_queue(tx->netdev_txq, pkts, bytes);
923 
924 	/* start the queue if we've stopped it */
925 #ifndef CONFIG_BQL
926 	/* Make sure that the doorbells are synced */
927 	smp_mb();
928 #endif
929 	if (try_to_wake && netif_tx_queue_stopped(tx->netdev_txq) &&
930 	    likely(gve_can_tx(tx, GVE_TX_START_THRESH))) {
931 		tx->wake_queue++;
932 		netif_tx_wake_queue(tx->netdev_txq);
933 	}
934 
935 	return pkts;
936 }
937 
gve_tx_load_event_counter(struct gve_priv * priv,struct gve_tx_ring * tx)938 u32 gve_tx_load_event_counter(struct gve_priv *priv,
939 			      struct gve_tx_ring *tx)
940 {
941 	u32 counter_index = be32_to_cpu(tx->q_resources->counter_index);
942 	__be32 counter = READ_ONCE(priv->counter_array[counter_index]);
943 
944 	return be32_to_cpu(counter);
945 }
946 
gve_xsk_tx(struct gve_priv * priv,struct gve_tx_ring * tx,int budget)947 static int gve_xsk_tx(struct gve_priv *priv, struct gve_tx_ring *tx,
948 		      int budget)
949 {
950 	struct xdp_desc desc;
951 	int sent = 0, nsegs;
952 	void *data;
953 
954 	spin_lock(&tx->xdp_lock);
955 	while (sent < budget) {
956 		if (!gve_can_tx(tx, GVE_TX_START_THRESH) ||
957 		    !xsk_tx_peek_desc(tx->xsk_pool, &desc))
958 			goto out;
959 
960 		data = xsk_buff_raw_get_data(tx->xsk_pool, desc.addr);
961 		nsegs = gve_tx_fill_xdp(priv, tx, data, desc.len, NULL, true);
962 		tx->req += nsegs;
963 		sent++;
964 	}
965 out:
966 	if (sent > 0) {
967 		gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
968 		xsk_tx_release(tx->xsk_pool);
969 	}
970 	spin_unlock(&tx->xdp_lock);
971 	return sent;
972 }
973 
gve_xsk_tx_poll(struct gve_notify_block * rx_block,int budget)974 int gve_xsk_tx_poll(struct gve_notify_block *rx_block, int budget)
975 {
976 	struct gve_rx_ring *rx = rx_block->rx;
977 	struct gve_priv *priv = rx->gve;
978 	struct gve_tx_ring *tx;
979 	int sent = 0;
980 
981 	tx = &priv->tx[gve_xdp_tx_queue_id(priv, rx->q_num)];
982 	if (tx->xsk_pool) {
983 		sent = gve_xsk_tx(priv, tx, budget);
984 
985 		u64_stats_update_begin(&tx->statss);
986 		tx->xdp_xsk_sent += sent;
987 		u64_stats_update_end(&tx->statss);
988 		if (xsk_uses_need_wakeup(tx->xsk_pool))
989 			xsk_set_tx_need_wakeup(tx->xsk_pool);
990 	}
991 
992 	return sent;
993 }
994 
gve_xdp_poll(struct gve_notify_block * block,int budget)995 bool gve_xdp_poll(struct gve_notify_block *block, int budget)
996 {
997 	struct gve_priv *priv = block->priv;
998 	struct gve_tx_ring *tx = block->tx;
999 	u32 nic_done;
1000 	u32 to_do;
1001 
1002 	/* Find out how much work there is to be done */
1003 	nic_done = gve_tx_load_event_counter(priv, tx);
1004 	to_do = min_t(u32, (nic_done - tx->done), budget);
1005 	gve_clean_xdp_done(priv, tx, to_do);
1006 
1007 	/* If we still have work we want to repoll */
1008 	return nic_done != tx->done;
1009 }
1010 
gve_tx_poll(struct gve_notify_block * block,int budget)1011 bool gve_tx_poll(struct gve_notify_block *block, int budget)
1012 {
1013 	struct gve_priv *priv = block->priv;
1014 	struct gve_tx_ring *tx = block->tx;
1015 	u32 nic_done;
1016 	u32 to_do;
1017 
1018 	/* If budget is 0, do all the work */
1019 	if (budget == 0)
1020 		budget = INT_MAX;
1021 
1022 	/* In TX path, it may try to clean completed pkts in order to xmit,
1023 	 * to avoid cleaning conflict, use spin_lock(), it yields better
1024 	 * concurrency between xmit/clean than netif's lock.
1025 	 */
1026 	spin_lock(&tx->clean_lock);
1027 	/* Find out how much work there is to be done */
1028 	nic_done = gve_tx_load_event_counter(priv, tx);
1029 	to_do = min_t(u32, (nic_done - tx->done), budget);
1030 	gve_clean_tx_done(priv, tx, to_do, true);
1031 	spin_unlock(&tx->clean_lock);
1032 	/* If we still have work we want to repoll */
1033 	return nic_done != tx->done;
1034 }
1035 
gve_tx_clean_pending(struct gve_priv * priv,struct gve_tx_ring * tx)1036 bool gve_tx_clean_pending(struct gve_priv *priv, struct gve_tx_ring *tx)
1037 {
1038 	u32 nic_done = gve_tx_load_event_counter(priv, tx);
1039 
1040 	return nic_done != tx->done;
1041 }
1042