xref: /openbmc/linux/drivers/net/ethernet/sfc/siena/tx_common.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1d48523cbSMartin Habets // SPDX-License-Identifier: GPL-2.0-only
2d48523cbSMartin Habets /****************************************************************************
3d48523cbSMartin Habets  * Driver for Solarflare network controllers and boards
4d48523cbSMartin Habets  * Copyright 2018 Solarflare Communications Inc.
5d48523cbSMartin Habets  *
6d48523cbSMartin Habets  * This program is free software; you can redistribute it and/or modify it
7d48523cbSMartin Habets  * under the terms of the GNU General Public License version 2 as published
8d48523cbSMartin Habets  * by the Free Software Foundation, incorporated herein by reference.
9d48523cbSMartin Habets  */
10d48523cbSMartin Habets 
11d48523cbSMartin Habets #include "net_driver.h"
12d48523cbSMartin Habets #include "efx.h"
13d48523cbSMartin Habets #include "nic_common.h"
14d48523cbSMartin Habets #include "tx_common.h"
15*d457a0e3SEric Dumazet #include <net/gso.h>
16d48523cbSMartin Habets 
efx_tx_cb_page_count(struct efx_tx_queue * tx_queue)17d48523cbSMartin Habets static unsigned int efx_tx_cb_page_count(struct efx_tx_queue *tx_queue)
18d48523cbSMartin Habets {
19d48523cbSMartin Habets 	return DIV_ROUND_UP(tx_queue->ptr_mask + 1,
20d48523cbSMartin Habets 			    PAGE_SIZE >> EFX_TX_CB_ORDER);
21d48523cbSMartin Habets }
22d48523cbSMartin Habets 
efx_siena_probe_tx_queue(struct efx_tx_queue * tx_queue)237f9e4b2aSMartin Habets int efx_siena_probe_tx_queue(struct efx_tx_queue *tx_queue)
24d48523cbSMartin Habets {
25d48523cbSMartin Habets 	struct efx_nic *efx = tx_queue->efx;
26d48523cbSMartin Habets 	unsigned int entries;
27d48523cbSMartin Habets 	int rc;
28d48523cbSMartin Habets 
29d48523cbSMartin Habets 	/* Create the smallest power-of-two aligned ring */
30d48523cbSMartin Habets 	entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
31d48523cbSMartin Habets 	EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
32d48523cbSMartin Habets 	tx_queue->ptr_mask = entries - 1;
33d48523cbSMartin Habets 
34d48523cbSMartin Habets 	netif_dbg(efx, probe, efx->net_dev,
35d48523cbSMartin Habets 		  "creating TX queue %d size %#x mask %#x\n",
36d48523cbSMartin Habets 		  tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
37d48523cbSMartin Habets 
38d48523cbSMartin Habets 	/* Allocate software ring */
39d48523cbSMartin Habets 	tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
40d48523cbSMartin Habets 				   GFP_KERNEL);
41d48523cbSMartin Habets 	if (!tx_queue->buffer)
42d48523cbSMartin Habets 		return -ENOMEM;
43d48523cbSMartin Habets 
44d48523cbSMartin Habets 	tx_queue->cb_page = kcalloc(efx_tx_cb_page_count(tx_queue),
45d48523cbSMartin Habets 				    sizeof(tx_queue->cb_page[0]), GFP_KERNEL);
46d48523cbSMartin Habets 	if (!tx_queue->cb_page) {
47d48523cbSMartin Habets 		rc = -ENOMEM;
48d48523cbSMartin Habets 		goto fail1;
49d48523cbSMartin Habets 	}
50d48523cbSMartin Habets 
51d48523cbSMartin Habets 	/* Allocate hardware ring, determine TXQ type */
52d48523cbSMartin Habets 	rc = efx_nic_probe_tx(tx_queue);
53d48523cbSMartin Habets 	if (rc)
54d48523cbSMartin Habets 		goto fail2;
55d48523cbSMartin Habets 
56d48523cbSMartin Habets 	tx_queue->channel->tx_queue_by_type[tx_queue->type] = tx_queue;
57d48523cbSMartin Habets 	return 0;
58d48523cbSMartin Habets 
59d48523cbSMartin Habets fail2:
60d48523cbSMartin Habets 	kfree(tx_queue->cb_page);
61d48523cbSMartin Habets 	tx_queue->cb_page = NULL;
62d48523cbSMartin Habets fail1:
63d48523cbSMartin Habets 	kfree(tx_queue->buffer);
64d48523cbSMartin Habets 	tx_queue->buffer = NULL;
65d48523cbSMartin Habets 	return rc;
66d48523cbSMartin Habets }
67d48523cbSMartin Habets 
efx_siena_init_tx_queue(struct efx_tx_queue * tx_queue)687f9e4b2aSMartin Habets void efx_siena_init_tx_queue(struct efx_tx_queue *tx_queue)
69d48523cbSMartin Habets {
70d48523cbSMartin Habets 	struct efx_nic *efx = tx_queue->efx;
71d48523cbSMartin Habets 
72d48523cbSMartin Habets 	netif_dbg(efx, drv, efx->net_dev,
73d48523cbSMartin Habets 		  "initialising TX queue %d\n", tx_queue->queue);
74d48523cbSMartin Habets 
75d48523cbSMartin Habets 	tx_queue->insert_count = 0;
76d48523cbSMartin Habets 	tx_queue->notify_count = 0;
77d48523cbSMartin Habets 	tx_queue->write_count = 0;
78d48523cbSMartin Habets 	tx_queue->packet_write_count = 0;
79d48523cbSMartin Habets 	tx_queue->old_write_count = 0;
80d48523cbSMartin Habets 	tx_queue->read_count = 0;
81d48523cbSMartin Habets 	tx_queue->old_read_count = 0;
82d48523cbSMartin Habets 	tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
83d48523cbSMartin Habets 	tx_queue->xmit_pending = false;
8495e96f77SMartin Habets 	tx_queue->timestamping = (efx_siena_ptp_use_mac_tx_timestamps(efx) &&
8595e96f77SMartin Habets 				  tx_queue->channel == efx_siena_ptp_channel(efx));
86d48523cbSMartin Habets 	tx_queue->completed_timestamp_major = 0;
87d48523cbSMartin Habets 	tx_queue->completed_timestamp_minor = 0;
88d48523cbSMartin Habets 
89d48523cbSMartin Habets 	tx_queue->xdp_tx = efx_channel_is_xdp_tx(tx_queue->channel);
90d48523cbSMartin Habets 	tx_queue->tso_version = 0;
91d48523cbSMartin Habets 
92d48523cbSMartin Habets 	/* Set up TX descriptor ring */
93d48523cbSMartin Habets 	efx_nic_init_tx(tx_queue);
94d48523cbSMartin Habets 
95d48523cbSMartin Habets 	tx_queue->initialised = true;
96d48523cbSMartin Habets }
97d48523cbSMartin Habets 
efx_siena_remove_tx_queue(struct efx_tx_queue * tx_queue)987f9e4b2aSMartin Habets void efx_siena_remove_tx_queue(struct efx_tx_queue *tx_queue)
99d48523cbSMartin Habets {
100d48523cbSMartin Habets 	int i;
101d48523cbSMartin Habets 
102d48523cbSMartin Habets 	if (!tx_queue->buffer)
103d48523cbSMartin Habets 		return;
104d48523cbSMartin Habets 
105d48523cbSMartin Habets 	netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
106d48523cbSMartin Habets 		  "destroying TX queue %d\n", tx_queue->queue);
107d48523cbSMartin Habets 	efx_nic_remove_tx(tx_queue);
108d48523cbSMartin Habets 
109d48523cbSMartin Habets 	if (tx_queue->cb_page) {
110d48523cbSMartin Habets 		for (i = 0; i < efx_tx_cb_page_count(tx_queue); i++)
111c8443b69SMartin Habets 			efx_siena_free_buffer(tx_queue->efx,
112d48523cbSMartin Habets 					      &tx_queue->cb_page[i]);
113d48523cbSMartin Habets 		kfree(tx_queue->cb_page);
114d48523cbSMartin Habets 		tx_queue->cb_page = NULL;
115d48523cbSMartin Habets 	}
116d48523cbSMartin Habets 
117d48523cbSMartin Habets 	kfree(tx_queue->buffer);
118d48523cbSMartin Habets 	tx_queue->buffer = NULL;
119d48523cbSMartin Habets 	tx_queue->channel->tx_queue_by_type[tx_queue->type] = NULL;
120d48523cbSMartin Habets }
121d48523cbSMartin Habets 
efx_dequeue_buffer(struct efx_tx_queue * tx_queue,struct efx_tx_buffer * buffer,unsigned int * pkts_compl,unsigned int * bytes_compl)1227f9e4b2aSMartin Habets static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
123d48523cbSMartin Habets 			       struct efx_tx_buffer *buffer,
124d48523cbSMartin Habets 			       unsigned int *pkts_compl,
125d48523cbSMartin Habets 			       unsigned int *bytes_compl)
126d48523cbSMartin Habets {
127d48523cbSMartin Habets 	if (buffer->unmap_len) {
128d48523cbSMartin Habets 		struct device *dma_dev = &tx_queue->efx->pci_dev->dev;
129d48523cbSMartin Habets 		dma_addr_t unmap_addr = buffer->dma_addr - buffer->dma_offset;
130d48523cbSMartin Habets 
131d48523cbSMartin Habets 		if (buffer->flags & EFX_TX_BUF_MAP_SINGLE)
132d48523cbSMartin Habets 			dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len,
133d48523cbSMartin Habets 					 DMA_TO_DEVICE);
134d48523cbSMartin Habets 		else
135d48523cbSMartin Habets 			dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len,
136d48523cbSMartin Habets 				       DMA_TO_DEVICE);
137d48523cbSMartin Habets 		buffer->unmap_len = 0;
138d48523cbSMartin Habets 	}
139d48523cbSMartin Habets 
140d48523cbSMartin Habets 	if (buffer->flags & EFX_TX_BUF_SKB) {
141d48523cbSMartin Habets 		struct sk_buff *skb = (struct sk_buff *)buffer->skb;
142d48523cbSMartin Habets 
143d48523cbSMartin Habets 		EFX_WARN_ON_PARANOID(!pkts_compl || !bytes_compl);
144d48523cbSMartin Habets 		(*pkts_compl)++;
145d48523cbSMartin Habets 		(*bytes_compl) += skb->len;
146d48523cbSMartin Habets 		if (tx_queue->timestamping &&
147d48523cbSMartin Habets 		    (tx_queue->completed_timestamp_major ||
148d48523cbSMartin Habets 		     tx_queue->completed_timestamp_minor)) {
149d48523cbSMartin Habets 			struct skb_shared_hwtstamps hwtstamp;
150d48523cbSMartin Habets 
151d48523cbSMartin Habets 			hwtstamp.hwtstamp =
15295e96f77SMartin Habets 				efx_siena_ptp_nic_to_kernel_time(tx_queue);
153d48523cbSMartin Habets 			skb_tstamp_tx(skb, &hwtstamp);
154d48523cbSMartin Habets 
155d48523cbSMartin Habets 			tx_queue->completed_timestamp_major = 0;
156d48523cbSMartin Habets 			tx_queue->completed_timestamp_minor = 0;
157d48523cbSMartin Habets 		}
158d48523cbSMartin Habets 		dev_consume_skb_any((struct sk_buff *)buffer->skb);
159d48523cbSMartin Habets 		netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
160d48523cbSMartin Habets 			   "TX queue %d transmission id %x complete\n",
161d48523cbSMartin Habets 			   tx_queue->queue, tx_queue->read_count);
162d48523cbSMartin Habets 	} else if (buffer->flags & EFX_TX_BUF_XDP) {
163d48523cbSMartin Habets 		xdp_return_frame_rx_napi(buffer->xdpf);
164d48523cbSMartin Habets 	}
165d48523cbSMartin Habets 
166d48523cbSMartin Habets 	buffer->len = 0;
167d48523cbSMartin Habets 	buffer->flags = 0;
168d48523cbSMartin Habets }
169d48523cbSMartin Habets 
efx_siena_fini_tx_queue(struct efx_tx_queue * tx_queue)1707f9e4b2aSMartin Habets void efx_siena_fini_tx_queue(struct efx_tx_queue *tx_queue)
1717f9e4b2aSMartin Habets {
1727f9e4b2aSMartin Habets 	struct efx_tx_buffer *buffer;
1737f9e4b2aSMartin Habets 
1747f9e4b2aSMartin Habets 	netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
1757f9e4b2aSMartin Habets 		  "shutting down TX queue %d\n", tx_queue->queue);
1767f9e4b2aSMartin Habets 
1777f9e4b2aSMartin Habets 	if (!tx_queue->buffer)
1787f9e4b2aSMartin Habets 		return;
1797f9e4b2aSMartin Habets 
1807f9e4b2aSMartin Habets 	/* Free any buffers left in the ring */
1817f9e4b2aSMartin Habets 	while (tx_queue->read_count != tx_queue->write_count) {
1827f9e4b2aSMartin Habets 		unsigned int pkts_compl = 0, bytes_compl = 0;
1837f9e4b2aSMartin Habets 
1847f9e4b2aSMartin Habets 		buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
1857f9e4b2aSMartin Habets 		efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
1867f9e4b2aSMartin Habets 
1877f9e4b2aSMartin Habets 		++tx_queue->read_count;
1887f9e4b2aSMartin Habets 	}
1897f9e4b2aSMartin Habets 	tx_queue->xmit_pending = false;
1907f9e4b2aSMartin Habets 	netdev_tx_reset_queue(tx_queue->core_txq);
1917f9e4b2aSMartin Habets }
1927f9e4b2aSMartin Habets 
193d48523cbSMartin Habets /* Remove packets from the TX queue
194d48523cbSMartin Habets  *
195d48523cbSMartin Habets  * This removes packets from the TX queue, up to and including the
196d48523cbSMartin Habets  * specified index.
197d48523cbSMartin Habets  */
efx_dequeue_buffers(struct efx_tx_queue * tx_queue,unsigned int index,unsigned int * pkts_compl,unsigned int * bytes_compl)198d48523cbSMartin Habets static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
199d48523cbSMartin Habets 				unsigned int index,
200d48523cbSMartin Habets 				unsigned int *pkts_compl,
201d48523cbSMartin Habets 				unsigned int *bytes_compl)
202d48523cbSMartin Habets {
203d48523cbSMartin Habets 	struct efx_nic *efx = tx_queue->efx;
204d48523cbSMartin Habets 	unsigned int stop_index, read_ptr;
205d48523cbSMartin Habets 
206d48523cbSMartin Habets 	stop_index = (index + 1) & tx_queue->ptr_mask;
207d48523cbSMartin Habets 	read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
208d48523cbSMartin Habets 
209d48523cbSMartin Habets 	while (read_ptr != stop_index) {
210d48523cbSMartin Habets 		struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
211d48523cbSMartin Habets 
212d48523cbSMartin Habets 		if (!efx_tx_buffer_in_use(buffer)) {
213d48523cbSMartin Habets 			netif_err(efx, tx_err, efx->net_dev,
214d48523cbSMartin Habets 				  "TX queue %d spurious TX completion id %d\n",
215d48523cbSMartin Habets 				  tx_queue->queue, read_ptr);
21671ad88f6SMartin Habets 			efx_siena_schedule_reset(efx, RESET_TYPE_TX_SKIP);
217d48523cbSMartin Habets 			return;
218d48523cbSMartin Habets 		}
219d48523cbSMartin Habets 
220d48523cbSMartin Habets 		efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
221d48523cbSMartin Habets 
222d48523cbSMartin Habets 		++tx_queue->read_count;
223d48523cbSMartin Habets 		read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
224d48523cbSMartin Habets 	}
225d48523cbSMartin Habets }
226d48523cbSMartin Habets 
efx_siena_xmit_done_check_empty(struct efx_tx_queue * tx_queue)22771ad88f6SMartin Habets void efx_siena_xmit_done_check_empty(struct efx_tx_queue *tx_queue)
228d48523cbSMartin Habets {
229d48523cbSMartin Habets 	if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
230d48523cbSMartin Habets 		tx_queue->old_write_count = READ_ONCE(tx_queue->write_count);
231d48523cbSMartin Habets 		if (tx_queue->read_count == tx_queue->old_write_count) {
232d48523cbSMartin Habets 			/* Ensure that read_count is flushed. */
233d48523cbSMartin Habets 			smp_mb();
234d48523cbSMartin Habets 			tx_queue->empty_read_count =
235d48523cbSMartin Habets 				tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
236d48523cbSMartin Habets 		}
237d48523cbSMartin Habets 	}
238d48523cbSMartin Habets }
239d48523cbSMartin Habets 
efx_siena_xmit_done(struct efx_tx_queue * tx_queue,unsigned int index)24071ad88f6SMartin Habets void efx_siena_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
241d48523cbSMartin Habets {
242d48523cbSMartin Habets 	unsigned int fill_level, pkts_compl = 0, bytes_compl = 0;
243d48523cbSMartin Habets 	struct efx_nic *efx = tx_queue->efx;
244d48523cbSMartin Habets 
245d48523cbSMartin Habets 	EFX_WARN_ON_ONCE_PARANOID(index > tx_queue->ptr_mask);
246d48523cbSMartin Habets 
247d48523cbSMartin Habets 	efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
248d48523cbSMartin Habets 	tx_queue->pkts_compl += pkts_compl;
249d48523cbSMartin Habets 	tx_queue->bytes_compl += bytes_compl;
250d48523cbSMartin Habets 
251d48523cbSMartin Habets 	if (pkts_compl > 1)
252d48523cbSMartin Habets 		++tx_queue->merge_events;
253d48523cbSMartin Habets 
254d48523cbSMartin Habets 	/* See if we need to restart the netif queue.  This memory
255d48523cbSMartin Habets 	 * barrier ensures that we write read_count (inside
256d48523cbSMartin Habets 	 * efx_dequeue_buffers()) before reading the queue status.
257d48523cbSMartin Habets 	 */
258d48523cbSMartin Habets 	smp_mb();
259d48523cbSMartin Habets 	if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
260d48523cbSMartin Habets 	    likely(efx->port_enabled) &&
261d48523cbSMartin Habets 	    likely(netif_device_present(efx->net_dev))) {
262d48523cbSMartin Habets 		fill_level = efx_channel_tx_fill_level(tx_queue->channel);
263d48523cbSMartin Habets 		if (fill_level <= efx->txq_wake_thresh)
264d48523cbSMartin Habets 			netif_tx_wake_queue(tx_queue->core_txq);
265d48523cbSMartin Habets 	}
266d48523cbSMartin Habets 
26771ad88f6SMartin Habets 	efx_siena_xmit_done_check_empty(tx_queue);
268d48523cbSMartin Habets }
269d48523cbSMartin Habets 
270d48523cbSMartin Habets /* Remove buffers put into a tx_queue for the current packet.
271d48523cbSMartin Habets  * None of the buffers must have an skb attached.
272d48523cbSMartin Habets  */
efx_siena_enqueue_unwind(struct efx_tx_queue * tx_queue,unsigned int insert_count)2737f9e4b2aSMartin Habets void efx_siena_enqueue_unwind(struct efx_tx_queue *tx_queue,
274d48523cbSMartin Habets 			      unsigned int insert_count)
275d48523cbSMartin Habets {
276d48523cbSMartin Habets 	struct efx_tx_buffer *buffer;
277d48523cbSMartin Habets 	unsigned int bytes_compl = 0;
278d48523cbSMartin Habets 	unsigned int pkts_compl = 0;
279d48523cbSMartin Habets 
280d48523cbSMartin Habets 	/* Work backwards until we hit the original insert pointer value */
281d48523cbSMartin Habets 	while (tx_queue->insert_count != insert_count) {
282d48523cbSMartin Habets 		--tx_queue->insert_count;
283d48523cbSMartin Habets 		buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
284d48523cbSMartin Habets 		efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
285d48523cbSMartin Habets 	}
286d48523cbSMartin Habets }
287d48523cbSMartin Habets 
efx_siena_tx_map_chunk(struct efx_tx_queue * tx_queue,dma_addr_t dma_addr,size_t len)2887f9e4b2aSMartin Habets struct efx_tx_buffer *efx_siena_tx_map_chunk(struct efx_tx_queue *tx_queue,
289d48523cbSMartin Habets 					     dma_addr_t dma_addr, size_t len)
290d48523cbSMartin Habets {
291d48523cbSMartin Habets 	const struct efx_nic_type *nic_type = tx_queue->efx->type;
292d48523cbSMartin Habets 	struct efx_tx_buffer *buffer;
293d48523cbSMartin Habets 	unsigned int dma_len;
294d48523cbSMartin Habets 
295d48523cbSMartin Habets 	/* Map the fragment taking account of NIC-dependent DMA limits. */
296d48523cbSMartin Habets 	do {
297d48523cbSMartin Habets 		buffer = efx_tx_queue_get_insert_buffer(tx_queue);
298d48523cbSMartin Habets 
299d48523cbSMartin Habets 		if (nic_type->tx_limit_len)
300d48523cbSMartin Habets 			dma_len = nic_type->tx_limit_len(tx_queue, dma_addr, len);
301d48523cbSMartin Habets 		else
302d48523cbSMartin Habets 			dma_len = len;
303d48523cbSMartin Habets 
304d48523cbSMartin Habets 		buffer->len = dma_len;
305d48523cbSMartin Habets 		buffer->dma_addr = dma_addr;
306d48523cbSMartin Habets 		buffer->flags = EFX_TX_BUF_CONT;
307d48523cbSMartin Habets 		len -= dma_len;
308d48523cbSMartin Habets 		dma_addr += dma_len;
309d48523cbSMartin Habets 		++tx_queue->insert_count;
310d48523cbSMartin Habets 	} while (len);
311d48523cbSMartin Habets 
312d48523cbSMartin Habets 	return buffer;
313d48523cbSMartin Habets }
314d48523cbSMartin Habets 
efx_tx_tso_header_length(struct sk_buff * skb)3157f9e4b2aSMartin Habets static int efx_tx_tso_header_length(struct sk_buff *skb)
316d48523cbSMartin Habets {
317d48523cbSMartin Habets 	size_t header_len;
318d48523cbSMartin Habets 
319d48523cbSMartin Habets 	if (skb->encapsulation)
320d48523cbSMartin Habets 		header_len = skb_inner_transport_header(skb) -
321d48523cbSMartin Habets 				skb->data +
322d48523cbSMartin Habets 				(inner_tcp_hdr(skb)->doff << 2u);
323d48523cbSMartin Habets 	else
324d48523cbSMartin Habets 		header_len = skb_transport_header(skb) - skb->data +
325d48523cbSMartin Habets 				(tcp_hdr(skb)->doff << 2u);
326d48523cbSMartin Habets 	return header_len;
327d48523cbSMartin Habets }
328d48523cbSMartin Habets 
329d48523cbSMartin Habets /* Map all data from an SKB for DMA and create descriptors on the queue. */
efx_siena_tx_map_data(struct efx_tx_queue * tx_queue,struct sk_buff * skb,unsigned int segment_count)3307f9e4b2aSMartin Habets int efx_siena_tx_map_data(struct efx_tx_queue *tx_queue, struct sk_buff *skb,
331d48523cbSMartin Habets 			  unsigned int segment_count)
332d48523cbSMartin Habets {
333d48523cbSMartin Habets 	struct efx_nic *efx = tx_queue->efx;
334d48523cbSMartin Habets 	struct device *dma_dev = &efx->pci_dev->dev;
335d48523cbSMartin Habets 	unsigned int frag_index, nr_frags;
336d48523cbSMartin Habets 	dma_addr_t dma_addr, unmap_addr;
337d48523cbSMartin Habets 	unsigned short dma_flags;
338d48523cbSMartin Habets 	size_t len, unmap_len;
339d48523cbSMartin Habets 
340d48523cbSMartin Habets 	nr_frags = skb_shinfo(skb)->nr_frags;
341d48523cbSMartin Habets 	frag_index = 0;
342d48523cbSMartin Habets 
343d48523cbSMartin Habets 	/* Map header data. */
344d48523cbSMartin Habets 	len = skb_headlen(skb);
345d48523cbSMartin Habets 	dma_addr = dma_map_single(dma_dev, skb->data, len, DMA_TO_DEVICE);
346d48523cbSMartin Habets 	dma_flags = EFX_TX_BUF_MAP_SINGLE;
347d48523cbSMartin Habets 	unmap_len = len;
348d48523cbSMartin Habets 	unmap_addr = dma_addr;
349d48523cbSMartin Habets 
350d48523cbSMartin Habets 	if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
351d48523cbSMartin Habets 		return -EIO;
352d48523cbSMartin Habets 
353d48523cbSMartin Habets 	if (segment_count) {
354d48523cbSMartin Habets 		/* For TSO we need to put the header in to a separate
355d48523cbSMartin Habets 		 * descriptor. Map this separately if necessary.
356d48523cbSMartin Habets 		 */
357d48523cbSMartin Habets 		size_t header_len = efx_tx_tso_header_length(skb);
358d48523cbSMartin Habets 
359d48523cbSMartin Habets 		if (header_len != len) {
360d48523cbSMartin Habets 			tx_queue->tso_long_headers++;
3617f9e4b2aSMartin Habets 			efx_siena_tx_map_chunk(tx_queue, dma_addr, header_len);
362d48523cbSMartin Habets 			len -= header_len;
363d48523cbSMartin Habets 			dma_addr += header_len;
364d48523cbSMartin Habets 		}
365d48523cbSMartin Habets 	}
366d48523cbSMartin Habets 
367d48523cbSMartin Habets 	/* Add descriptors for each fragment. */
368d48523cbSMartin Habets 	do {
369d48523cbSMartin Habets 		struct efx_tx_buffer *buffer;
370d48523cbSMartin Habets 		skb_frag_t *fragment;
371d48523cbSMartin Habets 
3727f9e4b2aSMartin Habets 		buffer = efx_siena_tx_map_chunk(tx_queue, dma_addr, len);
373d48523cbSMartin Habets 
374d48523cbSMartin Habets 		/* The final descriptor for a fragment is responsible for
375d48523cbSMartin Habets 		 * unmapping the whole fragment.
376d48523cbSMartin Habets 		 */
377d48523cbSMartin Habets 		buffer->flags = EFX_TX_BUF_CONT | dma_flags;
378d48523cbSMartin Habets 		buffer->unmap_len = unmap_len;
379d48523cbSMartin Habets 		buffer->dma_offset = buffer->dma_addr - unmap_addr;
380d48523cbSMartin Habets 
381d48523cbSMartin Habets 		if (frag_index >= nr_frags) {
382d48523cbSMartin Habets 			/* Store SKB details with the final buffer for
383d48523cbSMartin Habets 			 * the completion.
384d48523cbSMartin Habets 			 */
385d48523cbSMartin Habets 			buffer->skb = skb;
386d48523cbSMartin Habets 			buffer->flags = EFX_TX_BUF_SKB | dma_flags;
387d48523cbSMartin Habets 			return 0;
388d48523cbSMartin Habets 		}
389d48523cbSMartin Habets 
390d48523cbSMartin Habets 		/* Move on to the next fragment. */
391d48523cbSMartin Habets 		fragment = &skb_shinfo(skb)->frags[frag_index++];
392d48523cbSMartin Habets 		len = skb_frag_size(fragment);
393d48523cbSMartin Habets 		dma_addr = skb_frag_dma_map(dma_dev, fragment, 0, len,
394d48523cbSMartin Habets 					    DMA_TO_DEVICE);
395d48523cbSMartin Habets 		dma_flags = 0;
396d48523cbSMartin Habets 		unmap_len = len;
397d48523cbSMartin Habets 		unmap_addr = dma_addr;
398d48523cbSMartin Habets 
399d48523cbSMartin Habets 		if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
400d48523cbSMartin Habets 			return -EIO;
401d48523cbSMartin Habets 	} while (1);
402d48523cbSMartin Habets }
403d48523cbSMartin Habets 
efx_siena_tx_max_skb_descs(struct efx_nic * efx)4047f9e4b2aSMartin Habets unsigned int efx_siena_tx_max_skb_descs(struct efx_nic *efx)
405d48523cbSMartin Habets {
406d48523cbSMartin Habets 	/* Header and payload descriptor for each output segment, plus
407d48523cbSMartin Habets 	 * one for every input fragment boundary within a segment
408d48523cbSMartin Habets 	 */
409d48523cbSMartin Habets 	unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS;
410d48523cbSMartin Habets 
411d48523cbSMartin Habets 	/* Possibly one more per segment for option descriptors */
412d48523cbSMartin Habets 	if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
413d48523cbSMartin Habets 		max_descs += EFX_TSO_MAX_SEGS;
414d48523cbSMartin Habets 
415d48523cbSMartin Habets 	/* Possibly more for PCIe page boundaries within input fragments */
416d48523cbSMartin Habets 	if (PAGE_SIZE > EFX_PAGE_SIZE)
417d48523cbSMartin Habets 		max_descs += max_t(unsigned int, MAX_SKB_FRAGS,
418d48523cbSMartin Habets 				   DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE));
419d48523cbSMartin Habets 
420d48523cbSMartin Habets 	return max_descs;
421d48523cbSMartin Habets }
422d48523cbSMartin Habets 
423d48523cbSMartin Habets /*
424d48523cbSMartin Habets  * Fallback to software TSO.
425d48523cbSMartin Habets  *
426d48523cbSMartin Habets  * This is used if we are unable to send a GSO packet through hardware TSO.
427d48523cbSMartin Habets  * This should only ever happen due to per-queue restrictions - unsupported
428d48523cbSMartin Habets  * packets should first be filtered by the feature flags.
429d48523cbSMartin Habets  *
430d48523cbSMartin Habets  * Returns 0 on success, error code otherwise.
431d48523cbSMartin Habets  */
efx_siena_tx_tso_fallback(struct efx_tx_queue * tx_queue,struct sk_buff * skb)4327f9e4b2aSMartin Habets int efx_siena_tx_tso_fallback(struct efx_tx_queue *tx_queue,
4337f9e4b2aSMartin Habets 			      struct sk_buff *skb)
434d48523cbSMartin Habets {
435d48523cbSMartin Habets 	struct sk_buff *segments, *next;
436d48523cbSMartin Habets 
437d48523cbSMartin Habets 	segments = skb_gso_segment(skb, 0);
438d48523cbSMartin Habets 	if (IS_ERR(segments))
439d48523cbSMartin Habets 		return PTR_ERR(segments);
440d48523cbSMartin Habets 
441d48523cbSMartin Habets 	dev_consume_skb_any(skb);
442d48523cbSMartin Habets 
443d48523cbSMartin Habets 	skb_list_walk_safe(segments, skb, next) {
444d48523cbSMartin Habets 		skb_mark_not_on_list(skb);
445d48523cbSMartin Habets 		efx_enqueue_skb(tx_queue, skb);
446d48523cbSMartin Habets 	}
447d48523cbSMartin Habets 
448d48523cbSMartin Habets 	return 0;
449d48523cbSMartin Habets }
450