xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/sfc/ef100_tx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /****************************************************************************
3*4882a593Smuzhiyun  * Driver for Solarflare network controllers and boards
4*4882a593Smuzhiyun  * Copyright 2018 Solarflare Communications Inc.
5*4882a593Smuzhiyun  * Copyright 2019-2020 Xilinx Inc.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify it
8*4882a593Smuzhiyun  * under the terms of the GNU General Public License version 2 as published
9*4882a593Smuzhiyun  * by the Free Software Foundation, incorporated herein by reference.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <net/ip6_checksum.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "net_driver.h"
15*4882a593Smuzhiyun #include "tx_common.h"
16*4882a593Smuzhiyun #include "nic_common.h"
17*4882a593Smuzhiyun #include "mcdi_functions.h"
18*4882a593Smuzhiyun #include "ef100_regs.h"
19*4882a593Smuzhiyun #include "io.h"
20*4882a593Smuzhiyun #include "ef100_tx.h"
21*4882a593Smuzhiyun #include "ef100_nic.h"
22*4882a593Smuzhiyun 
ef100_tx_probe(struct efx_tx_queue * tx_queue)23*4882a593Smuzhiyun int ef100_tx_probe(struct efx_tx_queue *tx_queue)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	/* Allocate an extra descriptor for the QMDA status completion entry */
26*4882a593Smuzhiyun 	return efx_nic_alloc_buffer(tx_queue->efx, &tx_queue->txd.buf,
27*4882a593Smuzhiyun 				    (tx_queue->ptr_mask + 2) *
28*4882a593Smuzhiyun 				    sizeof(efx_oword_t),
29*4882a593Smuzhiyun 				    GFP_KERNEL);
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun 
ef100_tx_init(struct efx_tx_queue * tx_queue)32*4882a593Smuzhiyun void ef100_tx_init(struct efx_tx_queue *tx_queue)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 	/* must be the inverse of lookup in efx_get_tx_channel */
35*4882a593Smuzhiyun 	tx_queue->core_txq =
36*4882a593Smuzhiyun 		netdev_get_tx_queue(tx_queue->efx->net_dev,
37*4882a593Smuzhiyun 				    tx_queue->channel->channel -
38*4882a593Smuzhiyun 				    tx_queue->efx->tx_channel_offset);
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	/* This value is purely documentational; as EF100 never passes through
41*4882a593Smuzhiyun 	 * the switch statement in tx.c:__efx_enqueue_skb(), that switch does
42*4882a593Smuzhiyun 	 * not handle case 3.  EF100's TSOv3 descriptors are generated by
43*4882a593Smuzhiyun 	 * ef100_make_tso_desc().
44*4882a593Smuzhiyun 	 * Meanwhile, all efx_mcdi_tx_init() cares about is that it's not 2.
45*4882a593Smuzhiyun 	 */
46*4882a593Smuzhiyun 	tx_queue->tso_version = 3;
47*4882a593Smuzhiyun 	if (efx_mcdi_tx_init(tx_queue))
48*4882a593Smuzhiyun 		netdev_WARN(tx_queue->efx->net_dev,
49*4882a593Smuzhiyun 			    "failed to initialise TXQ %d\n", tx_queue->queue);
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
ef100_tx_can_tso(struct efx_tx_queue * tx_queue,struct sk_buff * skb)52*4882a593Smuzhiyun static bool ef100_tx_can_tso(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	struct efx_nic *efx = tx_queue->efx;
55*4882a593Smuzhiyun 	struct ef100_nic_data *nic_data;
56*4882a593Smuzhiyun 	struct efx_tx_buffer *buffer;
57*4882a593Smuzhiyun 	struct tcphdr *tcphdr;
58*4882a593Smuzhiyun 	struct iphdr *iphdr;
59*4882a593Smuzhiyun 	size_t header_len;
60*4882a593Smuzhiyun 	u32 mss;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	nic_data = efx->nic_data;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	if (!skb_is_gso_tcp(skb))
65*4882a593Smuzhiyun 		return false;
66*4882a593Smuzhiyun 	if (!(efx->net_dev->features & NETIF_F_TSO))
67*4882a593Smuzhiyun 		return false;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	mss = skb_shinfo(skb)->gso_size;
70*4882a593Smuzhiyun 	if (unlikely(mss < 4)) {
71*4882a593Smuzhiyun 		WARN_ONCE(1, "MSS of %u is too small for TSO\n", mss);
72*4882a593Smuzhiyun 		return false;
73*4882a593Smuzhiyun 	}
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	header_len = efx_tx_tso_header_length(skb);
76*4882a593Smuzhiyun 	if (header_len > nic_data->tso_max_hdr_len)
77*4882a593Smuzhiyun 		return false;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	if (skb_shinfo(skb)->gso_segs > nic_data->tso_max_payload_num_segs) {
80*4882a593Smuzhiyun 		/* net_dev->gso_max_segs should've caught this */
81*4882a593Smuzhiyun 		WARN_ON_ONCE(1);
82*4882a593Smuzhiyun 		return false;
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	if (skb->data_len / mss > nic_data->tso_max_frames)
86*4882a593Smuzhiyun 		return false;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	/* net_dev->gso_max_size should've caught this */
89*4882a593Smuzhiyun 	if (WARN_ON_ONCE(skb->data_len > nic_data->tso_max_payload_len))
90*4882a593Smuzhiyun 		return false;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/* Reserve an empty buffer for the TSO V3 descriptor.
93*4882a593Smuzhiyun 	 * Convey the length of the header since we already know it.
94*4882a593Smuzhiyun 	 */
95*4882a593Smuzhiyun 	buffer = efx_tx_queue_get_insert_buffer(tx_queue);
96*4882a593Smuzhiyun 	buffer->flags = EFX_TX_BUF_TSO_V3 | EFX_TX_BUF_CONT;
97*4882a593Smuzhiyun 	buffer->len = header_len;
98*4882a593Smuzhiyun 	buffer->unmap_len = 0;
99*4882a593Smuzhiyun 	buffer->skb = skb;
100*4882a593Smuzhiyun 	++tx_queue->insert_count;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* Adjust the TCP checksum to exclude the total length, since we set
103*4882a593Smuzhiyun 	 * ED_INNER_IP_LEN in the descriptor.
104*4882a593Smuzhiyun 	 */
105*4882a593Smuzhiyun 	tcphdr = tcp_hdr(skb);
106*4882a593Smuzhiyun 	if (skb_is_gso_v6(skb)) {
107*4882a593Smuzhiyun 		tcphdr->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
108*4882a593Smuzhiyun 						 &ipv6_hdr(skb)->daddr,
109*4882a593Smuzhiyun 						 0, IPPROTO_TCP, 0);
110*4882a593Smuzhiyun 	} else {
111*4882a593Smuzhiyun 		iphdr = ip_hdr(skb);
112*4882a593Smuzhiyun 		tcphdr->check = ~csum_tcpudp_magic(iphdr->saddr, iphdr->daddr,
113*4882a593Smuzhiyun 						   0, IPPROTO_TCP, 0);
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 	return true;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
ef100_tx_desc(struct efx_tx_queue * tx_queue,unsigned int index)118*4882a593Smuzhiyun static efx_oword_t *ef100_tx_desc(struct efx_tx_queue *tx_queue, unsigned int index)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	if (likely(tx_queue->txd.buf.addr))
121*4882a593Smuzhiyun 		return ((efx_oword_t *)tx_queue->txd.buf.addr) + index;
122*4882a593Smuzhiyun 	else
123*4882a593Smuzhiyun 		return NULL;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
ef100_notify_tx_desc(struct efx_tx_queue * tx_queue)126*4882a593Smuzhiyun static void ef100_notify_tx_desc(struct efx_tx_queue *tx_queue)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	unsigned int write_ptr;
129*4882a593Smuzhiyun 	efx_dword_t reg;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	tx_queue->xmit_pending = false;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	if (unlikely(tx_queue->notify_count == tx_queue->write_count))
134*4882a593Smuzhiyun 		return;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
137*4882a593Smuzhiyun 	/* The write pointer goes into the high word */
138*4882a593Smuzhiyun 	EFX_POPULATE_DWORD_1(reg, ERF_GZ_TX_RING_PIDX, write_ptr);
139*4882a593Smuzhiyun 	efx_writed_page(tx_queue->efx, &reg,
140*4882a593Smuzhiyun 			ER_GZ_TX_RING_DOORBELL, tx_queue->queue);
141*4882a593Smuzhiyun 	tx_queue->notify_count = tx_queue->write_count;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
ef100_tx_push_buffers(struct efx_tx_queue * tx_queue)144*4882a593Smuzhiyun static void ef100_tx_push_buffers(struct efx_tx_queue *tx_queue)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	ef100_notify_tx_desc(tx_queue);
147*4882a593Smuzhiyun 	++tx_queue->pushes;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
ef100_set_tx_csum_partial(const struct sk_buff * skb,struct efx_tx_buffer * buffer,efx_oword_t * txd)150*4882a593Smuzhiyun static void ef100_set_tx_csum_partial(const struct sk_buff *skb,
151*4882a593Smuzhiyun 				      struct efx_tx_buffer *buffer, efx_oword_t *txd)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	efx_oword_t csum;
154*4882a593Smuzhiyun 	int csum_start;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	if (!skb || skb->ip_summed != CHECKSUM_PARTIAL)
157*4882a593Smuzhiyun 		return;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* skb->csum_start has the offset from head, but we need the offset
160*4882a593Smuzhiyun 	 * from data.
161*4882a593Smuzhiyun 	 */
162*4882a593Smuzhiyun 	csum_start = skb_checksum_start_offset(skb);
163*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_3(csum,
164*4882a593Smuzhiyun 			     ESF_GZ_TX_SEND_CSO_PARTIAL_EN, 1,
165*4882a593Smuzhiyun 			     ESF_GZ_TX_SEND_CSO_PARTIAL_START_W,
166*4882a593Smuzhiyun 			     csum_start >> 1,
167*4882a593Smuzhiyun 			     ESF_GZ_TX_SEND_CSO_PARTIAL_CSUM_W,
168*4882a593Smuzhiyun 			     skb->csum_offset >> 1);
169*4882a593Smuzhiyun 	EFX_OR_OWORD(*txd, *txd, csum);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
ef100_set_tx_hw_vlan(const struct sk_buff * skb,efx_oword_t * txd)172*4882a593Smuzhiyun static void ef100_set_tx_hw_vlan(const struct sk_buff *skb, efx_oword_t *txd)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	u16 vlan_tci = skb_vlan_tag_get(skb);
175*4882a593Smuzhiyun 	efx_oword_t vlan;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_2(vlan,
178*4882a593Smuzhiyun 			     ESF_GZ_TX_SEND_VLAN_INSERT_EN, 1,
179*4882a593Smuzhiyun 			     ESF_GZ_TX_SEND_VLAN_INSERT_TCI, vlan_tci);
180*4882a593Smuzhiyun 	EFX_OR_OWORD(*txd, *txd, vlan);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
ef100_make_send_desc(struct efx_nic * efx,const struct sk_buff * skb,struct efx_tx_buffer * buffer,efx_oword_t * txd,unsigned int segment_count)183*4882a593Smuzhiyun static void ef100_make_send_desc(struct efx_nic *efx,
184*4882a593Smuzhiyun 				 const struct sk_buff *skb,
185*4882a593Smuzhiyun 				 struct efx_tx_buffer *buffer, efx_oword_t *txd,
186*4882a593Smuzhiyun 				 unsigned int segment_count)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	/* TX send descriptor */
189*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_3(*txd,
190*4882a593Smuzhiyun 			     ESF_GZ_TX_SEND_NUM_SEGS, segment_count,
191*4882a593Smuzhiyun 			     ESF_GZ_TX_SEND_LEN, buffer->len,
192*4882a593Smuzhiyun 			     ESF_GZ_TX_SEND_ADDR, buffer->dma_addr);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	if (likely(efx->net_dev->features & NETIF_F_HW_CSUM))
195*4882a593Smuzhiyun 		ef100_set_tx_csum_partial(skb, buffer, txd);
196*4882a593Smuzhiyun 	if (efx->net_dev->features & NETIF_F_HW_VLAN_CTAG_TX &&
197*4882a593Smuzhiyun 	    skb && skb_vlan_tag_present(skb))
198*4882a593Smuzhiyun 		ef100_set_tx_hw_vlan(skb, txd);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
ef100_make_tso_desc(struct efx_nic * efx,const struct sk_buff * skb,struct efx_tx_buffer * buffer,efx_oword_t * txd,unsigned int segment_count)201*4882a593Smuzhiyun static void ef100_make_tso_desc(struct efx_nic *efx,
202*4882a593Smuzhiyun 				const struct sk_buff *skb,
203*4882a593Smuzhiyun 				struct efx_tx_buffer *buffer, efx_oword_t *txd,
204*4882a593Smuzhiyun 				unsigned int segment_count)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	u32 mangleid = (efx->net_dev->features & NETIF_F_TSO_MANGLEID) ||
207*4882a593Smuzhiyun 		skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID ?
208*4882a593Smuzhiyun 		ESE_GZ_TX_DESC_IP4_ID_NO_OP :
209*4882a593Smuzhiyun 		ESE_GZ_TX_DESC_IP4_ID_INC_MOD16;
210*4882a593Smuzhiyun 	u16 vlan_enable =  efx->net_dev->features & NETIF_F_HW_VLAN_CTAG_TX ?
211*4882a593Smuzhiyun 		skb_vlan_tag_present(skb) : 0;
212*4882a593Smuzhiyun 	unsigned int len, ip_offset, tcp_offset, payload_segs;
213*4882a593Smuzhiyun 	u16 vlan_tci = skb_vlan_tag_get(skb);
214*4882a593Smuzhiyun 	u32 mss = skb_shinfo(skb)->gso_size;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	len = skb->len - buffer->len;
217*4882a593Smuzhiyun 	/* We use 1 for the TSO descriptor and 1 for the header */
218*4882a593Smuzhiyun 	payload_segs = segment_count - 2;
219*4882a593Smuzhiyun 	ip_offset =  skb_network_offset(skb);
220*4882a593Smuzhiyun 	tcp_offset = skb_transport_offset(skb);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_13(*txd,
223*4882a593Smuzhiyun 			      ESF_GZ_TX_DESC_TYPE, ESE_GZ_TX_DESC_TYPE_TSO,
224*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_MSS, mss,
225*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_HDR_NUM_SEGS, 1,
226*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_PAYLOAD_NUM_SEGS, payload_segs,
227*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_HDR_LEN_W, buffer->len >> 1,
228*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_PAYLOAD_LEN, len,
229*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_CSO_INNER_L4, 1,
230*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_INNER_L3_OFF_W, ip_offset >> 1,
231*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_INNER_L4_OFF_W, tcp_offset >> 1,
232*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_ED_INNER_IP4_ID, mangleid,
233*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_ED_INNER_IP_LEN, 1,
234*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_VLAN_INSERT_EN, vlan_enable,
235*4882a593Smuzhiyun 			      ESF_GZ_TX_TSO_VLAN_INSERT_TCI, vlan_tci
236*4882a593Smuzhiyun 		);
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
ef100_tx_make_descriptors(struct efx_tx_queue * tx_queue,const struct sk_buff * skb,unsigned int segment_count)239*4882a593Smuzhiyun static void ef100_tx_make_descriptors(struct efx_tx_queue *tx_queue,
240*4882a593Smuzhiyun 				      const struct sk_buff *skb,
241*4882a593Smuzhiyun 				      unsigned int segment_count)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	unsigned int old_write_count = tx_queue->write_count;
244*4882a593Smuzhiyun 	unsigned int new_write_count = old_write_count;
245*4882a593Smuzhiyun 	struct efx_tx_buffer *buffer;
246*4882a593Smuzhiyun 	unsigned int next_desc_type;
247*4882a593Smuzhiyun 	unsigned int write_ptr;
248*4882a593Smuzhiyun 	efx_oword_t *txd;
249*4882a593Smuzhiyun 	unsigned int nr_descs = tx_queue->insert_count - old_write_count;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	if (unlikely(nr_descs == 0))
252*4882a593Smuzhiyun 		return;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	if (segment_count)
255*4882a593Smuzhiyun 		next_desc_type = ESE_GZ_TX_DESC_TYPE_TSO;
256*4882a593Smuzhiyun 	else
257*4882a593Smuzhiyun 		next_desc_type = ESE_GZ_TX_DESC_TYPE_SEND;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	/* if it's a raw write (such as XDP) then always SEND single frames */
260*4882a593Smuzhiyun 	if (!skb)
261*4882a593Smuzhiyun 		nr_descs = 1;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	do {
264*4882a593Smuzhiyun 		write_ptr = new_write_count & tx_queue->ptr_mask;
265*4882a593Smuzhiyun 		buffer = &tx_queue->buffer[write_ptr];
266*4882a593Smuzhiyun 		txd = ef100_tx_desc(tx_queue, write_ptr);
267*4882a593Smuzhiyun 		++new_write_count;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 		/* Create TX descriptor ring entry */
270*4882a593Smuzhiyun 		tx_queue->packet_write_count = new_write_count;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 		switch (next_desc_type) {
273*4882a593Smuzhiyun 		case ESE_GZ_TX_DESC_TYPE_SEND:
274*4882a593Smuzhiyun 			ef100_make_send_desc(tx_queue->efx, skb,
275*4882a593Smuzhiyun 					     buffer, txd, nr_descs);
276*4882a593Smuzhiyun 			break;
277*4882a593Smuzhiyun 		case ESE_GZ_TX_DESC_TYPE_TSO:
278*4882a593Smuzhiyun 			/* TX TSO descriptor */
279*4882a593Smuzhiyun 			WARN_ON_ONCE(!(buffer->flags & EFX_TX_BUF_TSO_V3));
280*4882a593Smuzhiyun 			ef100_make_tso_desc(tx_queue->efx, skb,
281*4882a593Smuzhiyun 					    buffer, txd, nr_descs);
282*4882a593Smuzhiyun 			break;
283*4882a593Smuzhiyun 		default:
284*4882a593Smuzhiyun 			/* TX segment descriptor */
285*4882a593Smuzhiyun 			EFX_POPULATE_OWORD_3(*txd,
286*4882a593Smuzhiyun 					     ESF_GZ_TX_DESC_TYPE, ESE_GZ_TX_DESC_TYPE_SEG,
287*4882a593Smuzhiyun 					     ESF_GZ_TX_SEG_LEN, buffer->len,
288*4882a593Smuzhiyun 					     ESF_GZ_TX_SEG_ADDR, buffer->dma_addr);
289*4882a593Smuzhiyun 		}
290*4882a593Smuzhiyun 		/* if it's a raw write (such as XDP) then always SEND */
291*4882a593Smuzhiyun 		next_desc_type = skb ? ESE_GZ_TX_DESC_TYPE_SEG :
292*4882a593Smuzhiyun 				       ESE_GZ_TX_DESC_TYPE_SEND;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	} while (new_write_count != tx_queue->insert_count);
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	wmb(); /* Ensure descriptors are written before they are fetched */
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	tx_queue->write_count = new_write_count;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/* The write_count above must be updated before reading
301*4882a593Smuzhiyun 	 * channel->holdoff_doorbell to avoid a race with the
302*4882a593Smuzhiyun 	 * completion path, so ensure these operations are not
303*4882a593Smuzhiyun 	 * re-ordered.  This also flushes the update of write_count
304*4882a593Smuzhiyun 	 * back into the cache.
305*4882a593Smuzhiyun 	 */
306*4882a593Smuzhiyun 	smp_mb();
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
ef100_tx_write(struct efx_tx_queue * tx_queue)309*4882a593Smuzhiyun void ef100_tx_write(struct efx_tx_queue *tx_queue)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	ef100_tx_make_descriptors(tx_queue, NULL, 0);
312*4882a593Smuzhiyun 	ef100_tx_push_buffers(tx_queue);
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
ef100_ev_tx(struct efx_channel * channel,const efx_qword_t * p_event)315*4882a593Smuzhiyun void ef100_ev_tx(struct efx_channel *channel, const efx_qword_t *p_event)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	unsigned int tx_done =
318*4882a593Smuzhiyun 		EFX_QWORD_FIELD(*p_event, ESF_GZ_EV_TXCMPL_NUM_DESC);
319*4882a593Smuzhiyun 	unsigned int qlabel =
320*4882a593Smuzhiyun 		EFX_QWORD_FIELD(*p_event, ESF_GZ_EV_TXCMPL_Q_LABEL);
321*4882a593Smuzhiyun 	struct efx_tx_queue *tx_queue =
322*4882a593Smuzhiyun 		efx_channel_get_tx_queue(channel, qlabel);
323*4882a593Smuzhiyun 	unsigned int tx_index = (tx_queue->read_count + tx_done - 1) &
324*4882a593Smuzhiyun 				tx_queue->ptr_mask;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	efx_xmit_done(tx_queue, tx_index);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun /* Add a socket buffer to a TX queue
330*4882a593Smuzhiyun  *
331*4882a593Smuzhiyun  * You must hold netif_tx_lock() to call this function.
332*4882a593Smuzhiyun  *
333*4882a593Smuzhiyun  * Returns 0 on success, error code otherwise. In case of an error this
334*4882a593Smuzhiyun  * function will free the SKB.
335*4882a593Smuzhiyun  */
ef100_enqueue_skb(struct efx_tx_queue * tx_queue,struct sk_buff * skb)336*4882a593Smuzhiyun int ef100_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun 	unsigned int old_insert_count = tx_queue->insert_count;
339*4882a593Smuzhiyun 	struct efx_nic *efx = tx_queue->efx;
340*4882a593Smuzhiyun 	bool xmit_more = netdev_xmit_more();
341*4882a593Smuzhiyun 	unsigned int fill_level;
342*4882a593Smuzhiyun 	unsigned int segments;
343*4882a593Smuzhiyun 	int rc;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	if (!tx_queue->buffer || !tx_queue->ptr_mask) {
346*4882a593Smuzhiyun 		netif_stop_queue(efx->net_dev);
347*4882a593Smuzhiyun 		dev_kfree_skb_any(skb);
348*4882a593Smuzhiyun 		return -ENODEV;
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	segments = skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 0;
352*4882a593Smuzhiyun 	if (segments == 1)
353*4882a593Smuzhiyun 		segments = 0;	/* Don't use TSO/GSO for a single segment. */
354*4882a593Smuzhiyun 	if (segments && !ef100_tx_can_tso(tx_queue, skb)) {
355*4882a593Smuzhiyun 		rc = efx_tx_tso_fallback(tx_queue, skb);
356*4882a593Smuzhiyun 		tx_queue->tso_fallbacks++;
357*4882a593Smuzhiyun 		if (rc)
358*4882a593Smuzhiyun 			goto err;
359*4882a593Smuzhiyun 		else
360*4882a593Smuzhiyun 			return 0;
361*4882a593Smuzhiyun 	}
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	/* Map for DMA and create descriptors */
364*4882a593Smuzhiyun 	rc = efx_tx_map_data(tx_queue, skb, segments);
365*4882a593Smuzhiyun 	if (rc)
366*4882a593Smuzhiyun 		goto err;
367*4882a593Smuzhiyun 	ef100_tx_make_descriptors(tx_queue, skb, segments);
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	fill_level = efx_channel_tx_old_fill_level(tx_queue->channel);
370*4882a593Smuzhiyun 	if (fill_level > efx->txq_stop_thresh) {
371*4882a593Smuzhiyun 		struct efx_tx_queue *txq2;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 		netif_tx_stop_queue(tx_queue->core_txq);
374*4882a593Smuzhiyun 		/* Re-read after a memory barrier in case we've raced with
375*4882a593Smuzhiyun 		 * the completion path. Otherwise there's a danger we'll never
376*4882a593Smuzhiyun 		 * restart the queue if all completions have just happened.
377*4882a593Smuzhiyun 		 */
378*4882a593Smuzhiyun 		smp_mb();
379*4882a593Smuzhiyun 		efx_for_each_channel_tx_queue(txq2, tx_queue->channel)
380*4882a593Smuzhiyun 			txq2->old_read_count = READ_ONCE(txq2->read_count);
381*4882a593Smuzhiyun 		fill_level = efx_channel_tx_old_fill_level(tx_queue->channel);
382*4882a593Smuzhiyun 		if (fill_level < efx->txq_stop_thresh)
383*4882a593Smuzhiyun 			netif_tx_start_queue(tx_queue->core_txq);
384*4882a593Smuzhiyun 	}
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	tx_queue->xmit_pending = true;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	/* If xmit_more then we don't need to push the doorbell, unless there
389*4882a593Smuzhiyun 	 * are 256 descriptors already queued in which case we have to push to
390*4882a593Smuzhiyun 	 * ensure we never push more than 256 at once.
391*4882a593Smuzhiyun 	 */
392*4882a593Smuzhiyun 	if (__netdev_tx_sent_queue(tx_queue->core_txq, skb->len, xmit_more) ||
393*4882a593Smuzhiyun 	    tx_queue->write_count - tx_queue->notify_count > 255)
394*4882a593Smuzhiyun 		ef100_tx_push_buffers(tx_queue);
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (segments) {
397*4882a593Smuzhiyun 		tx_queue->tso_bursts++;
398*4882a593Smuzhiyun 		tx_queue->tso_packets += segments;
399*4882a593Smuzhiyun 		tx_queue->tx_packets  += segments;
400*4882a593Smuzhiyun 	} else {
401*4882a593Smuzhiyun 		tx_queue->tx_packets++;
402*4882a593Smuzhiyun 	}
403*4882a593Smuzhiyun 	return 0;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun err:
406*4882a593Smuzhiyun 	efx_enqueue_unwind(tx_queue, old_insert_count);
407*4882a593Smuzhiyun 	if (!IS_ERR_OR_NULL(skb))
408*4882a593Smuzhiyun 		dev_kfree_skb_any(skb);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	/* If we're not expecting another transmit and we had something to push
411*4882a593Smuzhiyun 	 * on this queue then we need to push here to get the previous packets
412*4882a593Smuzhiyun 	 * out.  We only enter this branch from before the xmit_more handling
413*4882a593Smuzhiyun 	 * above, so xmit_pending still refers to the old state.
414*4882a593Smuzhiyun 	 */
415*4882a593Smuzhiyun 	if (tx_queue->xmit_pending && !xmit_more)
416*4882a593Smuzhiyun 		ef100_tx_push_buffers(tx_queue);
417*4882a593Smuzhiyun 	return rc;
418*4882a593Smuzhiyun }
419