xref: /linux/drivers/net/ethernet/chelsio/cxgb4/sge.c (revision 9a6b55ac)
1 /*
2  * This file is part of the Chelsio T4 Ethernet driver for Linux.
3  *
4  * Copyright (c) 2003-2014 Chelsio Communications, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #include <linux/skbuff.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/if_vlan.h>
39 #include <linux/ip.h>
40 #include <linux/dma-mapping.h>
41 #include <linux/jiffies.h>
42 #include <linux/prefetch.h>
43 #include <linux/export.h>
44 #include <net/xfrm.h>
45 #include <net/ipv6.h>
46 #include <net/tcp.h>
47 #include <net/busy_poll.h>
48 #ifdef CONFIG_CHELSIO_T4_FCOE
49 #include <scsi/fc/fc_fcoe.h>
50 #endif /* CONFIG_CHELSIO_T4_FCOE */
51 #include "cxgb4.h"
52 #include "t4_regs.h"
53 #include "t4_values.h"
54 #include "t4_msg.h"
55 #include "t4fw_api.h"
56 #include "cxgb4_ptp.h"
57 #include "cxgb4_uld.h"
58 #include "cxgb4_tc_mqprio.h"
59 #include "sched.h"
60 
61 /*
62  * Rx buffer size.  We use largish buffers if possible but settle for single
63  * pages under memory shortage.
64  */
65 #if PAGE_SHIFT >= 16
66 # define FL_PG_ORDER 0
67 #else
68 # define FL_PG_ORDER (16 - PAGE_SHIFT)
69 #endif
70 
71 /* RX_PULL_LEN should be <= RX_COPY_THRES */
72 #define RX_COPY_THRES    256
73 #define RX_PULL_LEN      128
74 
75 /*
76  * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
77  * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
78  */
79 #define RX_PKT_SKB_LEN   512
80 
81 /*
82  * Max number of Tx descriptors we clean up at a time.  Should be modest as
83  * freeing skbs isn't cheap and it happens while holding locks.  We just need
84  * to free packets faster than they arrive, we eventually catch up and keep
85  * the amortized cost reasonable.  Must be >= 2 * TXQ_STOP_THRES.  It should
86  * also match the CIDX Flush Threshold.
87  */
88 #define MAX_TX_RECLAIM 32
89 
90 /*
91  * Max number of Rx buffers we replenish at a time.  Again keep this modest,
92  * allocating buffers isn't cheap either.
93  */
94 #define MAX_RX_REFILL 16U
95 
96 /*
97  * Period of the Rx queue check timer.  This timer is infrequent as it has
98  * something to do only when the system experiences severe memory shortage.
99  */
100 #define RX_QCHECK_PERIOD (HZ / 2)
101 
102 /*
103  * Period of the Tx queue check timer.
104  */
105 #define TX_QCHECK_PERIOD (HZ / 2)
106 
107 /*
108  * Max number of Tx descriptors to be reclaimed by the Tx timer.
109  */
110 #define MAX_TIMER_TX_RECLAIM 100
111 
112 /*
113  * Timer index used when backing off due to memory shortage.
114  */
115 #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
116 
117 /*
118  * Suspension threshold for non-Ethernet Tx queues.  We require enough room
119  * for a full sized WR.
120  */
121 #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
122 
123 /*
124  * Max Tx descriptor space we allow for an Ethernet packet to be inlined
125  * into a WR.
126  */
127 #define MAX_IMM_TX_PKT_LEN 256
128 
129 /*
130  * Max size of a WR sent through a control Tx queue.
131  */
132 #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
133 
134 struct rx_sw_desc {                /* SW state per Rx descriptor */
135 	struct page *page;
136 	dma_addr_t dma_addr;
137 };
138 
139 /*
140  * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
141  * buffer).  We currently only support two sizes for 1500- and 9000-byte MTUs.
142  * We could easily support more but there doesn't seem to be much need for
143  * that ...
144  */
145 #define FL_MTU_SMALL 1500
146 #define FL_MTU_LARGE 9000
147 
148 static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
149 					  unsigned int mtu)
150 {
151 	struct sge *s = &adapter->sge;
152 
153 	return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
154 }
155 
156 #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
157 #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
158 
159 /*
160  * Bits 0..3 of rx_sw_desc.dma_addr have special meaning.  The hardware uses
161  * these to specify the buffer size as an index into the SGE Free List Buffer
162  * Size register array.  We also use bit 4, when the buffer has been unmapped
163  * for DMA, but this is of course never sent to the hardware and is only used
164  * to prevent double unmappings.  All of the above requires that the Free List
165  * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
166  * 32-byte or or a power of 2 greater in alignment.  Since the SGE's minimal
167  * Free List Buffer alignment is 32 bytes, this works out for us ...
168  */
169 enum {
170 	RX_BUF_FLAGS     = 0x1f,   /* bottom five bits are special */
171 	RX_BUF_SIZE      = 0x0f,   /* bottom three bits are for buf sizes */
172 	RX_UNMAPPED_BUF  = 0x10,   /* buffer is not mapped */
173 
174 	/*
175 	 * XXX We shouldn't depend on being able to use these indices.
176 	 * XXX Especially when some other Master PF has initialized the
177 	 * XXX adapter or we use the Firmware Configuration File.  We
178 	 * XXX should really search through the Host Buffer Size register
179 	 * XXX array for the appropriately sized buffer indices.
180 	 */
181 	RX_SMALL_PG_BUF  = 0x0,   /* small (PAGE_SIZE) page buffer */
182 	RX_LARGE_PG_BUF  = 0x1,   /* buffer large (FL_PG_ORDER) page buffer */
183 
184 	RX_SMALL_MTU_BUF = 0x2,   /* small MTU buffer */
185 	RX_LARGE_MTU_BUF = 0x3,   /* large MTU buffer */
186 };
187 
188 static int timer_pkt_quota[] = {1, 1, 2, 3, 4, 5};
189 #define MIN_NAPI_WORK  1
190 
191 static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
192 {
193 	return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
194 }
195 
196 static inline bool is_buf_mapped(const struct rx_sw_desc *d)
197 {
198 	return !(d->dma_addr & RX_UNMAPPED_BUF);
199 }
200 
201 /**
202  *	txq_avail - return the number of available slots in a Tx queue
203  *	@q: the Tx queue
204  *
205  *	Returns the number of descriptors in a Tx queue available to write new
206  *	packets.
207  */
208 static inline unsigned int txq_avail(const struct sge_txq *q)
209 {
210 	return q->size - 1 - q->in_use;
211 }
212 
213 /**
214  *	fl_cap - return the capacity of a free-buffer list
215  *	@fl: the FL
216  *
217  *	Returns the capacity of a free-buffer list.  The capacity is less than
218  *	the size because one descriptor needs to be left unpopulated, otherwise
219  *	HW will think the FL is empty.
220  */
221 static inline unsigned int fl_cap(const struct sge_fl *fl)
222 {
223 	return fl->size - 8;   /* 1 descriptor = 8 buffers */
224 }
225 
226 /**
227  *	fl_starving - return whether a Free List is starving.
228  *	@adapter: pointer to the adapter
229  *	@fl: the Free List
230  *
231  *	Tests specified Free List to see whether the number of buffers
232  *	available to the hardware has falled below our "starvation"
233  *	threshold.
234  */
235 static inline bool fl_starving(const struct adapter *adapter,
236 			       const struct sge_fl *fl)
237 {
238 	const struct sge *s = &adapter->sge;
239 
240 	return fl->avail - fl->pend_cred <= s->fl_starve_thres;
241 }
242 
243 int cxgb4_map_skb(struct device *dev, const struct sk_buff *skb,
244 		  dma_addr_t *addr)
245 {
246 	const skb_frag_t *fp, *end;
247 	const struct skb_shared_info *si;
248 
249 	*addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
250 	if (dma_mapping_error(dev, *addr))
251 		goto out_err;
252 
253 	si = skb_shinfo(skb);
254 	end = &si->frags[si->nr_frags];
255 
256 	for (fp = si->frags; fp < end; fp++) {
257 		*++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
258 					   DMA_TO_DEVICE);
259 		if (dma_mapping_error(dev, *addr))
260 			goto unwind;
261 	}
262 	return 0;
263 
264 unwind:
265 	while (fp-- > si->frags)
266 		dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
267 
268 	dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
269 out_err:
270 	return -ENOMEM;
271 }
272 EXPORT_SYMBOL(cxgb4_map_skb);
273 
274 static void unmap_skb(struct device *dev, const struct sk_buff *skb,
275 		      const dma_addr_t *addr)
276 {
277 	const skb_frag_t *fp, *end;
278 	const struct skb_shared_info *si;
279 
280 	dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
281 
282 	si = skb_shinfo(skb);
283 	end = &si->frags[si->nr_frags];
284 	for (fp = si->frags; fp < end; fp++)
285 		dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
286 }
287 
288 #ifdef CONFIG_NEED_DMA_MAP_STATE
289 /**
290  *	deferred_unmap_destructor - unmap a packet when it is freed
291  *	@skb: the packet
292  *
293  *	This is the packet destructor used for Tx packets that need to remain
294  *	mapped until they are freed rather than until their Tx descriptors are
295  *	freed.
296  */
297 static void deferred_unmap_destructor(struct sk_buff *skb)
298 {
299 	unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
300 }
301 #endif
302 
303 /**
304  *	free_tx_desc - reclaims Tx descriptors and their buffers
305  *	@adapter: the adapter
306  *	@q: the Tx queue to reclaim descriptors from
307  *	@n: the number of descriptors to reclaim
308  *	@unmap: whether the buffers should be unmapped for DMA
309  *
310  *	Reclaims Tx descriptors from an SGE Tx queue and frees the associated
311  *	Tx buffers.  Called with the Tx queue lock held.
312  */
313 void free_tx_desc(struct adapter *adap, struct sge_txq *q,
314 		  unsigned int n, bool unmap)
315 {
316 	unsigned int cidx = q->cidx;
317 	struct tx_sw_desc *d;
318 
319 	d = &q->sdesc[cidx];
320 	while (n--) {
321 		if (d->skb) {                       /* an SGL is present */
322 			if (unmap && d->addr[0]) {
323 				unmap_skb(adap->pdev_dev, d->skb, d->addr);
324 				memset(d->addr, 0, sizeof(d->addr));
325 			}
326 			dev_consume_skb_any(d->skb);
327 			d->skb = NULL;
328 		}
329 		++d;
330 		if (++cidx == q->size) {
331 			cidx = 0;
332 			d = q->sdesc;
333 		}
334 	}
335 	q->cidx = cidx;
336 }
337 
338 /*
339  * Return the number of reclaimable descriptors in a Tx queue.
340  */
341 static inline int reclaimable(const struct sge_txq *q)
342 {
343 	int hw_cidx = ntohs(READ_ONCE(q->stat->cidx));
344 	hw_cidx -= q->cidx;
345 	return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
346 }
347 
348 /**
349  *	reclaim_completed_tx - reclaims completed TX Descriptors
350  *	@adap: the adapter
351  *	@q: the Tx queue to reclaim completed descriptors from
352  *	@maxreclaim: the maximum number of TX Descriptors to reclaim or -1
353  *	@unmap: whether the buffers should be unmapped for DMA
354  *
355  *	Reclaims Tx Descriptors that the SGE has indicated it has processed,
356  *	and frees the associated buffers if possible.  If @max == -1, then
357  *	we'll use a defaiult maximum.  Called with the TX Queue locked.
358  */
359 static inline int reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
360 				       int maxreclaim, bool unmap)
361 {
362 	int reclaim = reclaimable(q);
363 
364 	if (reclaim) {
365 		/*
366 		 * Limit the amount of clean up work we do at a time to keep
367 		 * the Tx lock hold time O(1).
368 		 */
369 		if (maxreclaim < 0)
370 			maxreclaim = MAX_TX_RECLAIM;
371 		if (reclaim > maxreclaim)
372 			reclaim = maxreclaim;
373 
374 		free_tx_desc(adap, q, reclaim, unmap);
375 		q->in_use -= reclaim;
376 	}
377 
378 	return reclaim;
379 }
380 
381 /**
382  *	cxgb4_reclaim_completed_tx - reclaims completed Tx descriptors
383  *	@adap: the adapter
384  *	@q: the Tx queue to reclaim completed descriptors from
385  *	@unmap: whether the buffers should be unmapped for DMA
386  *
387  *	Reclaims Tx descriptors that the SGE has indicated it has processed,
388  *	and frees the associated buffers if possible.  Called with the Tx
389  *	queue locked.
390  */
391 void cxgb4_reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
392 				bool unmap)
393 {
394 	(void)reclaim_completed_tx(adap, q, -1, unmap);
395 }
396 EXPORT_SYMBOL(cxgb4_reclaim_completed_tx);
397 
398 static inline int get_buf_size(struct adapter *adapter,
399 			       const struct rx_sw_desc *d)
400 {
401 	struct sge *s = &adapter->sge;
402 	unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
403 	int buf_size;
404 
405 	switch (rx_buf_size_idx) {
406 	case RX_SMALL_PG_BUF:
407 		buf_size = PAGE_SIZE;
408 		break;
409 
410 	case RX_LARGE_PG_BUF:
411 		buf_size = PAGE_SIZE << s->fl_pg_order;
412 		break;
413 
414 	case RX_SMALL_MTU_BUF:
415 		buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
416 		break;
417 
418 	case RX_LARGE_MTU_BUF:
419 		buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
420 		break;
421 
422 	default:
423 		BUG();
424 	}
425 
426 	return buf_size;
427 }
428 
429 /**
430  *	free_rx_bufs - free the Rx buffers on an SGE free list
431  *	@adap: the adapter
432  *	@q: the SGE free list to free buffers from
433  *	@n: how many buffers to free
434  *
435  *	Release the next @n buffers on an SGE free-buffer Rx queue.   The
436  *	buffers must be made inaccessible to HW before calling this function.
437  */
438 static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
439 {
440 	while (n--) {
441 		struct rx_sw_desc *d = &q->sdesc[q->cidx];
442 
443 		if (is_buf_mapped(d))
444 			dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
445 				       get_buf_size(adap, d),
446 				       PCI_DMA_FROMDEVICE);
447 		put_page(d->page);
448 		d->page = NULL;
449 		if (++q->cidx == q->size)
450 			q->cidx = 0;
451 		q->avail--;
452 	}
453 }
454 
455 /**
456  *	unmap_rx_buf - unmap the current Rx buffer on an SGE free list
457  *	@adap: the adapter
458  *	@q: the SGE free list
459  *
460  *	Unmap the current buffer on an SGE free-buffer Rx queue.   The
461  *	buffer must be made inaccessible to HW before calling this function.
462  *
463  *	This is similar to @free_rx_bufs above but does not free the buffer.
464  *	Do note that the FL still loses any further access to the buffer.
465  */
466 static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
467 {
468 	struct rx_sw_desc *d = &q->sdesc[q->cidx];
469 
470 	if (is_buf_mapped(d))
471 		dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
472 			       get_buf_size(adap, d), PCI_DMA_FROMDEVICE);
473 	d->page = NULL;
474 	if (++q->cidx == q->size)
475 		q->cidx = 0;
476 	q->avail--;
477 }
478 
479 static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
480 {
481 	if (q->pend_cred >= 8) {
482 		u32 val = adap->params.arch.sge_fl_db;
483 
484 		if (is_t4(adap->params.chip))
485 			val |= PIDX_V(q->pend_cred / 8);
486 		else
487 			val |= PIDX_T5_V(q->pend_cred / 8);
488 
489 		/* Make sure all memory writes to the Free List queue are
490 		 * committed before we tell the hardware about them.
491 		 */
492 		wmb();
493 
494 		/* If we don't have access to the new User Doorbell (T5+), use
495 		 * the old doorbell mechanism; otherwise use the new BAR2
496 		 * mechanism.
497 		 */
498 		if (unlikely(q->bar2_addr == NULL)) {
499 			t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
500 				     val | QID_V(q->cntxt_id));
501 		} else {
502 			writel(val | QID_V(q->bar2_qid),
503 			       q->bar2_addr + SGE_UDB_KDOORBELL);
504 
505 			/* This Write memory Barrier will force the write to
506 			 * the User Doorbell area to be flushed.
507 			 */
508 			wmb();
509 		}
510 		q->pend_cred &= 7;
511 	}
512 }
513 
514 static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
515 				  dma_addr_t mapping)
516 {
517 	sd->page = pg;
518 	sd->dma_addr = mapping;      /* includes size low bits */
519 }
520 
521 /**
522  *	refill_fl - refill an SGE Rx buffer ring
523  *	@adap: the adapter
524  *	@q: the ring to refill
525  *	@n: the number of new buffers to allocate
526  *	@gfp: the gfp flags for the allocations
527  *
528  *	(Re)populate an SGE free-buffer queue with up to @n new packet buffers,
529  *	allocated with the supplied gfp flags.  The caller must assure that
530  *	@n does not exceed the queue's capacity.  If afterwards the queue is
531  *	found critically low mark it as starving in the bitmap of starving FLs.
532  *
533  *	Returns the number of buffers allocated.
534  */
535 static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
536 			      gfp_t gfp)
537 {
538 	struct sge *s = &adap->sge;
539 	struct page *pg;
540 	dma_addr_t mapping;
541 	unsigned int cred = q->avail;
542 	__be64 *d = &q->desc[q->pidx];
543 	struct rx_sw_desc *sd = &q->sdesc[q->pidx];
544 	int node;
545 
546 #ifdef CONFIG_DEBUG_FS
547 	if (test_bit(q->cntxt_id - adap->sge.egr_start, adap->sge.blocked_fl))
548 		goto out;
549 #endif
550 
551 	gfp |= __GFP_NOWARN;
552 	node = dev_to_node(adap->pdev_dev);
553 
554 	if (s->fl_pg_order == 0)
555 		goto alloc_small_pages;
556 
557 	/*
558 	 * Prefer large buffers
559 	 */
560 	while (n) {
561 		pg = alloc_pages_node(node, gfp | __GFP_COMP, s->fl_pg_order);
562 		if (unlikely(!pg)) {
563 			q->large_alloc_failed++;
564 			break;       /* fall back to single pages */
565 		}
566 
567 		mapping = dma_map_page(adap->pdev_dev, pg, 0,
568 				       PAGE_SIZE << s->fl_pg_order,
569 				       PCI_DMA_FROMDEVICE);
570 		if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
571 			__free_pages(pg, s->fl_pg_order);
572 			q->mapping_err++;
573 			goto out;   /* do not try small pages for this error */
574 		}
575 		mapping |= RX_LARGE_PG_BUF;
576 		*d++ = cpu_to_be64(mapping);
577 
578 		set_rx_sw_desc(sd, pg, mapping);
579 		sd++;
580 
581 		q->avail++;
582 		if (++q->pidx == q->size) {
583 			q->pidx = 0;
584 			sd = q->sdesc;
585 			d = q->desc;
586 		}
587 		n--;
588 	}
589 
590 alloc_small_pages:
591 	while (n--) {
592 		pg = alloc_pages_node(node, gfp, 0);
593 		if (unlikely(!pg)) {
594 			q->alloc_failed++;
595 			break;
596 		}
597 
598 		mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
599 				       PCI_DMA_FROMDEVICE);
600 		if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
601 			put_page(pg);
602 			q->mapping_err++;
603 			goto out;
604 		}
605 		*d++ = cpu_to_be64(mapping);
606 
607 		set_rx_sw_desc(sd, pg, mapping);
608 		sd++;
609 
610 		q->avail++;
611 		if (++q->pidx == q->size) {
612 			q->pidx = 0;
613 			sd = q->sdesc;
614 			d = q->desc;
615 		}
616 	}
617 
618 out:	cred = q->avail - cred;
619 	q->pend_cred += cred;
620 	ring_fl_db(adap, q);
621 
622 	if (unlikely(fl_starving(adap, q))) {
623 		smp_wmb();
624 		q->low++;
625 		set_bit(q->cntxt_id - adap->sge.egr_start,
626 			adap->sge.starving_fl);
627 	}
628 
629 	return cred;
630 }
631 
632 static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
633 {
634 	refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
635 		  GFP_ATOMIC);
636 }
637 
638 /**
639  *	alloc_ring - allocate resources for an SGE descriptor ring
640  *	@dev: the PCI device's core device
641  *	@nelem: the number of descriptors
642  *	@elem_size: the size of each descriptor
643  *	@sw_size: the size of the SW state associated with each ring element
644  *	@phys: the physical address of the allocated ring
645  *	@metadata: address of the array holding the SW state for the ring
646  *	@stat_size: extra space in HW ring for status information
647  *	@node: preferred node for memory allocations
648  *
649  *	Allocates resources for an SGE descriptor ring, such as Tx queues,
650  *	free buffer lists, or response queues.  Each SGE ring requires
651  *	space for its HW descriptors plus, optionally, space for the SW state
652  *	associated with each HW entry (the metadata).  The function returns
653  *	three values: the virtual address for the HW ring (the return value
654  *	of the function), the bus address of the HW ring, and the address
655  *	of the SW ring.
656  */
657 static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
658 			size_t sw_size, dma_addr_t *phys, void *metadata,
659 			size_t stat_size, int node)
660 {
661 	size_t len = nelem * elem_size + stat_size;
662 	void *s = NULL;
663 	void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
664 
665 	if (!p)
666 		return NULL;
667 	if (sw_size) {
668 		s = kcalloc_node(sw_size, nelem, GFP_KERNEL, node);
669 
670 		if (!s) {
671 			dma_free_coherent(dev, len, p, *phys);
672 			return NULL;
673 		}
674 	}
675 	if (metadata)
676 		*(void **)metadata = s;
677 	return p;
678 }
679 
680 /**
681  *	sgl_len - calculates the size of an SGL of the given capacity
682  *	@n: the number of SGL entries
683  *
684  *	Calculates the number of flits needed for a scatter/gather list that
685  *	can hold the given number of entries.
686  */
687 static inline unsigned int sgl_len(unsigned int n)
688 {
689 	/* A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
690 	 * addresses.  The DSGL Work Request starts off with a 32-bit DSGL
691 	 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
692 	 * repeated sequences of { Length[i], Length[i+1], Address[i],
693 	 * Address[i+1] } (this ensures that all addresses are on 64-bit
694 	 * boundaries).  If N is even, then Length[N+1] should be set to 0 and
695 	 * Address[N+1] is omitted.
696 	 *
697 	 * The following calculation incorporates all of the above.  It's
698 	 * somewhat hard to follow but, briefly: the "+2" accounts for the
699 	 * first two flits which include the DSGL header, Length0 and
700 	 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
701 	 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
702 	 * finally the "+((n-1)&1)" adds the one remaining flit needed if
703 	 * (n-1) is odd ...
704 	 */
705 	n--;
706 	return (3 * n) / 2 + (n & 1) + 2;
707 }
708 
709 /**
710  *	flits_to_desc - returns the num of Tx descriptors for the given flits
711  *	@n: the number of flits
712  *
713  *	Returns the number of Tx descriptors needed for the supplied number
714  *	of flits.
715  */
716 static inline unsigned int flits_to_desc(unsigned int n)
717 {
718 	BUG_ON(n > SGE_MAX_WR_LEN / 8);
719 	return DIV_ROUND_UP(n, 8);
720 }
721 
722 /**
723  *	is_eth_imm - can an Ethernet packet be sent as immediate data?
724  *	@skb: the packet
725  *
726  *	Returns whether an Ethernet packet is small enough to fit as
727  *	immediate data. Return value corresponds to headroom required.
728  */
729 static inline int is_eth_imm(const struct sk_buff *skb, unsigned int chip_ver)
730 {
731 	int hdrlen = 0;
732 
733 	if (skb->encapsulation && skb_shinfo(skb)->gso_size &&
734 	    chip_ver > CHELSIO_T5) {
735 		hdrlen = sizeof(struct cpl_tx_tnl_lso);
736 		hdrlen += sizeof(struct cpl_tx_pkt_core);
737 	} else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
738 		return 0;
739 	} else {
740 		hdrlen = skb_shinfo(skb)->gso_size ?
741 			 sizeof(struct cpl_tx_pkt_lso_core) : 0;
742 		hdrlen += sizeof(struct cpl_tx_pkt);
743 	}
744 	if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
745 		return hdrlen;
746 	return 0;
747 }
748 
749 /**
750  *	calc_tx_flits - calculate the number of flits for a packet Tx WR
751  *	@skb: the packet
752  *
753  *	Returns the number of flits needed for a Tx WR for the given Ethernet
754  *	packet, including the needed WR and CPL headers.
755  */
756 static inline unsigned int calc_tx_flits(const struct sk_buff *skb,
757 					 unsigned int chip_ver)
758 {
759 	unsigned int flits;
760 	int hdrlen = is_eth_imm(skb, chip_ver);
761 
762 	/* If the skb is small enough, we can pump it out as a work request
763 	 * with only immediate data.  In that case we just have to have the
764 	 * TX Packet header plus the skb data in the Work Request.
765 	 */
766 
767 	if (hdrlen)
768 		return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
769 
770 	/* Otherwise, we're going to have to construct a Scatter gather list
771 	 * of the skb body and fragments.  We also include the flits necessary
772 	 * for the TX Packet Work Request and CPL.  We always have a firmware
773 	 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
774 	 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
775 	 * message or, if we're doing a Large Send Offload, an LSO CPL message
776 	 * with an embedded TX Packet Write CPL message.
777 	 */
778 	flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
779 	if (skb_shinfo(skb)->gso_size) {
780 		if (skb->encapsulation && chip_ver > CHELSIO_T5) {
781 			hdrlen = sizeof(struct fw_eth_tx_pkt_wr) +
782 				 sizeof(struct cpl_tx_tnl_lso);
783 		} else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
784 			u32 pkt_hdrlen;
785 
786 			pkt_hdrlen = eth_get_headlen(skb->dev, skb->data,
787 						     skb_headlen(skb));
788 			hdrlen = sizeof(struct fw_eth_tx_eo_wr) +
789 				 round_up(pkt_hdrlen, 16);
790 		} else {
791 			hdrlen = sizeof(struct fw_eth_tx_pkt_wr) +
792 				 sizeof(struct cpl_tx_pkt_lso_core);
793 		}
794 
795 		hdrlen += sizeof(struct cpl_tx_pkt_core);
796 		flits += (hdrlen / sizeof(__be64));
797 	} else {
798 		flits += (sizeof(struct fw_eth_tx_pkt_wr) +
799 			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
800 	}
801 	return flits;
802 }
803 
804 /**
805  *	calc_tx_descs - calculate the number of Tx descriptors for a packet
806  *	@skb: the packet
807  *
808  *	Returns the number of Tx descriptors needed for the given Ethernet
809  *	packet, including the needed WR and CPL headers.
810  */
811 static inline unsigned int calc_tx_descs(const struct sk_buff *skb,
812 					 unsigned int chip_ver)
813 {
814 	return flits_to_desc(calc_tx_flits(skb, chip_ver));
815 }
816 
817 /**
818  *	cxgb4_write_sgl - populate a scatter/gather list for a packet
819  *	@skb: the packet
820  *	@q: the Tx queue we are writing into
821  *	@sgl: starting location for writing the SGL
822  *	@end: points right after the end of the SGL
823  *	@start: start offset into skb main-body data to include in the SGL
824  *	@addr: the list of bus addresses for the SGL elements
825  *
826  *	Generates a gather list for the buffers that make up a packet.
827  *	The caller must provide adequate space for the SGL that will be written.
828  *	The SGL includes all of the packet's page fragments and the data in its
829  *	main body except for the first @start bytes.  @sgl must be 16-byte
830  *	aligned and within a Tx descriptor with available space.  @end points
831  *	right after the end of the SGL but does not account for any potential
832  *	wrap around, i.e., @end > @sgl.
833  */
834 void cxgb4_write_sgl(const struct sk_buff *skb, struct sge_txq *q,
835 		     struct ulptx_sgl *sgl, u64 *end, unsigned int start,
836 		     const dma_addr_t *addr)
837 {
838 	unsigned int i, len;
839 	struct ulptx_sge_pair *to;
840 	const struct skb_shared_info *si = skb_shinfo(skb);
841 	unsigned int nfrags = si->nr_frags;
842 	struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
843 
844 	len = skb_headlen(skb) - start;
845 	if (likely(len)) {
846 		sgl->len0 = htonl(len);
847 		sgl->addr0 = cpu_to_be64(addr[0] + start);
848 		nfrags++;
849 	} else {
850 		sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
851 		sgl->addr0 = cpu_to_be64(addr[1]);
852 	}
853 
854 	sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
855 			      ULPTX_NSGE_V(nfrags));
856 	if (likely(--nfrags == 0))
857 		return;
858 	/*
859 	 * Most of the complexity below deals with the possibility we hit the
860 	 * end of the queue in the middle of writing the SGL.  For this case
861 	 * only we create the SGL in a temporary buffer and then copy it.
862 	 */
863 	to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
864 
865 	for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
866 		to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
867 		to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
868 		to->addr[0] = cpu_to_be64(addr[i]);
869 		to->addr[1] = cpu_to_be64(addr[++i]);
870 	}
871 	if (nfrags) {
872 		to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
873 		to->len[1] = cpu_to_be32(0);
874 		to->addr[0] = cpu_to_be64(addr[i + 1]);
875 	}
876 	if (unlikely((u8 *)end > (u8 *)q->stat)) {
877 		unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
878 
879 		if (likely(part0))
880 			memcpy(sgl->sge, buf, part0);
881 		part1 = (u8 *)end - (u8 *)q->stat;
882 		memcpy(q->desc, (u8 *)buf + part0, part1);
883 		end = (void *)q->desc + part1;
884 	}
885 	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
886 		*end = 0;
887 }
888 EXPORT_SYMBOL(cxgb4_write_sgl);
889 
890 /* This function copies 64 byte coalesced work request to
891  * memory mapped BAR2 space. For coalesced WR SGE fetches
892  * data from the FIFO instead of from Host.
893  */
894 static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
895 {
896 	int count = 8;
897 
898 	while (count) {
899 		writeq(*src, dst);
900 		src++;
901 		dst++;
902 		count--;
903 	}
904 }
905 
906 /**
907  *	cxgb4_ring_tx_db - check and potentially ring a Tx queue's doorbell
908  *	@adap: the adapter
909  *	@q: the Tx queue
910  *	@n: number of new descriptors to give to HW
911  *
912  *	Ring the doorbel for a Tx queue.
913  */
914 inline void cxgb4_ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
915 {
916 	/* Make sure that all writes to the TX Descriptors are committed
917 	 * before we tell the hardware about them.
918 	 */
919 	wmb();
920 
921 	/* If we don't have access to the new User Doorbell (T5+), use the old
922 	 * doorbell mechanism; otherwise use the new BAR2 mechanism.
923 	 */
924 	if (unlikely(q->bar2_addr == NULL)) {
925 		u32 val = PIDX_V(n);
926 		unsigned long flags;
927 
928 		/* For T4 we need to participate in the Doorbell Recovery
929 		 * mechanism.
930 		 */
931 		spin_lock_irqsave(&q->db_lock, flags);
932 		if (!q->db_disabled)
933 			t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
934 				     QID_V(q->cntxt_id) | val);
935 		else
936 			q->db_pidx_inc += n;
937 		q->db_pidx = q->pidx;
938 		spin_unlock_irqrestore(&q->db_lock, flags);
939 	} else {
940 		u32 val = PIDX_T5_V(n);
941 
942 		/* T4 and later chips share the same PIDX field offset within
943 		 * the doorbell, but T5 and later shrank the field in order to
944 		 * gain a bit for Doorbell Priority.  The field was absurdly
945 		 * large in the first place (14 bits) so we just use the T5
946 		 * and later limits and warn if a Queue ID is too large.
947 		 */
948 		WARN_ON(val & DBPRIO_F);
949 
950 		/* If we're only writing a single TX Descriptor and we can use
951 		 * Inferred QID registers, we can use the Write Combining
952 		 * Gather Buffer; otherwise we use the simple doorbell.
953 		 */
954 		if (n == 1 && q->bar2_qid == 0) {
955 			int index = (q->pidx
956 				     ? (q->pidx - 1)
957 				     : (q->size - 1));
958 			u64 *wr = (u64 *)&q->desc[index];
959 
960 			cxgb_pio_copy((u64 __iomem *)
961 				      (q->bar2_addr + SGE_UDB_WCDOORBELL),
962 				      wr);
963 		} else {
964 			writel(val | QID_V(q->bar2_qid),
965 			       q->bar2_addr + SGE_UDB_KDOORBELL);
966 		}
967 
968 		/* This Write Memory Barrier will force the write to the User
969 		 * Doorbell area to be flushed.  This is needed to prevent
970 		 * writes on different CPUs for the same queue from hitting
971 		 * the adapter out of order.  This is required when some Work
972 		 * Requests take the Write Combine Gather Buffer path (user
973 		 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
974 		 * take the traditional path where we simply increment the
975 		 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
976 		 * hardware DMA read the actual Work Request.
977 		 */
978 		wmb();
979 	}
980 }
981 EXPORT_SYMBOL(cxgb4_ring_tx_db);
982 
983 /**
984  *	cxgb4_inline_tx_skb - inline a packet's data into Tx descriptors
985  *	@skb: the packet
986  *	@q: the Tx queue where the packet will be inlined
987  *	@pos: starting position in the Tx queue where to inline the packet
988  *
989  *	Inline a packet's contents directly into Tx descriptors, starting at
990  *	the given position within the Tx DMA ring.
991  *	Most of the complexity of this operation is dealing with wrap arounds
992  *	in the middle of the packet we want to inline.
993  */
994 void cxgb4_inline_tx_skb(const struct sk_buff *skb,
995 			 const struct sge_txq *q, void *pos)
996 {
997 	int left = (void *)q->stat - pos;
998 	u64 *p;
999 
1000 	if (likely(skb->len <= left)) {
1001 		if (likely(!skb->data_len))
1002 			skb_copy_from_linear_data(skb, pos, skb->len);
1003 		else
1004 			skb_copy_bits(skb, 0, pos, skb->len);
1005 		pos += skb->len;
1006 	} else {
1007 		skb_copy_bits(skb, 0, pos, left);
1008 		skb_copy_bits(skb, left, q->desc, skb->len - left);
1009 		pos = (void *)q->desc + (skb->len - left);
1010 	}
1011 
1012 	/* 0-pad to multiple of 16 */
1013 	p = PTR_ALIGN(pos, 8);
1014 	if ((uintptr_t)p & 8)
1015 		*p = 0;
1016 }
1017 EXPORT_SYMBOL(cxgb4_inline_tx_skb);
1018 
1019 static void *inline_tx_skb_header(const struct sk_buff *skb,
1020 				  const struct sge_txq *q,  void *pos,
1021 				  int length)
1022 {
1023 	u64 *p;
1024 	int left = (void *)q->stat - pos;
1025 
1026 	if (likely(length <= left)) {
1027 		memcpy(pos, skb->data, length);
1028 		pos += length;
1029 	} else {
1030 		memcpy(pos, skb->data, left);
1031 		memcpy(q->desc, skb->data + left, length - left);
1032 		pos = (void *)q->desc + (length - left);
1033 	}
1034 	/* 0-pad to multiple of 16 */
1035 	p = PTR_ALIGN(pos, 8);
1036 	if ((uintptr_t)p & 8) {
1037 		*p = 0;
1038 		return p + 1;
1039 	}
1040 	return p;
1041 }
1042 
1043 /*
1044  * Figure out what HW csum a packet wants and return the appropriate control
1045  * bits.
1046  */
1047 static u64 hwcsum(enum chip_type chip, const struct sk_buff *skb)
1048 {
1049 	int csum_type;
1050 	bool inner_hdr_csum = false;
1051 	u16 proto, ver;
1052 
1053 	if (skb->encapsulation &&
1054 	    (CHELSIO_CHIP_VERSION(chip) > CHELSIO_T5))
1055 		inner_hdr_csum = true;
1056 
1057 	if (inner_hdr_csum) {
1058 		ver = inner_ip_hdr(skb)->version;
1059 		proto = (ver == 4) ? inner_ip_hdr(skb)->protocol :
1060 			inner_ipv6_hdr(skb)->nexthdr;
1061 	} else {
1062 		ver = ip_hdr(skb)->version;
1063 		proto = (ver == 4) ? ip_hdr(skb)->protocol :
1064 			ipv6_hdr(skb)->nexthdr;
1065 	}
1066 
1067 	if (ver == 4) {
1068 		if (proto == IPPROTO_TCP)
1069 			csum_type = TX_CSUM_TCPIP;
1070 		else if (proto == IPPROTO_UDP)
1071 			csum_type = TX_CSUM_UDPIP;
1072 		else {
1073 nocsum:			/*
1074 			 * unknown protocol, disable HW csum
1075 			 * and hope a bad packet is detected
1076 			 */
1077 			return TXPKT_L4CSUM_DIS_F;
1078 		}
1079 	} else {
1080 		/*
1081 		 * this doesn't work with extension headers
1082 		 */
1083 		if (proto == IPPROTO_TCP)
1084 			csum_type = TX_CSUM_TCPIP6;
1085 		else if (proto == IPPROTO_UDP)
1086 			csum_type = TX_CSUM_UDPIP6;
1087 		else
1088 			goto nocsum;
1089 	}
1090 
1091 	if (likely(csum_type >= TX_CSUM_TCPIP)) {
1092 		int eth_hdr_len, l4_len;
1093 		u64 hdr_len;
1094 
1095 		if (inner_hdr_csum) {
1096 			/* This allows checksum offload for all encapsulated
1097 			 * packets like GRE etc..
1098 			 */
1099 			l4_len = skb_inner_network_header_len(skb);
1100 			eth_hdr_len = skb_inner_network_offset(skb) - ETH_HLEN;
1101 		} else {
1102 			l4_len = skb_network_header_len(skb);
1103 			eth_hdr_len = skb_network_offset(skb) - ETH_HLEN;
1104 		}
1105 		hdr_len = TXPKT_IPHDR_LEN_V(l4_len);
1106 
1107 		if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1108 			hdr_len |= TXPKT_ETHHDR_LEN_V(eth_hdr_len);
1109 		else
1110 			hdr_len |= T6_TXPKT_ETHHDR_LEN_V(eth_hdr_len);
1111 		return TXPKT_CSUM_TYPE_V(csum_type) | hdr_len;
1112 	} else {
1113 		int start = skb_transport_offset(skb);
1114 
1115 		return TXPKT_CSUM_TYPE_V(csum_type) |
1116 			TXPKT_CSUM_START_V(start) |
1117 			TXPKT_CSUM_LOC_V(start + skb->csum_offset);
1118 	}
1119 }
1120 
1121 static void eth_txq_stop(struct sge_eth_txq *q)
1122 {
1123 	netif_tx_stop_queue(q->txq);
1124 	q->q.stops++;
1125 }
1126 
1127 static inline void txq_advance(struct sge_txq *q, unsigned int n)
1128 {
1129 	q->in_use += n;
1130 	q->pidx += n;
1131 	if (q->pidx >= q->size)
1132 		q->pidx -= q->size;
1133 }
1134 
1135 #ifdef CONFIG_CHELSIO_T4_FCOE
1136 static inline int
1137 cxgb_fcoe_offload(struct sk_buff *skb, struct adapter *adap,
1138 		  const struct port_info *pi, u64 *cntrl)
1139 {
1140 	const struct cxgb_fcoe *fcoe = &pi->fcoe;
1141 
1142 	if (!(fcoe->flags & CXGB_FCOE_ENABLED))
1143 		return 0;
1144 
1145 	if (skb->protocol != htons(ETH_P_FCOE))
1146 		return 0;
1147 
1148 	skb_reset_mac_header(skb);
1149 	skb->mac_len = sizeof(struct ethhdr);
1150 
1151 	skb_set_network_header(skb, skb->mac_len);
1152 	skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
1153 
1154 	if (!cxgb_fcoe_sof_eof_supported(adap, skb))
1155 		return -ENOTSUPP;
1156 
1157 	/* FC CRC offload */
1158 	*cntrl = TXPKT_CSUM_TYPE_V(TX_CSUM_FCOE) |
1159 		     TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F |
1160 		     TXPKT_CSUM_START_V(CXGB_FCOE_TXPKT_CSUM_START) |
1161 		     TXPKT_CSUM_END_V(CXGB_FCOE_TXPKT_CSUM_END) |
1162 		     TXPKT_CSUM_LOC_V(CXGB_FCOE_TXPKT_CSUM_END);
1163 	return 0;
1164 }
1165 #endif /* CONFIG_CHELSIO_T4_FCOE */
1166 
1167 /* Returns tunnel type if hardware supports offloading of the same.
1168  * It is called only for T5 and onwards.
1169  */
1170 enum cpl_tx_tnl_lso_type cxgb_encap_offload_supported(struct sk_buff *skb)
1171 {
1172 	u8 l4_hdr = 0;
1173 	enum cpl_tx_tnl_lso_type tnl_type = TX_TNL_TYPE_OPAQUE;
1174 	struct port_info *pi = netdev_priv(skb->dev);
1175 	struct adapter *adapter = pi->adapter;
1176 
1177 	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
1178 	    skb->inner_protocol != htons(ETH_P_TEB))
1179 		return tnl_type;
1180 
1181 	switch (vlan_get_protocol(skb)) {
1182 	case htons(ETH_P_IP):
1183 		l4_hdr = ip_hdr(skb)->protocol;
1184 		break;
1185 	case htons(ETH_P_IPV6):
1186 		l4_hdr = ipv6_hdr(skb)->nexthdr;
1187 		break;
1188 	default:
1189 		return tnl_type;
1190 	}
1191 
1192 	switch (l4_hdr) {
1193 	case IPPROTO_UDP:
1194 		if (adapter->vxlan_port == udp_hdr(skb)->dest)
1195 			tnl_type = TX_TNL_TYPE_VXLAN;
1196 		else if (adapter->geneve_port == udp_hdr(skb)->dest)
1197 			tnl_type = TX_TNL_TYPE_GENEVE;
1198 		break;
1199 	default:
1200 		return tnl_type;
1201 	}
1202 
1203 	return tnl_type;
1204 }
1205 
1206 static inline void t6_fill_tnl_lso(struct sk_buff *skb,
1207 				   struct cpl_tx_tnl_lso *tnl_lso,
1208 				   enum cpl_tx_tnl_lso_type tnl_type)
1209 {
1210 	u32 val;
1211 	int in_eth_xtra_len;
1212 	int l3hdr_len = skb_network_header_len(skb);
1213 	int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1214 	const struct skb_shared_info *ssi = skb_shinfo(skb);
1215 	bool v6 = (ip_hdr(skb)->version == 6);
1216 
1217 	val = CPL_TX_TNL_LSO_OPCODE_V(CPL_TX_TNL_LSO) |
1218 	      CPL_TX_TNL_LSO_FIRST_F |
1219 	      CPL_TX_TNL_LSO_LAST_F |
1220 	      (v6 ? CPL_TX_TNL_LSO_IPV6OUT_F : 0) |
1221 	      CPL_TX_TNL_LSO_ETHHDRLENOUT_V(eth_xtra_len / 4) |
1222 	      CPL_TX_TNL_LSO_IPHDRLENOUT_V(l3hdr_len / 4) |
1223 	      (v6 ? 0 : CPL_TX_TNL_LSO_IPHDRCHKOUT_F) |
1224 	      CPL_TX_TNL_LSO_IPLENSETOUT_F |
1225 	      (v6 ? 0 : CPL_TX_TNL_LSO_IPIDINCOUT_F);
1226 	tnl_lso->op_to_IpIdSplitOut = htonl(val);
1227 
1228 	tnl_lso->IpIdOffsetOut = 0;
1229 
1230 	/* Get the tunnel header length */
1231 	val = skb_inner_mac_header(skb) - skb_mac_header(skb);
1232 	in_eth_xtra_len = skb_inner_network_header(skb) -
1233 			  skb_inner_mac_header(skb) - ETH_HLEN;
1234 
1235 	switch (tnl_type) {
1236 	case TX_TNL_TYPE_VXLAN:
1237 	case TX_TNL_TYPE_GENEVE:
1238 		tnl_lso->UdpLenSetOut_to_TnlHdrLen =
1239 			htons(CPL_TX_TNL_LSO_UDPCHKCLROUT_F |
1240 			CPL_TX_TNL_LSO_UDPLENSETOUT_F);
1241 		break;
1242 	default:
1243 		tnl_lso->UdpLenSetOut_to_TnlHdrLen = 0;
1244 		break;
1245 	}
1246 
1247 	tnl_lso->UdpLenSetOut_to_TnlHdrLen |=
1248 		 htons(CPL_TX_TNL_LSO_TNLHDRLEN_V(val) |
1249 		       CPL_TX_TNL_LSO_TNLTYPE_V(tnl_type));
1250 
1251 	tnl_lso->r1 = 0;
1252 
1253 	val = CPL_TX_TNL_LSO_ETHHDRLEN_V(in_eth_xtra_len / 4) |
1254 	      CPL_TX_TNL_LSO_IPV6_V(inner_ip_hdr(skb)->version == 6) |
1255 	      CPL_TX_TNL_LSO_IPHDRLEN_V(skb_inner_network_header_len(skb) / 4) |
1256 	      CPL_TX_TNL_LSO_TCPHDRLEN_V(inner_tcp_hdrlen(skb) / 4);
1257 	tnl_lso->Flow_to_TcpHdrLen = htonl(val);
1258 
1259 	tnl_lso->IpIdOffset = htons(0);
1260 
1261 	tnl_lso->IpIdSplit_to_Mss = htons(CPL_TX_TNL_LSO_MSS_V(ssi->gso_size));
1262 	tnl_lso->TCPSeqOffset = htonl(0);
1263 	tnl_lso->EthLenOffset_Size = htonl(CPL_TX_TNL_LSO_SIZE_V(skb->len));
1264 }
1265 
1266 static inline void *write_tso_wr(struct adapter *adap, struct sk_buff *skb,
1267 				 struct cpl_tx_pkt_lso_core *lso)
1268 {
1269 	int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1270 	int l3hdr_len = skb_network_header_len(skb);
1271 	const struct skb_shared_info *ssi;
1272 	bool ipv6 = false;
1273 
1274 	ssi = skb_shinfo(skb);
1275 	if (ssi->gso_type & SKB_GSO_TCPV6)
1276 		ipv6 = true;
1277 
1278 	lso->lso_ctrl = htonl(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
1279 			      LSO_FIRST_SLICE_F | LSO_LAST_SLICE_F |
1280 			      LSO_IPV6_V(ipv6) |
1281 			      LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
1282 			      LSO_IPHDR_LEN_V(l3hdr_len / 4) |
1283 			      LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
1284 	lso->ipid_ofst = htons(0);
1285 	lso->mss = htons(ssi->gso_size);
1286 	lso->seqno_offset = htonl(0);
1287 	if (is_t4(adap->params.chip))
1288 		lso->len = htonl(skb->len);
1289 	else
1290 		lso->len = htonl(LSO_T5_XFER_SIZE_V(skb->len));
1291 
1292 	return (void *)(lso + 1);
1293 }
1294 
1295 /**
1296  *	t4_sge_eth_txq_egress_update - handle Ethernet TX Queue update
1297  *	@adap: the adapter
1298  *	@eq: the Ethernet TX Queue
1299  *	@maxreclaim: the maximum number of TX Descriptors to reclaim or -1
1300  *
1301  *	We're typically called here to update the state of an Ethernet TX
1302  *	Queue with respect to the hardware's progress in consuming the TX
1303  *	Work Requests that we've put on that Egress Queue.  This happens
1304  *	when we get Egress Queue Update messages and also prophylactically
1305  *	in regular timer-based Ethernet TX Queue maintenance.
1306  */
1307 int t4_sge_eth_txq_egress_update(struct adapter *adap, struct sge_eth_txq *eq,
1308 				 int maxreclaim)
1309 {
1310 	struct sge_txq *q = &eq->q;
1311 	unsigned int reclaimed;
1312 
1313 	if (!q->in_use || !__netif_tx_trylock(eq->txq))
1314 		return 0;
1315 
1316 	/* Reclaim pending completed TX Descriptors. */
1317 	reclaimed = reclaim_completed_tx(adap, &eq->q, maxreclaim, true);
1318 
1319 	/* If the TX Queue is currently stopped and there's now more than half
1320 	 * the queue available, restart it.  Otherwise bail out since the rest
1321 	 * of what we want do here is with the possibility of shipping any
1322 	 * currently buffered Coalesced TX Work Request.
1323 	 */
1324 	if (netif_tx_queue_stopped(eq->txq) && txq_avail(q) > (q->size / 2)) {
1325 		netif_tx_wake_queue(eq->txq);
1326 		eq->q.restarts++;
1327 	}
1328 
1329 	__netif_tx_unlock(eq->txq);
1330 	return reclaimed;
1331 }
1332 
1333 static inline int cxgb4_validate_skb(struct sk_buff *skb,
1334 				     struct net_device *dev,
1335 				     u32 min_pkt_len)
1336 {
1337 	u32 max_pkt_len;
1338 
1339 	/* The chip min packet length is 10 octets but some firmware
1340 	 * commands have a minimum packet length requirement. So, play
1341 	 * safe and reject anything shorter than @min_pkt_len.
1342 	 */
1343 	if (unlikely(skb->len < min_pkt_len))
1344 		return -EINVAL;
1345 
1346 	/* Discard the packet if the length is greater than mtu */
1347 	max_pkt_len = ETH_HLEN + dev->mtu;
1348 
1349 	if (skb_vlan_tagged(skb))
1350 		max_pkt_len += VLAN_HLEN;
1351 
1352 	if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
1353 		return -EINVAL;
1354 
1355 	return 0;
1356 }
1357 
1358 static void *write_eo_udp_wr(struct sk_buff *skb, struct fw_eth_tx_eo_wr *wr,
1359 			     u32 hdr_len)
1360 {
1361 	wr->u.udpseg.type = FW_ETH_TX_EO_TYPE_UDPSEG;
1362 	wr->u.udpseg.ethlen = skb_network_offset(skb);
1363 	wr->u.udpseg.iplen = cpu_to_be16(skb_network_header_len(skb));
1364 	wr->u.udpseg.udplen = sizeof(struct udphdr);
1365 	wr->u.udpseg.rtplen = 0;
1366 	wr->u.udpseg.r4 = 0;
1367 	if (skb_shinfo(skb)->gso_size)
1368 		wr->u.udpseg.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1369 	else
1370 		wr->u.udpseg.mss = cpu_to_be16(skb->len - hdr_len);
1371 	wr->u.udpseg.schedpktsize = wr->u.udpseg.mss;
1372 	wr->u.udpseg.plen = cpu_to_be32(skb->len - hdr_len);
1373 
1374 	return (void *)(wr + 1);
1375 }
1376 
1377 /**
1378  *	cxgb4_eth_xmit - add a packet to an Ethernet Tx queue
1379  *	@skb: the packet
1380  *	@dev: the egress net device
1381  *
1382  *	Add a packet to an SGE Ethernet Tx queue.  Runs with softirqs disabled.
1383  */
1384 static netdev_tx_t cxgb4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
1385 {
1386 	enum cpl_tx_tnl_lso_type tnl_type = TX_TNL_TYPE_OPAQUE;
1387 	bool ptp_enabled = is_ptp_enabled(skb, dev);
1388 	unsigned int last_desc, flits, ndesc;
1389 	u32 wr_mid, ctrl0, op, sgl_off = 0;
1390 	const struct skb_shared_info *ssi;
1391 	int len, qidx, credits, ret, left;
1392 	struct tx_sw_desc *sgl_sdesc;
1393 	struct fw_eth_tx_eo_wr *eowr;
1394 	struct fw_eth_tx_pkt_wr *wr;
1395 	struct cpl_tx_pkt_core *cpl;
1396 	const struct port_info *pi;
1397 	bool immediate = false;
1398 	u64 cntrl, *end, *sgl;
1399 	struct sge_eth_txq *q;
1400 	unsigned int chip_ver;
1401 	struct adapter *adap;
1402 
1403 	ret = cxgb4_validate_skb(skb, dev, ETH_HLEN);
1404 	if (ret)
1405 		goto out_free;
1406 
1407 	pi = netdev_priv(dev);
1408 	adap = pi->adapter;
1409 	ssi = skb_shinfo(skb);
1410 #ifdef CONFIG_CHELSIO_IPSEC_INLINE
1411 	if (xfrm_offload(skb) && !ssi->gso_size)
1412 		return adap->uld[CXGB4_ULD_CRYPTO].tx_handler(skb, dev);
1413 #endif /* CHELSIO_IPSEC_INLINE */
1414 
1415 	qidx = skb_get_queue_mapping(skb);
1416 	if (ptp_enabled) {
1417 		spin_lock(&adap->ptp_lock);
1418 		if (!(adap->ptp_tx_skb)) {
1419 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1420 			adap->ptp_tx_skb = skb_get(skb);
1421 		} else {
1422 			spin_unlock(&adap->ptp_lock);
1423 			goto out_free;
1424 		}
1425 		q = &adap->sge.ptptxq;
1426 	} else {
1427 		q = &adap->sge.ethtxq[qidx + pi->first_qset];
1428 	}
1429 	skb_tx_timestamp(skb);
1430 
1431 	reclaim_completed_tx(adap, &q->q, -1, true);
1432 	cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
1433 
1434 #ifdef CONFIG_CHELSIO_T4_FCOE
1435 	ret = cxgb_fcoe_offload(skb, adap, pi, &cntrl);
1436 	if (unlikely(ret == -ENOTSUPP)) {
1437 		if (ptp_enabled)
1438 			spin_unlock(&adap->ptp_lock);
1439 		goto out_free;
1440 	}
1441 #endif /* CONFIG_CHELSIO_T4_FCOE */
1442 
1443 	chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
1444 	flits = calc_tx_flits(skb, chip_ver);
1445 	ndesc = flits_to_desc(flits);
1446 	credits = txq_avail(&q->q) - ndesc;
1447 
1448 	if (unlikely(credits < 0)) {
1449 		eth_txq_stop(q);
1450 		dev_err(adap->pdev_dev,
1451 			"%s: Tx ring %u full while queue awake!\n",
1452 			dev->name, qidx);
1453 		if (ptp_enabled)
1454 			spin_unlock(&adap->ptp_lock);
1455 		return NETDEV_TX_BUSY;
1456 	}
1457 
1458 	if (is_eth_imm(skb, chip_ver))
1459 		immediate = true;
1460 
1461 	if (skb->encapsulation && chip_ver > CHELSIO_T5)
1462 		tnl_type = cxgb_encap_offload_supported(skb);
1463 
1464 	last_desc = q->q.pidx + ndesc - 1;
1465 	if (last_desc >= q->q.size)
1466 		last_desc -= q->q.size;
1467 	sgl_sdesc = &q->q.sdesc[last_desc];
1468 
1469 	if (!immediate &&
1470 	    unlikely(cxgb4_map_skb(adap->pdev_dev, skb, sgl_sdesc->addr) < 0)) {
1471 		memset(sgl_sdesc->addr, 0, sizeof(sgl_sdesc->addr));
1472 		q->mapping_err++;
1473 		if (ptp_enabled)
1474 			spin_unlock(&adap->ptp_lock);
1475 		goto out_free;
1476 	}
1477 
1478 	wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
1479 	if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1480 		/* After we're done injecting the Work Request for this
1481 		 * packet, we'll be below our "stop threshold" so stop the TX
1482 		 * Queue now and schedule a request for an SGE Egress Queue
1483 		 * Update message. The queue will get started later on when
1484 		 * the firmware processes this Work Request and sends us an
1485 		 * Egress Queue Status Update message indicating that space
1486 		 * has opened up.
1487 		 */
1488 		eth_txq_stop(q);
1489 
1490 		/* If we're using the SGE Doorbell Queue Timer facility, we
1491 		 * don't need to ask the Firmware to send us Egress Queue CIDX
1492 		 * Updates: the Hardware will do this automatically.  And
1493 		 * since we send the Ingress Queue CIDX Updates to the
1494 		 * corresponding Ethernet Response Queue, we'll get them very
1495 		 * quickly.
1496 		 */
1497 		if (!q->dbqt)
1498 			wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
1499 	}
1500 
1501 	wr = (void *)&q->q.desc[q->q.pidx];
1502 	eowr = (void *)&q->q.desc[q->q.pidx];
1503 	wr->equiq_to_len16 = htonl(wr_mid);
1504 	wr->r3 = cpu_to_be64(0);
1505 	if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
1506 		end = (u64 *)eowr + flits;
1507 	else
1508 		end = (u64 *)wr + flits;
1509 
1510 	len = immediate ? skb->len : 0;
1511 	len += sizeof(*cpl);
1512 	if (ssi->gso_size && !(ssi->gso_type & SKB_GSO_UDP_L4)) {
1513 		struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
1514 		struct cpl_tx_tnl_lso *tnl_lso = (void *)(wr + 1);
1515 
1516 		if (tnl_type)
1517 			len += sizeof(*tnl_lso);
1518 		else
1519 			len += sizeof(*lso);
1520 
1521 		wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
1522 				       FW_WR_IMMDLEN_V(len));
1523 		if (tnl_type) {
1524 			struct iphdr *iph = ip_hdr(skb);
1525 
1526 			t6_fill_tnl_lso(skb, tnl_lso, tnl_type);
1527 			cpl = (void *)(tnl_lso + 1);
1528 			/* Driver is expected to compute partial checksum that
1529 			 * does not include the IP Total Length.
1530 			 */
1531 			if (iph->version == 4) {
1532 				iph->check = 0;
1533 				iph->tot_len = 0;
1534 				iph->check = (u16)(~ip_fast_csum((u8 *)iph,
1535 								 iph->ihl));
1536 			}
1537 			if (skb->ip_summed == CHECKSUM_PARTIAL)
1538 				cntrl = hwcsum(adap->params.chip, skb);
1539 		} else {
1540 			cpl = write_tso_wr(adap, skb, lso);
1541 			cntrl = hwcsum(adap->params.chip, skb);
1542 		}
1543 		sgl = (u64 *)(cpl + 1); /* sgl start here */
1544 		q->tso++;
1545 		q->tx_cso += ssi->gso_segs;
1546 	} else if (ssi->gso_size) {
1547 		u64 *start;
1548 		u32 hdrlen;
1549 
1550 		hdrlen = eth_get_headlen(dev, skb->data, skb_headlen(skb));
1551 		len += hdrlen;
1552 		wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_EO_WR) |
1553 					     FW_ETH_TX_EO_WR_IMMDLEN_V(len));
1554 		cpl = write_eo_udp_wr(skb, eowr, hdrlen);
1555 		cntrl = hwcsum(adap->params.chip, skb);
1556 
1557 		start = (u64 *)(cpl + 1);
1558 		sgl = (u64 *)inline_tx_skb_header(skb, &q->q, (void *)start,
1559 						  hdrlen);
1560 		if (unlikely(start > sgl)) {
1561 			left = (u8 *)end - (u8 *)q->q.stat;
1562 			end = (void *)q->q.desc + left;
1563 		}
1564 		sgl_off = hdrlen;
1565 		q->uso++;
1566 		q->tx_cso += ssi->gso_segs;
1567 	} else {
1568 		if (ptp_enabled)
1569 			op = FW_PTP_TX_PKT_WR;
1570 		else
1571 			op = FW_ETH_TX_PKT_WR;
1572 		wr->op_immdlen = htonl(FW_WR_OP_V(op) |
1573 				       FW_WR_IMMDLEN_V(len));
1574 		cpl = (void *)(wr + 1);
1575 		sgl = (u64 *)(cpl + 1);
1576 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1577 			cntrl = hwcsum(adap->params.chip, skb) |
1578 				TXPKT_IPCSUM_DIS_F;
1579 			q->tx_cso++;
1580 		}
1581 	}
1582 
1583 	if (unlikely((u8 *)sgl >= (u8 *)q->q.stat)) {
1584 		/* If current position is already at the end of the
1585 		 * txq, reset the current to point to start of the queue
1586 		 * and update the end ptr as well.
1587 		 */
1588 		left = (u8 *)end - (u8 *)q->q.stat;
1589 		end = (void *)q->q.desc + left;
1590 		sgl = (void *)q->q.desc;
1591 	}
1592 
1593 	if (skb_vlan_tag_present(skb)) {
1594 		q->vlan_ins++;
1595 		cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
1596 #ifdef CONFIG_CHELSIO_T4_FCOE
1597 		if (skb->protocol == htons(ETH_P_FCOE))
1598 			cntrl |= TXPKT_VLAN_V(
1599 				 ((skb->priority & 0x7) << VLAN_PRIO_SHIFT));
1600 #endif /* CONFIG_CHELSIO_T4_FCOE */
1601 	}
1602 
1603 	ctrl0 = TXPKT_OPCODE_V(CPL_TX_PKT_XT) | TXPKT_INTF_V(pi->tx_chan) |
1604 		TXPKT_PF_V(adap->pf);
1605 	if (ptp_enabled)
1606 		ctrl0 |= TXPKT_TSTAMP_F;
1607 #ifdef CONFIG_CHELSIO_T4_DCB
1608 	if (is_t4(adap->params.chip))
1609 		ctrl0 |= TXPKT_OVLAN_IDX_V(q->dcb_prio);
1610 	else
1611 		ctrl0 |= TXPKT_T5_OVLAN_IDX_V(q->dcb_prio);
1612 #endif
1613 	cpl->ctrl0 = htonl(ctrl0);
1614 	cpl->pack = htons(0);
1615 	cpl->len = htons(skb->len);
1616 	cpl->ctrl1 = cpu_to_be64(cntrl);
1617 
1618 	if (immediate) {
1619 		cxgb4_inline_tx_skb(skb, &q->q, sgl);
1620 		dev_consume_skb_any(skb);
1621 	} else {
1622 		cxgb4_write_sgl(skb, &q->q, (void *)sgl, end, sgl_off,
1623 				sgl_sdesc->addr);
1624 		skb_orphan(skb);
1625 		sgl_sdesc->skb = skb;
1626 	}
1627 
1628 	txq_advance(&q->q, ndesc);
1629 
1630 	cxgb4_ring_tx_db(adap, &q->q, ndesc);
1631 	if (ptp_enabled)
1632 		spin_unlock(&adap->ptp_lock);
1633 	return NETDEV_TX_OK;
1634 
1635 out_free:
1636 	dev_kfree_skb_any(skb);
1637 	return NETDEV_TX_OK;
1638 }
1639 
1640 /* Constants ... */
1641 enum {
1642 	/* Egress Queue sizes, producer and consumer indices are all in units
1643 	 * of Egress Context Units bytes.  Note that as far as the hardware is
1644 	 * concerned, the free list is an Egress Queue (the host produces free
1645 	 * buffers which the hardware consumes) and free list entries are
1646 	 * 64-bit PCI DMA addresses.
1647 	 */
1648 	EQ_UNIT = SGE_EQ_IDXSIZE,
1649 	FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
1650 	TXD_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
1651 
1652 	T4VF_ETHTXQ_MAX_HDR = (sizeof(struct fw_eth_tx_pkt_vm_wr) +
1653 			       sizeof(struct cpl_tx_pkt_lso_core) +
1654 			       sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64),
1655 };
1656 
1657 /**
1658  *	t4vf_is_eth_imm - can an Ethernet packet be sent as immediate data?
1659  *	@skb: the packet
1660  *
1661  *	Returns whether an Ethernet packet is small enough to fit completely as
1662  *	immediate data.
1663  */
1664 static inline int t4vf_is_eth_imm(const struct sk_buff *skb)
1665 {
1666 	/* The VF Driver uses the FW_ETH_TX_PKT_VM_WR firmware Work Request
1667 	 * which does not accommodate immediate data.  We could dike out all
1668 	 * of the support code for immediate data but that would tie our hands
1669 	 * too much if we ever want to enhace the firmware.  It would also
1670 	 * create more differences between the PF and VF Drivers.
1671 	 */
1672 	return false;
1673 }
1674 
1675 /**
1676  *	t4vf_calc_tx_flits - calculate the number of flits for a packet TX WR
1677  *	@skb: the packet
1678  *
1679  *	Returns the number of flits needed for a TX Work Request for the
1680  *	given Ethernet packet, including the needed WR and CPL headers.
1681  */
1682 static inline unsigned int t4vf_calc_tx_flits(const struct sk_buff *skb)
1683 {
1684 	unsigned int flits;
1685 
1686 	/* If the skb is small enough, we can pump it out as a work request
1687 	 * with only immediate data.  In that case we just have to have the
1688 	 * TX Packet header plus the skb data in the Work Request.
1689 	 */
1690 	if (t4vf_is_eth_imm(skb))
1691 		return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt),
1692 				    sizeof(__be64));
1693 
1694 	/* Otherwise, we're going to have to construct a Scatter gather list
1695 	 * of the skb body and fragments.  We also include the flits necessary
1696 	 * for the TX Packet Work Request and CPL.  We always have a firmware
1697 	 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
1698 	 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
1699 	 * message or, if we're doing a Large Send Offload, an LSO CPL message
1700 	 * with an embedded TX Packet Write CPL message.
1701 	 */
1702 	flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
1703 	if (skb_shinfo(skb)->gso_size)
1704 		flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
1705 			  sizeof(struct cpl_tx_pkt_lso_core) +
1706 			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
1707 	else
1708 		flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
1709 			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
1710 	return flits;
1711 }
1712 
1713 /**
1714  *	cxgb4_vf_eth_xmit - add a packet to an Ethernet TX queue
1715  *	@skb: the packet
1716  *	@dev: the egress net device
1717  *
1718  *	Add a packet to an SGE Ethernet TX queue.  Runs with softirqs disabled.
1719  */
1720 static netdev_tx_t cxgb4_vf_eth_xmit(struct sk_buff *skb,
1721 				     struct net_device *dev)
1722 {
1723 	unsigned int last_desc, flits, ndesc;
1724 	const struct skb_shared_info *ssi;
1725 	struct fw_eth_tx_pkt_vm_wr *wr;
1726 	struct tx_sw_desc *sgl_sdesc;
1727 	struct cpl_tx_pkt_core *cpl;
1728 	const struct port_info *pi;
1729 	struct sge_eth_txq *txq;
1730 	struct adapter *adapter;
1731 	int qidx, credits, ret;
1732 	size_t fw_hdr_copy_len;
1733 	u64 cntrl, *end;
1734 	u32 wr_mid;
1735 
1736 	/* The chip minimum packet length is 10 octets but the firmware
1737 	 * command that we are using requires that we copy the Ethernet header
1738 	 * (including the VLAN tag) into the header so we reject anything
1739 	 * smaller than that ...
1740 	 */
1741 	fw_hdr_copy_len = sizeof(wr->ethmacdst) + sizeof(wr->ethmacsrc) +
1742 			  sizeof(wr->ethtype) + sizeof(wr->vlantci);
1743 	ret = cxgb4_validate_skb(skb, dev, fw_hdr_copy_len);
1744 	if (ret)
1745 		goto out_free;
1746 
1747 	/* Figure out which TX Queue we're going to use. */
1748 	pi = netdev_priv(dev);
1749 	adapter = pi->adapter;
1750 	qidx = skb_get_queue_mapping(skb);
1751 	WARN_ON(qidx >= pi->nqsets);
1752 	txq = &adapter->sge.ethtxq[pi->first_qset + qidx];
1753 
1754 	/* Take this opportunity to reclaim any TX Descriptors whose DMA
1755 	 * transfers have completed.
1756 	 */
1757 	reclaim_completed_tx(adapter, &txq->q, -1, true);
1758 
1759 	/* Calculate the number of flits and TX Descriptors we're going to
1760 	 * need along with how many TX Descriptors will be left over after
1761 	 * we inject our Work Request.
1762 	 */
1763 	flits = t4vf_calc_tx_flits(skb);
1764 	ndesc = flits_to_desc(flits);
1765 	credits = txq_avail(&txq->q) - ndesc;
1766 
1767 	if (unlikely(credits < 0)) {
1768 		/* Not enough room for this packet's Work Request.  Stop the
1769 		 * TX Queue and return a "busy" condition.  The queue will get
1770 		 * started later on when the firmware informs us that space
1771 		 * has opened up.
1772 		 */
1773 		eth_txq_stop(txq);
1774 		dev_err(adapter->pdev_dev,
1775 			"%s: TX ring %u full while queue awake!\n",
1776 			dev->name, qidx);
1777 		return NETDEV_TX_BUSY;
1778 	}
1779 
1780 	last_desc = txq->q.pidx + ndesc - 1;
1781 	if (last_desc >= txq->q.size)
1782 		last_desc -= txq->q.size;
1783 	sgl_sdesc = &txq->q.sdesc[last_desc];
1784 
1785 	if (!t4vf_is_eth_imm(skb) &&
1786 	    unlikely(cxgb4_map_skb(adapter->pdev_dev, skb,
1787 				   sgl_sdesc->addr) < 0)) {
1788 		/* We need to map the skb into PCI DMA space (because it can't
1789 		 * be in-lined directly into the Work Request) and the mapping
1790 		 * operation failed.  Record the error and drop the packet.
1791 		 */
1792 		memset(sgl_sdesc->addr, 0, sizeof(sgl_sdesc->addr));
1793 		txq->mapping_err++;
1794 		goto out_free;
1795 	}
1796 
1797 	wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
1798 	if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1799 		/* After we're done injecting the Work Request for this
1800 		 * packet, we'll be below our "stop threshold" so stop the TX
1801 		 * Queue now and schedule a request for an SGE Egress Queue
1802 		 * Update message.  The queue will get started later on when
1803 		 * the firmware processes this Work Request and sends us an
1804 		 * Egress Queue Status Update message indicating that space
1805 		 * has opened up.
1806 		 */
1807 		eth_txq_stop(txq);
1808 
1809 		/* If we're using the SGE Doorbell Queue Timer facility, we
1810 		 * don't need to ask the Firmware to send us Egress Queue CIDX
1811 		 * Updates: the Hardware will do this automatically.  And
1812 		 * since we send the Ingress Queue CIDX Updates to the
1813 		 * corresponding Ethernet Response Queue, we'll get them very
1814 		 * quickly.
1815 		 */
1816 		if (!txq->dbqt)
1817 			wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
1818 	}
1819 
1820 	/* Start filling in our Work Request.  Note that we do _not_ handle
1821 	 * the WR Header wrapping around the TX Descriptor Ring.  If our
1822 	 * maximum header size ever exceeds one TX Descriptor, we'll need to
1823 	 * do something else here.
1824 	 */
1825 	WARN_ON(DIV_ROUND_UP(T4VF_ETHTXQ_MAX_HDR, TXD_PER_EQ_UNIT) > 1);
1826 	wr = (void *)&txq->q.desc[txq->q.pidx];
1827 	wr->equiq_to_len16 = cpu_to_be32(wr_mid);
1828 	wr->r3[0] = cpu_to_be32(0);
1829 	wr->r3[1] = cpu_to_be32(0);
1830 	skb_copy_from_linear_data(skb, (void *)wr->ethmacdst, fw_hdr_copy_len);
1831 	end = (u64 *)wr + flits;
1832 
1833 	/* If this is a Large Send Offload packet we'll put in an LSO CPL
1834 	 * message with an encapsulated TX Packet CPL message.  Otherwise we
1835 	 * just use a TX Packet CPL message.
1836 	 */
1837 	ssi = skb_shinfo(skb);
1838 	if (ssi->gso_size) {
1839 		struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
1840 		bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
1841 		int l3hdr_len = skb_network_header_len(skb);
1842 		int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1843 
1844 		wr->op_immdlen =
1845 			cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
1846 				    FW_WR_IMMDLEN_V(sizeof(*lso) +
1847 						    sizeof(*cpl)));
1848 		 /* Fill in the LSO CPL message. */
1849 		lso->lso_ctrl =
1850 			cpu_to_be32(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
1851 				    LSO_FIRST_SLICE_F |
1852 				    LSO_LAST_SLICE_F |
1853 				    LSO_IPV6_V(v6) |
1854 				    LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
1855 				    LSO_IPHDR_LEN_V(l3hdr_len / 4) |
1856 				    LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
1857 		lso->ipid_ofst = cpu_to_be16(0);
1858 		lso->mss = cpu_to_be16(ssi->gso_size);
1859 		lso->seqno_offset = cpu_to_be32(0);
1860 		if (is_t4(adapter->params.chip))
1861 			lso->len = cpu_to_be32(skb->len);
1862 		else
1863 			lso->len = cpu_to_be32(LSO_T5_XFER_SIZE_V(skb->len));
1864 
1865 		/* Set up TX Packet CPL pointer, control word and perform
1866 		 * accounting.
1867 		 */
1868 		cpl = (void *)(lso + 1);
1869 
1870 		if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
1871 			cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len);
1872 		else
1873 			cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len);
1874 
1875 		cntrl |= TXPKT_CSUM_TYPE_V(v6 ?
1876 					   TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
1877 			 TXPKT_IPHDR_LEN_V(l3hdr_len);
1878 		txq->tso++;
1879 		txq->tx_cso += ssi->gso_segs;
1880 	} else {
1881 		int len;
1882 
1883 		len = (t4vf_is_eth_imm(skb)
1884 		       ? skb->len + sizeof(*cpl)
1885 		       : sizeof(*cpl));
1886 		wr->op_immdlen =
1887 			cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
1888 				    FW_WR_IMMDLEN_V(len));
1889 
1890 		/* Set up TX Packet CPL pointer, control word and perform
1891 		 * accounting.
1892 		 */
1893 		cpl = (void *)(wr + 1);
1894 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1895 			cntrl = hwcsum(adapter->params.chip, skb) |
1896 				TXPKT_IPCSUM_DIS_F;
1897 			txq->tx_cso++;
1898 		} else {
1899 			cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
1900 		}
1901 	}
1902 
1903 	/* If there's a VLAN tag present, add that to the list of things to
1904 	 * do in this Work Request.
1905 	 */
1906 	if (skb_vlan_tag_present(skb)) {
1907 		txq->vlan_ins++;
1908 		cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
1909 	}
1910 
1911 	 /* Fill in the TX Packet CPL message header. */
1912 	cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE_V(CPL_TX_PKT_XT) |
1913 				 TXPKT_INTF_V(pi->port_id) |
1914 				 TXPKT_PF_V(0));
1915 	cpl->pack = cpu_to_be16(0);
1916 	cpl->len = cpu_to_be16(skb->len);
1917 	cpl->ctrl1 = cpu_to_be64(cntrl);
1918 
1919 	/* Fill in the body of the TX Packet CPL message with either in-lined
1920 	 * data or a Scatter/Gather List.
1921 	 */
1922 	if (t4vf_is_eth_imm(skb)) {
1923 		/* In-line the packet's data and free the skb since we don't
1924 		 * need it any longer.
1925 		 */
1926 		cxgb4_inline_tx_skb(skb, &txq->q, cpl + 1);
1927 		dev_consume_skb_any(skb);
1928 	} else {
1929 		/* Write the skb's Scatter/Gather list into the TX Packet CPL
1930 		 * message and retain a pointer to the skb so we can free it
1931 		 * later when its DMA completes.  (We store the skb pointer
1932 		 * in the Software Descriptor corresponding to the last TX
1933 		 * Descriptor used by the Work Request.)
1934 		 *
1935 		 * The retained skb will be freed when the corresponding TX
1936 		 * Descriptors are reclaimed after their DMAs complete.
1937 		 * However, this could take quite a while since, in general,
1938 		 * the hardware is set up to be lazy about sending DMA
1939 		 * completion notifications to us and we mostly perform TX
1940 		 * reclaims in the transmit routine.
1941 		 *
1942 		 * This is good for performamce but means that we rely on new
1943 		 * TX packets arriving to run the destructors of completed
1944 		 * packets, which open up space in their sockets' send queues.
1945 		 * Sometimes we do not get such new packets causing TX to
1946 		 * stall.  A single UDP transmitter is a good example of this
1947 		 * situation.  We have a clean up timer that periodically
1948 		 * reclaims completed packets but it doesn't run often enough
1949 		 * (nor do we want it to) to prevent lengthy stalls.  A
1950 		 * solution to this problem is to run the destructor early,
1951 		 * after the packet is queued but before it's DMAd.  A con is
1952 		 * that we lie to socket memory accounting, but the amount of
1953 		 * extra memory is reasonable (limited by the number of TX
1954 		 * descriptors), the packets do actually get freed quickly by
1955 		 * new packets almost always, and for protocols like TCP that
1956 		 * wait for acks to really free up the data the extra memory
1957 		 * is even less.  On the positive side we run the destructors
1958 		 * on the sending CPU rather than on a potentially different
1959 		 * completing CPU, usually a good thing.
1960 		 *
1961 		 * Run the destructor before telling the DMA engine about the
1962 		 * packet to make sure it doesn't complete and get freed
1963 		 * prematurely.
1964 		 */
1965 		struct ulptx_sgl *sgl = (struct ulptx_sgl *)(cpl + 1);
1966 		struct sge_txq *tq = &txq->q;
1967 
1968 		/* If the Work Request header was an exact multiple of our TX
1969 		 * Descriptor length, then it's possible that the starting SGL
1970 		 * pointer lines up exactly with the end of our TX Descriptor
1971 		 * ring.  If that's the case, wrap around to the beginning
1972 		 * here ...
1973 		 */
1974 		if (unlikely((void *)sgl == (void *)tq->stat)) {
1975 			sgl = (void *)tq->desc;
1976 			end = (void *)((void *)tq->desc +
1977 				       ((void *)end - (void *)tq->stat));
1978 		}
1979 
1980 		cxgb4_write_sgl(skb, tq, sgl, end, 0, sgl_sdesc->addr);
1981 		skb_orphan(skb);
1982 		sgl_sdesc->skb = skb;
1983 	}
1984 
1985 	/* Advance our internal TX Queue state, tell the hardware about
1986 	 * the new TX descriptors and return success.
1987 	 */
1988 	txq_advance(&txq->q, ndesc);
1989 
1990 	cxgb4_ring_tx_db(adapter, &txq->q, ndesc);
1991 	return NETDEV_TX_OK;
1992 
1993 out_free:
1994 	/* An error of some sort happened.  Free the TX skb and tell the
1995 	 * OS that we've "dealt" with the packet ...
1996 	 */
1997 	dev_kfree_skb_any(skb);
1998 	return NETDEV_TX_OK;
1999 }
2000 
2001 /**
2002  * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
2003  * @q: the SGE control Tx queue
2004  *
2005  * This is a variant of cxgb4_reclaim_completed_tx() that is used
2006  * for Tx queues that send only immediate data (presently just
2007  * the control queues) and	thus do not have any sk_buffs to release.
2008  */
2009 static inline void reclaim_completed_tx_imm(struct sge_txq *q)
2010 {
2011 	int hw_cidx = ntohs(READ_ONCE(q->stat->cidx));
2012 	int reclaim = hw_cidx - q->cidx;
2013 
2014 	if (reclaim < 0)
2015 		reclaim += q->size;
2016 
2017 	q->in_use -= reclaim;
2018 	q->cidx = hw_cidx;
2019 }
2020 
2021 static inline void eosw_txq_advance_index(u32 *idx, u32 n, u32 max)
2022 {
2023 	u32 val = *idx + n;
2024 
2025 	if (val >= max)
2026 		val -= max;
2027 
2028 	*idx = val;
2029 }
2030 
2031 void cxgb4_eosw_txq_free_desc(struct adapter *adap,
2032 			      struct sge_eosw_txq *eosw_txq, u32 ndesc)
2033 {
2034 	struct tx_sw_desc *d;
2035 
2036 	d = &eosw_txq->desc[eosw_txq->last_cidx];
2037 	while (ndesc--) {
2038 		if (d->skb) {
2039 			if (d->addr[0]) {
2040 				unmap_skb(adap->pdev_dev, d->skb, d->addr);
2041 				memset(d->addr, 0, sizeof(d->addr));
2042 			}
2043 			dev_consume_skb_any(d->skb);
2044 			d->skb = NULL;
2045 		}
2046 		eosw_txq_advance_index(&eosw_txq->last_cidx, 1,
2047 				       eosw_txq->ndesc);
2048 		d = &eosw_txq->desc[eosw_txq->last_cidx];
2049 	}
2050 }
2051 
2052 static inline void eosw_txq_advance(struct sge_eosw_txq *eosw_txq, u32 n)
2053 {
2054 	eosw_txq_advance_index(&eosw_txq->pidx, n, eosw_txq->ndesc);
2055 	eosw_txq->inuse += n;
2056 }
2057 
2058 static inline int eosw_txq_enqueue(struct sge_eosw_txq *eosw_txq,
2059 				   struct sk_buff *skb)
2060 {
2061 	if (eosw_txq->inuse == eosw_txq->ndesc)
2062 		return -ENOMEM;
2063 
2064 	eosw_txq->desc[eosw_txq->pidx].skb = skb;
2065 	return 0;
2066 }
2067 
2068 static inline struct sk_buff *eosw_txq_peek(struct sge_eosw_txq *eosw_txq)
2069 {
2070 	return eosw_txq->desc[eosw_txq->last_pidx].skb;
2071 }
2072 
2073 static inline u8 ethofld_calc_tx_flits(struct adapter *adap,
2074 				       struct sk_buff *skb, u32 hdr_len)
2075 {
2076 	u8 flits, nsgl = 0;
2077 	u32 wrlen;
2078 
2079 	wrlen = sizeof(struct fw_eth_tx_eo_wr) + sizeof(struct cpl_tx_pkt_core);
2080 	if (skb_shinfo(skb)->gso_size &&
2081 	    !(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4))
2082 		wrlen += sizeof(struct cpl_tx_pkt_lso_core);
2083 
2084 	wrlen += roundup(hdr_len, 16);
2085 
2086 	/* Packet headers + WR + CPLs */
2087 	flits = DIV_ROUND_UP(wrlen, 8);
2088 
2089 	if (skb_shinfo(skb)->nr_frags > 0) {
2090 		if (skb_headlen(skb) - hdr_len)
2091 			nsgl = sgl_len(skb_shinfo(skb)->nr_frags + 1);
2092 		else
2093 			nsgl = sgl_len(skb_shinfo(skb)->nr_frags);
2094 	} else if (skb->len - hdr_len) {
2095 		nsgl = sgl_len(1);
2096 	}
2097 
2098 	return flits + nsgl;
2099 }
2100 
2101 static inline void *write_eo_wr(struct adapter *adap,
2102 				struct sge_eosw_txq *eosw_txq,
2103 				struct sk_buff *skb, struct fw_eth_tx_eo_wr *wr,
2104 				u32 hdr_len, u32 wrlen)
2105 {
2106 	const struct skb_shared_info *ssi = skb_shinfo(skb);
2107 	struct cpl_tx_pkt_core *cpl;
2108 	u32 immd_len, wrlen16;
2109 	bool compl = false;
2110 	u8 ver, proto;
2111 
2112 	ver = ip_hdr(skb)->version;
2113 	proto = (ver == 6) ? ipv6_hdr(skb)->nexthdr : ip_hdr(skb)->protocol;
2114 
2115 	wrlen16 = DIV_ROUND_UP(wrlen, 16);
2116 	immd_len = sizeof(struct cpl_tx_pkt_core);
2117 	if (skb_shinfo(skb)->gso_size &&
2118 	    !(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4))
2119 		immd_len += sizeof(struct cpl_tx_pkt_lso_core);
2120 	immd_len += hdr_len;
2121 
2122 	if (!eosw_txq->ncompl ||
2123 	    eosw_txq->last_compl >= adap->params.ofldq_wr_cred / 2) {
2124 		compl = true;
2125 		eosw_txq->ncompl++;
2126 		eosw_txq->last_compl = 0;
2127 	}
2128 
2129 	wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_EO_WR) |
2130 				     FW_ETH_TX_EO_WR_IMMDLEN_V(immd_len) |
2131 				     FW_WR_COMPL_V(compl));
2132 	wr->equiq_to_len16 = cpu_to_be32(FW_WR_LEN16_V(wrlen16) |
2133 					 FW_WR_FLOWID_V(eosw_txq->hwtid));
2134 	wr->r3 = 0;
2135 	if (proto == IPPROTO_UDP) {
2136 		cpl = write_eo_udp_wr(skb, wr, hdr_len);
2137 	} else {
2138 		wr->u.tcpseg.type = FW_ETH_TX_EO_TYPE_TCPSEG;
2139 		wr->u.tcpseg.ethlen = skb_network_offset(skb);
2140 		wr->u.tcpseg.iplen = cpu_to_be16(skb_network_header_len(skb));
2141 		wr->u.tcpseg.tcplen = tcp_hdrlen(skb);
2142 		wr->u.tcpseg.tsclk_tsoff = 0;
2143 		wr->u.tcpseg.r4 = 0;
2144 		wr->u.tcpseg.r5 = 0;
2145 		wr->u.tcpseg.plen = cpu_to_be32(skb->len - hdr_len);
2146 
2147 		if (ssi->gso_size) {
2148 			struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
2149 
2150 			wr->u.tcpseg.mss = cpu_to_be16(ssi->gso_size);
2151 			cpl = write_tso_wr(adap, skb, lso);
2152 		} else {
2153 			wr->u.tcpseg.mss = cpu_to_be16(0xffff);
2154 			cpl = (void *)(wr + 1);
2155 		}
2156 	}
2157 
2158 	eosw_txq->cred -= wrlen16;
2159 	eosw_txq->last_compl += wrlen16;
2160 	return cpl;
2161 }
2162 
2163 static void ethofld_hard_xmit(struct net_device *dev,
2164 			      struct sge_eosw_txq *eosw_txq)
2165 {
2166 	struct port_info *pi = netdev2pinfo(dev);
2167 	struct adapter *adap = netdev2adap(dev);
2168 	u32 wrlen, wrlen16, hdr_len, data_len;
2169 	enum sge_eosw_state next_state;
2170 	u64 cntrl, *start, *end, *sgl;
2171 	struct sge_eohw_txq *eohw_txq;
2172 	struct cpl_tx_pkt_core *cpl;
2173 	struct fw_eth_tx_eo_wr *wr;
2174 	bool skip_eotx_wr = false;
2175 	struct tx_sw_desc *d;
2176 	struct sk_buff *skb;
2177 	u8 flits, ndesc;
2178 	int left;
2179 
2180 	eohw_txq = &adap->sge.eohw_txq[eosw_txq->hwqid];
2181 	spin_lock(&eohw_txq->lock);
2182 	reclaim_completed_tx_imm(&eohw_txq->q);
2183 
2184 	d = &eosw_txq->desc[eosw_txq->last_pidx];
2185 	skb = d->skb;
2186 	skb_tx_timestamp(skb);
2187 
2188 	wr = (struct fw_eth_tx_eo_wr *)&eohw_txq->q.desc[eohw_txq->q.pidx];
2189 	if (unlikely(eosw_txq->state != CXGB4_EO_STATE_ACTIVE &&
2190 		     eosw_txq->last_pidx == eosw_txq->flowc_idx)) {
2191 		hdr_len = skb->len;
2192 		data_len = 0;
2193 		flits = DIV_ROUND_UP(hdr_len, 8);
2194 		if (eosw_txq->state == CXGB4_EO_STATE_FLOWC_OPEN_SEND)
2195 			next_state = CXGB4_EO_STATE_FLOWC_OPEN_REPLY;
2196 		else
2197 			next_state = CXGB4_EO_STATE_FLOWC_CLOSE_REPLY;
2198 		skip_eotx_wr = true;
2199 	} else {
2200 		hdr_len = eth_get_headlen(dev, skb->data, skb_headlen(skb));
2201 		data_len = skb->len - hdr_len;
2202 		flits = ethofld_calc_tx_flits(adap, skb, hdr_len);
2203 	}
2204 	ndesc = flits_to_desc(flits);
2205 	wrlen = flits * 8;
2206 	wrlen16 = DIV_ROUND_UP(wrlen, 16);
2207 
2208 	/* If there are no CPL credits, then wait for credits
2209 	 * to come back and retry again
2210 	 */
2211 	if (unlikely(wrlen16 > eosw_txq->cred))
2212 		goto out_unlock;
2213 
2214 	if (unlikely(skip_eotx_wr)) {
2215 		start = (u64 *)wr;
2216 		eosw_txq->state = next_state;
2217 		goto write_wr_headers;
2218 	}
2219 
2220 	cpl = write_eo_wr(adap, eosw_txq, skb, wr, hdr_len, wrlen);
2221 	cntrl = hwcsum(adap->params.chip, skb);
2222 	if (skb_vlan_tag_present(skb))
2223 		cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
2224 
2225 	cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE_V(CPL_TX_PKT_XT) |
2226 				 TXPKT_INTF_V(pi->tx_chan) |
2227 				 TXPKT_PF_V(adap->pf));
2228 	cpl->pack = 0;
2229 	cpl->len = cpu_to_be16(skb->len);
2230 	cpl->ctrl1 = cpu_to_be64(cntrl);
2231 
2232 	start = (u64 *)(cpl + 1);
2233 
2234 write_wr_headers:
2235 	sgl = (u64 *)inline_tx_skb_header(skb, &eohw_txq->q, (void *)start,
2236 					  hdr_len);
2237 	if (data_len) {
2238 		if (unlikely(cxgb4_map_skb(adap->pdev_dev, skb, d->addr))) {
2239 			memset(d->addr, 0, sizeof(d->addr));
2240 			eohw_txq->mapping_err++;
2241 			goto out_unlock;
2242 		}
2243 
2244 		end = (u64 *)wr + flits;
2245 		if (unlikely(start > sgl)) {
2246 			left = (u8 *)end - (u8 *)eohw_txq->q.stat;
2247 			end = (void *)eohw_txq->q.desc + left;
2248 		}
2249 
2250 		if (unlikely((u8 *)sgl >= (u8 *)eohw_txq->q.stat)) {
2251 			/* If current position is already at the end of the
2252 			 * txq, reset the current to point to start of the queue
2253 			 * and update the end ptr as well.
2254 			 */
2255 			left = (u8 *)end - (u8 *)eohw_txq->q.stat;
2256 
2257 			end = (void *)eohw_txq->q.desc + left;
2258 			sgl = (void *)eohw_txq->q.desc;
2259 		}
2260 
2261 		cxgb4_write_sgl(skb, &eohw_txq->q, (void *)sgl, end, hdr_len,
2262 				d->addr);
2263 	}
2264 
2265 	if (skb_shinfo(skb)->gso_size) {
2266 		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
2267 			eohw_txq->uso++;
2268 		else
2269 			eohw_txq->tso++;
2270 		eohw_txq->tx_cso += skb_shinfo(skb)->gso_segs;
2271 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2272 		eohw_txq->tx_cso++;
2273 	}
2274 
2275 	if (skb_vlan_tag_present(skb))
2276 		eohw_txq->vlan_ins++;
2277 
2278 	txq_advance(&eohw_txq->q, ndesc);
2279 	cxgb4_ring_tx_db(adap, &eohw_txq->q, ndesc);
2280 	eosw_txq_advance_index(&eosw_txq->last_pidx, 1, eosw_txq->ndesc);
2281 
2282 out_unlock:
2283 	spin_unlock(&eohw_txq->lock);
2284 }
2285 
2286 static void ethofld_xmit(struct net_device *dev, struct sge_eosw_txq *eosw_txq)
2287 {
2288 	struct sk_buff *skb;
2289 	int pktcount;
2290 
2291 	switch (eosw_txq->state) {
2292 	case CXGB4_EO_STATE_ACTIVE:
2293 	case CXGB4_EO_STATE_FLOWC_OPEN_SEND:
2294 	case CXGB4_EO_STATE_FLOWC_CLOSE_SEND:
2295 		pktcount = eosw_txq->pidx - eosw_txq->last_pidx;
2296 		if (pktcount < 0)
2297 			pktcount += eosw_txq->ndesc;
2298 		break;
2299 	case CXGB4_EO_STATE_FLOWC_OPEN_REPLY:
2300 	case CXGB4_EO_STATE_FLOWC_CLOSE_REPLY:
2301 	case CXGB4_EO_STATE_CLOSED:
2302 	default:
2303 		return;
2304 	}
2305 
2306 	while (pktcount--) {
2307 		skb = eosw_txq_peek(eosw_txq);
2308 		if (!skb) {
2309 			eosw_txq_advance_index(&eosw_txq->last_pidx, 1,
2310 					       eosw_txq->ndesc);
2311 			continue;
2312 		}
2313 
2314 		ethofld_hard_xmit(dev, eosw_txq);
2315 	}
2316 }
2317 
2318 static netdev_tx_t cxgb4_ethofld_xmit(struct sk_buff *skb,
2319 				      struct net_device *dev)
2320 {
2321 	struct cxgb4_tc_port_mqprio *tc_port_mqprio;
2322 	struct port_info *pi = netdev2pinfo(dev);
2323 	struct adapter *adap = netdev2adap(dev);
2324 	struct sge_eosw_txq *eosw_txq;
2325 	u32 qid;
2326 	int ret;
2327 
2328 	ret = cxgb4_validate_skb(skb, dev, ETH_HLEN);
2329 	if (ret)
2330 		goto out_free;
2331 
2332 	tc_port_mqprio = &adap->tc_mqprio->port_mqprio[pi->port_id];
2333 	qid = skb_get_queue_mapping(skb) - pi->nqsets;
2334 	eosw_txq = &tc_port_mqprio->eosw_txq[qid];
2335 	spin_lock_bh(&eosw_txq->lock);
2336 	if (eosw_txq->state != CXGB4_EO_STATE_ACTIVE)
2337 		goto out_unlock;
2338 
2339 	ret = eosw_txq_enqueue(eosw_txq, skb);
2340 	if (ret)
2341 		goto out_unlock;
2342 
2343 	/* SKB is queued for processing until credits are available.
2344 	 * So, call the destructor now and we'll free the skb later
2345 	 * after it has been successfully transmitted.
2346 	 */
2347 	skb_orphan(skb);
2348 
2349 	eosw_txq_advance(eosw_txq, 1);
2350 	ethofld_xmit(dev, eosw_txq);
2351 	spin_unlock_bh(&eosw_txq->lock);
2352 	return NETDEV_TX_OK;
2353 
2354 out_unlock:
2355 	spin_unlock_bh(&eosw_txq->lock);
2356 out_free:
2357 	dev_kfree_skb_any(skb);
2358 	return NETDEV_TX_OK;
2359 }
2360 
2361 netdev_tx_t t4_start_xmit(struct sk_buff *skb, struct net_device *dev)
2362 {
2363 	struct port_info *pi = netdev_priv(dev);
2364 	u16 qid = skb_get_queue_mapping(skb);
2365 
2366 	if (unlikely(pi->eth_flags & PRIV_FLAG_PORT_TX_VM))
2367 		return cxgb4_vf_eth_xmit(skb, dev);
2368 
2369 	if (unlikely(qid >= pi->nqsets))
2370 		return cxgb4_ethofld_xmit(skb, dev);
2371 
2372 	return cxgb4_eth_xmit(skb, dev);
2373 }
2374 
2375 /**
2376  * cxgb4_ethofld_send_flowc - Send ETHOFLD flowc request to bind eotid to tc.
2377  * @dev - netdevice
2378  * @eotid - ETHOFLD tid to bind/unbind
2379  * @tc - traffic class. If set to FW_SCHED_CLS_NONE, then unbinds the @eotid
2380  *
2381  * Send a FLOWC work request to bind an ETHOFLD TID to a traffic class.
2382  * If @tc is set to FW_SCHED_CLS_NONE, then the @eotid is unbound from
2383  * a traffic class.
2384  */
2385 int cxgb4_ethofld_send_flowc(struct net_device *dev, u32 eotid, u32 tc)
2386 {
2387 	struct port_info *pi = netdev2pinfo(dev);
2388 	struct adapter *adap = netdev2adap(dev);
2389 	enum sge_eosw_state next_state;
2390 	struct sge_eosw_txq *eosw_txq;
2391 	u32 len, len16, nparams = 6;
2392 	struct fw_flowc_wr *flowc;
2393 	struct eotid_entry *entry;
2394 	struct sge_ofld_rxq *rxq;
2395 	struct sk_buff *skb;
2396 	int ret = 0;
2397 
2398 	len = sizeof(*flowc) + sizeof(struct fw_flowc_mnemval) * nparams;
2399 	len16 = DIV_ROUND_UP(len, 16);
2400 
2401 	entry = cxgb4_lookup_eotid(&adap->tids, eotid);
2402 	if (!entry)
2403 		return -ENOMEM;
2404 
2405 	eosw_txq = (struct sge_eosw_txq *)entry->data;
2406 	if (!eosw_txq)
2407 		return -ENOMEM;
2408 
2409 	skb = alloc_skb(len, GFP_KERNEL);
2410 	if (!skb)
2411 		return -ENOMEM;
2412 
2413 	spin_lock_bh(&eosw_txq->lock);
2414 	if (tc != FW_SCHED_CLS_NONE) {
2415 		if (eosw_txq->state != CXGB4_EO_STATE_CLOSED)
2416 			goto out_unlock;
2417 
2418 		next_state = CXGB4_EO_STATE_FLOWC_OPEN_SEND;
2419 	} else {
2420 		if (eosw_txq->state != CXGB4_EO_STATE_ACTIVE)
2421 			goto out_unlock;
2422 
2423 		next_state = CXGB4_EO_STATE_FLOWC_CLOSE_SEND;
2424 	}
2425 
2426 	flowc = __skb_put(skb, len);
2427 	memset(flowc, 0, len);
2428 
2429 	rxq = &adap->sge.eohw_rxq[eosw_txq->hwqid];
2430 	flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(len16) |
2431 					  FW_WR_FLOWID_V(eosw_txq->hwtid));
2432 	flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
2433 					   FW_FLOWC_WR_NPARAMS_V(nparams) |
2434 					   FW_WR_COMPL_V(1));
2435 	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
2436 	flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V(adap->pf));
2437 	flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
2438 	flowc->mnemval[1].val = cpu_to_be32(pi->tx_chan);
2439 	flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
2440 	flowc->mnemval[2].val = cpu_to_be32(pi->tx_chan);
2441 	flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
2442 	flowc->mnemval[3].val = cpu_to_be32(rxq->rspq.abs_id);
2443 	flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS;
2444 	flowc->mnemval[4].val = cpu_to_be32(tc);
2445 	flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_EOSTATE;
2446 	flowc->mnemval[5].val = cpu_to_be32(tc == FW_SCHED_CLS_NONE ?
2447 					    FW_FLOWC_MNEM_EOSTATE_CLOSING :
2448 					    FW_FLOWC_MNEM_EOSTATE_ESTABLISHED);
2449 
2450 	eosw_txq->cred -= len16;
2451 	eosw_txq->ncompl++;
2452 	eosw_txq->last_compl = 0;
2453 
2454 	ret = eosw_txq_enqueue(eosw_txq, skb);
2455 	if (ret) {
2456 		dev_consume_skb_any(skb);
2457 		goto out_unlock;
2458 	}
2459 
2460 	eosw_txq->state = next_state;
2461 	eosw_txq->flowc_idx = eosw_txq->pidx;
2462 	eosw_txq_advance(eosw_txq, 1);
2463 	ethofld_xmit(dev, eosw_txq);
2464 
2465 out_unlock:
2466 	spin_unlock_bh(&eosw_txq->lock);
2467 	return ret;
2468 }
2469 
2470 /**
2471  *	is_imm - check whether a packet can be sent as immediate data
2472  *	@skb: the packet
2473  *
2474  *	Returns true if a packet can be sent as a WR with immediate data.
2475  */
2476 static inline int is_imm(const struct sk_buff *skb)
2477 {
2478 	return skb->len <= MAX_CTRL_WR_LEN;
2479 }
2480 
2481 /**
2482  *	ctrlq_check_stop - check if a control queue is full and should stop
2483  *	@q: the queue
2484  *	@wr: most recent WR written to the queue
2485  *
2486  *	Check if a control queue has become full and should be stopped.
2487  *	We clean up control queue descriptors very lazily, only when we are out.
2488  *	If the queue is still full after reclaiming any completed descriptors
2489  *	we suspend it and have the last WR wake it up.
2490  */
2491 static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
2492 {
2493 	reclaim_completed_tx_imm(&q->q);
2494 	if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
2495 		wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
2496 		q->q.stops++;
2497 		q->full = 1;
2498 	}
2499 }
2500 
2501 /**
2502  *	ctrl_xmit - send a packet through an SGE control Tx queue
2503  *	@q: the control queue
2504  *	@skb: the packet
2505  *
2506  *	Send a packet through an SGE control Tx queue.  Packets sent through
2507  *	a control queue must fit entirely as immediate data.
2508  */
2509 static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
2510 {
2511 	unsigned int ndesc;
2512 	struct fw_wr_hdr *wr;
2513 
2514 	if (unlikely(!is_imm(skb))) {
2515 		WARN_ON(1);
2516 		dev_kfree_skb(skb);
2517 		return NET_XMIT_DROP;
2518 	}
2519 
2520 	ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
2521 	spin_lock(&q->sendq.lock);
2522 
2523 	if (unlikely(q->full)) {
2524 		skb->priority = ndesc;                  /* save for restart */
2525 		__skb_queue_tail(&q->sendq, skb);
2526 		spin_unlock(&q->sendq.lock);
2527 		return NET_XMIT_CN;
2528 	}
2529 
2530 	wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
2531 	cxgb4_inline_tx_skb(skb, &q->q, wr);
2532 
2533 	txq_advance(&q->q, ndesc);
2534 	if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
2535 		ctrlq_check_stop(q, wr);
2536 
2537 	cxgb4_ring_tx_db(q->adap, &q->q, ndesc);
2538 	spin_unlock(&q->sendq.lock);
2539 
2540 	kfree_skb(skb);
2541 	return NET_XMIT_SUCCESS;
2542 }
2543 
2544 /**
2545  *	restart_ctrlq - restart a suspended control queue
2546  *	@data: the control queue to restart
2547  *
2548  *	Resumes transmission on a suspended Tx control queue.
2549  */
2550 static void restart_ctrlq(unsigned long data)
2551 {
2552 	struct sk_buff *skb;
2553 	unsigned int written = 0;
2554 	struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
2555 
2556 	spin_lock(&q->sendq.lock);
2557 	reclaim_completed_tx_imm(&q->q);
2558 	BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES);  /* q should be empty */
2559 
2560 	while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
2561 		struct fw_wr_hdr *wr;
2562 		unsigned int ndesc = skb->priority;     /* previously saved */
2563 
2564 		written += ndesc;
2565 		/* Write descriptors and free skbs outside the lock to limit
2566 		 * wait times.  q->full is still set so new skbs will be queued.
2567 		 */
2568 		wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
2569 		txq_advance(&q->q, ndesc);
2570 		spin_unlock(&q->sendq.lock);
2571 
2572 		cxgb4_inline_tx_skb(skb, &q->q, wr);
2573 		kfree_skb(skb);
2574 
2575 		if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
2576 			unsigned long old = q->q.stops;
2577 
2578 			ctrlq_check_stop(q, wr);
2579 			if (q->q.stops != old) {          /* suspended anew */
2580 				spin_lock(&q->sendq.lock);
2581 				goto ringdb;
2582 			}
2583 		}
2584 		if (written > 16) {
2585 			cxgb4_ring_tx_db(q->adap, &q->q, written);
2586 			written = 0;
2587 		}
2588 		spin_lock(&q->sendq.lock);
2589 	}
2590 	q->full = 0;
2591 ringdb:
2592 	if (written)
2593 		cxgb4_ring_tx_db(q->adap, &q->q, written);
2594 	spin_unlock(&q->sendq.lock);
2595 }
2596 
2597 /**
2598  *	t4_mgmt_tx - send a management message
2599  *	@adap: the adapter
2600  *	@skb: the packet containing the management message
2601  *
2602  *	Send a management message through control queue 0.
2603  */
2604 int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
2605 {
2606 	int ret;
2607 
2608 	local_bh_disable();
2609 	ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
2610 	local_bh_enable();
2611 	return ret;
2612 }
2613 
2614 /**
2615  *	is_ofld_imm - check whether a packet can be sent as immediate data
2616  *	@skb: the packet
2617  *
2618  *	Returns true if a packet can be sent as an offload WR with immediate
2619  *	data.  We currently use the same limit as for Ethernet packets.
2620  */
2621 static inline int is_ofld_imm(const struct sk_buff *skb)
2622 {
2623 	struct work_request_hdr *req = (struct work_request_hdr *)skb->data;
2624 	unsigned long opcode = FW_WR_OP_G(ntohl(req->wr_hi));
2625 
2626 	if (opcode == FW_CRYPTO_LOOKASIDE_WR)
2627 		return skb->len <= SGE_MAX_WR_LEN;
2628 	else
2629 		return skb->len <= MAX_IMM_TX_PKT_LEN;
2630 }
2631 
2632 /**
2633  *	calc_tx_flits_ofld - calculate # of flits for an offload packet
2634  *	@skb: the packet
2635  *
2636  *	Returns the number of flits needed for the given offload packet.
2637  *	These packets are already fully constructed and no additional headers
2638  *	will be added.
2639  */
2640 static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
2641 {
2642 	unsigned int flits, cnt;
2643 
2644 	if (is_ofld_imm(skb))
2645 		return DIV_ROUND_UP(skb->len, 8);
2646 
2647 	flits = skb_transport_offset(skb) / 8U;   /* headers */
2648 	cnt = skb_shinfo(skb)->nr_frags;
2649 	if (skb_tail_pointer(skb) != skb_transport_header(skb))
2650 		cnt++;
2651 	return flits + sgl_len(cnt);
2652 }
2653 
2654 /**
2655  *	txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
2656  *	@adap: the adapter
2657  *	@q: the queue to stop
2658  *
2659  *	Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
2660  *	inability to map packets.  A periodic timer attempts to restart
2661  *	queues so marked.
2662  */
2663 static void txq_stop_maperr(struct sge_uld_txq *q)
2664 {
2665 	q->mapping_err++;
2666 	q->q.stops++;
2667 	set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
2668 		q->adap->sge.txq_maperr);
2669 }
2670 
2671 /**
2672  *	ofldtxq_stop - stop an offload Tx queue that has become full
2673  *	@q: the queue to stop
2674  *	@wr: the Work Request causing the queue to become full
2675  *
2676  *	Stops an offload Tx queue that has become full and modifies the packet
2677  *	being written to request a wakeup.
2678  */
2679 static void ofldtxq_stop(struct sge_uld_txq *q, struct fw_wr_hdr *wr)
2680 {
2681 	wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
2682 	q->q.stops++;
2683 	q->full = 1;
2684 }
2685 
2686 /**
2687  *	service_ofldq - service/restart a suspended offload queue
2688  *	@q: the offload queue
2689  *
2690  *	Services an offload Tx queue by moving packets from its Pending Send
2691  *	Queue to the Hardware TX ring.  The function starts and ends with the
2692  *	Send Queue locked, but drops the lock while putting the skb at the
2693  *	head of the Send Queue onto the Hardware TX Ring.  Dropping the lock
2694  *	allows more skbs to be added to the Send Queue by other threads.
2695  *	The packet being processed at the head of the Pending Send Queue is
2696  *	left on the queue in case we experience DMA Mapping errors, etc.
2697  *	and need to give up and restart later.
2698  *
2699  *	service_ofldq() can be thought of as a task which opportunistically
2700  *	uses other threads execution contexts.  We use the Offload Queue
2701  *	boolean "service_ofldq_running" to make sure that only one instance
2702  *	is ever running at a time ...
2703  */
2704 static void service_ofldq(struct sge_uld_txq *q)
2705 {
2706 	u64 *pos, *before, *end;
2707 	int credits;
2708 	struct sk_buff *skb;
2709 	struct sge_txq *txq;
2710 	unsigned int left;
2711 	unsigned int written = 0;
2712 	unsigned int flits, ndesc;
2713 
2714 	/* If another thread is currently in service_ofldq() processing the
2715 	 * Pending Send Queue then there's nothing to do. Otherwise, flag
2716 	 * that we're doing the work and continue.  Examining/modifying
2717 	 * the Offload Queue boolean "service_ofldq_running" must be done
2718 	 * while holding the Pending Send Queue Lock.
2719 	 */
2720 	if (q->service_ofldq_running)
2721 		return;
2722 	q->service_ofldq_running = true;
2723 
2724 	while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
2725 		/* We drop the lock while we're working with the skb at the
2726 		 * head of the Pending Send Queue.  This allows more skbs to
2727 		 * be added to the Pending Send Queue while we're working on
2728 		 * this one.  We don't need to lock to guard the TX Ring
2729 		 * updates because only one thread of execution is ever
2730 		 * allowed into service_ofldq() at a time.
2731 		 */
2732 		spin_unlock(&q->sendq.lock);
2733 
2734 		cxgb4_reclaim_completed_tx(q->adap, &q->q, false);
2735 
2736 		flits = skb->priority;                /* previously saved */
2737 		ndesc = flits_to_desc(flits);
2738 		credits = txq_avail(&q->q) - ndesc;
2739 		BUG_ON(credits < 0);
2740 		if (unlikely(credits < TXQ_STOP_THRES))
2741 			ofldtxq_stop(q, (struct fw_wr_hdr *)skb->data);
2742 
2743 		pos = (u64 *)&q->q.desc[q->q.pidx];
2744 		if (is_ofld_imm(skb))
2745 			cxgb4_inline_tx_skb(skb, &q->q, pos);
2746 		else if (cxgb4_map_skb(q->adap->pdev_dev, skb,
2747 				       (dma_addr_t *)skb->head)) {
2748 			txq_stop_maperr(q);
2749 			spin_lock(&q->sendq.lock);
2750 			break;
2751 		} else {
2752 			int last_desc, hdr_len = skb_transport_offset(skb);
2753 
2754 			/* The WR headers  may not fit within one descriptor.
2755 			 * So we need to deal with wrap-around here.
2756 			 */
2757 			before = (u64 *)pos;
2758 			end = (u64 *)pos + flits;
2759 			txq = &q->q;
2760 			pos = (void *)inline_tx_skb_header(skb, &q->q,
2761 							   (void *)pos,
2762 							   hdr_len);
2763 			if (before > (u64 *)pos) {
2764 				left = (u8 *)end - (u8 *)txq->stat;
2765 				end = (void *)txq->desc + left;
2766 			}
2767 
2768 			/* If current position is already at the end of the
2769 			 * ofld queue, reset the current to point to
2770 			 * start of the queue and update the end ptr as well.
2771 			 */
2772 			if (pos == (u64 *)txq->stat) {
2773 				left = (u8 *)end - (u8 *)txq->stat;
2774 				end = (void *)txq->desc + left;
2775 				pos = (void *)txq->desc;
2776 			}
2777 
2778 			cxgb4_write_sgl(skb, &q->q, (void *)pos,
2779 					end, hdr_len,
2780 					(dma_addr_t *)skb->head);
2781 #ifdef CONFIG_NEED_DMA_MAP_STATE
2782 			skb->dev = q->adap->port[0];
2783 			skb->destructor = deferred_unmap_destructor;
2784 #endif
2785 			last_desc = q->q.pidx + ndesc - 1;
2786 			if (last_desc >= q->q.size)
2787 				last_desc -= q->q.size;
2788 			q->q.sdesc[last_desc].skb = skb;
2789 		}
2790 
2791 		txq_advance(&q->q, ndesc);
2792 		written += ndesc;
2793 		if (unlikely(written > 32)) {
2794 			cxgb4_ring_tx_db(q->adap, &q->q, written);
2795 			written = 0;
2796 		}
2797 
2798 		/* Reacquire the Pending Send Queue Lock so we can unlink the
2799 		 * skb we've just successfully transferred to the TX Ring and
2800 		 * loop for the next skb which may be at the head of the
2801 		 * Pending Send Queue.
2802 		 */
2803 		spin_lock(&q->sendq.lock);
2804 		__skb_unlink(skb, &q->sendq);
2805 		if (is_ofld_imm(skb))
2806 			kfree_skb(skb);
2807 	}
2808 	if (likely(written))
2809 		cxgb4_ring_tx_db(q->adap, &q->q, written);
2810 
2811 	/*Indicate that no thread is processing the Pending Send Queue
2812 	 * currently.
2813 	 */
2814 	q->service_ofldq_running = false;
2815 }
2816 
2817 /**
2818  *	ofld_xmit - send a packet through an offload queue
2819  *	@q: the Tx offload queue
2820  *	@skb: the packet
2821  *
2822  *	Send an offload packet through an SGE offload queue.
2823  */
2824 static int ofld_xmit(struct sge_uld_txq *q, struct sk_buff *skb)
2825 {
2826 	skb->priority = calc_tx_flits_ofld(skb);       /* save for restart */
2827 	spin_lock(&q->sendq.lock);
2828 
2829 	/* Queue the new skb onto the Offload Queue's Pending Send Queue.  If
2830 	 * that results in this new skb being the only one on the queue, start
2831 	 * servicing it.  If there are other skbs already on the list, then
2832 	 * either the queue is currently being processed or it's been stopped
2833 	 * for some reason and it'll be restarted at a later time.  Restart
2834 	 * paths are triggered by events like experiencing a DMA Mapping Error
2835 	 * or filling the Hardware TX Ring.
2836 	 */
2837 	__skb_queue_tail(&q->sendq, skb);
2838 	if (q->sendq.qlen == 1)
2839 		service_ofldq(q);
2840 
2841 	spin_unlock(&q->sendq.lock);
2842 	return NET_XMIT_SUCCESS;
2843 }
2844 
2845 /**
2846  *	restart_ofldq - restart a suspended offload queue
2847  *	@data: the offload queue to restart
2848  *
2849  *	Resumes transmission on a suspended Tx offload queue.
2850  */
2851 static void restart_ofldq(unsigned long data)
2852 {
2853 	struct sge_uld_txq *q = (struct sge_uld_txq *)data;
2854 
2855 	spin_lock(&q->sendq.lock);
2856 	q->full = 0;            /* the queue actually is completely empty now */
2857 	service_ofldq(q);
2858 	spin_unlock(&q->sendq.lock);
2859 }
2860 
2861 /**
2862  *	skb_txq - return the Tx queue an offload packet should use
2863  *	@skb: the packet
2864  *
2865  *	Returns the Tx queue an offload packet should use as indicated by bits
2866  *	1-15 in the packet's queue_mapping.
2867  */
2868 static inline unsigned int skb_txq(const struct sk_buff *skb)
2869 {
2870 	return skb->queue_mapping >> 1;
2871 }
2872 
2873 /**
2874  *	is_ctrl_pkt - return whether an offload packet is a control packet
2875  *	@skb: the packet
2876  *
2877  *	Returns whether an offload packet should use an OFLD or a CTRL
2878  *	Tx queue as indicated by bit 0 in the packet's queue_mapping.
2879  */
2880 static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
2881 {
2882 	return skb->queue_mapping & 1;
2883 }
2884 
2885 static inline int uld_send(struct adapter *adap, struct sk_buff *skb,
2886 			   unsigned int tx_uld_type)
2887 {
2888 	struct sge_uld_txq_info *txq_info;
2889 	struct sge_uld_txq *txq;
2890 	unsigned int idx = skb_txq(skb);
2891 
2892 	if (unlikely(is_ctrl_pkt(skb))) {
2893 		/* Single ctrl queue is a requirement for LE workaround path */
2894 		if (adap->tids.nsftids)
2895 			idx = 0;
2896 		return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
2897 	}
2898 
2899 	txq_info = adap->sge.uld_txq_info[tx_uld_type];
2900 	if (unlikely(!txq_info)) {
2901 		WARN_ON(true);
2902 		return NET_XMIT_DROP;
2903 	}
2904 
2905 	txq = &txq_info->uldtxq[idx];
2906 	return ofld_xmit(txq, skb);
2907 }
2908 
2909 /**
2910  *	t4_ofld_send - send an offload packet
2911  *	@adap: the adapter
2912  *	@skb: the packet
2913  *
2914  *	Sends an offload packet.  We use the packet queue_mapping to select the
2915  *	appropriate Tx queue as follows: bit 0 indicates whether the packet
2916  *	should be sent as regular or control, bits 1-15 select the queue.
2917  */
2918 int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
2919 {
2920 	int ret;
2921 
2922 	local_bh_disable();
2923 	ret = uld_send(adap, skb, CXGB4_TX_OFLD);
2924 	local_bh_enable();
2925 	return ret;
2926 }
2927 
2928 /**
2929  *	cxgb4_ofld_send - send an offload packet
2930  *	@dev: the net device
2931  *	@skb: the packet
2932  *
2933  *	Sends an offload packet.  This is an exported version of @t4_ofld_send,
2934  *	intended for ULDs.
2935  */
2936 int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
2937 {
2938 	return t4_ofld_send(netdev2adap(dev), skb);
2939 }
2940 EXPORT_SYMBOL(cxgb4_ofld_send);
2941 
2942 static void *inline_tx_header(const void *src,
2943 			      const struct sge_txq *q,
2944 			      void *pos, int length)
2945 {
2946 	int left = (void *)q->stat - pos;
2947 	u64 *p;
2948 
2949 	if (likely(length <= left)) {
2950 		memcpy(pos, src, length);
2951 		pos += length;
2952 	} else {
2953 		memcpy(pos, src, left);
2954 		memcpy(q->desc, src + left, length - left);
2955 		pos = (void *)q->desc + (length - left);
2956 	}
2957 	/* 0-pad to multiple of 16 */
2958 	p = PTR_ALIGN(pos, 8);
2959 	if ((uintptr_t)p & 8) {
2960 		*p = 0;
2961 		return p + 1;
2962 	}
2963 	return p;
2964 }
2965 
2966 /**
2967  *      ofld_xmit_direct - copy a WR into offload queue
2968  *      @q: the Tx offload queue
2969  *      @src: location of WR
2970  *      @len: WR length
2971  *
2972  *      Copy an immediate WR into an uncontended SGE offload queue.
2973  */
2974 static int ofld_xmit_direct(struct sge_uld_txq *q, const void *src,
2975 			    unsigned int len)
2976 {
2977 	unsigned int ndesc;
2978 	int credits;
2979 	u64 *pos;
2980 
2981 	/* Use the lower limit as the cut-off */
2982 	if (len > MAX_IMM_OFLD_TX_DATA_WR_LEN) {
2983 		WARN_ON(1);
2984 		return NET_XMIT_DROP;
2985 	}
2986 
2987 	/* Don't return NET_XMIT_CN here as the current
2988 	 * implementation doesn't queue the request
2989 	 * using an skb when the following conditions not met
2990 	 */
2991 	if (!spin_trylock(&q->sendq.lock))
2992 		return NET_XMIT_DROP;
2993 
2994 	if (q->full || !skb_queue_empty(&q->sendq) ||
2995 	    q->service_ofldq_running) {
2996 		spin_unlock(&q->sendq.lock);
2997 		return NET_XMIT_DROP;
2998 	}
2999 	ndesc = flits_to_desc(DIV_ROUND_UP(len, 8));
3000 	credits = txq_avail(&q->q) - ndesc;
3001 	pos = (u64 *)&q->q.desc[q->q.pidx];
3002 
3003 	/* ofldtxq_stop modifies WR header in-situ */
3004 	inline_tx_header(src, &q->q, pos, len);
3005 	if (unlikely(credits < TXQ_STOP_THRES))
3006 		ofldtxq_stop(q, (struct fw_wr_hdr *)pos);
3007 	txq_advance(&q->q, ndesc);
3008 	cxgb4_ring_tx_db(q->adap, &q->q, ndesc);
3009 
3010 	spin_unlock(&q->sendq.lock);
3011 	return NET_XMIT_SUCCESS;
3012 }
3013 
3014 int cxgb4_immdata_send(struct net_device *dev, unsigned int idx,
3015 		       const void *src, unsigned int len)
3016 {
3017 	struct sge_uld_txq_info *txq_info;
3018 	struct sge_uld_txq *txq;
3019 	struct adapter *adap;
3020 	int ret;
3021 
3022 	adap = netdev2adap(dev);
3023 
3024 	local_bh_disable();
3025 	txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
3026 	if (unlikely(!txq_info)) {
3027 		WARN_ON(true);
3028 		local_bh_enable();
3029 		return NET_XMIT_DROP;
3030 	}
3031 	txq = &txq_info->uldtxq[idx];
3032 
3033 	ret = ofld_xmit_direct(txq, src, len);
3034 	local_bh_enable();
3035 	return net_xmit_eval(ret);
3036 }
3037 EXPORT_SYMBOL(cxgb4_immdata_send);
3038 
3039 /**
3040  *	t4_crypto_send - send crypto packet
3041  *	@adap: the adapter
3042  *	@skb: the packet
3043  *
3044  *	Sends crypto packet.  We use the packet queue_mapping to select the
3045  *	appropriate Tx queue as follows: bit 0 indicates whether the packet
3046  *	should be sent as regular or control, bits 1-15 select the queue.
3047  */
3048 static int t4_crypto_send(struct adapter *adap, struct sk_buff *skb)
3049 {
3050 	int ret;
3051 
3052 	local_bh_disable();
3053 	ret = uld_send(adap, skb, CXGB4_TX_CRYPTO);
3054 	local_bh_enable();
3055 	return ret;
3056 }
3057 
3058 /**
3059  *	cxgb4_crypto_send - send crypto packet
3060  *	@dev: the net device
3061  *	@skb: the packet
3062  *
3063  *	Sends crypto packet.  This is an exported version of @t4_crypto_send,
3064  *	intended for ULDs.
3065  */
3066 int cxgb4_crypto_send(struct net_device *dev, struct sk_buff *skb)
3067 {
3068 	return t4_crypto_send(netdev2adap(dev), skb);
3069 }
3070 EXPORT_SYMBOL(cxgb4_crypto_send);
3071 
3072 static inline void copy_frags(struct sk_buff *skb,
3073 			      const struct pkt_gl *gl, unsigned int offset)
3074 {
3075 	int i;
3076 
3077 	/* usually there's just one frag */
3078 	__skb_fill_page_desc(skb, 0, gl->frags[0].page,
3079 			     gl->frags[0].offset + offset,
3080 			     gl->frags[0].size - offset);
3081 	skb_shinfo(skb)->nr_frags = gl->nfrags;
3082 	for (i = 1; i < gl->nfrags; i++)
3083 		__skb_fill_page_desc(skb, i, gl->frags[i].page,
3084 				     gl->frags[i].offset,
3085 				     gl->frags[i].size);
3086 
3087 	/* get a reference to the last page, we don't own it */
3088 	get_page(gl->frags[gl->nfrags - 1].page);
3089 }
3090 
3091 /**
3092  *	cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
3093  *	@gl: the gather list
3094  *	@skb_len: size of sk_buff main body if it carries fragments
3095  *	@pull_len: amount of data to move to the sk_buff's main body
3096  *
3097  *	Builds an sk_buff from the given packet gather list.  Returns the
3098  *	sk_buff or %NULL if sk_buff allocation failed.
3099  */
3100 struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
3101 				   unsigned int skb_len, unsigned int pull_len)
3102 {
3103 	struct sk_buff *skb;
3104 
3105 	/*
3106 	 * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
3107 	 * size, which is expected since buffers are at least PAGE_SIZEd.
3108 	 * In this case packets up to RX_COPY_THRES have only one fragment.
3109 	 */
3110 	if (gl->tot_len <= RX_COPY_THRES) {
3111 		skb = dev_alloc_skb(gl->tot_len);
3112 		if (unlikely(!skb))
3113 			goto out;
3114 		__skb_put(skb, gl->tot_len);
3115 		skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
3116 	} else {
3117 		skb = dev_alloc_skb(skb_len);
3118 		if (unlikely(!skb))
3119 			goto out;
3120 		__skb_put(skb, pull_len);
3121 		skb_copy_to_linear_data(skb, gl->va, pull_len);
3122 
3123 		copy_frags(skb, gl, pull_len);
3124 		skb->len = gl->tot_len;
3125 		skb->data_len = skb->len - pull_len;
3126 		skb->truesize += skb->data_len;
3127 	}
3128 out:	return skb;
3129 }
3130 EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
3131 
3132 /**
3133  *	t4_pktgl_free - free a packet gather list
3134  *	@gl: the gather list
3135  *
3136  *	Releases the pages of a packet gather list.  We do not own the last
3137  *	page on the list and do not free it.
3138  */
3139 static void t4_pktgl_free(const struct pkt_gl *gl)
3140 {
3141 	int n;
3142 	const struct page_frag *p;
3143 
3144 	for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
3145 		put_page(p->page);
3146 }
3147 
3148 /*
3149  * Process an MPS trace packet.  Give it an unused protocol number so it won't
3150  * be delivered to anyone and send it to the stack for capture.
3151  */
3152 static noinline int handle_trace_pkt(struct adapter *adap,
3153 				     const struct pkt_gl *gl)
3154 {
3155 	struct sk_buff *skb;
3156 
3157 	skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
3158 	if (unlikely(!skb)) {
3159 		t4_pktgl_free(gl);
3160 		return 0;
3161 	}
3162 
3163 	if (is_t4(adap->params.chip))
3164 		__skb_pull(skb, sizeof(struct cpl_trace_pkt));
3165 	else
3166 		__skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
3167 
3168 	skb_reset_mac_header(skb);
3169 	skb->protocol = htons(0xffff);
3170 	skb->dev = adap->port[0];
3171 	netif_receive_skb(skb);
3172 	return 0;
3173 }
3174 
3175 /**
3176  * cxgb4_sgetim_to_hwtstamp - convert sge time stamp to hw time stamp
3177  * @adap: the adapter
3178  * @hwtstamps: time stamp structure to update
3179  * @sgetstamp: 60bit iqe timestamp
3180  *
3181  * Every ingress queue entry has the 60-bit timestamp, convert that timestamp
3182  * which is in Core Clock ticks into ktime_t and assign it
3183  **/
3184 static void cxgb4_sgetim_to_hwtstamp(struct adapter *adap,
3185 				     struct skb_shared_hwtstamps *hwtstamps,
3186 				     u64 sgetstamp)
3187 {
3188 	u64 ns;
3189 	u64 tmp = (sgetstamp * 1000 * 1000 + adap->params.vpd.cclk / 2);
3190 
3191 	ns = div_u64(tmp, adap->params.vpd.cclk);
3192 
3193 	memset(hwtstamps, 0, sizeof(*hwtstamps));
3194 	hwtstamps->hwtstamp = ns_to_ktime(ns);
3195 }
3196 
3197 static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
3198 		   const struct cpl_rx_pkt *pkt, unsigned long tnl_hdr_len)
3199 {
3200 	struct adapter *adapter = rxq->rspq.adap;
3201 	struct sge *s = &adapter->sge;
3202 	struct port_info *pi;
3203 	int ret;
3204 	struct sk_buff *skb;
3205 
3206 	skb = napi_get_frags(&rxq->rspq.napi);
3207 	if (unlikely(!skb)) {
3208 		t4_pktgl_free(gl);
3209 		rxq->stats.rx_drops++;
3210 		return;
3211 	}
3212 
3213 	copy_frags(skb, gl, s->pktshift);
3214 	if (tnl_hdr_len)
3215 		skb->csum_level = 1;
3216 	skb->len = gl->tot_len - s->pktshift;
3217 	skb->data_len = skb->len;
3218 	skb->truesize += skb->data_len;
3219 	skb->ip_summed = CHECKSUM_UNNECESSARY;
3220 	skb_record_rx_queue(skb, rxq->rspq.idx);
3221 	pi = netdev_priv(skb->dev);
3222 	if (pi->rxtstamp)
3223 		cxgb4_sgetim_to_hwtstamp(adapter, skb_hwtstamps(skb),
3224 					 gl->sgetstamp);
3225 	if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
3226 		skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
3227 			     PKT_HASH_TYPE_L3);
3228 
3229 	if (unlikely(pkt->vlan_ex)) {
3230 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
3231 		rxq->stats.vlan_ex++;
3232 	}
3233 	ret = napi_gro_frags(&rxq->rspq.napi);
3234 	if (ret == GRO_HELD)
3235 		rxq->stats.lro_pkts++;
3236 	else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
3237 		rxq->stats.lro_merged++;
3238 	rxq->stats.pkts++;
3239 	rxq->stats.rx_cso++;
3240 }
3241 
3242 enum {
3243 	RX_NON_PTP_PKT = 0,
3244 	RX_PTP_PKT_SUC = 1,
3245 	RX_PTP_PKT_ERR = 2
3246 };
3247 
3248 /**
3249  *     t4_systim_to_hwstamp - read hardware time stamp
3250  *     @adap: the adapter
3251  *     @skb: the packet
3252  *
3253  *     Read Time Stamp from MPS packet and insert in skb which
3254  *     is forwarded to PTP application
3255  */
3256 static noinline int t4_systim_to_hwstamp(struct adapter *adapter,
3257 					 struct sk_buff *skb)
3258 {
3259 	struct skb_shared_hwtstamps *hwtstamps;
3260 	struct cpl_rx_mps_pkt *cpl = NULL;
3261 	unsigned char *data;
3262 	int offset;
3263 
3264 	cpl = (struct cpl_rx_mps_pkt *)skb->data;
3265 	if (!(CPL_RX_MPS_PKT_TYPE_G(ntohl(cpl->op_to_r1_hi)) &
3266 	     X_CPL_RX_MPS_PKT_TYPE_PTP))
3267 		return RX_PTP_PKT_ERR;
3268 
3269 	data = skb->data + sizeof(*cpl);
3270 	skb_pull(skb, 2 * sizeof(u64) + sizeof(struct cpl_rx_mps_pkt));
3271 	offset = ETH_HLEN + IPV4_HLEN(skb->data) + UDP_HLEN;
3272 	if (skb->len < offset + OFF_PTP_SEQUENCE_ID + sizeof(short))
3273 		return RX_PTP_PKT_ERR;
3274 
3275 	hwtstamps = skb_hwtstamps(skb);
3276 	memset(hwtstamps, 0, sizeof(*hwtstamps));
3277 	hwtstamps->hwtstamp = ns_to_ktime(be64_to_cpu(*((u64 *)data)));
3278 
3279 	return RX_PTP_PKT_SUC;
3280 }
3281 
3282 /**
3283  *     t4_rx_hststamp - Recv PTP Event Message
3284  *     @adap: the adapter
3285  *     @rsp: the response queue descriptor holding the RX_PKT message
3286  *     @skb: the packet
3287  *
3288  *     PTP enabled and MPS packet, read HW timestamp
3289  */
3290 static int t4_rx_hststamp(struct adapter *adapter, const __be64 *rsp,
3291 			  struct sge_eth_rxq *rxq, struct sk_buff *skb)
3292 {
3293 	int ret;
3294 
3295 	if (unlikely((*(u8 *)rsp == CPL_RX_MPS_PKT) &&
3296 		     !is_t4(adapter->params.chip))) {
3297 		ret = t4_systim_to_hwstamp(adapter, skb);
3298 		if (ret == RX_PTP_PKT_ERR) {
3299 			kfree_skb(skb);
3300 			rxq->stats.rx_drops++;
3301 		}
3302 		return ret;
3303 	}
3304 	return RX_NON_PTP_PKT;
3305 }
3306 
3307 /**
3308  *      t4_tx_hststamp - Loopback PTP Transmit Event Message
3309  *      @adap: the adapter
3310  *      @skb: the packet
3311  *      @dev: the ingress net device
3312  *
3313  *      Read hardware timestamp for the loopback PTP Tx event message
3314  */
3315 static int t4_tx_hststamp(struct adapter *adapter, struct sk_buff *skb,
3316 			  struct net_device *dev)
3317 {
3318 	struct port_info *pi = netdev_priv(dev);
3319 
3320 	if (!is_t4(adapter->params.chip) && adapter->ptp_tx_skb) {
3321 		cxgb4_ptp_read_hwstamp(adapter, pi);
3322 		kfree_skb(skb);
3323 		return 0;
3324 	}
3325 	return 1;
3326 }
3327 
3328 /**
3329  *	t4_tx_completion_handler - handle CPL_SGE_EGR_UPDATE messages
3330  *	@rspq: Ethernet RX Response Queue associated with Ethernet TX Queue
3331  *	@rsp: Response Entry pointer into Response Queue
3332  *	@gl: Gather List pointer
3333  *
3334  *	For adapters which support the SGE Doorbell Queue Timer facility,
3335  *	we configure the Ethernet TX Queues to send CIDX Updates to the
3336  *	Associated Ethernet RX Response Queue with CPL_SGE_EGR_UPDATE
3337  *	messages.  This adds a small load to PCIe Link RX bandwidth and,
3338  *	potentially, higher CPU Interrupt load, but allows us to respond
3339  *	much more quickly to the CIDX Updates.  This is important for
3340  *	Upper Layer Software which isn't willing to have a large amount
3341  *	of TX Data outstanding before receiving DMA Completions.
3342  */
3343 static void t4_tx_completion_handler(struct sge_rspq *rspq,
3344 				     const __be64 *rsp,
3345 				     const struct pkt_gl *gl)
3346 {
3347 	u8 opcode = ((const struct rss_header *)rsp)->opcode;
3348 	struct port_info *pi = netdev_priv(rspq->netdev);
3349 	struct adapter *adapter = rspq->adap;
3350 	struct sge *s = &adapter->sge;
3351 	struct sge_eth_txq *txq;
3352 
3353 	/* skip RSS header */
3354 	rsp++;
3355 
3356 	/* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
3357 	 */
3358 	if (unlikely(opcode == CPL_FW4_MSG &&
3359 		     ((const struct cpl_fw4_msg *)rsp)->type ==
3360 							FW_TYPE_RSSCPL)) {
3361 		rsp++;
3362 		opcode = ((const struct rss_header *)rsp)->opcode;
3363 		rsp++;
3364 	}
3365 
3366 	if (unlikely(opcode != CPL_SGE_EGR_UPDATE)) {
3367 		pr_info("%s: unexpected FW4/CPL %#x on Rx queue\n",
3368 			__func__, opcode);
3369 		return;
3370 	}
3371 
3372 	txq = &s->ethtxq[pi->first_qset + rspq->idx];
3373 
3374 	/* We've got the Hardware Consumer Index Update in the Egress Update
3375 	 * message.  If we're using the SGE Doorbell Queue Timer mechanism,
3376 	 * these Egress Update messages will be our sole CIDX Updates we get
3377 	 * since we don't want to chew up PCIe bandwidth for both Ingress
3378 	 * Messages and Status Page writes.  However, The code which manages
3379 	 * reclaiming successfully DMA'ed TX Work Requests uses the CIDX value
3380 	 * stored in the Status Page at the end of the TX Queue.  It's easiest
3381 	 * to simply copy the CIDX Update value from the Egress Update message
3382 	 * to the Status Page.  Also note that no Endian issues need to be
3383 	 * considered here since both are Big Endian and we're just copying
3384 	 * bytes consistently ...
3385 	 */
3386 	if (txq->dbqt) {
3387 		struct cpl_sge_egr_update *egr;
3388 
3389 		egr = (struct cpl_sge_egr_update *)rsp;
3390 		WRITE_ONCE(txq->q.stat->cidx, egr->cidx);
3391 	}
3392 
3393 	t4_sge_eth_txq_egress_update(adapter, txq, -1);
3394 }
3395 
3396 /**
3397  *	t4_ethrx_handler - process an ingress ethernet packet
3398  *	@q: the response queue that received the packet
3399  *	@rsp: the response queue descriptor holding the RX_PKT message
3400  *	@si: the gather list of packet fragments
3401  *
3402  *	Process an ingress ethernet packet and deliver it to the stack.
3403  */
3404 int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
3405 		     const struct pkt_gl *si)
3406 {
3407 	bool csum_ok;
3408 	struct sk_buff *skb;
3409 	const struct cpl_rx_pkt *pkt;
3410 	struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
3411 	struct adapter *adapter = q->adap;
3412 	struct sge *s = &q->adap->sge;
3413 	int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
3414 			    CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
3415 	u16 err_vec, tnl_hdr_len = 0;
3416 	struct port_info *pi;
3417 	int ret = 0;
3418 
3419 	/* If we're looking at TX Queue CIDX Update, handle that separately
3420 	 * and return.
3421 	 */
3422 	if (unlikely((*(u8 *)rsp == CPL_FW4_MSG) ||
3423 		     (*(u8 *)rsp == CPL_SGE_EGR_UPDATE))) {
3424 		t4_tx_completion_handler(q, rsp, si);
3425 		return 0;
3426 	}
3427 
3428 	if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
3429 		return handle_trace_pkt(q->adap, si);
3430 
3431 	pkt = (const struct cpl_rx_pkt *)rsp;
3432 	/* Compressed error vector is enabled for T6 only */
3433 	if (q->adap->params.tp.rx_pkt_encap) {
3434 		err_vec = T6_COMPR_RXERR_VEC_G(be16_to_cpu(pkt->err_vec));
3435 		tnl_hdr_len = T6_RX_TNLHDR_LEN_G(ntohs(pkt->err_vec));
3436 	} else {
3437 		err_vec = be16_to_cpu(pkt->err_vec);
3438 	}
3439 
3440 	csum_ok = pkt->csum_calc && !err_vec &&
3441 		  (q->netdev->features & NETIF_F_RXCSUM);
3442 
3443 	if (err_vec)
3444 		rxq->stats.bad_rx_pkts++;
3445 
3446 	if (((pkt->l2info & htonl(RXF_TCP_F)) ||
3447 	     tnl_hdr_len) &&
3448 	    (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
3449 		do_gro(rxq, si, pkt, tnl_hdr_len);
3450 		return 0;
3451 	}
3452 
3453 	skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
3454 	if (unlikely(!skb)) {
3455 		t4_pktgl_free(si);
3456 		rxq->stats.rx_drops++;
3457 		return 0;
3458 	}
3459 	pi = netdev_priv(q->netdev);
3460 
3461 	/* Handle PTP Event Rx packet */
3462 	if (unlikely(pi->ptp_enable)) {
3463 		ret = t4_rx_hststamp(adapter, rsp, rxq, skb);
3464 		if (ret == RX_PTP_PKT_ERR)
3465 			return 0;
3466 	}
3467 	if (likely(!ret))
3468 		__skb_pull(skb, s->pktshift); /* remove ethernet header pad */
3469 
3470 	/* Handle the PTP Event Tx Loopback packet */
3471 	if (unlikely(pi->ptp_enable && !ret &&
3472 		     (pkt->l2info & htonl(RXF_UDP_F)) &&
3473 		     cxgb4_ptp_is_ptp_rx(skb))) {
3474 		if (!t4_tx_hststamp(adapter, skb, q->netdev))
3475 			return 0;
3476 	}
3477 
3478 	skb->protocol = eth_type_trans(skb, q->netdev);
3479 	skb_record_rx_queue(skb, q->idx);
3480 	if (skb->dev->features & NETIF_F_RXHASH)
3481 		skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
3482 			     PKT_HASH_TYPE_L3);
3483 
3484 	rxq->stats.pkts++;
3485 
3486 	if (pi->rxtstamp)
3487 		cxgb4_sgetim_to_hwtstamp(q->adap, skb_hwtstamps(skb),
3488 					 si->sgetstamp);
3489 	if (csum_ok && (pkt->l2info & htonl(RXF_UDP_F | RXF_TCP_F))) {
3490 		if (!pkt->ip_frag) {
3491 			skb->ip_summed = CHECKSUM_UNNECESSARY;
3492 			rxq->stats.rx_cso++;
3493 		} else if (pkt->l2info & htonl(RXF_IP_F)) {
3494 			__sum16 c = (__force __sum16)pkt->csum;
3495 			skb->csum = csum_unfold(c);
3496 
3497 			if (tnl_hdr_len) {
3498 				skb->ip_summed = CHECKSUM_UNNECESSARY;
3499 				skb->csum_level = 1;
3500 			} else {
3501 				skb->ip_summed = CHECKSUM_COMPLETE;
3502 			}
3503 			rxq->stats.rx_cso++;
3504 		}
3505 	} else {
3506 		skb_checksum_none_assert(skb);
3507 #ifdef CONFIG_CHELSIO_T4_FCOE
3508 #define CPL_RX_PKT_FLAGS (RXF_PSH_F | RXF_SYN_F | RXF_UDP_F | \
3509 			  RXF_TCP_F | RXF_IP_F | RXF_IP6_F | RXF_LRO_F)
3510 
3511 		if (!(pkt->l2info & cpu_to_be32(CPL_RX_PKT_FLAGS))) {
3512 			if ((pkt->l2info & cpu_to_be32(RXF_FCOE_F)) &&
3513 			    (pi->fcoe.flags & CXGB_FCOE_ENABLED)) {
3514 				if (q->adap->params.tp.rx_pkt_encap)
3515 					csum_ok = err_vec &
3516 						  T6_COMPR_RXERR_SUM_F;
3517 				else
3518 					csum_ok = err_vec & RXERR_CSUM_F;
3519 				if (!csum_ok)
3520 					skb->ip_summed = CHECKSUM_UNNECESSARY;
3521 			}
3522 		}
3523 
3524 #undef CPL_RX_PKT_FLAGS
3525 #endif /* CONFIG_CHELSIO_T4_FCOE */
3526 	}
3527 
3528 	if (unlikely(pkt->vlan_ex)) {
3529 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
3530 		rxq->stats.vlan_ex++;
3531 	}
3532 	skb_mark_napi_id(skb, &q->napi);
3533 	netif_receive_skb(skb);
3534 	return 0;
3535 }
3536 
3537 /**
3538  *	restore_rx_bufs - put back a packet's Rx buffers
3539  *	@si: the packet gather list
3540  *	@q: the SGE free list
3541  *	@frags: number of FL buffers to restore
3542  *
3543  *	Puts back on an FL the Rx buffers associated with @si.  The buffers
3544  *	have already been unmapped and are left unmapped, we mark them so to
3545  *	prevent further unmapping attempts.
3546  *
3547  *	This function undoes a series of @unmap_rx_buf calls when we find out
3548  *	that the current packet can't be processed right away afterall and we
3549  *	need to come back to it later.  This is a very rare event and there's
3550  *	no effort to make this particularly efficient.
3551  */
3552 static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
3553 			    int frags)
3554 {
3555 	struct rx_sw_desc *d;
3556 
3557 	while (frags--) {
3558 		if (q->cidx == 0)
3559 			q->cidx = q->size - 1;
3560 		else
3561 			q->cidx--;
3562 		d = &q->sdesc[q->cidx];
3563 		d->page = si->frags[frags].page;
3564 		d->dma_addr |= RX_UNMAPPED_BUF;
3565 		q->avail++;
3566 	}
3567 }
3568 
3569 /**
3570  *	is_new_response - check if a response is newly written
3571  *	@r: the response descriptor
3572  *	@q: the response queue
3573  *
3574  *	Returns true if a response descriptor contains a yet unprocessed
3575  *	response.
3576  */
3577 static inline bool is_new_response(const struct rsp_ctrl *r,
3578 				   const struct sge_rspq *q)
3579 {
3580 	return (r->type_gen >> RSPD_GEN_S) == q->gen;
3581 }
3582 
3583 /**
3584  *	rspq_next - advance to the next entry in a response queue
3585  *	@q: the queue
3586  *
3587  *	Updates the state of a response queue to advance it to the next entry.
3588  */
3589 static inline void rspq_next(struct sge_rspq *q)
3590 {
3591 	q->cur_desc = (void *)q->cur_desc + q->iqe_len;
3592 	if (unlikely(++q->cidx == q->size)) {
3593 		q->cidx = 0;
3594 		q->gen ^= 1;
3595 		q->cur_desc = q->desc;
3596 	}
3597 }
3598 
3599 /**
3600  *	process_responses - process responses from an SGE response queue
3601  *	@q: the ingress queue to process
3602  *	@budget: how many responses can be processed in this round
3603  *
3604  *	Process responses from an SGE response queue up to the supplied budget.
3605  *	Responses include received packets as well as control messages from FW
3606  *	or HW.
3607  *
3608  *	Additionally choose the interrupt holdoff time for the next interrupt
3609  *	on this queue.  If the system is under memory shortage use a fairly
3610  *	long delay to help recovery.
3611  */
3612 static int process_responses(struct sge_rspq *q, int budget)
3613 {
3614 	int ret, rsp_type;
3615 	int budget_left = budget;
3616 	const struct rsp_ctrl *rc;
3617 	struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
3618 	struct adapter *adapter = q->adap;
3619 	struct sge *s = &adapter->sge;
3620 
3621 	while (likely(budget_left)) {
3622 		rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
3623 		if (!is_new_response(rc, q)) {
3624 			if (q->flush_handler)
3625 				q->flush_handler(q);
3626 			break;
3627 		}
3628 
3629 		dma_rmb();
3630 		rsp_type = RSPD_TYPE_G(rc->type_gen);
3631 		if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
3632 			struct page_frag *fp;
3633 			struct pkt_gl si;
3634 			const struct rx_sw_desc *rsd;
3635 			u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
3636 
3637 			if (len & RSPD_NEWBUF_F) {
3638 				if (likely(q->offset > 0)) {
3639 					free_rx_bufs(q->adap, &rxq->fl, 1);
3640 					q->offset = 0;
3641 				}
3642 				len = RSPD_LEN_G(len);
3643 			}
3644 			si.tot_len = len;
3645 
3646 			/* gather packet fragments */
3647 			for (frags = 0, fp = si.frags; ; frags++, fp++) {
3648 				rsd = &rxq->fl.sdesc[rxq->fl.cidx];
3649 				bufsz = get_buf_size(adapter, rsd);
3650 				fp->page = rsd->page;
3651 				fp->offset = q->offset;
3652 				fp->size = min(bufsz, len);
3653 				len -= fp->size;
3654 				if (!len)
3655 					break;
3656 				unmap_rx_buf(q->adap, &rxq->fl);
3657 			}
3658 
3659 			si.sgetstamp = SGE_TIMESTAMP_G(
3660 					be64_to_cpu(rc->last_flit));
3661 			/*
3662 			 * Last buffer remains mapped so explicitly make it
3663 			 * coherent for CPU access.
3664 			 */
3665 			dma_sync_single_for_cpu(q->adap->pdev_dev,
3666 						get_buf_addr(rsd),
3667 						fp->size, DMA_FROM_DEVICE);
3668 
3669 			si.va = page_address(si.frags[0].page) +
3670 				si.frags[0].offset;
3671 			prefetch(si.va);
3672 
3673 			si.nfrags = frags + 1;
3674 			ret = q->handler(q, q->cur_desc, &si);
3675 			if (likely(ret == 0))
3676 				q->offset += ALIGN(fp->size, s->fl_align);
3677 			else
3678 				restore_rx_bufs(&si, &rxq->fl, frags);
3679 		} else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
3680 			ret = q->handler(q, q->cur_desc, NULL);
3681 		} else {
3682 			ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
3683 		}
3684 
3685 		if (unlikely(ret)) {
3686 			/* couldn't process descriptor, back off for recovery */
3687 			q->next_intr_params = QINTR_TIMER_IDX_V(NOMEM_TMR_IDX);
3688 			break;
3689 		}
3690 
3691 		rspq_next(q);
3692 		budget_left--;
3693 	}
3694 
3695 	if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 16)
3696 		__refill_fl(q->adap, &rxq->fl);
3697 	return budget - budget_left;
3698 }
3699 
3700 /**
3701  *	napi_rx_handler - the NAPI handler for Rx processing
3702  *	@napi: the napi instance
3703  *	@budget: how many packets we can process in this round
3704  *
3705  *	Handler for new data events when using NAPI.  This does not need any
3706  *	locking or protection from interrupts as data interrupts are off at
3707  *	this point and other adapter interrupts do not interfere (the latter
3708  *	in not a concern at all with MSI-X as non-data interrupts then have
3709  *	a separate handler).
3710  */
3711 static int napi_rx_handler(struct napi_struct *napi, int budget)
3712 {
3713 	unsigned int params;
3714 	struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
3715 	int work_done;
3716 	u32 val;
3717 
3718 	work_done = process_responses(q, budget);
3719 	if (likely(work_done < budget)) {
3720 		int timer_index;
3721 
3722 		napi_complete_done(napi, work_done);
3723 		timer_index = QINTR_TIMER_IDX_G(q->next_intr_params);
3724 
3725 		if (q->adaptive_rx) {
3726 			if (work_done > max(timer_pkt_quota[timer_index],
3727 					    MIN_NAPI_WORK))
3728 				timer_index = (timer_index + 1);
3729 			else
3730 				timer_index = timer_index - 1;
3731 
3732 			timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
3733 			q->next_intr_params =
3734 					QINTR_TIMER_IDX_V(timer_index) |
3735 					QINTR_CNT_EN_V(0);
3736 			params = q->next_intr_params;
3737 		} else {
3738 			params = q->next_intr_params;
3739 			q->next_intr_params = q->intr_params;
3740 		}
3741 	} else
3742 		params = QINTR_TIMER_IDX_V(7);
3743 
3744 	val = CIDXINC_V(work_done) | SEINTARM_V(params);
3745 
3746 	/* If we don't have access to the new User GTS (T5+), use the old
3747 	 * doorbell mechanism; otherwise use the new BAR2 mechanism.
3748 	 */
3749 	if (unlikely(q->bar2_addr == NULL)) {
3750 		t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
3751 			     val | INGRESSQID_V((u32)q->cntxt_id));
3752 	} else {
3753 		writel(val | INGRESSQID_V(q->bar2_qid),
3754 		       q->bar2_addr + SGE_UDB_GTS);
3755 		wmb();
3756 	}
3757 	return work_done;
3758 }
3759 
3760 void cxgb4_ethofld_restart(unsigned long data)
3761 {
3762 	struct sge_eosw_txq *eosw_txq = (struct sge_eosw_txq *)data;
3763 	int pktcount;
3764 
3765 	spin_lock(&eosw_txq->lock);
3766 	pktcount = eosw_txq->cidx - eosw_txq->last_cidx;
3767 	if (pktcount < 0)
3768 		pktcount += eosw_txq->ndesc;
3769 
3770 	if (pktcount) {
3771 		cxgb4_eosw_txq_free_desc(netdev2adap(eosw_txq->netdev),
3772 					 eosw_txq, pktcount);
3773 		eosw_txq->inuse -= pktcount;
3774 	}
3775 
3776 	/* There may be some packets waiting for completions. So,
3777 	 * attempt to send these packets now.
3778 	 */
3779 	ethofld_xmit(eosw_txq->netdev, eosw_txq);
3780 	spin_unlock(&eosw_txq->lock);
3781 }
3782 
3783 /* cxgb4_ethofld_rx_handler - Process ETHOFLD Tx completions
3784  * @q: the response queue that received the packet
3785  * @rsp: the response queue descriptor holding the CPL message
3786  * @si: the gather list of packet fragments
3787  *
3788  * Process a ETHOFLD Tx completion. Increment the cidx here, but
3789  * free up the descriptors in a tasklet later.
3790  */
3791 int cxgb4_ethofld_rx_handler(struct sge_rspq *q, const __be64 *rsp,
3792 			     const struct pkt_gl *si)
3793 {
3794 	u8 opcode = ((const struct rss_header *)rsp)->opcode;
3795 
3796 	/* skip RSS header */
3797 	rsp++;
3798 
3799 	if (opcode == CPL_FW4_ACK) {
3800 		const struct cpl_fw4_ack *cpl;
3801 		struct sge_eosw_txq *eosw_txq;
3802 		struct eotid_entry *entry;
3803 		struct sk_buff *skb;
3804 		u32 hdr_len, eotid;
3805 		u8 flits, wrlen16;
3806 		int credits;
3807 
3808 		cpl = (const struct cpl_fw4_ack *)rsp;
3809 		eotid = CPL_FW4_ACK_FLOWID_G(ntohl(OPCODE_TID(cpl))) -
3810 			q->adap->tids.eotid_base;
3811 		entry = cxgb4_lookup_eotid(&q->adap->tids, eotid);
3812 		if (!entry)
3813 			goto out_done;
3814 
3815 		eosw_txq = (struct sge_eosw_txq *)entry->data;
3816 		if (!eosw_txq)
3817 			goto out_done;
3818 
3819 		spin_lock(&eosw_txq->lock);
3820 		credits = cpl->credits;
3821 		while (credits > 0) {
3822 			skb = eosw_txq->desc[eosw_txq->cidx].skb;
3823 			if (!skb)
3824 				break;
3825 
3826 			if (unlikely((eosw_txq->state ==
3827 				      CXGB4_EO_STATE_FLOWC_OPEN_REPLY ||
3828 				      eosw_txq->state ==
3829 				      CXGB4_EO_STATE_FLOWC_CLOSE_REPLY) &&
3830 				     eosw_txq->cidx == eosw_txq->flowc_idx)) {
3831 				flits = DIV_ROUND_UP(skb->len, 8);
3832 				if (eosw_txq->state ==
3833 				    CXGB4_EO_STATE_FLOWC_OPEN_REPLY)
3834 					eosw_txq->state = CXGB4_EO_STATE_ACTIVE;
3835 				else
3836 					eosw_txq->state = CXGB4_EO_STATE_CLOSED;
3837 				complete(&eosw_txq->completion);
3838 			} else {
3839 				hdr_len = eth_get_headlen(eosw_txq->netdev,
3840 							  skb->data,
3841 							  skb_headlen(skb));
3842 				flits = ethofld_calc_tx_flits(q->adap, skb,
3843 							      hdr_len);
3844 			}
3845 			eosw_txq_advance_index(&eosw_txq->cidx, 1,
3846 					       eosw_txq->ndesc);
3847 			wrlen16 = DIV_ROUND_UP(flits * 8, 16);
3848 			credits -= wrlen16;
3849 		}
3850 
3851 		eosw_txq->cred += cpl->credits;
3852 		eosw_txq->ncompl--;
3853 
3854 		spin_unlock(&eosw_txq->lock);
3855 
3856 		/* Schedule a tasklet to reclaim SKBs and restart ETHOFLD Tx,
3857 		 * if there were packets waiting for completion.
3858 		 */
3859 		tasklet_schedule(&eosw_txq->qresume_tsk);
3860 	}
3861 
3862 out_done:
3863 	return 0;
3864 }
3865 
3866 /*
3867  * The MSI-X interrupt handler for an SGE response queue.
3868  */
3869 irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
3870 {
3871 	struct sge_rspq *q = cookie;
3872 
3873 	napi_schedule(&q->napi);
3874 	return IRQ_HANDLED;
3875 }
3876 
3877 /*
3878  * Process the indirect interrupt entries in the interrupt queue and kick off
3879  * NAPI for each queue that has generated an entry.
3880  */
3881 static unsigned int process_intrq(struct adapter *adap)
3882 {
3883 	unsigned int credits;
3884 	const struct rsp_ctrl *rc;
3885 	struct sge_rspq *q = &adap->sge.intrq;
3886 	u32 val;
3887 
3888 	spin_lock(&adap->sge.intrq_lock);
3889 	for (credits = 0; ; credits++) {
3890 		rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
3891 		if (!is_new_response(rc, q))
3892 			break;
3893 
3894 		dma_rmb();
3895 		if (RSPD_TYPE_G(rc->type_gen) == RSPD_TYPE_INTR_X) {
3896 			unsigned int qid = ntohl(rc->pldbuflen_qid);
3897 
3898 			qid -= adap->sge.ingr_start;
3899 			napi_schedule(&adap->sge.ingr_map[qid]->napi);
3900 		}
3901 
3902 		rspq_next(q);
3903 	}
3904 
3905 	val =  CIDXINC_V(credits) | SEINTARM_V(q->intr_params);
3906 
3907 	/* If we don't have access to the new User GTS (T5+), use the old
3908 	 * doorbell mechanism; otherwise use the new BAR2 mechanism.
3909 	 */
3910 	if (unlikely(q->bar2_addr == NULL)) {
3911 		t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
3912 			     val | INGRESSQID_V(q->cntxt_id));
3913 	} else {
3914 		writel(val | INGRESSQID_V(q->bar2_qid),
3915 		       q->bar2_addr + SGE_UDB_GTS);
3916 		wmb();
3917 	}
3918 	spin_unlock(&adap->sge.intrq_lock);
3919 	return credits;
3920 }
3921 
3922 /*
3923  * The MSI interrupt handler, which handles data events from SGE response queues
3924  * as well as error and other async events as they all use the same MSI vector.
3925  */
3926 static irqreturn_t t4_intr_msi(int irq, void *cookie)
3927 {
3928 	struct adapter *adap = cookie;
3929 
3930 	if (adap->flags & CXGB4_MASTER_PF)
3931 		t4_slow_intr_handler(adap);
3932 	process_intrq(adap);
3933 	return IRQ_HANDLED;
3934 }
3935 
3936 /*
3937  * Interrupt handler for legacy INTx interrupts.
3938  * Handles data events from SGE response queues as well as error and other
3939  * async events as they all use the same interrupt line.
3940  */
3941 static irqreturn_t t4_intr_intx(int irq, void *cookie)
3942 {
3943 	struct adapter *adap = cookie;
3944 
3945 	t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI_A), 0);
3946 	if (((adap->flags & CXGB4_MASTER_PF) && t4_slow_intr_handler(adap)) |
3947 	    process_intrq(adap))
3948 		return IRQ_HANDLED;
3949 	return IRQ_NONE;             /* probably shared interrupt */
3950 }
3951 
3952 /**
3953  *	t4_intr_handler - select the top-level interrupt handler
3954  *	@adap: the adapter
3955  *
3956  *	Selects the top-level interrupt handler based on the type of interrupts
3957  *	(MSI-X, MSI, or INTx).
3958  */
3959 irq_handler_t t4_intr_handler(struct adapter *adap)
3960 {
3961 	if (adap->flags & CXGB4_USING_MSIX)
3962 		return t4_sge_intr_msix;
3963 	if (adap->flags & CXGB4_USING_MSI)
3964 		return t4_intr_msi;
3965 	return t4_intr_intx;
3966 }
3967 
3968 static void sge_rx_timer_cb(struct timer_list *t)
3969 {
3970 	unsigned long m;
3971 	unsigned int i;
3972 	struct adapter *adap = from_timer(adap, t, sge.rx_timer);
3973 	struct sge *s = &adap->sge;
3974 
3975 	for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
3976 		for (m = s->starving_fl[i]; m; m &= m - 1) {
3977 			struct sge_eth_rxq *rxq;
3978 			unsigned int id = __ffs(m) + i * BITS_PER_LONG;
3979 			struct sge_fl *fl = s->egr_map[id];
3980 
3981 			clear_bit(id, s->starving_fl);
3982 			smp_mb__after_atomic();
3983 
3984 			if (fl_starving(adap, fl)) {
3985 				rxq = container_of(fl, struct sge_eth_rxq, fl);
3986 				if (napi_reschedule(&rxq->rspq.napi))
3987 					fl->starving++;
3988 				else
3989 					set_bit(id, s->starving_fl);
3990 			}
3991 		}
3992 	/* The remainder of the SGE RX Timer Callback routine is dedicated to
3993 	 * global Master PF activities like checking for chip ingress stalls,
3994 	 * etc.
3995 	 */
3996 	if (!(adap->flags & CXGB4_MASTER_PF))
3997 		goto done;
3998 
3999 	t4_idma_monitor(adap, &s->idma_monitor, HZ, RX_QCHECK_PERIOD);
4000 
4001 done:
4002 	mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
4003 }
4004 
4005 static void sge_tx_timer_cb(struct timer_list *t)
4006 {
4007 	struct adapter *adap = from_timer(adap, t, sge.tx_timer);
4008 	struct sge *s = &adap->sge;
4009 	unsigned long m, period;
4010 	unsigned int i, budget;
4011 
4012 	for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
4013 		for (m = s->txq_maperr[i]; m; m &= m - 1) {
4014 			unsigned long id = __ffs(m) + i * BITS_PER_LONG;
4015 			struct sge_uld_txq *txq = s->egr_map[id];
4016 
4017 			clear_bit(id, s->txq_maperr);
4018 			tasklet_schedule(&txq->qresume_tsk);
4019 		}
4020 
4021 	if (!is_t4(adap->params.chip)) {
4022 		struct sge_eth_txq *q = &s->ptptxq;
4023 		int avail;
4024 
4025 		spin_lock(&adap->ptp_lock);
4026 		avail = reclaimable(&q->q);
4027 
4028 		if (avail) {
4029 			free_tx_desc(adap, &q->q, avail, false);
4030 			q->q.in_use -= avail;
4031 		}
4032 		spin_unlock(&adap->ptp_lock);
4033 	}
4034 
4035 	budget = MAX_TIMER_TX_RECLAIM;
4036 	i = s->ethtxq_rover;
4037 	do {
4038 		budget -= t4_sge_eth_txq_egress_update(adap, &s->ethtxq[i],
4039 						       budget);
4040 		if (!budget)
4041 			break;
4042 
4043 		if (++i >= s->ethqsets)
4044 			i = 0;
4045 	} while (i != s->ethtxq_rover);
4046 	s->ethtxq_rover = i;
4047 
4048 	if (budget == 0) {
4049 		/* If we found too many reclaimable packets schedule a timer
4050 		 * in the near future to continue where we left off.
4051 		 */
4052 		period = 2;
4053 	} else {
4054 		/* We reclaimed all reclaimable TX Descriptors, so reschedule
4055 		 * at the normal period.
4056 		 */
4057 		period = TX_QCHECK_PERIOD;
4058 	}
4059 
4060 	mod_timer(&s->tx_timer, jiffies + period);
4061 }
4062 
4063 /**
4064  *	bar2_address - return the BAR2 address for an SGE Queue's Registers
4065  *	@adapter: the adapter
4066  *	@qid: the SGE Queue ID
4067  *	@qtype: the SGE Queue Type (Egress or Ingress)
4068  *	@pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
4069  *
4070  *	Returns the BAR2 address for the SGE Queue Registers associated with
4071  *	@qid.  If BAR2 SGE Registers aren't available, returns NULL.  Also
4072  *	returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
4073  *	Queue Registers.  If the BAR2 Queue ID is 0, then "Inferred Queue ID"
4074  *	Registers are supported (e.g. the Write Combining Doorbell Buffer).
4075  */
4076 static void __iomem *bar2_address(struct adapter *adapter,
4077 				  unsigned int qid,
4078 				  enum t4_bar2_qtype qtype,
4079 				  unsigned int *pbar2_qid)
4080 {
4081 	u64 bar2_qoffset;
4082 	int ret;
4083 
4084 	ret = t4_bar2_sge_qregs(adapter, qid, qtype, 0,
4085 				&bar2_qoffset, pbar2_qid);
4086 	if (ret)
4087 		return NULL;
4088 
4089 	return adapter->bar2 + bar2_qoffset;
4090 }
4091 
4092 /* @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
4093  * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
4094  */
4095 int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
4096 		     struct net_device *dev, int intr_idx,
4097 		     struct sge_fl *fl, rspq_handler_t hnd,
4098 		     rspq_flush_handler_t flush_hnd, int cong)
4099 {
4100 	int ret, flsz = 0;
4101 	struct fw_iq_cmd c;
4102 	struct sge *s = &adap->sge;
4103 	struct port_info *pi = netdev_priv(dev);
4104 	int relaxed = !(adap->flags & CXGB4_ROOT_NO_RELAXED_ORDERING);
4105 
4106 	/* Size needs to be multiple of 16, including status entry. */
4107 	iq->size = roundup(iq->size, 16);
4108 
4109 	iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
4110 			      &iq->phys_addr, NULL, 0,
4111 			      dev_to_node(adap->pdev_dev));
4112 	if (!iq->desc)
4113 		return -ENOMEM;
4114 
4115 	memset(&c, 0, sizeof(c));
4116 	c.op_to_vfn = htonl(FW_CMD_OP_V(FW_IQ_CMD) | FW_CMD_REQUEST_F |
4117 			    FW_CMD_WRITE_F | FW_CMD_EXEC_F |
4118 			    FW_IQ_CMD_PFN_V(adap->pf) | FW_IQ_CMD_VFN_V(0));
4119 	c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC_F | FW_IQ_CMD_IQSTART_F |
4120 				 FW_LEN16(c));
4121 	c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
4122 		FW_IQ_CMD_IQASYNCH_V(fwevtq) | FW_IQ_CMD_VIID_V(pi->viid) |
4123 		FW_IQ_CMD_IQANDST_V(intr_idx < 0) |
4124 		FW_IQ_CMD_IQANUD_V(UPDATEDELIVERY_INTERRUPT_X) |
4125 		FW_IQ_CMD_IQANDSTINDEX_V(intr_idx >= 0 ? intr_idx :
4126 							-intr_idx - 1));
4127 	c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH_V(pi->tx_chan) |
4128 		FW_IQ_CMD_IQGTSMODE_F |
4129 		FW_IQ_CMD_IQINTCNTTHRESH_V(iq->pktcnt_idx) |
4130 		FW_IQ_CMD_IQESIZE_V(ilog2(iq->iqe_len) - 4));
4131 	c.iqsize = htons(iq->size);
4132 	c.iqaddr = cpu_to_be64(iq->phys_addr);
4133 	if (cong >= 0)
4134 		c.iqns_to_fl0congen = htonl(FW_IQ_CMD_IQFLINTCONGEN_F |
4135 				FW_IQ_CMD_IQTYPE_V(cong ? FW_IQ_IQTYPE_NIC
4136 							:  FW_IQ_IQTYPE_OFLD));
4137 
4138 	if (fl) {
4139 		unsigned int chip_ver =
4140 			CHELSIO_CHIP_VERSION(adap->params.chip);
4141 
4142 		/* Allocate the ring for the hardware free list (with space
4143 		 * for its status page) along with the associated software
4144 		 * descriptor ring.  The free list size needs to be a multiple
4145 		 * of the Egress Queue Unit and at least 2 Egress Units larger
4146 		 * than the SGE's Egress Congrestion Threshold
4147 		 * (fl_starve_thres - 1).
4148 		 */
4149 		if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
4150 			fl->size = s->fl_starve_thres - 1 + 2 * 8;
4151 		fl->size = roundup(fl->size, 8);
4152 		fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
4153 				      sizeof(struct rx_sw_desc), &fl->addr,
4154 				      &fl->sdesc, s->stat_len,
4155 				      dev_to_node(adap->pdev_dev));
4156 		if (!fl->desc)
4157 			goto fl_nomem;
4158 
4159 		flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
4160 		c.iqns_to_fl0congen |= htonl(FW_IQ_CMD_FL0PACKEN_F |
4161 					     FW_IQ_CMD_FL0FETCHRO_V(relaxed) |
4162 					     FW_IQ_CMD_FL0DATARO_V(relaxed) |
4163 					     FW_IQ_CMD_FL0PADEN_F);
4164 		if (cong >= 0)
4165 			c.iqns_to_fl0congen |=
4166 				htonl(FW_IQ_CMD_FL0CNGCHMAP_V(cong) |
4167 				      FW_IQ_CMD_FL0CONGCIF_F |
4168 				      FW_IQ_CMD_FL0CONGEN_F);
4169 		/* In T6, for egress queue type FL there is internal overhead
4170 		 * of 16B for header going into FLM module.  Hence the maximum
4171 		 * allowed burst size is 448 bytes.  For T4/T5, the hardware
4172 		 * doesn't coalesce fetch requests if more than 64 bytes of
4173 		 * Free List pointers are provided, so we use a 128-byte Fetch
4174 		 * Burst Minimum there (T6 implements coalescing so we can use
4175 		 * the smaller 64-byte value there).
4176 		 */
4177 		c.fl0dcaen_to_fl0cidxfthresh =
4178 			htons(FW_IQ_CMD_FL0FBMIN_V(chip_ver <= CHELSIO_T5 ?
4179 						   FETCHBURSTMIN_128B_X :
4180 						   FETCHBURSTMIN_64B_T6_X) |
4181 			      FW_IQ_CMD_FL0FBMAX_V((chip_ver <= CHELSIO_T5) ?
4182 						   FETCHBURSTMAX_512B_X :
4183 						   FETCHBURSTMAX_256B_X));
4184 		c.fl0size = htons(flsz);
4185 		c.fl0addr = cpu_to_be64(fl->addr);
4186 	}
4187 
4188 	ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
4189 	if (ret)
4190 		goto err;
4191 
4192 	netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
4193 	iq->cur_desc = iq->desc;
4194 	iq->cidx = 0;
4195 	iq->gen = 1;
4196 	iq->next_intr_params = iq->intr_params;
4197 	iq->cntxt_id = ntohs(c.iqid);
4198 	iq->abs_id = ntohs(c.physiqid);
4199 	iq->bar2_addr = bar2_address(adap,
4200 				     iq->cntxt_id,
4201 				     T4_BAR2_QTYPE_INGRESS,
4202 				     &iq->bar2_qid);
4203 	iq->size--;                           /* subtract status entry */
4204 	iq->netdev = dev;
4205 	iq->handler = hnd;
4206 	iq->flush_handler = flush_hnd;
4207 
4208 	memset(&iq->lro_mgr, 0, sizeof(struct t4_lro_mgr));
4209 	skb_queue_head_init(&iq->lro_mgr.lroq);
4210 
4211 	/* set offset to -1 to distinguish ingress queues without FL */
4212 	iq->offset = fl ? 0 : -1;
4213 
4214 	adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
4215 
4216 	if (fl) {
4217 		fl->cntxt_id = ntohs(c.fl0id);
4218 		fl->avail = fl->pend_cred = 0;
4219 		fl->pidx = fl->cidx = 0;
4220 		fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
4221 		adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
4222 
4223 		/* Note, we must initialize the BAR2 Free List User Doorbell
4224 		 * information before refilling the Free List!
4225 		 */
4226 		fl->bar2_addr = bar2_address(adap,
4227 					     fl->cntxt_id,
4228 					     T4_BAR2_QTYPE_EGRESS,
4229 					     &fl->bar2_qid);
4230 		refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
4231 	}
4232 
4233 	/* For T5 and later we attempt to set up the Congestion Manager values
4234 	 * of the new RX Ethernet Queue.  This should really be handled by
4235 	 * firmware because it's more complex than any host driver wants to
4236 	 * get involved with and it's different per chip and this is almost
4237 	 * certainly wrong.  Firmware would be wrong as well, but it would be
4238 	 * a lot easier to fix in one place ...  For now we do something very
4239 	 * simple (and hopefully less wrong).
4240 	 */
4241 	if (!is_t4(adap->params.chip) && cong >= 0) {
4242 		u32 param, val, ch_map = 0;
4243 		int i;
4244 		u16 cng_ch_bits_log = adap->params.arch.cng_ch_bits_log;
4245 
4246 		param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
4247 			 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
4248 			 FW_PARAMS_PARAM_YZ_V(iq->cntxt_id));
4249 		if (cong == 0) {
4250 			val = CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_QUEUE_X);
4251 		} else {
4252 			val =
4253 			    CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_CHANNEL_X);
4254 			for (i = 0; i < 4; i++) {
4255 				if (cong & (1 << i))
4256 					ch_map |= 1 << (i << cng_ch_bits_log);
4257 			}
4258 			val |= CONMCTXT_CNGCHMAP_V(ch_map);
4259 		}
4260 		ret = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
4261 				    &param, &val);
4262 		if (ret)
4263 			dev_warn(adap->pdev_dev, "Failed to set Congestion"
4264 				 " Manager Context for Ingress Queue %d: %d\n",
4265 				 iq->cntxt_id, -ret);
4266 	}
4267 
4268 	return 0;
4269 
4270 fl_nomem:
4271 	ret = -ENOMEM;
4272 err:
4273 	if (iq->desc) {
4274 		dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
4275 				  iq->desc, iq->phys_addr);
4276 		iq->desc = NULL;
4277 	}
4278 	if (fl && fl->desc) {
4279 		kfree(fl->sdesc);
4280 		fl->sdesc = NULL;
4281 		dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
4282 				  fl->desc, fl->addr);
4283 		fl->desc = NULL;
4284 	}
4285 	return ret;
4286 }
4287 
4288 static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
4289 {
4290 	q->cntxt_id = id;
4291 	q->bar2_addr = bar2_address(adap,
4292 				    q->cntxt_id,
4293 				    T4_BAR2_QTYPE_EGRESS,
4294 				    &q->bar2_qid);
4295 	q->in_use = 0;
4296 	q->cidx = q->pidx = 0;
4297 	q->stops = q->restarts = 0;
4298 	q->stat = (void *)&q->desc[q->size];
4299 	spin_lock_init(&q->db_lock);
4300 	adap->sge.egr_map[id - adap->sge.egr_start] = q;
4301 }
4302 
4303 /**
4304  *	t4_sge_alloc_eth_txq - allocate an Ethernet TX Queue
4305  *	@adap: the adapter
4306  *	@txq: the SGE Ethernet TX Queue to initialize
4307  *	@dev: the Linux Network Device
4308  *	@netdevq: the corresponding Linux TX Queue
4309  *	@iqid: the Ingress Queue to which to deliver CIDX Update messages
4310  *	@dbqt: whether this TX Queue will use the SGE Doorbell Queue Timers
4311  */
4312 int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
4313 			 struct net_device *dev, struct netdev_queue *netdevq,
4314 			 unsigned int iqid, u8 dbqt)
4315 {
4316 	unsigned int chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
4317 	struct port_info *pi = netdev_priv(dev);
4318 	struct sge *s = &adap->sge;
4319 	struct fw_eq_eth_cmd c;
4320 	int ret, nentries;
4321 
4322 	/* Add status entries */
4323 	nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
4324 
4325 	txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
4326 			sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
4327 			&txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
4328 			netdev_queue_numa_node_read(netdevq));
4329 	if (!txq->q.desc)
4330 		return -ENOMEM;
4331 
4332 	memset(&c, 0, sizeof(c));
4333 	c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_ETH_CMD) | FW_CMD_REQUEST_F |
4334 			    FW_CMD_WRITE_F | FW_CMD_EXEC_F |
4335 			    FW_EQ_ETH_CMD_PFN_V(adap->pf) |
4336 			    FW_EQ_ETH_CMD_VFN_V(0));
4337 	c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC_F |
4338 				 FW_EQ_ETH_CMD_EQSTART_F | FW_LEN16(c));
4339 
4340 	/* For TX Ethernet Queues using the SGE Doorbell Queue Timer
4341 	 * mechanism, we use Ingress Queue messages for Hardware Consumer
4342 	 * Index Updates on the TX Queue.  Otherwise we have the Hardware
4343 	 * write the CIDX Updates into the Status Page at the end of the
4344 	 * TX Queue.
4345 	 */
4346 	c.autoequiqe_to_viid = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
4347 				     FW_EQ_ETH_CMD_VIID_V(pi->viid));
4348 
4349 	c.fetchszm_to_iqid =
4350 		htonl(FW_EQ_ETH_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
4351 		      FW_EQ_ETH_CMD_PCIECHN_V(pi->tx_chan) |
4352 		      FW_EQ_ETH_CMD_FETCHRO_F | FW_EQ_ETH_CMD_IQID_V(iqid));
4353 
4354 	/* Note that the CIDX Flush Threshold should match MAX_TX_RECLAIM. */
4355 	c.dcaen_to_eqsize =
4356 		htonl(FW_EQ_ETH_CMD_FBMIN_V(chip_ver <= CHELSIO_T5
4357 					    ? FETCHBURSTMIN_64B_X
4358 					    : FETCHBURSTMIN_64B_T6_X) |
4359 		      FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
4360 		      FW_EQ_ETH_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
4361 		      FW_EQ_ETH_CMD_EQSIZE_V(nentries));
4362 
4363 	c.eqaddr = cpu_to_be64(txq->q.phys_addr);
4364 
4365 	/* If we're using the SGE Doorbell Queue Timer mechanism, pass in the
4366 	 * currently configured Timer Index.  THis can be changed later via an
4367 	 * ethtool -C tx-usecs {Timer Val} command.  Note that the SGE
4368 	 * Doorbell Queue mode is currently automatically enabled in the
4369 	 * Firmware by setting either AUTOEQUEQE or AUTOEQUIQE ...
4370 	 */
4371 	if (dbqt)
4372 		c.timeren_timerix =
4373 			cpu_to_be32(FW_EQ_ETH_CMD_TIMEREN_F |
4374 				    FW_EQ_ETH_CMD_TIMERIX_V(txq->dbqtimerix));
4375 
4376 	ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
4377 	if (ret) {
4378 		kfree(txq->q.sdesc);
4379 		txq->q.sdesc = NULL;
4380 		dma_free_coherent(adap->pdev_dev,
4381 				  nentries * sizeof(struct tx_desc),
4382 				  txq->q.desc, txq->q.phys_addr);
4383 		txq->q.desc = NULL;
4384 		return ret;
4385 	}
4386 
4387 	txq->q.q_type = CXGB4_TXQ_ETH;
4388 	init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_G(ntohl(c.eqid_pkd)));
4389 	txq->txq = netdevq;
4390 	txq->tso = 0;
4391 	txq->uso = 0;
4392 	txq->tx_cso = 0;
4393 	txq->vlan_ins = 0;
4394 	txq->mapping_err = 0;
4395 	txq->dbqt = dbqt;
4396 
4397 	return 0;
4398 }
4399 
4400 int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
4401 			  struct net_device *dev, unsigned int iqid,
4402 			  unsigned int cmplqid)
4403 {
4404 	unsigned int chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
4405 	struct port_info *pi = netdev_priv(dev);
4406 	struct sge *s = &adap->sge;
4407 	struct fw_eq_ctrl_cmd c;
4408 	int ret, nentries;
4409 
4410 	/* Add status entries */
4411 	nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
4412 
4413 	txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
4414 				 sizeof(struct tx_desc), 0, &txq->q.phys_addr,
4415 				 NULL, 0, dev_to_node(adap->pdev_dev));
4416 	if (!txq->q.desc)
4417 		return -ENOMEM;
4418 
4419 	c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST_F |
4420 			    FW_CMD_WRITE_F | FW_CMD_EXEC_F |
4421 			    FW_EQ_CTRL_CMD_PFN_V(adap->pf) |
4422 			    FW_EQ_CTRL_CMD_VFN_V(0));
4423 	c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC_F |
4424 				 FW_EQ_CTRL_CMD_EQSTART_F | FW_LEN16(c));
4425 	c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID_V(cmplqid));
4426 	c.physeqid_pkd = htonl(0);
4427 	c.fetchszm_to_iqid =
4428 		htonl(FW_EQ_CTRL_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
4429 		      FW_EQ_CTRL_CMD_PCIECHN_V(pi->tx_chan) |
4430 		      FW_EQ_CTRL_CMD_FETCHRO_F | FW_EQ_CTRL_CMD_IQID_V(iqid));
4431 	c.dcaen_to_eqsize =
4432 		htonl(FW_EQ_CTRL_CMD_FBMIN_V(chip_ver <= CHELSIO_T5
4433 					     ? FETCHBURSTMIN_64B_X
4434 					     : FETCHBURSTMIN_64B_T6_X) |
4435 		      FW_EQ_CTRL_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
4436 		      FW_EQ_CTRL_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
4437 		      FW_EQ_CTRL_CMD_EQSIZE_V(nentries));
4438 	c.eqaddr = cpu_to_be64(txq->q.phys_addr);
4439 
4440 	ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
4441 	if (ret) {
4442 		dma_free_coherent(adap->pdev_dev,
4443 				  nentries * sizeof(struct tx_desc),
4444 				  txq->q.desc, txq->q.phys_addr);
4445 		txq->q.desc = NULL;
4446 		return ret;
4447 	}
4448 
4449 	txq->q.q_type = CXGB4_TXQ_CTRL;
4450 	init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_G(ntohl(c.cmpliqid_eqid)));
4451 	txq->adap = adap;
4452 	skb_queue_head_init(&txq->sendq);
4453 	tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
4454 	txq->full = 0;
4455 	return 0;
4456 }
4457 
4458 int t4_sge_mod_ctrl_txq(struct adapter *adap, unsigned int eqid,
4459 			unsigned int cmplqid)
4460 {
4461 	u32 param, val;
4462 
4463 	param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
4464 		 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_EQ_CMPLIQID_CTRL) |
4465 		 FW_PARAMS_PARAM_YZ_V(eqid));
4466 	val = cmplqid;
4467 	return t4_set_params(adap, adap->mbox, adap->pf, 0, 1, &param, &val);
4468 }
4469 
4470 static int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_txq *q,
4471 				 struct net_device *dev, u32 cmd, u32 iqid)
4472 {
4473 	unsigned int chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
4474 	struct port_info *pi = netdev_priv(dev);
4475 	struct sge *s = &adap->sge;
4476 	struct fw_eq_ofld_cmd c;
4477 	u32 fb_min, nentries;
4478 	int ret;
4479 
4480 	/* Add status entries */
4481 	nentries = q->size + s->stat_len / sizeof(struct tx_desc);
4482 	q->desc = alloc_ring(adap->pdev_dev, q->size, sizeof(struct tx_desc),
4483 			     sizeof(struct tx_sw_desc), &q->phys_addr,
4484 			     &q->sdesc, s->stat_len, NUMA_NO_NODE);
4485 	if (!q->desc)
4486 		return -ENOMEM;
4487 
4488 	if (chip_ver <= CHELSIO_T5)
4489 		fb_min = FETCHBURSTMIN_64B_X;
4490 	else
4491 		fb_min = FETCHBURSTMIN_64B_T6_X;
4492 
4493 	memset(&c, 0, sizeof(c));
4494 	c.op_to_vfn = htonl(FW_CMD_OP_V(cmd) | FW_CMD_REQUEST_F |
4495 			    FW_CMD_WRITE_F | FW_CMD_EXEC_F |
4496 			    FW_EQ_OFLD_CMD_PFN_V(adap->pf) |
4497 			    FW_EQ_OFLD_CMD_VFN_V(0));
4498 	c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC_F |
4499 				 FW_EQ_OFLD_CMD_EQSTART_F | FW_LEN16(c));
4500 	c.fetchszm_to_iqid =
4501 		htonl(FW_EQ_OFLD_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
4502 		      FW_EQ_OFLD_CMD_PCIECHN_V(pi->tx_chan) |
4503 		      FW_EQ_OFLD_CMD_FETCHRO_F | FW_EQ_OFLD_CMD_IQID_V(iqid));
4504 	c.dcaen_to_eqsize =
4505 		htonl(FW_EQ_OFLD_CMD_FBMIN_V(fb_min) |
4506 		      FW_EQ_OFLD_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
4507 		      FW_EQ_OFLD_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
4508 		      FW_EQ_OFLD_CMD_EQSIZE_V(nentries));
4509 	c.eqaddr = cpu_to_be64(q->phys_addr);
4510 
4511 	ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
4512 	if (ret) {
4513 		kfree(q->sdesc);
4514 		q->sdesc = NULL;
4515 		dma_free_coherent(adap->pdev_dev,
4516 				  nentries * sizeof(struct tx_desc),
4517 				  q->desc, q->phys_addr);
4518 		q->desc = NULL;
4519 		return ret;
4520 	}
4521 
4522 	init_txq(adap, q, FW_EQ_OFLD_CMD_EQID_G(ntohl(c.eqid_pkd)));
4523 	return 0;
4524 }
4525 
4526 int t4_sge_alloc_uld_txq(struct adapter *adap, struct sge_uld_txq *txq,
4527 			 struct net_device *dev, unsigned int iqid,
4528 			 unsigned int uld_type)
4529 {
4530 	u32 cmd = FW_EQ_OFLD_CMD;
4531 	int ret;
4532 
4533 	if (unlikely(uld_type == CXGB4_TX_CRYPTO))
4534 		cmd = FW_EQ_CTRL_CMD;
4535 
4536 	ret = t4_sge_alloc_ofld_txq(adap, &txq->q, dev, cmd, iqid);
4537 	if (ret)
4538 		return ret;
4539 
4540 	txq->q.q_type = CXGB4_TXQ_ULD;
4541 	txq->adap = adap;
4542 	skb_queue_head_init(&txq->sendq);
4543 	tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
4544 	txq->full = 0;
4545 	txq->mapping_err = 0;
4546 	return 0;
4547 }
4548 
4549 int t4_sge_alloc_ethofld_txq(struct adapter *adap, struct sge_eohw_txq *txq,
4550 			     struct net_device *dev, u32 iqid)
4551 {
4552 	int ret;
4553 
4554 	ret = t4_sge_alloc_ofld_txq(adap, &txq->q, dev, FW_EQ_OFLD_CMD, iqid);
4555 	if (ret)
4556 		return ret;
4557 
4558 	txq->q.q_type = CXGB4_TXQ_ULD;
4559 	spin_lock_init(&txq->lock);
4560 	txq->adap = adap;
4561 	txq->tso = 0;
4562 	txq->uso = 0;
4563 	txq->tx_cso = 0;
4564 	txq->vlan_ins = 0;
4565 	txq->mapping_err = 0;
4566 	return 0;
4567 }
4568 
4569 void free_txq(struct adapter *adap, struct sge_txq *q)
4570 {
4571 	struct sge *s = &adap->sge;
4572 
4573 	dma_free_coherent(adap->pdev_dev,
4574 			  q->size * sizeof(struct tx_desc) + s->stat_len,
4575 			  q->desc, q->phys_addr);
4576 	q->cntxt_id = 0;
4577 	q->sdesc = NULL;
4578 	q->desc = NULL;
4579 }
4580 
4581 void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
4582 		  struct sge_fl *fl)
4583 {
4584 	struct sge *s = &adap->sge;
4585 	unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
4586 
4587 	adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
4588 	t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
4589 		   rq->cntxt_id, fl_id, 0xffff);
4590 	dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
4591 			  rq->desc, rq->phys_addr);
4592 	netif_napi_del(&rq->napi);
4593 	rq->netdev = NULL;
4594 	rq->cntxt_id = rq->abs_id = 0;
4595 	rq->desc = NULL;
4596 
4597 	if (fl) {
4598 		free_rx_bufs(adap, fl, fl->avail);
4599 		dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
4600 				  fl->desc, fl->addr);
4601 		kfree(fl->sdesc);
4602 		fl->sdesc = NULL;
4603 		fl->cntxt_id = 0;
4604 		fl->desc = NULL;
4605 	}
4606 }
4607 
4608 /**
4609  *      t4_free_ofld_rxqs - free a block of consecutive Rx queues
4610  *      @adap: the adapter
4611  *      @n: number of queues
4612  *      @q: pointer to first queue
4613  *
4614  *      Release the resources of a consecutive block of offload Rx queues.
4615  */
4616 void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
4617 {
4618 	for ( ; n; n--, q++)
4619 		if (q->rspq.desc)
4620 			free_rspq_fl(adap, &q->rspq,
4621 				     q->fl.size ? &q->fl : NULL);
4622 }
4623 
4624 void t4_sge_free_ethofld_txq(struct adapter *adap, struct sge_eohw_txq *txq)
4625 {
4626 	if (txq->q.desc) {
4627 		t4_ofld_eq_free(adap, adap->mbox, adap->pf, 0,
4628 				txq->q.cntxt_id);
4629 		free_tx_desc(adap, &txq->q, txq->q.in_use, false);
4630 		kfree(txq->q.sdesc);
4631 		free_txq(adap, &txq->q);
4632 	}
4633 }
4634 
4635 /**
4636  *	t4_free_sge_resources - free SGE resources
4637  *	@adap: the adapter
4638  *
4639  *	Frees resources used by the SGE queue sets.
4640  */
4641 void t4_free_sge_resources(struct adapter *adap)
4642 {
4643 	int i;
4644 	struct sge_eth_rxq *eq;
4645 	struct sge_eth_txq *etq;
4646 
4647 	/* stop all Rx queues in order to start them draining */
4648 	for (i = 0; i < adap->sge.ethqsets; i++) {
4649 		eq = &adap->sge.ethrxq[i];
4650 		if (eq->rspq.desc)
4651 			t4_iq_stop(adap, adap->mbox, adap->pf, 0,
4652 				   FW_IQ_TYPE_FL_INT_CAP,
4653 				   eq->rspq.cntxt_id,
4654 				   eq->fl.size ? eq->fl.cntxt_id : 0xffff,
4655 				   0xffff);
4656 	}
4657 
4658 	/* clean up Ethernet Tx/Rx queues */
4659 	for (i = 0; i < adap->sge.ethqsets; i++) {
4660 		eq = &adap->sge.ethrxq[i];
4661 		if (eq->rspq.desc)
4662 			free_rspq_fl(adap, &eq->rspq,
4663 				     eq->fl.size ? &eq->fl : NULL);
4664 		if (eq->msix) {
4665 			cxgb4_free_msix_idx_in_bmap(adap, eq->msix->idx);
4666 			eq->msix = NULL;
4667 		}
4668 
4669 		etq = &adap->sge.ethtxq[i];
4670 		if (etq->q.desc) {
4671 			t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
4672 				       etq->q.cntxt_id);
4673 			__netif_tx_lock_bh(etq->txq);
4674 			free_tx_desc(adap, &etq->q, etq->q.in_use, true);
4675 			__netif_tx_unlock_bh(etq->txq);
4676 			kfree(etq->q.sdesc);
4677 			free_txq(adap, &etq->q);
4678 		}
4679 	}
4680 
4681 	/* clean up control Tx queues */
4682 	for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
4683 		struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
4684 
4685 		if (cq->q.desc) {
4686 			tasklet_kill(&cq->qresume_tsk);
4687 			t4_ctrl_eq_free(adap, adap->mbox, adap->pf, 0,
4688 					cq->q.cntxt_id);
4689 			__skb_queue_purge(&cq->sendq);
4690 			free_txq(adap, &cq->q);
4691 		}
4692 	}
4693 
4694 	if (adap->sge.fw_evtq.desc) {
4695 		free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
4696 		if (adap->sge.fwevtq_msix_idx >= 0)
4697 			cxgb4_free_msix_idx_in_bmap(adap,
4698 						    adap->sge.fwevtq_msix_idx);
4699 	}
4700 
4701 	if (adap->sge.nd_msix_idx >= 0)
4702 		cxgb4_free_msix_idx_in_bmap(adap, adap->sge.nd_msix_idx);
4703 
4704 	if (adap->sge.intrq.desc)
4705 		free_rspq_fl(adap, &adap->sge.intrq, NULL);
4706 
4707 	if (!is_t4(adap->params.chip)) {
4708 		etq = &adap->sge.ptptxq;
4709 		if (etq->q.desc) {
4710 			t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
4711 				       etq->q.cntxt_id);
4712 			spin_lock_bh(&adap->ptp_lock);
4713 			free_tx_desc(adap, &etq->q, etq->q.in_use, true);
4714 			spin_unlock_bh(&adap->ptp_lock);
4715 			kfree(etq->q.sdesc);
4716 			free_txq(adap, &etq->q);
4717 		}
4718 	}
4719 
4720 	/* clear the reverse egress queue map */
4721 	memset(adap->sge.egr_map, 0,
4722 	       adap->sge.egr_sz * sizeof(*adap->sge.egr_map));
4723 }
4724 
4725 void t4_sge_start(struct adapter *adap)
4726 {
4727 	adap->sge.ethtxq_rover = 0;
4728 	mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
4729 	mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
4730 }
4731 
4732 /**
4733  *	t4_sge_stop - disable SGE operation
4734  *	@adap: the adapter
4735  *
4736  *	Stop tasklets and timers associated with the DMA engine.  Note that
4737  *	this is effective only if measures have been taken to disable any HW
4738  *	events that may restart them.
4739  */
4740 void t4_sge_stop(struct adapter *adap)
4741 {
4742 	int i;
4743 	struct sge *s = &adap->sge;
4744 
4745 	if (in_interrupt())  /* actions below require waiting */
4746 		return;
4747 
4748 	if (s->rx_timer.function)
4749 		del_timer_sync(&s->rx_timer);
4750 	if (s->tx_timer.function)
4751 		del_timer_sync(&s->tx_timer);
4752 
4753 	if (is_offload(adap)) {
4754 		struct sge_uld_txq_info *txq_info;
4755 
4756 		txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
4757 		if (txq_info) {
4758 			struct sge_uld_txq *txq = txq_info->uldtxq;
4759 
4760 			for_each_ofldtxq(&adap->sge, i) {
4761 				if (txq->q.desc)
4762 					tasklet_kill(&txq->qresume_tsk);
4763 			}
4764 		}
4765 	}
4766 
4767 	if (is_pci_uld(adap)) {
4768 		struct sge_uld_txq_info *txq_info;
4769 
4770 		txq_info = adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
4771 		if (txq_info) {
4772 			struct sge_uld_txq *txq = txq_info->uldtxq;
4773 
4774 			for_each_ofldtxq(&adap->sge, i) {
4775 				if (txq->q.desc)
4776 					tasklet_kill(&txq->qresume_tsk);
4777 			}
4778 		}
4779 	}
4780 
4781 	for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
4782 		struct sge_ctrl_txq *cq = &s->ctrlq[i];
4783 
4784 		if (cq->q.desc)
4785 			tasklet_kill(&cq->qresume_tsk);
4786 	}
4787 }
4788 
4789 /**
4790  *	t4_sge_init_soft - grab core SGE values needed by SGE code
4791  *	@adap: the adapter
4792  *
4793  *	We need to grab the SGE operating parameters that we need to have
4794  *	in order to do our job and make sure we can live with them.
4795  */
4796 
4797 static int t4_sge_init_soft(struct adapter *adap)
4798 {
4799 	struct sge *s = &adap->sge;
4800 	u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
4801 	u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
4802 	u32 ingress_rx_threshold;
4803 
4804 	/*
4805 	 * Verify that CPL messages are going to the Ingress Queue for
4806 	 * process_responses() and that only packet data is going to the
4807 	 * Free Lists.
4808 	 */
4809 	if ((t4_read_reg(adap, SGE_CONTROL_A) & RXPKTCPLMODE_F) !=
4810 	    RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
4811 		dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
4812 		return -EINVAL;
4813 	}
4814 
4815 	/*
4816 	 * Validate the Host Buffer Register Array indices that we want to
4817 	 * use ...
4818 	 *
4819 	 * XXX Note that we should really read through the Host Buffer Size
4820 	 * XXX register array and find the indices of the Buffer Sizes which
4821 	 * XXX meet our needs!
4822 	 */
4823 	#define READ_FL_BUF(x) \
4824 		t4_read_reg(adap, SGE_FL_BUFFER_SIZE0_A+(x)*sizeof(u32))
4825 
4826 	fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
4827 	fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
4828 	fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
4829 	fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
4830 
4831 	/* We only bother using the Large Page logic if the Large Page Buffer
4832 	 * is larger than our Page Size Buffer.
4833 	 */
4834 	if (fl_large_pg <= fl_small_pg)
4835 		fl_large_pg = 0;
4836 
4837 	#undef READ_FL_BUF
4838 
4839 	/* The Page Size Buffer must be exactly equal to our Page Size and the
4840 	 * Large Page Size Buffer should be 0 (per above) or a power of 2.
4841 	 */
4842 	if (fl_small_pg != PAGE_SIZE ||
4843 	    (fl_large_pg & (fl_large_pg-1)) != 0) {
4844 		dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
4845 			fl_small_pg, fl_large_pg);
4846 		return -EINVAL;
4847 	}
4848 	if (fl_large_pg)
4849 		s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
4850 
4851 	if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
4852 	    fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
4853 		dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
4854 			fl_small_mtu, fl_large_mtu);
4855 		return -EINVAL;
4856 	}
4857 
4858 	/*
4859 	 * Retrieve our RX interrupt holdoff timer values and counter
4860 	 * threshold values from the SGE parameters.
4861 	 */
4862 	timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1_A);
4863 	timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3_A);
4864 	timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5_A);
4865 	s->timer_val[0] = core_ticks_to_us(adap,
4866 		TIMERVALUE0_G(timer_value_0_and_1));
4867 	s->timer_val[1] = core_ticks_to_us(adap,
4868 		TIMERVALUE1_G(timer_value_0_and_1));
4869 	s->timer_val[2] = core_ticks_to_us(adap,
4870 		TIMERVALUE2_G(timer_value_2_and_3));
4871 	s->timer_val[3] = core_ticks_to_us(adap,
4872 		TIMERVALUE3_G(timer_value_2_and_3));
4873 	s->timer_val[4] = core_ticks_to_us(adap,
4874 		TIMERVALUE4_G(timer_value_4_and_5));
4875 	s->timer_val[5] = core_ticks_to_us(adap,
4876 		TIMERVALUE5_G(timer_value_4_and_5));
4877 
4878 	ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD_A);
4879 	s->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
4880 	s->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
4881 	s->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
4882 	s->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
4883 
4884 	return 0;
4885 }
4886 
4887 /**
4888  *     t4_sge_init - initialize SGE
4889  *     @adap: the adapter
4890  *
4891  *     Perform low-level SGE code initialization needed every time after a
4892  *     chip reset.
4893  */
4894 int t4_sge_init(struct adapter *adap)
4895 {
4896 	struct sge *s = &adap->sge;
4897 	u32 sge_control, sge_conm_ctrl;
4898 	int ret, egress_threshold;
4899 
4900 	/*
4901 	 * Ingress Padding Boundary and Egress Status Page Size are set up by
4902 	 * t4_fixup_host_params().
4903 	 */
4904 	sge_control = t4_read_reg(adap, SGE_CONTROL_A);
4905 	s->pktshift = PKTSHIFT_G(sge_control);
4906 	s->stat_len = (sge_control & EGRSTATUSPAGESIZE_F) ? 128 : 64;
4907 
4908 	s->fl_align = t4_fl_pkt_align(adap);
4909 	ret = t4_sge_init_soft(adap);
4910 	if (ret < 0)
4911 		return ret;
4912 
4913 	/*
4914 	 * A FL with <= fl_starve_thres buffers is starving and a periodic
4915 	 * timer will attempt to refill it.  This needs to be larger than the
4916 	 * SGE's Egress Congestion Threshold.  If it isn't, then we can get
4917 	 * stuck waiting for new packets while the SGE is waiting for us to
4918 	 * give it more Free List entries.  (Note that the SGE's Egress
4919 	 * Congestion Threshold is in units of 2 Free List pointers.) For T4,
4920 	 * there was only a single field to control this.  For T5 there's the
4921 	 * original field which now only applies to Unpacked Mode Free List
4922 	 * buffers and a new field which only applies to Packed Mode Free List
4923 	 * buffers.
4924 	 */
4925 	sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL_A);
4926 	switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
4927 	case CHELSIO_T4:
4928 		egress_threshold = EGRTHRESHOLD_G(sge_conm_ctrl);
4929 		break;
4930 	case CHELSIO_T5:
4931 		egress_threshold = EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
4932 		break;
4933 	case CHELSIO_T6:
4934 		egress_threshold = T6_EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
4935 		break;
4936 	default:
4937 		dev_err(adap->pdev_dev, "Unsupported Chip version %d\n",
4938 			CHELSIO_CHIP_VERSION(adap->params.chip));
4939 		return -EINVAL;
4940 	}
4941 	s->fl_starve_thres = 2*egress_threshold + 1;
4942 
4943 	t4_idma_monitor_init(adap, &s->idma_monitor);
4944 
4945 	/* Set up timers used for recuring callbacks to process RX and TX
4946 	 * administrative tasks.
4947 	 */
4948 	timer_setup(&s->rx_timer, sge_rx_timer_cb, 0);
4949 	timer_setup(&s->tx_timer, sge_tx_timer_cb, 0);
4950 
4951 	spin_lock_init(&s->intrq_lock);
4952 
4953 	return 0;
4954 }
4955