18c2ecf20Sopenharmony_ci/*****************************************************************************
28c2ecf20Sopenharmony_ci *                                                                           *
38c2ecf20Sopenharmony_ci * File: sge.c                                                               *
48c2ecf20Sopenharmony_ci * $Revision: 1.26 $                                                         *
58c2ecf20Sopenharmony_ci * $Date: 2005/06/21 18:29:48 $                                              *
68c2ecf20Sopenharmony_ci * Description:                                                              *
78c2ecf20Sopenharmony_ci *  DMA engine.                                                              *
88c2ecf20Sopenharmony_ci *  part of the Chelsio 10Gb Ethernet Driver.                                *
98c2ecf20Sopenharmony_ci *                                                                           *
108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify      *
118c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License, version 2, as       *
128c2ecf20Sopenharmony_ci * published by the Free Software Foundation.                                *
138c2ecf20Sopenharmony_ci *                                                                           *
148c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License along   *
158c2ecf20Sopenharmony_ci * with this program; if not, see <http://www.gnu.org/licenses/>.            *
168c2ecf20Sopenharmony_ci *                                                                           *
178c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED    *
188c2ecf20Sopenharmony_ci * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF      *
198c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.                     *
208c2ecf20Sopenharmony_ci *                                                                           *
218c2ecf20Sopenharmony_ci * http://www.chelsio.com                                                    *
228c2ecf20Sopenharmony_ci *                                                                           *
238c2ecf20Sopenharmony_ci * Copyright (c) 2003 - 2005 Chelsio Communications, Inc.                    *
248c2ecf20Sopenharmony_ci * All rights reserved.                                                      *
258c2ecf20Sopenharmony_ci *                                                                           *
268c2ecf20Sopenharmony_ci * Maintainers: maintainers@chelsio.com                                      *
278c2ecf20Sopenharmony_ci *                                                                           *
288c2ecf20Sopenharmony_ci * Authors: Dimitrios Michailidis   <dm@chelsio.com>                         *
298c2ecf20Sopenharmony_ci *          Tina Yang               <tainay@chelsio.com>                     *
308c2ecf20Sopenharmony_ci *          Felix Marti             <felix@chelsio.com>                      *
318c2ecf20Sopenharmony_ci *          Scott Bardone           <sbardone@chelsio.com>                   *
328c2ecf20Sopenharmony_ci *          Kurt Ottaway            <kottaway@chelsio.com>                   *
338c2ecf20Sopenharmony_ci *          Frank DiMambro          <frank@chelsio.com>                      *
348c2ecf20Sopenharmony_ci *                                                                           *
358c2ecf20Sopenharmony_ci * History:                                                                  *
368c2ecf20Sopenharmony_ci *                                                                           *
378c2ecf20Sopenharmony_ci ****************************************************************************/
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include "common.h"
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#include <linux/types.h>
428c2ecf20Sopenharmony_ci#include <linux/errno.h>
438c2ecf20Sopenharmony_ci#include <linux/pci.h>
448c2ecf20Sopenharmony_ci#include <linux/ktime.h>
458c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
468c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
478c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
488c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
498c2ecf20Sopenharmony_ci#include <linux/mm.h>
508c2ecf20Sopenharmony_ci#include <linux/tcp.h>
518c2ecf20Sopenharmony_ci#include <linux/ip.h>
528c2ecf20Sopenharmony_ci#include <linux/in.h>
538c2ecf20Sopenharmony_ci#include <linux/if_arp.h>
548c2ecf20Sopenharmony_ci#include <linux/slab.h>
558c2ecf20Sopenharmony_ci#include <linux/prefetch.h>
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#include "cpl5_cmd.h"
588c2ecf20Sopenharmony_ci#include "sge.h"
598c2ecf20Sopenharmony_ci#include "regs.h"
608c2ecf20Sopenharmony_ci#include "espi.h"
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* This belongs in if_ether.h */
638c2ecf20Sopenharmony_ci#define ETH_P_CPL5 0xf
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define SGE_CMDQ_N		2
668c2ecf20Sopenharmony_ci#define SGE_FREELQ_N		2
678c2ecf20Sopenharmony_ci#define SGE_CMDQ0_E_N		1024
688c2ecf20Sopenharmony_ci#define SGE_CMDQ1_E_N		128
698c2ecf20Sopenharmony_ci#define SGE_FREEL_SIZE		4096
708c2ecf20Sopenharmony_ci#define SGE_JUMBO_FREEL_SIZE	512
718c2ecf20Sopenharmony_ci#define SGE_FREEL_REFILL_THRESH	16
728c2ecf20Sopenharmony_ci#define SGE_RESPQ_E_N		1024
738c2ecf20Sopenharmony_ci#define SGE_INTRTIMER_NRES	1000
748c2ecf20Sopenharmony_ci#define SGE_RX_SM_BUF_SIZE	1536
758c2ecf20Sopenharmony_ci#define SGE_TX_DESC_MAX_PLEN	16384
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define SGE_RESPQ_REPLENISH_THRES (SGE_RESPQ_E_N / 4)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/*
808c2ecf20Sopenharmony_ci * Period of the TX buffer reclaim timer.  This timer does not need to run
818c2ecf20Sopenharmony_ci * frequently as TX buffers are usually reclaimed by new TX packets.
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_ci#define TX_RECLAIM_PERIOD (HZ / 4)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define M_CMD_LEN       0x7fffffff
868c2ecf20Sopenharmony_ci#define V_CMD_LEN(v)    (v)
878c2ecf20Sopenharmony_ci#define G_CMD_LEN(v)    ((v) & M_CMD_LEN)
888c2ecf20Sopenharmony_ci#define V_CMD_GEN1(v)   ((v) << 31)
898c2ecf20Sopenharmony_ci#define V_CMD_GEN2(v)   (v)
908c2ecf20Sopenharmony_ci#define F_CMD_DATAVALID (1 << 1)
918c2ecf20Sopenharmony_ci#define F_CMD_SOP       (1 << 2)
928c2ecf20Sopenharmony_ci#define V_CMD_EOP(v)    ((v) << 3)
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/*
958c2ecf20Sopenharmony_ci * Command queue, receive buffer list, and response queue descriptors.
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_ci#if defined(__BIG_ENDIAN_BITFIELD)
988c2ecf20Sopenharmony_cistruct cmdQ_e {
998c2ecf20Sopenharmony_ci	u32 addr_lo;
1008c2ecf20Sopenharmony_ci	u32 len_gen;
1018c2ecf20Sopenharmony_ci	u32 flags;
1028c2ecf20Sopenharmony_ci	u32 addr_hi;
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistruct freelQ_e {
1068c2ecf20Sopenharmony_ci	u32 addr_lo;
1078c2ecf20Sopenharmony_ci	u32 len_gen;
1088c2ecf20Sopenharmony_ci	u32 gen2;
1098c2ecf20Sopenharmony_ci	u32 addr_hi;
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistruct respQ_e {
1138c2ecf20Sopenharmony_ci	u32 Qsleeping		: 4;
1148c2ecf20Sopenharmony_ci	u32 Cmdq1CreditReturn	: 5;
1158c2ecf20Sopenharmony_ci	u32 Cmdq1DmaComplete	: 5;
1168c2ecf20Sopenharmony_ci	u32 Cmdq0CreditReturn	: 5;
1178c2ecf20Sopenharmony_ci	u32 Cmdq0DmaComplete	: 5;
1188c2ecf20Sopenharmony_ci	u32 FreelistQid		: 2;
1198c2ecf20Sopenharmony_ci	u32 CreditValid		: 1;
1208c2ecf20Sopenharmony_ci	u32 DataValid		: 1;
1218c2ecf20Sopenharmony_ci	u32 Offload		: 1;
1228c2ecf20Sopenharmony_ci	u32 Eop			: 1;
1238c2ecf20Sopenharmony_ci	u32 Sop			: 1;
1248c2ecf20Sopenharmony_ci	u32 GenerationBit	: 1;
1258c2ecf20Sopenharmony_ci	u32 BufferLength;
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci#elif defined(__LITTLE_ENDIAN_BITFIELD)
1288c2ecf20Sopenharmony_cistruct cmdQ_e {
1298c2ecf20Sopenharmony_ci	u32 len_gen;
1308c2ecf20Sopenharmony_ci	u32 addr_lo;
1318c2ecf20Sopenharmony_ci	u32 addr_hi;
1328c2ecf20Sopenharmony_ci	u32 flags;
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistruct freelQ_e {
1368c2ecf20Sopenharmony_ci	u32 len_gen;
1378c2ecf20Sopenharmony_ci	u32 addr_lo;
1388c2ecf20Sopenharmony_ci	u32 addr_hi;
1398c2ecf20Sopenharmony_ci	u32 gen2;
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistruct respQ_e {
1438c2ecf20Sopenharmony_ci	u32 BufferLength;
1448c2ecf20Sopenharmony_ci	u32 GenerationBit	: 1;
1458c2ecf20Sopenharmony_ci	u32 Sop			: 1;
1468c2ecf20Sopenharmony_ci	u32 Eop			: 1;
1478c2ecf20Sopenharmony_ci	u32 Offload		: 1;
1488c2ecf20Sopenharmony_ci	u32 DataValid		: 1;
1498c2ecf20Sopenharmony_ci	u32 CreditValid		: 1;
1508c2ecf20Sopenharmony_ci	u32 FreelistQid		: 2;
1518c2ecf20Sopenharmony_ci	u32 Cmdq0DmaComplete	: 5;
1528c2ecf20Sopenharmony_ci	u32 Cmdq0CreditReturn	: 5;
1538c2ecf20Sopenharmony_ci	u32 Cmdq1DmaComplete	: 5;
1548c2ecf20Sopenharmony_ci	u32 Cmdq1CreditReturn	: 5;
1558c2ecf20Sopenharmony_ci	u32 Qsleeping		: 4;
1568c2ecf20Sopenharmony_ci} ;
1578c2ecf20Sopenharmony_ci#endif
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/*
1608c2ecf20Sopenharmony_ci * SW Context Command and Freelist Queue Descriptors
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_cistruct cmdQ_ce {
1638c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1648c2ecf20Sopenharmony_ci	DEFINE_DMA_UNMAP_ADDR(dma_addr);
1658c2ecf20Sopenharmony_ci	DEFINE_DMA_UNMAP_LEN(dma_len);
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistruct freelQ_ce {
1698c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1708c2ecf20Sopenharmony_ci	DEFINE_DMA_UNMAP_ADDR(dma_addr);
1718c2ecf20Sopenharmony_ci	DEFINE_DMA_UNMAP_LEN(dma_len);
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/*
1758c2ecf20Sopenharmony_ci * SW command, freelist and response rings
1768c2ecf20Sopenharmony_ci */
1778c2ecf20Sopenharmony_cistruct cmdQ {
1788c2ecf20Sopenharmony_ci	unsigned long   status;         /* HW DMA fetch status */
1798c2ecf20Sopenharmony_ci	unsigned int    in_use;         /* # of in-use command descriptors */
1808c2ecf20Sopenharmony_ci	unsigned int	size;	        /* # of descriptors */
1818c2ecf20Sopenharmony_ci	unsigned int    processed;      /* total # of descs HW has processed */
1828c2ecf20Sopenharmony_ci	unsigned int    cleaned;        /* total # of descs SW has reclaimed */
1838c2ecf20Sopenharmony_ci	unsigned int    stop_thres;     /* SW TX queue suspend threshold */
1848c2ecf20Sopenharmony_ci	u16		pidx;           /* producer index (SW) */
1858c2ecf20Sopenharmony_ci	u16		cidx;           /* consumer index (HW) */
1868c2ecf20Sopenharmony_ci	u8		genbit;         /* current generation (=valid) bit */
1878c2ecf20Sopenharmony_ci	u8              sop;            /* is next entry start of packet? */
1888c2ecf20Sopenharmony_ci	struct cmdQ_e  *entries;        /* HW command descriptor Q */
1898c2ecf20Sopenharmony_ci	struct cmdQ_ce *centries;       /* SW command context descriptor Q */
1908c2ecf20Sopenharmony_ci	dma_addr_t	dma_addr;       /* DMA addr HW command descriptor Q */
1918c2ecf20Sopenharmony_ci	spinlock_t	lock;           /* Lock to protect cmdQ enqueuing */
1928c2ecf20Sopenharmony_ci};
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistruct freelQ {
1958c2ecf20Sopenharmony_ci	unsigned int	credits;        /* # of available RX buffers */
1968c2ecf20Sopenharmony_ci	unsigned int	size;	        /* free list capacity */
1978c2ecf20Sopenharmony_ci	u16		pidx;           /* producer index (SW) */
1988c2ecf20Sopenharmony_ci	u16		cidx;           /* consumer index (HW) */
1998c2ecf20Sopenharmony_ci	u16		rx_buffer_size; /* Buffer size on this free list */
2008c2ecf20Sopenharmony_ci	u16             dma_offset;     /* DMA offset to align IP headers */
2018c2ecf20Sopenharmony_ci	u16             recycleq_idx;   /* skb recycle q to use */
2028c2ecf20Sopenharmony_ci	u8		genbit;	        /* current generation (=valid) bit */
2038c2ecf20Sopenharmony_ci	struct freelQ_e	*entries;       /* HW freelist descriptor Q */
2048c2ecf20Sopenharmony_ci	struct freelQ_ce *centries;     /* SW freelist context descriptor Q */
2058c2ecf20Sopenharmony_ci	dma_addr_t	dma_addr;       /* DMA addr HW freelist descriptor Q */
2068c2ecf20Sopenharmony_ci};
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistruct respQ {
2098c2ecf20Sopenharmony_ci	unsigned int	credits;        /* credits to be returned to SGE */
2108c2ecf20Sopenharmony_ci	unsigned int	size;	        /* # of response Q descriptors */
2118c2ecf20Sopenharmony_ci	u16		cidx;	        /* consumer index (SW) */
2128c2ecf20Sopenharmony_ci	u8		genbit;	        /* current generation(=valid) bit */
2138c2ecf20Sopenharmony_ci	struct respQ_e *entries;        /* HW response descriptor Q */
2148c2ecf20Sopenharmony_ci	dma_addr_t	dma_addr;       /* DMA addr HW response descriptor Q */
2158c2ecf20Sopenharmony_ci};
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/* Bit flags for cmdQ.status */
2188c2ecf20Sopenharmony_cienum {
2198c2ecf20Sopenharmony_ci	CMDQ_STAT_RUNNING = 1,          /* fetch engine is running */
2208c2ecf20Sopenharmony_ci	CMDQ_STAT_LAST_PKT_DB = 2       /* last packet rung the doorbell */
2218c2ecf20Sopenharmony_ci};
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci/* T204 TX SW scheduler */
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci/* Per T204 TX port */
2268c2ecf20Sopenharmony_cistruct sched_port {
2278c2ecf20Sopenharmony_ci	unsigned int	avail;		/* available bits - quota */
2288c2ecf20Sopenharmony_ci	unsigned int	drain_bits_per_1024ns; /* drain rate */
2298c2ecf20Sopenharmony_ci	unsigned int	speed;		/* drain rate, mbps */
2308c2ecf20Sopenharmony_ci	unsigned int	mtu;		/* mtu size */
2318c2ecf20Sopenharmony_ci	struct sk_buff_head skbq;	/* pending skbs */
2328c2ecf20Sopenharmony_ci};
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci/* Per T204 device */
2358c2ecf20Sopenharmony_cistruct sched {
2368c2ecf20Sopenharmony_ci	ktime_t         last_updated;   /* last time quotas were computed */
2378c2ecf20Sopenharmony_ci	unsigned int	max_avail;	/* max bits to be sent to any port */
2388c2ecf20Sopenharmony_ci	unsigned int	port;		/* port index (round robin ports) */
2398c2ecf20Sopenharmony_ci	unsigned int	num;		/* num skbs in per port queues */
2408c2ecf20Sopenharmony_ci	struct sched_port p[MAX_NPORTS];
2418c2ecf20Sopenharmony_ci	struct tasklet_struct sched_tsk;/* tasklet used to run scheduler */
2428c2ecf20Sopenharmony_ci	struct sge *sge;
2438c2ecf20Sopenharmony_ci};
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic void restart_sched(struct tasklet_struct *t);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci/*
2498c2ecf20Sopenharmony_ci * Main SGE data structure
2508c2ecf20Sopenharmony_ci *
2518c2ecf20Sopenharmony_ci * Interrupts are handled by a single CPU and it is likely that on a MP system
2528c2ecf20Sopenharmony_ci * the application is migrated to another CPU. In that scenario, we try to
2538c2ecf20Sopenharmony_ci * separate the RX(in irq context) and TX state in order to decrease memory
2548c2ecf20Sopenharmony_ci * contention.
2558c2ecf20Sopenharmony_ci */
2568c2ecf20Sopenharmony_cistruct sge {
2578c2ecf20Sopenharmony_ci	struct adapter *adapter;	/* adapter backpointer */
2588c2ecf20Sopenharmony_ci	struct net_device *netdev;      /* netdevice backpointer */
2598c2ecf20Sopenharmony_ci	struct freelQ	freelQ[SGE_FREELQ_N]; /* buffer free lists */
2608c2ecf20Sopenharmony_ci	struct respQ	respQ;		/* response Q */
2618c2ecf20Sopenharmony_ci	unsigned long   stopped_tx_queues; /* bitmap of suspended Tx queues */
2628c2ecf20Sopenharmony_ci	unsigned int	rx_pkt_pad;     /* RX padding for L2 packets */
2638c2ecf20Sopenharmony_ci	unsigned int	jumbo_fl;       /* jumbo freelist Q index */
2648c2ecf20Sopenharmony_ci	unsigned int	intrtimer_nres;	/* no-resource interrupt timer */
2658c2ecf20Sopenharmony_ci	unsigned int    fixed_intrtimer;/* non-adaptive interrupt timer */
2668c2ecf20Sopenharmony_ci	struct timer_list tx_reclaim_timer; /* reclaims TX buffers */
2678c2ecf20Sopenharmony_ci	struct timer_list espibug_timer;
2688c2ecf20Sopenharmony_ci	unsigned long	espibug_timeout;
2698c2ecf20Sopenharmony_ci	struct sk_buff	*espibug_skb[MAX_NPORTS];
2708c2ecf20Sopenharmony_ci	u32		sge_control;	/* shadow value of sge control reg */
2718c2ecf20Sopenharmony_ci	struct sge_intr_counts stats;
2728c2ecf20Sopenharmony_ci	struct sge_port_stats __percpu *port_stats[MAX_NPORTS];
2738c2ecf20Sopenharmony_ci	struct sched	*tx_sched;
2748c2ecf20Sopenharmony_ci	struct cmdQ cmdQ[SGE_CMDQ_N] ____cacheline_aligned_in_smp;
2758c2ecf20Sopenharmony_ci};
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic const u8 ch_mac_addr[ETH_ALEN] = {
2788c2ecf20Sopenharmony_ci	0x0, 0x7, 0x43, 0x0, 0x0, 0x0
2798c2ecf20Sopenharmony_ci};
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci/*
2828c2ecf20Sopenharmony_ci * stop tasklet and free all pending skb's
2838c2ecf20Sopenharmony_ci */
2848c2ecf20Sopenharmony_cistatic void tx_sched_stop(struct sge *sge)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	struct sched *s = sge->tx_sched;
2878c2ecf20Sopenharmony_ci	int i;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	tasklet_kill(&s->sched_tsk);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_NPORTS; i++)
2928c2ecf20Sopenharmony_ci		__skb_queue_purge(&s->p[s->port].skbq);
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci/*
2968c2ecf20Sopenharmony_ci * t1_sched_update_parms() is called when the MTU or link speed changes. It
2978c2ecf20Sopenharmony_ci * re-computes scheduler parameters to scope with the change.
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_ciunsigned int t1_sched_update_parms(struct sge *sge, unsigned int port,
3008c2ecf20Sopenharmony_ci				   unsigned int mtu, unsigned int speed)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct sched *s = sge->tx_sched;
3038c2ecf20Sopenharmony_ci	struct sched_port *p = &s->p[port];
3048c2ecf20Sopenharmony_ci	unsigned int max_avail_segs;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	pr_debug("%s mtu=%d speed=%d\n", __func__, mtu, speed);
3078c2ecf20Sopenharmony_ci	if (speed)
3088c2ecf20Sopenharmony_ci		p->speed = speed;
3098c2ecf20Sopenharmony_ci	if (mtu)
3108c2ecf20Sopenharmony_ci		p->mtu = mtu;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	if (speed || mtu) {
3138c2ecf20Sopenharmony_ci		unsigned long long drain = 1024ULL * p->speed * (p->mtu - 40);
3148c2ecf20Sopenharmony_ci		do_div(drain, (p->mtu + 50) * 1000);
3158c2ecf20Sopenharmony_ci		p->drain_bits_per_1024ns = (unsigned int) drain;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci		if (p->speed < 1000)
3188c2ecf20Sopenharmony_ci			p->drain_bits_per_1024ns =
3198c2ecf20Sopenharmony_ci				90 * p->drain_bits_per_1024ns / 100;
3208c2ecf20Sopenharmony_ci	}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204) {
3238c2ecf20Sopenharmony_ci		p->drain_bits_per_1024ns -= 16;
3248c2ecf20Sopenharmony_ci		s->max_avail = max(4096U, p->mtu + 16 + 14 + 4);
3258c2ecf20Sopenharmony_ci		max_avail_segs = max(1U, 4096 / (p->mtu - 40));
3268c2ecf20Sopenharmony_ci	} else {
3278c2ecf20Sopenharmony_ci		s->max_avail = 16384;
3288c2ecf20Sopenharmony_ci		max_avail_segs = max(1U, 9000 / (p->mtu - 40));
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	pr_debug("t1_sched_update_parms: mtu %u speed %u max_avail %u "
3328c2ecf20Sopenharmony_ci		 "max_avail_segs %u drain_bits_per_1024ns %u\n", p->mtu,
3338c2ecf20Sopenharmony_ci		 p->speed, s->max_avail, max_avail_segs,
3348c2ecf20Sopenharmony_ci		 p->drain_bits_per_1024ns);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	return max_avail_segs * (p->mtu - 40);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci#if 0
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci/*
3428c2ecf20Sopenharmony_ci * t1_sched_max_avail_bytes() tells the scheduler the maximum amount of
3438c2ecf20Sopenharmony_ci * data that can be pushed per port.
3448c2ecf20Sopenharmony_ci */
3458c2ecf20Sopenharmony_civoid t1_sched_set_max_avail_bytes(struct sge *sge, unsigned int val)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	struct sched *s = sge->tx_sched;
3488c2ecf20Sopenharmony_ci	unsigned int i;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	s->max_avail = val;
3518c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_NPORTS; i++)
3528c2ecf20Sopenharmony_ci		t1_sched_update_parms(sge, i, 0, 0);
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci/*
3568c2ecf20Sopenharmony_ci * t1_sched_set_drain_bits_per_us() tells the scheduler at which rate a port
3578c2ecf20Sopenharmony_ci * is draining.
3588c2ecf20Sopenharmony_ci */
3598c2ecf20Sopenharmony_civoid t1_sched_set_drain_bits_per_us(struct sge *sge, unsigned int port,
3608c2ecf20Sopenharmony_ci					 unsigned int val)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	struct sched *s = sge->tx_sched;
3638c2ecf20Sopenharmony_ci	struct sched_port *p = &s->p[port];
3648c2ecf20Sopenharmony_ci	p->drain_bits_per_1024ns = val * 1024 / 1000;
3658c2ecf20Sopenharmony_ci	t1_sched_update_parms(sge, port, 0, 0);
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci#endif  /*  0  */
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci/*
3718c2ecf20Sopenharmony_ci * tx_sched_init() allocates resources and does basic initialization.
3728c2ecf20Sopenharmony_ci */
3738c2ecf20Sopenharmony_cistatic int tx_sched_init(struct sge *sge)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct sched *s;
3768c2ecf20Sopenharmony_ci	int i;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	s = kzalloc(sizeof (struct sched), GFP_KERNEL);
3798c2ecf20Sopenharmony_ci	if (!s)
3808c2ecf20Sopenharmony_ci		return -ENOMEM;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	pr_debug("tx_sched_init\n");
3838c2ecf20Sopenharmony_ci	tasklet_setup(&s->sched_tsk, restart_sched);
3848c2ecf20Sopenharmony_ci	s->sge = sge;
3858c2ecf20Sopenharmony_ci	sge->tx_sched = s;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_NPORTS; i++) {
3888c2ecf20Sopenharmony_ci		skb_queue_head_init(&s->p[i].skbq);
3898c2ecf20Sopenharmony_ci		t1_sched_update_parms(sge, i, 1500, 1000);
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	return 0;
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci/*
3968c2ecf20Sopenharmony_ci * sched_update_avail() computes the delta since the last time it was called
3978c2ecf20Sopenharmony_ci * and updates the per port quota (number of bits that can be sent to the any
3988c2ecf20Sopenharmony_ci * port).
3998c2ecf20Sopenharmony_ci */
4008c2ecf20Sopenharmony_cistatic inline int sched_update_avail(struct sge *sge)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	struct sched *s = sge->tx_sched;
4038c2ecf20Sopenharmony_ci	ktime_t now = ktime_get();
4048c2ecf20Sopenharmony_ci	unsigned int i;
4058c2ecf20Sopenharmony_ci	long long delta_time_ns;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	delta_time_ns = ktime_to_ns(ktime_sub(now, s->last_updated));
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	pr_debug("sched_update_avail delta=%lld\n", delta_time_ns);
4108c2ecf20Sopenharmony_ci	if (delta_time_ns < 15000)
4118c2ecf20Sopenharmony_ci		return 0;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_NPORTS; i++) {
4148c2ecf20Sopenharmony_ci		struct sched_port *p = &s->p[i];
4158c2ecf20Sopenharmony_ci		unsigned int delta_avail;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci		delta_avail = (p->drain_bits_per_1024ns * delta_time_ns) >> 13;
4188c2ecf20Sopenharmony_ci		p->avail = min(p->avail + delta_avail, s->max_avail);
4198c2ecf20Sopenharmony_ci	}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	s->last_updated = now;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	return 1;
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci/*
4278c2ecf20Sopenharmony_ci * sched_skb() is called from two different places. In the tx path, any
4288c2ecf20Sopenharmony_ci * packet generating load on an output port will call sched_skb()
4298c2ecf20Sopenharmony_ci * (skb != NULL). In addition, sched_skb() is called from the irq/soft irq
4308c2ecf20Sopenharmony_ci * context (skb == NULL).
4318c2ecf20Sopenharmony_ci * The scheduler only returns a skb (which will then be sent) if the
4328c2ecf20Sopenharmony_ci * length of the skb is <= the current quota of the output port.
4338c2ecf20Sopenharmony_ci */
4348c2ecf20Sopenharmony_cistatic struct sk_buff *sched_skb(struct sge *sge, struct sk_buff *skb,
4358c2ecf20Sopenharmony_ci				unsigned int credits)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct sched *s = sge->tx_sched;
4388c2ecf20Sopenharmony_ci	struct sk_buff_head *skbq;
4398c2ecf20Sopenharmony_ci	unsigned int i, len, update = 1;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	pr_debug("sched_skb %p\n", skb);
4428c2ecf20Sopenharmony_ci	if (!skb) {
4438c2ecf20Sopenharmony_ci		if (!s->num)
4448c2ecf20Sopenharmony_ci			return NULL;
4458c2ecf20Sopenharmony_ci	} else {
4468c2ecf20Sopenharmony_ci		skbq = &s->p[skb->dev->if_port].skbq;
4478c2ecf20Sopenharmony_ci		__skb_queue_tail(skbq, skb);
4488c2ecf20Sopenharmony_ci		s->num++;
4498c2ecf20Sopenharmony_ci		skb = NULL;
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	if (credits < MAX_SKB_FRAGS + 1)
4538c2ecf20Sopenharmony_ci		goto out;
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ciagain:
4568c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_NPORTS; i++) {
4578c2ecf20Sopenharmony_ci		s->port = (s->port + 1) & (MAX_NPORTS - 1);
4588c2ecf20Sopenharmony_ci		skbq = &s->p[s->port].skbq;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci		skb = skb_peek(skbq);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci		if (!skb)
4638c2ecf20Sopenharmony_ci			continue;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci		len = skb->len;
4668c2ecf20Sopenharmony_ci		if (len <= s->p[s->port].avail) {
4678c2ecf20Sopenharmony_ci			s->p[s->port].avail -= len;
4688c2ecf20Sopenharmony_ci			s->num--;
4698c2ecf20Sopenharmony_ci			__skb_unlink(skb, skbq);
4708c2ecf20Sopenharmony_ci			goto out;
4718c2ecf20Sopenharmony_ci		}
4728c2ecf20Sopenharmony_ci		skb = NULL;
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	if (update-- && sched_update_avail(sge))
4768c2ecf20Sopenharmony_ci		goto again;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ciout:
4798c2ecf20Sopenharmony_ci	/* If there are more pending skbs, we use the hardware to schedule us
4808c2ecf20Sopenharmony_ci	 * again.
4818c2ecf20Sopenharmony_ci	 */
4828c2ecf20Sopenharmony_ci	if (s->num && !skb) {
4838c2ecf20Sopenharmony_ci		struct cmdQ *q = &sge->cmdQ[0];
4848c2ecf20Sopenharmony_ci		clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
4858c2ecf20Sopenharmony_ci		if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
4868c2ecf20Sopenharmony_ci			set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
4878c2ecf20Sopenharmony_ci			writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
4888c2ecf20Sopenharmony_ci		}
4898c2ecf20Sopenharmony_ci	}
4908c2ecf20Sopenharmony_ci	pr_debug("sched_skb ret %p\n", skb);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	return skb;
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci/*
4968c2ecf20Sopenharmony_ci * PIO to indicate that memory mapped Q contains valid descriptor(s).
4978c2ecf20Sopenharmony_ci */
4988c2ecf20Sopenharmony_cistatic inline void doorbell_pio(struct adapter *adapter, u32 val)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	wmb();
5018c2ecf20Sopenharmony_ci	writel(val, adapter->regs + A_SG_DOORBELL);
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci/*
5058c2ecf20Sopenharmony_ci * Frees all RX buffers on the freelist Q. The caller must make sure that
5068c2ecf20Sopenharmony_ci * the SGE is turned off before calling this function.
5078c2ecf20Sopenharmony_ci */
5088c2ecf20Sopenharmony_cistatic void free_freelQ_buffers(struct pci_dev *pdev, struct freelQ *q)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	unsigned int cidx = q->cidx;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	while (q->credits--) {
5138c2ecf20Sopenharmony_ci		struct freelQ_ce *ce = &q->centries[cidx];
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci		dma_unmap_single(&pdev->dev, dma_unmap_addr(ce, dma_addr),
5168c2ecf20Sopenharmony_ci				 dma_unmap_len(ce, dma_len), DMA_FROM_DEVICE);
5178c2ecf20Sopenharmony_ci		dev_kfree_skb(ce->skb);
5188c2ecf20Sopenharmony_ci		ce->skb = NULL;
5198c2ecf20Sopenharmony_ci		if (++cidx == q->size)
5208c2ecf20Sopenharmony_ci			cidx = 0;
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci/*
5258c2ecf20Sopenharmony_ci * Free RX free list and response queue resources.
5268c2ecf20Sopenharmony_ci */
5278c2ecf20Sopenharmony_cistatic void free_rx_resources(struct sge *sge)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	struct pci_dev *pdev = sge->adapter->pdev;
5308c2ecf20Sopenharmony_ci	unsigned int size, i;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	if (sge->respQ.entries) {
5338c2ecf20Sopenharmony_ci		size = sizeof(struct respQ_e) * sge->respQ.size;
5348c2ecf20Sopenharmony_ci		dma_free_coherent(&pdev->dev, size, sge->respQ.entries,
5358c2ecf20Sopenharmony_ci				  sge->respQ.dma_addr);
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	for (i = 0; i < SGE_FREELQ_N; i++) {
5398c2ecf20Sopenharmony_ci		struct freelQ *q = &sge->freelQ[i];
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci		if (q->centries) {
5428c2ecf20Sopenharmony_ci			free_freelQ_buffers(pdev, q);
5438c2ecf20Sopenharmony_ci			kfree(q->centries);
5448c2ecf20Sopenharmony_ci		}
5458c2ecf20Sopenharmony_ci		if (q->entries) {
5468c2ecf20Sopenharmony_ci			size = sizeof(struct freelQ_e) * q->size;
5478c2ecf20Sopenharmony_ci			dma_free_coherent(&pdev->dev, size, q->entries,
5488c2ecf20Sopenharmony_ci					  q->dma_addr);
5498c2ecf20Sopenharmony_ci		}
5508c2ecf20Sopenharmony_ci	}
5518c2ecf20Sopenharmony_ci}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci/*
5548c2ecf20Sopenharmony_ci * Allocates basic RX resources, consisting of memory mapped freelist Qs and a
5558c2ecf20Sopenharmony_ci * response queue.
5568c2ecf20Sopenharmony_ci */
5578c2ecf20Sopenharmony_cistatic int alloc_rx_resources(struct sge *sge, struct sge_params *p)
5588c2ecf20Sopenharmony_ci{
5598c2ecf20Sopenharmony_ci	struct pci_dev *pdev = sge->adapter->pdev;
5608c2ecf20Sopenharmony_ci	unsigned int size, i;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	for (i = 0; i < SGE_FREELQ_N; i++) {
5638c2ecf20Sopenharmony_ci		struct freelQ *q = &sge->freelQ[i];
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci		q->genbit = 1;
5668c2ecf20Sopenharmony_ci		q->size = p->freelQ_size[i];
5678c2ecf20Sopenharmony_ci		q->dma_offset = sge->rx_pkt_pad ? 0 : NET_IP_ALIGN;
5688c2ecf20Sopenharmony_ci		size = sizeof(struct freelQ_e) * q->size;
5698c2ecf20Sopenharmony_ci		q->entries = dma_alloc_coherent(&pdev->dev, size,
5708c2ecf20Sopenharmony_ci						&q->dma_addr, GFP_KERNEL);
5718c2ecf20Sopenharmony_ci		if (!q->entries)
5728c2ecf20Sopenharmony_ci			goto err_no_mem;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		size = sizeof(struct freelQ_ce) * q->size;
5758c2ecf20Sopenharmony_ci		q->centries = kzalloc(size, GFP_KERNEL);
5768c2ecf20Sopenharmony_ci		if (!q->centries)
5778c2ecf20Sopenharmony_ci			goto err_no_mem;
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	/*
5818c2ecf20Sopenharmony_ci	 * Calculate the buffer sizes for the two free lists.  FL0 accommodates
5828c2ecf20Sopenharmony_ci	 * regular sized Ethernet frames, FL1 is sized not to exceed 16K,
5838c2ecf20Sopenharmony_ci	 * including all the sk_buff overhead.
5848c2ecf20Sopenharmony_ci	 *
5858c2ecf20Sopenharmony_ci	 * Note: For T2 FL0 and FL1 are reversed.
5868c2ecf20Sopenharmony_ci	 */
5878c2ecf20Sopenharmony_ci	sge->freelQ[!sge->jumbo_fl].rx_buffer_size = SGE_RX_SM_BUF_SIZE +
5888c2ecf20Sopenharmony_ci		sizeof(struct cpl_rx_data) +
5898c2ecf20Sopenharmony_ci		sge->freelQ[!sge->jumbo_fl].dma_offset;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	size = (16 * 1024) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	sge->freelQ[sge->jumbo_fl].rx_buffer_size = size;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	/*
5968c2ecf20Sopenharmony_ci	 * Setup which skb recycle Q should be used when recycling buffers from
5978c2ecf20Sopenharmony_ci	 * each free list.
5988c2ecf20Sopenharmony_ci	 */
5998c2ecf20Sopenharmony_ci	sge->freelQ[!sge->jumbo_fl].recycleq_idx = 0;
6008c2ecf20Sopenharmony_ci	sge->freelQ[sge->jumbo_fl].recycleq_idx = 1;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	sge->respQ.genbit = 1;
6038c2ecf20Sopenharmony_ci	sge->respQ.size = SGE_RESPQ_E_N;
6048c2ecf20Sopenharmony_ci	sge->respQ.credits = 0;
6058c2ecf20Sopenharmony_ci	size = sizeof(struct respQ_e) * sge->respQ.size;
6068c2ecf20Sopenharmony_ci	sge->respQ.entries =
6078c2ecf20Sopenharmony_ci		dma_alloc_coherent(&pdev->dev, size, &sge->respQ.dma_addr,
6088c2ecf20Sopenharmony_ci				   GFP_KERNEL);
6098c2ecf20Sopenharmony_ci	if (!sge->respQ.entries)
6108c2ecf20Sopenharmony_ci		goto err_no_mem;
6118c2ecf20Sopenharmony_ci	return 0;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_cierr_no_mem:
6148c2ecf20Sopenharmony_ci	free_rx_resources(sge);
6158c2ecf20Sopenharmony_ci	return -ENOMEM;
6168c2ecf20Sopenharmony_ci}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci/*
6198c2ecf20Sopenharmony_ci * Reclaims n TX descriptors and frees the buffers associated with them.
6208c2ecf20Sopenharmony_ci */
6218c2ecf20Sopenharmony_cistatic void free_cmdQ_buffers(struct sge *sge, struct cmdQ *q, unsigned int n)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	struct cmdQ_ce *ce;
6248c2ecf20Sopenharmony_ci	struct pci_dev *pdev = sge->adapter->pdev;
6258c2ecf20Sopenharmony_ci	unsigned int cidx = q->cidx;
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	q->in_use -= n;
6288c2ecf20Sopenharmony_ci	ce = &q->centries[cidx];
6298c2ecf20Sopenharmony_ci	while (n--) {
6308c2ecf20Sopenharmony_ci		if (likely(dma_unmap_len(ce, dma_len))) {
6318c2ecf20Sopenharmony_ci			dma_unmap_single(&pdev->dev,
6328c2ecf20Sopenharmony_ci					 dma_unmap_addr(ce, dma_addr),
6338c2ecf20Sopenharmony_ci					 dma_unmap_len(ce, dma_len),
6348c2ecf20Sopenharmony_ci					 DMA_TO_DEVICE);
6358c2ecf20Sopenharmony_ci			if (q->sop)
6368c2ecf20Sopenharmony_ci				q->sop = 0;
6378c2ecf20Sopenharmony_ci		}
6388c2ecf20Sopenharmony_ci		if (ce->skb) {
6398c2ecf20Sopenharmony_ci			dev_kfree_skb_any(ce->skb);
6408c2ecf20Sopenharmony_ci			q->sop = 1;
6418c2ecf20Sopenharmony_ci		}
6428c2ecf20Sopenharmony_ci		ce++;
6438c2ecf20Sopenharmony_ci		if (++cidx == q->size) {
6448c2ecf20Sopenharmony_ci			cidx = 0;
6458c2ecf20Sopenharmony_ci			ce = q->centries;
6468c2ecf20Sopenharmony_ci		}
6478c2ecf20Sopenharmony_ci	}
6488c2ecf20Sopenharmony_ci	q->cidx = cidx;
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci/*
6528c2ecf20Sopenharmony_ci * Free TX resources.
6538c2ecf20Sopenharmony_ci *
6548c2ecf20Sopenharmony_ci * Assumes that SGE is stopped and all interrupts are disabled.
6558c2ecf20Sopenharmony_ci */
6568c2ecf20Sopenharmony_cistatic void free_tx_resources(struct sge *sge)
6578c2ecf20Sopenharmony_ci{
6588c2ecf20Sopenharmony_ci	struct pci_dev *pdev = sge->adapter->pdev;
6598c2ecf20Sopenharmony_ci	unsigned int size, i;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	for (i = 0; i < SGE_CMDQ_N; i++) {
6628c2ecf20Sopenharmony_ci		struct cmdQ *q = &sge->cmdQ[i];
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci		if (q->centries) {
6658c2ecf20Sopenharmony_ci			if (q->in_use)
6668c2ecf20Sopenharmony_ci				free_cmdQ_buffers(sge, q, q->in_use);
6678c2ecf20Sopenharmony_ci			kfree(q->centries);
6688c2ecf20Sopenharmony_ci		}
6698c2ecf20Sopenharmony_ci		if (q->entries) {
6708c2ecf20Sopenharmony_ci			size = sizeof(struct cmdQ_e) * q->size;
6718c2ecf20Sopenharmony_ci			dma_free_coherent(&pdev->dev, size, q->entries,
6728c2ecf20Sopenharmony_ci					  q->dma_addr);
6738c2ecf20Sopenharmony_ci		}
6748c2ecf20Sopenharmony_ci	}
6758c2ecf20Sopenharmony_ci}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci/*
6788c2ecf20Sopenharmony_ci * Allocates basic TX resources, consisting of memory mapped command Qs.
6798c2ecf20Sopenharmony_ci */
6808c2ecf20Sopenharmony_cistatic int alloc_tx_resources(struct sge *sge, struct sge_params *p)
6818c2ecf20Sopenharmony_ci{
6828c2ecf20Sopenharmony_ci	struct pci_dev *pdev = sge->adapter->pdev;
6838c2ecf20Sopenharmony_ci	unsigned int size, i;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	for (i = 0; i < SGE_CMDQ_N; i++) {
6868c2ecf20Sopenharmony_ci		struct cmdQ *q = &sge->cmdQ[i];
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci		q->genbit = 1;
6898c2ecf20Sopenharmony_ci		q->sop = 1;
6908c2ecf20Sopenharmony_ci		q->size = p->cmdQ_size[i];
6918c2ecf20Sopenharmony_ci		q->in_use = 0;
6928c2ecf20Sopenharmony_ci		q->status = 0;
6938c2ecf20Sopenharmony_ci		q->processed = q->cleaned = 0;
6948c2ecf20Sopenharmony_ci		q->stop_thres = 0;
6958c2ecf20Sopenharmony_ci		spin_lock_init(&q->lock);
6968c2ecf20Sopenharmony_ci		size = sizeof(struct cmdQ_e) * q->size;
6978c2ecf20Sopenharmony_ci		q->entries = dma_alloc_coherent(&pdev->dev, size,
6988c2ecf20Sopenharmony_ci						&q->dma_addr, GFP_KERNEL);
6998c2ecf20Sopenharmony_ci		if (!q->entries)
7008c2ecf20Sopenharmony_ci			goto err_no_mem;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci		size = sizeof(struct cmdQ_ce) * q->size;
7038c2ecf20Sopenharmony_ci		q->centries = kzalloc(size, GFP_KERNEL);
7048c2ecf20Sopenharmony_ci		if (!q->centries)
7058c2ecf20Sopenharmony_ci			goto err_no_mem;
7068c2ecf20Sopenharmony_ci	}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	/*
7098c2ecf20Sopenharmony_ci	 * CommandQ 0 handles Ethernet and TOE packets, while queue 1 is TOE
7108c2ecf20Sopenharmony_ci	 * only.  For queue 0 set the stop threshold so we can handle one more
7118c2ecf20Sopenharmony_ci	 * packet from each port, plus reserve an additional 24 entries for
7128c2ecf20Sopenharmony_ci	 * Ethernet packets only.  Queue 1 never suspends nor do we reserve
7138c2ecf20Sopenharmony_ci	 * space for Ethernet packets.
7148c2ecf20Sopenharmony_ci	 */
7158c2ecf20Sopenharmony_ci	sge->cmdQ[0].stop_thres = sge->adapter->params.nports *
7168c2ecf20Sopenharmony_ci		(MAX_SKB_FRAGS + 1);
7178c2ecf20Sopenharmony_ci	return 0;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cierr_no_mem:
7208c2ecf20Sopenharmony_ci	free_tx_resources(sge);
7218c2ecf20Sopenharmony_ci	return -ENOMEM;
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic inline void setup_ring_params(struct adapter *adapter, u64 addr,
7258c2ecf20Sopenharmony_ci				     u32 size, int base_reg_lo,
7268c2ecf20Sopenharmony_ci				     int base_reg_hi, int size_reg)
7278c2ecf20Sopenharmony_ci{
7288c2ecf20Sopenharmony_ci	writel((u32)addr, adapter->regs + base_reg_lo);
7298c2ecf20Sopenharmony_ci	writel(addr >> 32, adapter->regs + base_reg_hi);
7308c2ecf20Sopenharmony_ci	writel(size, adapter->regs + size_reg);
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci/*
7348c2ecf20Sopenharmony_ci * Enable/disable VLAN acceleration.
7358c2ecf20Sopenharmony_ci */
7368c2ecf20Sopenharmony_civoid t1_vlan_mode(struct adapter *adapter, netdev_features_t features)
7378c2ecf20Sopenharmony_ci{
7388c2ecf20Sopenharmony_ci	struct sge *sge = adapter->sge;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	if (features & NETIF_F_HW_VLAN_CTAG_RX)
7418c2ecf20Sopenharmony_ci		sge->sge_control |= F_VLAN_XTRACT;
7428c2ecf20Sopenharmony_ci	else
7438c2ecf20Sopenharmony_ci		sge->sge_control &= ~F_VLAN_XTRACT;
7448c2ecf20Sopenharmony_ci	if (adapter->open_device_map) {
7458c2ecf20Sopenharmony_ci		writel(sge->sge_control, adapter->regs + A_SG_CONTROL);
7468c2ecf20Sopenharmony_ci		readl(adapter->regs + A_SG_CONTROL);   /* flush */
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci/*
7518c2ecf20Sopenharmony_ci * Programs the various SGE registers. However, the engine is not yet enabled,
7528c2ecf20Sopenharmony_ci * but sge->sge_control is setup and ready to go.
7538c2ecf20Sopenharmony_ci */
7548c2ecf20Sopenharmony_cistatic void configure_sge(struct sge *sge, struct sge_params *p)
7558c2ecf20Sopenharmony_ci{
7568c2ecf20Sopenharmony_ci	struct adapter *ap = sge->adapter;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	writel(0, ap->regs + A_SG_CONTROL);
7598c2ecf20Sopenharmony_ci	setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].size,
7608c2ecf20Sopenharmony_ci			  A_SG_CMD0BASELWR, A_SG_CMD0BASEUPR, A_SG_CMD0SIZE);
7618c2ecf20Sopenharmony_ci	setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].size,
7628c2ecf20Sopenharmony_ci			  A_SG_CMD1BASELWR, A_SG_CMD1BASEUPR, A_SG_CMD1SIZE);
7638c2ecf20Sopenharmony_ci	setup_ring_params(ap, sge->freelQ[0].dma_addr,
7648c2ecf20Sopenharmony_ci			  sge->freelQ[0].size, A_SG_FL0BASELWR,
7658c2ecf20Sopenharmony_ci			  A_SG_FL0BASEUPR, A_SG_FL0SIZE);
7668c2ecf20Sopenharmony_ci	setup_ring_params(ap, sge->freelQ[1].dma_addr,
7678c2ecf20Sopenharmony_ci			  sge->freelQ[1].size, A_SG_FL1BASELWR,
7688c2ecf20Sopenharmony_ci			  A_SG_FL1BASEUPR, A_SG_FL1SIZE);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	/* The threshold comparison uses <. */
7718c2ecf20Sopenharmony_ci	writel(SGE_RX_SM_BUF_SIZE + 1, ap->regs + A_SG_FLTHRESHOLD);
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	setup_ring_params(ap, sge->respQ.dma_addr, sge->respQ.size,
7748c2ecf20Sopenharmony_ci			  A_SG_RSPBASELWR, A_SG_RSPBASEUPR, A_SG_RSPSIZE);
7758c2ecf20Sopenharmony_ci	writel((u32)sge->respQ.size - 1, ap->regs + A_SG_RSPQUEUECREDIT);
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	sge->sge_control = F_CMDQ0_ENABLE | F_CMDQ1_ENABLE | F_FL0_ENABLE |
7788c2ecf20Sopenharmony_ci		F_FL1_ENABLE | F_CPL_ENABLE | F_RESPONSE_QUEUE_ENABLE |
7798c2ecf20Sopenharmony_ci		V_CMDQ_PRIORITY(2) | F_DISABLE_CMDQ1_GTS | F_ISCSI_COALESCE |
7808c2ecf20Sopenharmony_ci		V_RX_PKT_OFFSET(sge->rx_pkt_pad);
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci#if defined(__BIG_ENDIAN_BITFIELD)
7838c2ecf20Sopenharmony_ci	sge->sge_control |= F_ENABLE_BIG_ENDIAN;
7848c2ecf20Sopenharmony_ci#endif
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	/* Initialize no-resource timer */
7878c2ecf20Sopenharmony_ci	sge->intrtimer_nres = SGE_INTRTIMER_NRES * core_ticks_per_usec(ap);
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	t1_sge_set_coalesce_params(sge, p);
7908c2ecf20Sopenharmony_ci}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci/*
7938c2ecf20Sopenharmony_ci * Return the payload capacity of the jumbo free-list buffers.
7948c2ecf20Sopenharmony_ci */
7958c2ecf20Sopenharmony_cistatic inline unsigned int jumbo_payload_capacity(const struct sge *sge)
7968c2ecf20Sopenharmony_ci{
7978c2ecf20Sopenharmony_ci	return sge->freelQ[sge->jumbo_fl].rx_buffer_size -
7988c2ecf20Sopenharmony_ci		sge->freelQ[sge->jumbo_fl].dma_offset -
7998c2ecf20Sopenharmony_ci		sizeof(struct cpl_rx_data);
8008c2ecf20Sopenharmony_ci}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci/*
8038c2ecf20Sopenharmony_ci * Frees all SGE related resources and the sge structure itself
8048c2ecf20Sopenharmony_ci */
8058c2ecf20Sopenharmony_civoid t1_sge_destroy(struct sge *sge)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	int i;
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	for_each_port(sge->adapter, i)
8108c2ecf20Sopenharmony_ci		free_percpu(sge->port_stats[i]);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	kfree(sge->tx_sched);
8138c2ecf20Sopenharmony_ci	free_tx_resources(sge);
8148c2ecf20Sopenharmony_ci	free_rx_resources(sge);
8158c2ecf20Sopenharmony_ci	kfree(sge);
8168c2ecf20Sopenharmony_ci}
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci/*
8198c2ecf20Sopenharmony_ci * Allocates new RX buffers on the freelist Q (and tracks them on the freelist
8208c2ecf20Sopenharmony_ci * context Q) until the Q is full or alloc_skb fails.
8218c2ecf20Sopenharmony_ci *
8228c2ecf20Sopenharmony_ci * It is possible that the generation bits already match, indicating that the
8238c2ecf20Sopenharmony_ci * buffer is already valid and nothing needs to be done. This happens when we
8248c2ecf20Sopenharmony_ci * copied a received buffer into a new sk_buff during the interrupt processing.
8258c2ecf20Sopenharmony_ci *
8268c2ecf20Sopenharmony_ci * If the SGE doesn't automatically align packets properly (!sge->rx_pkt_pad),
8278c2ecf20Sopenharmony_ci * we specify a RX_OFFSET in order to make sure that the IP header is 4B
8288c2ecf20Sopenharmony_ci * aligned.
8298c2ecf20Sopenharmony_ci */
8308c2ecf20Sopenharmony_cistatic void refill_free_list(struct sge *sge, struct freelQ *q)
8318c2ecf20Sopenharmony_ci{
8328c2ecf20Sopenharmony_ci	struct pci_dev *pdev = sge->adapter->pdev;
8338c2ecf20Sopenharmony_ci	struct freelQ_ce *ce = &q->centries[q->pidx];
8348c2ecf20Sopenharmony_ci	struct freelQ_e *e = &q->entries[q->pidx];
8358c2ecf20Sopenharmony_ci	unsigned int dma_len = q->rx_buffer_size - q->dma_offset;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	while (q->credits < q->size) {
8388c2ecf20Sopenharmony_ci		struct sk_buff *skb;
8398c2ecf20Sopenharmony_ci		dma_addr_t mapping;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci		skb = dev_alloc_skb(q->rx_buffer_size);
8428c2ecf20Sopenharmony_ci		if (!skb)
8438c2ecf20Sopenharmony_ci			break;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci		skb_reserve(skb, q->dma_offset);
8468c2ecf20Sopenharmony_ci		mapping = dma_map_single(&pdev->dev, skb->data, dma_len,
8478c2ecf20Sopenharmony_ci					 DMA_FROM_DEVICE);
8488c2ecf20Sopenharmony_ci		skb_reserve(skb, sge->rx_pkt_pad);
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci		ce->skb = skb;
8518c2ecf20Sopenharmony_ci		dma_unmap_addr_set(ce, dma_addr, mapping);
8528c2ecf20Sopenharmony_ci		dma_unmap_len_set(ce, dma_len, dma_len);
8538c2ecf20Sopenharmony_ci		e->addr_lo = (u32)mapping;
8548c2ecf20Sopenharmony_ci		e->addr_hi = (u64)mapping >> 32;
8558c2ecf20Sopenharmony_ci		e->len_gen = V_CMD_LEN(dma_len) | V_CMD_GEN1(q->genbit);
8568c2ecf20Sopenharmony_ci		wmb();
8578c2ecf20Sopenharmony_ci		e->gen2 = V_CMD_GEN2(q->genbit);
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci		e++;
8608c2ecf20Sopenharmony_ci		ce++;
8618c2ecf20Sopenharmony_ci		if (++q->pidx == q->size) {
8628c2ecf20Sopenharmony_ci			q->pidx = 0;
8638c2ecf20Sopenharmony_ci			q->genbit ^= 1;
8648c2ecf20Sopenharmony_ci			ce = q->centries;
8658c2ecf20Sopenharmony_ci			e = q->entries;
8668c2ecf20Sopenharmony_ci		}
8678c2ecf20Sopenharmony_ci		q->credits++;
8688c2ecf20Sopenharmony_ci	}
8698c2ecf20Sopenharmony_ci}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci/*
8728c2ecf20Sopenharmony_ci * Calls refill_free_list for both free lists. If we cannot fill at least 1/4
8738c2ecf20Sopenharmony_ci * of both rings, we go into 'few interrupt mode' in order to give the system
8748c2ecf20Sopenharmony_ci * time to free up resources.
8758c2ecf20Sopenharmony_ci */
8768c2ecf20Sopenharmony_cistatic void freelQs_empty(struct sge *sge)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci	struct adapter *adapter = sge->adapter;
8798c2ecf20Sopenharmony_ci	u32 irq_reg = readl(adapter->regs + A_SG_INT_ENABLE);
8808c2ecf20Sopenharmony_ci	u32 irqholdoff_reg;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	refill_free_list(sge, &sge->freelQ[0]);
8838c2ecf20Sopenharmony_ci	refill_free_list(sge, &sge->freelQ[1]);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	if (sge->freelQ[0].credits > (sge->freelQ[0].size >> 2) &&
8868c2ecf20Sopenharmony_ci	    sge->freelQ[1].credits > (sge->freelQ[1].size >> 2)) {
8878c2ecf20Sopenharmony_ci		irq_reg |= F_FL_EXHAUSTED;
8888c2ecf20Sopenharmony_ci		irqholdoff_reg = sge->fixed_intrtimer;
8898c2ecf20Sopenharmony_ci	} else {
8908c2ecf20Sopenharmony_ci		/* Clear the F_FL_EXHAUSTED interrupts for now */
8918c2ecf20Sopenharmony_ci		irq_reg &= ~F_FL_EXHAUSTED;
8928c2ecf20Sopenharmony_ci		irqholdoff_reg = sge->intrtimer_nres;
8938c2ecf20Sopenharmony_ci	}
8948c2ecf20Sopenharmony_ci	writel(irqholdoff_reg, adapter->regs + A_SG_INTRTIMER);
8958c2ecf20Sopenharmony_ci	writel(irq_reg, adapter->regs + A_SG_INT_ENABLE);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	/* We reenable the Qs to force a freelist GTS interrupt later */
8988c2ecf20Sopenharmony_ci	doorbell_pio(adapter, F_FL0_ENABLE | F_FL1_ENABLE);
8998c2ecf20Sopenharmony_ci}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci#define SGE_PL_INTR_MASK (F_PL_INTR_SGE_ERR | F_PL_INTR_SGE_DATA)
9028c2ecf20Sopenharmony_ci#define SGE_INT_FATAL (F_RESPQ_OVERFLOW | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
9038c2ecf20Sopenharmony_ci#define SGE_INT_ENABLE (F_RESPQ_EXHAUSTED | F_RESPQ_OVERFLOW | \
9048c2ecf20Sopenharmony_ci			F_FL_EXHAUSTED | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci/*
9078c2ecf20Sopenharmony_ci * Disable SGE Interrupts
9088c2ecf20Sopenharmony_ci */
9098c2ecf20Sopenharmony_civoid t1_sge_intr_disable(struct sge *sge)
9108c2ecf20Sopenharmony_ci{
9118c2ecf20Sopenharmony_ci	u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	writel(val & ~SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
9148c2ecf20Sopenharmony_ci	writel(0, sge->adapter->regs + A_SG_INT_ENABLE);
9158c2ecf20Sopenharmony_ci}
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci/*
9188c2ecf20Sopenharmony_ci * Enable SGE interrupts.
9198c2ecf20Sopenharmony_ci */
9208c2ecf20Sopenharmony_civoid t1_sge_intr_enable(struct sge *sge)
9218c2ecf20Sopenharmony_ci{
9228c2ecf20Sopenharmony_ci	u32 en = SGE_INT_ENABLE;
9238c2ecf20Sopenharmony_ci	u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	if (sge->adapter->port[0].dev->hw_features & NETIF_F_TSO)
9268c2ecf20Sopenharmony_ci		en &= ~F_PACKET_TOO_BIG;
9278c2ecf20Sopenharmony_ci	writel(en, sge->adapter->regs + A_SG_INT_ENABLE);
9288c2ecf20Sopenharmony_ci	writel(val | SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
9298c2ecf20Sopenharmony_ci}
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci/*
9328c2ecf20Sopenharmony_ci * Clear SGE interrupts.
9338c2ecf20Sopenharmony_ci */
9348c2ecf20Sopenharmony_civoid t1_sge_intr_clear(struct sge *sge)
9358c2ecf20Sopenharmony_ci{
9368c2ecf20Sopenharmony_ci	writel(SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_CAUSE);
9378c2ecf20Sopenharmony_ci	writel(0xffffffff, sge->adapter->regs + A_SG_INT_CAUSE);
9388c2ecf20Sopenharmony_ci}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci/*
9418c2ecf20Sopenharmony_ci * SGE 'Error' interrupt handler
9428c2ecf20Sopenharmony_ci */
9438c2ecf20Sopenharmony_ciint t1_sge_intr_error_handler(struct sge *sge)
9448c2ecf20Sopenharmony_ci{
9458c2ecf20Sopenharmony_ci	struct adapter *adapter = sge->adapter;
9468c2ecf20Sopenharmony_ci	u32 cause = readl(adapter->regs + A_SG_INT_CAUSE);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	if (adapter->port[0].dev->hw_features & NETIF_F_TSO)
9498c2ecf20Sopenharmony_ci		cause &= ~F_PACKET_TOO_BIG;
9508c2ecf20Sopenharmony_ci	if (cause & F_RESPQ_EXHAUSTED)
9518c2ecf20Sopenharmony_ci		sge->stats.respQ_empty++;
9528c2ecf20Sopenharmony_ci	if (cause & F_RESPQ_OVERFLOW) {
9538c2ecf20Sopenharmony_ci		sge->stats.respQ_overflow++;
9548c2ecf20Sopenharmony_ci		pr_alert("%s: SGE response queue overflow\n",
9558c2ecf20Sopenharmony_ci			 adapter->name);
9568c2ecf20Sopenharmony_ci	}
9578c2ecf20Sopenharmony_ci	if (cause & F_FL_EXHAUSTED) {
9588c2ecf20Sopenharmony_ci		sge->stats.freelistQ_empty++;
9598c2ecf20Sopenharmony_ci		freelQs_empty(sge);
9608c2ecf20Sopenharmony_ci	}
9618c2ecf20Sopenharmony_ci	if (cause & F_PACKET_TOO_BIG) {
9628c2ecf20Sopenharmony_ci		sge->stats.pkt_too_big++;
9638c2ecf20Sopenharmony_ci		pr_alert("%s: SGE max packet size exceeded\n",
9648c2ecf20Sopenharmony_ci			 adapter->name);
9658c2ecf20Sopenharmony_ci	}
9668c2ecf20Sopenharmony_ci	if (cause & F_PACKET_MISMATCH) {
9678c2ecf20Sopenharmony_ci		sge->stats.pkt_mismatch++;
9688c2ecf20Sopenharmony_ci		pr_alert("%s: SGE packet mismatch\n", adapter->name);
9698c2ecf20Sopenharmony_ci	}
9708c2ecf20Sopenharmony_ci	if (cause & SGE_INT_FATAL)
9718c2ecf20Sopenharmony_ci		t1_fatal_err(adapter);
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	writel(cause, adapter->regs + A_SG_INT_CAUSE);
9748c2ecf20Sopenharmony_ci	return 0;
9758c2ecf20Sopenharmony_ci}
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ciconst struct sge_intr_counts *t1_sge_get_intr_counts(const struct sge *sge)
9788c2ecf20Sopenharmony_ci{
9798c2ecf20Sopenharmony_ci	return &sge->stats;
9808c2ecf20Sopenharmony_ci}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_civoid t1_sge_get_port_stats(const struct sge *sge, int port,
9838c2ecf20Sopenharmony_ci			   struct sge_port_stats *ss)
9848c2ecf20Sopenharmony_ci{
9858c2ecf20Sopenharmony_ci	int cpu;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	memset(ss, 0, sizeof(*ss));
9888c2ecf20Sopenharmony_ci	for_each_possible_cpu(cpu) {
9898c2ecf20Sopenharmony_ci		struct sge_port_stats *st = per_cpu_ptr(sge->port_stats[port], cpu);
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci		ss->rx_cso_good += st->rx_cso_good;
9928c2ecf20Sopenharmony_ci		ss->tx_cso += st->tx_cso;
9938c2ecf20Sopenharmony_ci		ss->tx_tso += st->tx_tso;
9948c2ecf20Sopenharmony_ci		ss->tx_need_hdrroom += st->tx_need_hdrroom;
9958c2ecf20Sopenharmony_ci		ss->vlan_xtract += st->vlan_xtract;
9968c2ecf20Sopenharmony_ci		ss->vlan_insert += st->vlan_insert;
9978c2ecf20Sopenharmony_ci	}
9988c2ecf20Sopenharmony_ci}
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci/**
10018c2ecf20Sopenharmony_ci *	recycle_fl_buf - recycle a free list buffer
10028c2ecf20Sopenharmony_ci *	@fl: the free list
10038c2ecf20Sopenharmony_ci *	@idx: index of buffer to recycle
10048c2ecf20Sopenharmony_ci *
10058c2ecf20Sopenharmony_ci *	Recycles the specified buffer on the given free list by adding it at
10068c2ecf20Sopenharmony_ci *	the next available slot on the list.
10078c2ecf20Sopenharmony_ci */
10088c2ecf20Sopenharmony_cistatic void recycle_fl_buf(struct freelQ *fl, int idx)
10098c2ecf20Sopenharmony_ci{
10108c2ecf20Sopenharmony_ci	struct freelQ_e *from = &fl->entries[idx];
10118c2ecf20Sopenharmony_ci	struct freelQ_e *to = &fl->entries[fl->pidx];
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	fl->centries[fl->pidx] = fl->centries[idx];
10148c2ecf20Sopenharmony_ci	to->addr_lo = from->addr_lo;
10158c2ecf20Sopenharmony_ci	to->addr_hi = from->addr_hi;
10168c2ecf20Sopenharmony_ci	to->len_gen = G_CMD_LEN(from->len_gen) | V_CMD_GEN1(fl->genbit);
10178c2ecf20Sopenharmony_ci	wmb();
10188c2ecf20Sopenharmony_ci	to->gen2 = V_CMD_GEN2(fl->genbit);
10198c2ecf20Sopenharmony_ci	fl->credits++;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	if (++fl->pidx == fl->size) {
10228c2ecf20Sopenharmony_ci		fl->pidx = 0;
10238c2ecf20Sopenharmony_ci		fl->genbit ^= 1;
10248c2ecf20Sopenharmony_ci	}
10258c2ecf20Sopenharmony_ci}
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_cistatic int copybreak __read_mostly = 256;
10288c2ecf20Sopenharmony_cimodule_param(copybreak, int, 0);
10298c2ecf20Sopenharmony_ciMODULE_PARM_DESC(copybreak, "Receive copy threshold");
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci/**
10328c2ecf20Sopenharmony_ci *	get_packet - return the next ingress packet buffer
10338c2ecf20Sopenharmony_ci *	@adapter: the adapter that received the packet
10348c2ecf20Sopenharmony_ci *	@fl: the SGE free list holding the packet
10358c2ecf20Sopenharmony_ci *	@len: the actual packet length, excluding any SGE padding
10368c2ecf20Sopenharmony_ci *
10378c2ecf20Sopenharmony_ci *	Get the next packet from a free list and complete setup of the
10388c2ecf20Sopenharmony_ci *	sk_buff.  If the packet is small we make a copy and recycle the
10398c2ecf20Sopenharmony_ci *	original buffer, otherwise we use the original buffer itself.  If a
10408c2ecf20Sopenharmony_ci *	positive drop threshold is supplied packets are dropped and their
10418c2ecf20Sopenharmony_ci *	buffers recycled if (a) the number of remaining buffers is under the
10428c2ecf20Sopenharmony_ci *	threshold and the packet is too big to copy, or (b) the packet should
10438c2ecf20Sopenharmony_ci *	be copied but there is no memory for the copy.
10448c2ecf20Sopenharmony_ci */
10458c2ecf20Sopenharmony_cistatic inline struct sk_buff *get_packet(struct adapter *adapter,
10468c2ecf20Sopenharmony_ci					 struct freelQ *fl, unsigned int len)
10478c2ecf20Sopenharmony_ci{
10488c2ecf20Sopenharmony_ci	const struct freelQ_ce *ce = &fl->centries[fl->cidx];
10498c2ecf20Sopenharmony_ci	struct pci_dev *pdev = adapter->pdev;
10508c2ecf20Sopenharmony_ci	struct sk_buff *skb;
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci	if (len < copybreak) {
10538c2ecf20Sopenharmony_ci		skb = napi_alloc_skb(&adapter->napi, len);
10548c2ecf20Sopenharmony_ci		if (!skb)
10558c2ecf20Sopenharmony_ci			goto use_orig_buf;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci		skb_put(skb, len);
10588c2ecf20Sopenharmony_ci		dma_sync_single_for_cpu(&pdev->dev,
10598c2ecf20Sopenharmony_ci					dma_unmap_addr(ce, dma_addr),
10608c2ecf20Sopenharmony_ci					dma_unmap_len(ce, dma_len),
10618c2ecf20Sopenharmony_ci					DMA_FROM_DEVICE);
10628c2ecf20Sopenharmony_ci		skb_copy_from_linear_data(ce->skb, skb->data, len);
10638c2ecf20Sopenharmony_ci		dma_sync_single_for_device(&pdev->dev,
10648c2ecf20Sopenharmony_ci					   dma_unmap_addr(ce, dma_addr),
10658c2ecf20Sopenharmony_ci					   dma_unmap_len(ce, dma_len),
10668c2ecf20Sopenharmony_ci					   DMA_FROM_DEVICE);
10678c2ecf20Sopenharmony_ci		recycle_fl_buf(fl, fl->cidx);
10688c2ecf20Sopenharmony_ci		return skb;
10698c2ecf20Sopenharmony_ci	}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ciuse_orig_buf:
10728c2ecf20Sopenharmony_ci	if (fl->credits < 2) {
10738c2ecf20Sopenharmony_ci		recycle_fl_buf(fl, fl->cidx);
10748c2ecf20Sopenharmony_ci		return NULL;
10758c2ecf20Sopenharmony_ci	}
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	dma_unmap_single(&pdev->dev, dma_unmap_addr(ce, dma_addr),
10788c2ecf20Sopenharmony_ci			 dma_unmap_len(ce, dma_len), DMA_FROM_DEVICE);
10798c2ecf20Sopenharmony_ci	skb = ce->skb;
10808c2ecf20Sopenharmony_ci	prefetch(skb->data);
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	skb_put(skb, len);
10838c2ecf20Sopenharmony_ci	return skb;
10848c2ecf20Sopenharmony_ci}
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci/**
10878c2ecf20Sopenharmony_ci *	unexpected_offload - handle an unexpected offload packet
10888c2ecf20Sopenharmony_ci *	@adapter: the adapter
10898c2ecf20Sopenharmony_ci *	@fl: the free list that received the packet
10908c2ecf20Sopenharmony_ci *
10918c2ecf20Sopenharmony_ci *	Called when we receive an unexpected offload packet (e.g., the TOE
10928c2ecf20Sopenharmony_ci *	function is disabled or the card is a NIC).  Prints a message and
10938c2ecf20Sopenharmony_ci *	recycles the buffer.
10948c2ecf20Sopenharmony_ci */
10958c2ecf20Sopenharmony_cistatic void unexpected_offload(struct adapter *adapter, struct freelQ *fl)
10968c2ecf20Sopenharmony_ci{
10978c2ecf20Sopenharmony_ci	struct freelQ_ce *ce = &fl->centries[fl->cidx];
10988c2ecf20Sopenharmony_ci	struct sk_buff *skb = ce->skb;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	dma_sync_single_for_cpu(&adapter->pdev->dev,
11018c2ecf20Sopenharmony_ci				dma_unmap_addr(ce, dma_addr),
11028c2ecf20Sopenharmony_ci				dma_unmap_len(ce, dma_len), DMA_FROM_DEVICE);
11038c2ecf20Sopenharmony_ci	pr_err("%s: unexpected offload packet, cmd %u\n",
11048c2ecf20Sopenharmony_ci	       adapter->name, *skb->data);
11058c2ecf20Sopenharmony_ci	recycle_fl_buf(fl, fl->cidx);
11068c2ecf20Sopenharmony_ci}
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci/*
11098c2ecf20Sopenharmony_ci * T1/T2 SGE limits the maximum DMA size per TX descriptor to
11108c2ecf20Sopenharmony_ci * SGE_TX_DESC_MAX_PLEN (16KB). If the PAGE_SIZE is larger than 16KB, the
11118c2ecf20Sopenharmony_ci * stack might send more than SGE_TX_DESC_MAX_PLEN in a contiguous manner.
11128c2ecf20Sopenharmony_ci * Note that the *_large_page_tx_descs stuff will be optimized out when
11138c2ecf20Sopenharmony_ci * PAGE_SIZE <= SGE_TX_DESC_MAX_PLEN.
11148c2ecf20Sopenharmony_ci *
11158c2ecf20Sopenharmony_ci * compute_large_page_descs() computes how many additional descriptors are
11168c2ecf20Sopenharmony_ci * required to break down the stack's request.
11178c2ecf20Sopenharmony_ci */
11188c2ecf20Sopenharmony_cistatic inline unsigned int compute_large_page_tx_descs(struct sk_buff *skb)
11198c2ecf20Sopenharmony_ci{
11208c2ecf20Sopenharmony_ci	unsigned int count = 0;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
11238c2ecf20Sopenharmony_ci		unsigned int nfrags = skb_shinfo(skb)->nr_frags;
11248c2ecf20Sopenharmony_ci		unsigned int i, len = skb_headlen(skb);
11258c2ecf20Sopenharmony_ci		while (len > SGE_TX_DESC_MAX_PLEN) {
11268c2ecf20Sopenharmony_ci			count++;
11278c2ecf20Sopenharmony_ci			len -= SGE_TX_DESC_MAX_PLEN;
11288c2ecf20Sopenharmony_ci		}
11298c2ecf20Sopenharmony_ci		for (i = 0; nfrags--; i++) {
11308c2ecf20Sopenharmony_ci			const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
11318c2ecf20Sopenharmony_ci			len = skb_frag_size(frag);
11328c2ecf20Sopenharmony_ci			while (len > SGE_TX_DESC_MAX_PLEN) {
11338c2ecf20Sopenharmony_ci				count++;
11348c2ecf20Sopenharmony_ci				len -= SGE_TX_DESC_MAX_PLEN;
11358c2ecf20Sopenharmony_ci			}
11368c2ecf20Sopenharmony_ci		}
11378c2ecf20Sopenharmony_ci	}
11388c2ecf20Sopenharmony_ci	return count;
11398c2ecf20Sopenharmony_ci}
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci/*
11428c2ecf20Sopenharmony_ci * Write a cmdQ entry.
11438c2ecf20Sopenharmony_ci *
11448c2ecf20Sopenharmony_ci * Since this function writes the 'flags' field, it must not be used to
11458c2ecf20Sopenharmony_ci * write the first cmdQ entry.
11468c2ecf20Sopenharmony_ci */
11478c2ecf20Sopenharmony_cistatic inline void write_tx_desc(struct cmdQ_e *e, dma_addr_t mapping,
11488c2ecf20Sopenharmony_ci				 unsigned int len, unsigned int gen,
11498c2ecf20Sopenharmony_ci				 unsigned int eop)
11508c2ecf20Sopenharmony_ci{
11518c2ecf20Sopenharmony_ci	BUG_ON(len > SGE_TX_DESC_MAX_PLEN);
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_ci	e->addr_lo = (u32)mapping;
11548c2ecf20Sopenharmony_ci	e->addr_hi = (u64)mapping >> 32;
11558c2ecf20Sopenharmony_ci	e->len_gen = V_CMD_LEN(len) | V_CMD_GEN1(gen);
11568c2ecf20Sopenharmony_ci	e->flags = F_CMD_DATAVALID | V_CMD_EOP(eop) | V_CMD_GEN2(gen);
11578c2ecf20Sopenharmony_ci}
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci/*
11608c2ecf20Sopenharmony_ci * See comment for previous function.
11618c2ecf20Sopenharmony_ci *
11628c2ecf20Sopenharmony_ci * write_tx_descs_large_page() writes additional SGE tx descriptors if
11638c2ecf20Sopenharmony_ci * *desc_len exceeds HW's capability.
11648c2ecf20Sopenharmony_ci */
11658c2ecf20Sopenharmony_cistatic inline unsigned int write_large_page_tx_descs(unsigned int pidx,
11668c2ecf20Sopenharmony_ci						     struct cmdQ_e **e,
11678c2ecf20Sopenharmony_ci						     struct cmdQ_ce **ce,
11688c2ecf20Sopenharmony_ci						     unsigned int *gen,
11698c2ecf20Sopenharmony_ci						     dma_addr_t *desc_mapping,
11708c2ecf20Sopenharmony_ci						     unsigned int *desc_len,
11718c2ecf20Sopenharmony_ci						     unsigned int nfrags,
11728c2ecf20Sopenharmony_ci						     struct cmdQ *q)
11738c2ecf20Sopenharmony_ci{
11748c2ecf20Sopenharmony_ci	if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
11758c2ecf20Sopenharmony_ci		struct cmdQ_e *e1 = *e;
11768c2ecf20Sopenharmony_ci		struct cmdQ_ce *ce1 = *ce;
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci		while (*desc_len > SGE_TX_DESC_MAX_PLEN) {
11798c2ecf20Sopenharmony_ci			*desc_len -= SGE_TX_DESC_MAX_PLEN;
11808c2ecf20Sopenharmony_ci			write_tx_desc(e1, *desc_mapping, SGE_TX_DESC_MAX_PLEN,
11818c2ecf20Sopenharmony_ci				      *gen, nfrags == 0 && *desc_len == 0);
11828c2ecf20Sopenharmony_ci			ce1->skb = NULL;
11838c2ecf20Sopenharmony_ci			dma_unmap_len_set(ce1, dma_len, 0);
11848c2ecf20Sopenharmony_ci			*desc_mapping += SGE_TX_DESC_MAX_PLEN;
11858c2ecf20Sopenharmony_ci			if (*desc_len) {
11868c2ecf20Sopenharmony_ci				ce1++;
11878c2ecf20Sopenharmony_ci				e1++;
11888c2ecf20Sopenharmony_ci				if (++pidx == q->size) {
11898c2ecf20Sopenharmony_ci					pidx = 0;
11908c2ecf20Sopenharmony_ci					*gen ^= 1;
11918c2ecf20Sopenharmony_ci					ce1 = q->centries;
11928c2ecf20Sopenharmony_ci					e1 = q->entries;
11938c2ecf20Sopenharmony_ci				}
11948c2ecf20Sopenharmony_ci			}
11958c2ecf20Sopenharmony_ci		}
11968c2ecf20Sopenharmony_ci		*e = e1;
11978c2ecf20Sopenharmony_ci		*ce = ce1;
11988c2ecf20Sopenharmony_ci	}
11998c2ecf20Sopenharmony_ci	return pidx;
12008c2ecf20Sopenharmony_ci}
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci/*
12038c2ecf20Sopenharmony_ci * Write the command descriptors to transmit the given skb starting at
12048c2ecf20Sopenharmony_ci * descriptor pidx with the given generation.
12058c2ecf20Sopenharmony_ci */
12068c2ecf20Sopenharmony_cistatic inline void write_tx_descs(struct adapter *adapter, struct sk_buff *skb,
12078c2ecf20Sopenharmony_ci				  unsigned int pidx, unsigned int gen,
12088c2ecf20Sopenharmony_ci				  struct cmdQ *q)
12098c2ecf20Sopenharmony_ci{
12108c2ecf20Sopenharmony_ci	dma_addr_t mapping, desc_mapping;
12118c2ecf20Sopenharmony_ci	struct cmdQ_e *e, *e1;
12128c2ecf20Sopenharmony_ci	struct cmdQ_ce *ce;
12138c2ecf20Sopenharmony_ci	unsigned int i, flags, first_desc_len, desc_len,
12148c2ecf20Sopenharmony_ci	    nfrags = skb_shinfo(skb)->nr_frags;
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	e = e1 = &q->entries[pidx];
12178c2ecf20Sopenharmony_ci	ce = &q->centries[pidx];
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	mapping = dma_map_single(&adapter->pdev->dev, skb->data,
12208c2ecf20Sopenharmony_ci				 skb_headlen(skb), DMA_TO_DEVICE);
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	desc_mapping = mapping;
12238c2ecf20Sopenharmony_ci	desc_len = skb_headlen(skb);
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	flags = F_CMD_DATAVALID | F_CMD_SOP |
12268c2ecf20Sopenharmony_ci	    V_CMD_EOP(nfrags == 0 && desc_len <= SGE_TX_DESC_MAX_PLEN) |
12278c2ecf20Sopenharmony_ci	    V_CMD_GEN2(gen);
12288c2ecf20Sopenharmony_ci	first_desc_len = (desc_len <= SGE_TX_DESC_MAX_PLEN) ?
12298c2ecf20Sopenharmony_ci	    desc_len : SGE_TX_DESC_MAX_PLEN;
12308c2ecf20Sopenharmony_ci	e->addr_lo = (u32)desc_mapping;
12318c2ecf20Sopenharmony_ci	e->addr_hi = (u64)desc_mapping >> 32;
12328c2ecf20Sopenharmony_ci	e->len_gen = V_CMD_LEN(first_desc_len) | V_CMD_GEN1(gen);
12338c2ecf20Sopenharmony_ci	ce->skb = NULL;
12348c2ecf20Sopenharmony_ci	dma_unmap_len_set(ce, dma_len, 0);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN &&
12378c2ecf20Sopenharmony_ci	    desc_len > SGE_TX_DESC_MAX_PLEN) {
12388c2ecf20Sopenharmony_ci		desc_mapping += first_desc_len;
12398c2ecf20Sopenharmony_ci		desc_len -= first_desc_len;
12408c2ecf20Sopenharmony_ci		e1++;
12418c2ecf20Sopenharmony_ci		ce++;
12428c2ecf20Sopenharmony_ci		if (++pidx == q->size) {
12438c2ecf20Sopenharmony_ci			pidx = 0;
12448c2ecf20Sopenharmony_ci			gen ^= 1;
12458c2ecf20Sopenharmony_ci			e1 = q->entries;
12468c2ecf20Sopenharmony_ci			ce = q->centries;
12478c2ecf20Sopenharmony_ci		}
12488c2ecf20Sopenharmony_ci		pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
12498c2ecf20Sopenharmony_ci						 &desc_mapping, &desc_len,
12508c2ecf20Sopenharmony_ci						 nfrags, q);
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci		if (likely(desc_len))
12538c2ecf20Sopenharmony_ci			write_tx_desc(e1, desc_mapping, desc_len, gen,
12548c2ecf20Sopenharmony_ci				      nfrags == 0);
12558c2ecf20Sopenharmony_ci	}
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	ce->skb = NULL;
12588c2ecf20Sopenharmony_ci	dma_unmap_addr_set(ce, dma_addr, mapping);
12598c2ecf20Sopenharmony_ci	dma_unmap_len_set(ce, dma_len, skb_headlen(skb));
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	for (i = 0; nfrags--; i++) {
12628c2ecf20Sopenharmony_ci		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
12638c2ecf20Sopenharmony_ci		e1++;
12648c2ecf20Sopenharmony_ci		ce++;
12658c2ecf20Sopenharmony_ci		if (++pidx == q->size) {
12668c2ecf20Sopenharmony_ci			pidx = 0;
12678c2ecf20Sopenharmony_ci			gen ^= 1;
12688c2ecf20Sopenharmony_ci			e1 = q->entries;
12698c2ecf20Sopenharmony_ci			ce = q->centries;
12708c2ecf20Sopenharmony_ci		}
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci		mapping = skb_frag_dma_map(&adapter->pdev->dev, frag, 0,
12738c2ecf20Sopenharmony_ci					   skb_frag_size(frag), DMA_TO_DEVICE);
12748c2ecf20Sopenharmony_ci		desc_mapping = mapping;
12758c2ecf20Sopenharmony_ci		desc_len = skb_frag_size(frag);
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci		pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
12788c2ecf20Sopenharmony_ci						 &desc_mapping, &desc_len,
12798c2ecf20Sopenharmony_ci						 nfrags, q);
12808c2ecf20Sopenharmony_ci		if (likely(desc_len))
12818c2ecf20Sopenharmony_ci			write_tx_desc(e1, desc_mapping, desc_len, gen,
12828c2ecf20Sopenharmony_ci				      nfrags == 0);
12838c2ecf20Sopenharmony_ci		ce->skb = NULL;
12848c2ecf20Sopenharmony_ci		dma_unmap_addr_set(ce, dma_addr, mapping);
12858c2ecf20Sopenharmony_ci		dma_unmap_len_set(ce, dma_len, skb_frag_size(frag));
12868c2ecf20Sopenharmony_ci	}
12878c2ecf20Sopenharmony_ci	ce->skb = skb;
12888c2ecf20Sopenharmony_ci	wmb();
12898c2ecf20Sopenharmony_ci	e->flags = flags;
12908c2ecf20Sopenharmony_ci}
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci/*
12938c2ecf20Sopenharmony_ci * Clean up completed Tx buffers.
12948c2ecf20Sopenharmony_ci */
12958c2ecf20Sopenharmony_cistatic inline void reclaim_completed_tx(struct sge *sge, struct cmdQ *q)
12968c2ecf20Sopenharmony_ci{
12978c2ecf20Sopenharmony_ci	unsigned int reclaim = q->processed - q->cleaned;
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	if (reclaim) {
13008c2ecf20Sopenharmony_ci		pr_debug("reclaim_completed_tx processed:%d cleaned:%d\n",
13018c2ecf20Sopenharmony_ci			 q->processed, q->cleaned);
13028c2ecf20Sopenharmony_ci		free_cmdQ_buffers(sge, q, reclaim);
13038c2ecf20Sopenharmony_ci		q->cleaned += reclaim;
13048c2ecf20Sopenharmony_ci	}
13058c2ecf20Sopenharmony_ci}
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci/*
13088c2ecf20Sopenharmony_ci * Called from tasklet. Checks the scheduler for any
13098c2ecf20Sopenharmony_ci * pending skbs that can be sent.
13108c2ecf20Sopenharmony_ci */
13118c2ecf20Sopenharmony_cistatic void restart_sched(struct tasklet_struct *t)
13128c2ecf20Sopenharmony_ci{
13138c2ecf20Sopenharmony_ci	struct sched *s = from_tasklet(s, t, sched_tsk);
13148c2ecf20Sopenharmony_ci	struct sge *sge = s->sge;
13158c2ecf20Sopenharmony_ci	struct adapter *adapter = sge->adapter;
13168c2ecf20Sopenharmony_ci	struct cmdQ *q = &sge->cmdQ[0];
13178c2ecf20Sopenharmony_ci	struct sk_buff *skb;
13188c2ecf20Sopenharmony_ci	unsigned int credits, queued_skb = 0;
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_ci	spin_lock(&q->lock);
13218c2ecf20Sopenharmony_ci	reclaim_completed_tx(sge, q);
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ci	credits = q->size - q->in_use;
13248c2ecf20Sopenharmony_ci	pr_debug("restart_sched credits=%d\n", credits);
13258c2ecf20Sopenharmony_ci	while ((skb = sched_skb(sge, NULL, credits)) != NULL) {
13268c2ecf20Sopenharmony_ci		unsigned int genbit, pidx, count;
13278c2ecf20Sopenharmony_ci	        count = 1 + skb_shinfo(skb)->nr_frags;
13288c2ecf20Sopenharmony_ci		count += compute_large_page_tx_descs(skb);
13298c2ecf20Sopenharmony_ci		q->in_use += count;
13308c2ecf20Sopenharmony_ci		genbit = q->genbit;
13318c2ecf20Sopenharmony_ci		pidx = q->pidx;
13328c2ecf20Sopenharmony_ci		q->pidx += count;
13338c2ecf20Sopenharmony_ci		if (q->pidx >= q->size) {
13348c2ecf20Sopenharmony_ci			q->pidx -= q->size;
13358c2ecf20Sopenharmony_ci			q->genbit ^= 1;
13368c2ecf20Sopenharmony_ci		}
13378c2ecf20Sopenharmony_ci		write_tx_descs(adapter, skb, pidx, genbit, q);
13388c2ecf20Sopenharmony_ci	        credits = q->size - q->in_use;
13398c2ecf20Sopenharmony_ci		queued_skb = 1;
13408c2ecf20Sopenharmony_ci	}
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	if (queued_skb) {
13438c2ecf20Sopenharmony_ci		clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
13448c2ecf20Sopenharmony_ci		if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
13458c2ecf20Sopenharmony_ci			set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
13468c2ecf20Sopenharmony_ci			writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
13478c2ecf20Sopenharmony_ci		}
13488c2ecf20Sopenharmony_ci	}
13498c2ecf20Sopenharmony_ci	spin_unlock(&q->lock);
13508c2ecf20Sopenharmony_ci}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci/**
13538c2ecf20Sopenharmony_ci *	sge_rx - process an ingress ethernet packet
13548c2ecf20Sopenharmony_ci *	@sge: the sge structure
13558c2ecf20Sopenharmony_ci *	@fl: the free list that contains the packet buffer
13568c2ecf20Sopenharmony_ci *	@len: the packet length
13578c2ecf20Sopenharmony_ci *
13588c2ecf20Sopenharmony_ci *	Process an ingress ethernet pakcet and deliver it to the stack.
13598c2ecf20Sopenharmony_ci */
13608c2ecf20Sopenharmony_cistatic void sge_rx(struct sge *sge, struct freelQ *fl, unsigned int len)
13618c2ecf20Sopenharmony_ci{
13628c2ecf20Sopenharmony_ci	struct sk_buff *skb;
13638c2ecf20Sopenharmony_ci	const struct cpl_rx_pkt *p;
13648c2ecf20Sopenharmony_ci	struct adapter *adapter = sge->adapter;
13658c2ecf20Sopenharmony_ci	struct sge_port_stats *st;
13668c2ecf20Sopenharmony_ci	struct net_device *dev;
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	skb = get_packet(adapter, fl, len - sge->rx_pkt_pad);
13698c2ecf20Sopenharmony_ci	if (unlikely(!skb)) {
13708c2ecf20Sopenharmony_ci		sge->stats.rx_drops++;
13718c2ecf20Sopenharmony_ci		return;
13728c2ecf20Sopenharmony_ci	}
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	p = (const struct cpl_rx_pkt *) skb->data;
13758c2ecf20Sopenharmony_ci	if (p->iff >= adapter->params.nports) {
13768c2ecf20Sopenharmony_ci		kfree_skb(skb);
13778c2ecf20Sopenharmony_ci		return;
13788c2ecf20Sopenharmony_ci	}
13798c2ecf20Sopenharmony_ci	__skb_pull(skb, sizeof(*p));
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	st = this_cpu_ptr(sge->port_stats[p->iff]);
13828c2ecf20Sopenharmony_ci	dev = adapter->port[p->iff].dev;
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	skb->protocol = eth_type_trans(skb, dev);
13858c2ecf20Sopenharmony_ci	if ((dev->features & NETIF_F_RXCSUM) && p->csum == 0xffff &&
13868c2ecf20Sopenharmony_ci	    skb->protocol == htons(ETH_P_IP) &&
13878c2ecf20Sopenharmony_ci	    (skb->data[9] == IPPROTO_TCP || skb->data[9] == IPPROTO_UDP)) {
13888c2ecf20Sopenharmony_ci		++st->rx_cso_good;
13898c2ecf20Sopenharmony_ci		skb->ip_summed = CHECKSUM_UNNECESSARY;
13908c2ecf20Sopenharmony_ci	} else
13918c2ecf20Sopenharmony_ci		skb_checksum_none_assert(skb);
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	if (p->vlan_valid) {
13948c2ecf20Sopenharmony_ci		st->vlan_xtract++;
13958c2ecf20Sopenharmony_ci		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(p->vlan));
13968c2ecf20Sopenharmony_ci	}
13978c2ecf20Sopenharmony_ci	netif_receive_skb(skb);
13988c2ecf20Sopenharmony_ci}
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci/*
14018c2ecf20Sopenharmony_ci * Returns true if a command queue has enough available descriptors that
14028c2ecf20Sopenharmony_ci * we can resume Tx operation after temporarily disabling its packet queue.
14038c2ecf20Sopenharmony_ci */
14048c2ecf20Sopenharmony_cistatic inline int enough_free_Tx_descs(const struct cmdQ *q)
14058c2ecf20Sopenharmony_ci{
14068c2ecf20Sopenharmony_ci	unsigned int r = q->processed - q->cleaned;
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci	return q->in_use - r < (q->size >> 1);
14098c2ecf20Sopenharmony_ci}
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci/*
14128c2ecf20Sopenharmony_ci * Called when sufficient space has become available in the SGE command queues
14138c2ecf20Sopenharmony_ci * after the Tx packet schedulers have been suspended to restart the Tx path.
14148c2ecf20Sopenharmony_ci */
14158c2ecf20Sopenharmony_cistatic void restart_tx_queues(struct sge *sge)
14168c2ecf20Sopenharmony_ci{
14178c2ecf20Sopenharmony_ci	struct adapter *adap = sge->adapter;
14188c2ecf20Sopenharmony_ci	int i;
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	if (!enough_free_Tx_descs(&sge->cmdQ[0]))
14218c2ecf20Sopenharmony_ci		return;
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci	for_each_port(adap, i) {
14248c2ecf20Sopenharmony_ci		struct net_device *nd = adap->port[i].dev;
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci		if (test_and_clear_bit(nd->if_port, &sge->stopped_tx_queues) &&
14278c2ecf20Sopenharmony_ci		    netif_running(nd)) {
14288c2ecf20Sopenharmony_ci			sge->stats.cmdQ_restarted[2]++;
14298c2ecf20Sopenharmony_ci			netif_wake_queue(nd);
14308c2ecf20Sopenharmony_ci		}
14318c2ecf20Sopenharmony_ci	}
14328c2ecf20Sopenharmony_ci}
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci/*
14358c2ecf20Sopenharmony_ci * update_tx_info is called from the interrupt handler/NAPI to return cmdQ0
14368c2ecf20Sopenharmony_ci * information.
14378c2ecf20Sopenharmony_ci */
14388c2ecf20Sopenharmony_cistatic unsigned int update_tx_info(struct adapter *adapter,
14398c2ecf20Sopenharmony_ci					  unsigned int flags,
14408c2ecf20Sopenharmony_ci					  unsigned int pr0)
14418c2ecf20Sopenharmony_ci{
14428c2ecf20Sopenharmony_ci	struct sge *sge = adapter->sge;
14438c2ecf20Sopenharmony_ci	struct cmdQ *cmdq = &sge->cmdQ[0];
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_ci	cmdq->processed += pr0;
14468c2ecf20Sopenharmony_ci	if (flags & (F_FL0_ENABLE | F_FL1_ENABLE)) {
14478c2ecf20Sopenharmony_ci		freelQs_empty(sge);
14488c2ecf20Sopenharmony_ci		flags &= ~(F_FL0_ENABLE | F_FL1_ENABLE);
14498c2ecf20Sopenharmony_ci	}
14508c2ecf20Sopenharmony_ci	if (flags & F_CMDQ0_ENABLE) {
14518c2ecf20Sopenharmony_ci		clear_bit(CMDQ_STAT_RUNNING, &cmdq->status);
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci		if (cmdq->cleaned + cmdq->in_use != cmdq->processed &&
14548c2ecf20Sopenharmony_ci		    !test_and_set_bit(CMDQ_STAT_LAST_PKT_DB, &cmdq->status)) {
14558c2ecf20Sopenharmony_ci			set_bit(CMDQ_STAT_RUNNING, &cmdq->status);
14568c2ecf20Sopenharmony_ci			writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
14578c2ecf20Sopenharmony_ci		}
14588c2ecf20Sopenharmony_ci		if (sge->tx_sched)
14598c2ecf20Sopenharmony_ci			tasklet_hi_schedule(&sge->tx_sched->sched_tsk);
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci		flags &= ~F_CMDQ0_ENABLE;
14628c2ecf20Sopenharmony_ci	}
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	if (unlikely(sge->stopped_tx_queues != 0))
14658c2ecf20Sopenharmony_ci		restart_tx_queues(sge);
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci	return flags;
14688c2ecf20Sopenharmony_ci}
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci/*
14718c2ecf20Sopenharmony_ci * Process SGE responses, up to the supplied budget.  Returns the number of
14728c2ecf20Sopenharmony_ci * responses processed.  A negative budget is effectively unlimited.
14738c2ecf20Sopenharmony_ci */
14748c2ecf20Sopenharmony_cistatic int process_responses(struct adapter *adapter, int budget)
14758c2ecf20Sopenharmony_ci{
14768c2ecf20Sopenharmony_ci	struct sge *sge = adapter->sge;
14778c2ecf20Sopenharmony_ci	struct respQ *q = &sge->respQ;
14788c2ecf20Sopenharmony_ci	struct respQ_e *e = &q->entries[q->cidx];
14798c2ecf20Sopenharmony_ci	int done = 0;
14808c2ecf20Sopenharmony_ci	unsigned int flags = 0;
14818c2ecf20Sopenharmony_ci	unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
14828c2ecf20Sopenharmony_ci
14838c2ecf20Sopenharmony_ci	while (done < budget && e->GenerationBit == q->genbit) {
14848c2ecf20Sopenharmony_ci		flags |= e->Qsleeping;
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ci		cmdq_processed[0] += e->Cmdq0CreditReturn;
14878c2ecf20Sopenharmony_ci		cmdq_processed[1] += e->Cmdq1CreditReturn;
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci		/* We batch updates to the TX side to avoid cacheline
14908c2ecf20Sopenharmony_ci		 * ping-pong of TX state information on MP where the sender
14918c2ecf20Sopenharmony_ci		 * might run on a different CPU than this function...
14928c2ecf20Sopenharmony_ci		 */
14938c2ecf20Sopenharmony_ci		if (unlikely((flags & F_CMDQ0_ENABLE) || cmdq_processed[0] > 64)) {
14948c2ecf20Sopenharmony_ci			flags = update_tx_info(adapter, flags, cmdq_processed[0]);
14958c2ecf20Sopenharmony_ci			cmdq_processed[0] = 0;
14968c2ecf20Sopenharmony_ci		}
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci		if (unlikely(cmdq_processed[1] > 16)) {
14998c2ecf20Sopenharmony_ci			sge->cmdQ[1].processed += cmdq_processed[1];
15008c2ecf20Sopenharmony_ci			cmdq_processed[1] = 0;
15018c2ecf20Sopenharmony_ci		}
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci		if (likely(e->DataValid)) {
15048c2ecf20Sopenharmony_ci			struct freelQ *fl = &sge->freelQ[e->FreelistQid];
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci			BUG_ON(!e->Sop || !e->Eop);
15078c2ecf20Sopenharmony_ci			if (unlikely(e->Offload))
15088c2ecf20Sopenharmony_ci				unexpected_offload(adapter, fl);
15098c2ecf20Sopenharmony_ci			else
15108c2ecf20Sopenharmony_ci				sge_rx(sge, fl, e->BufferLength);
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci			++done;
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci			/*
15158c2ecf20Sopenharmony_ci			 * Note: this depends on each packet consuming a
15168c2ecf20Sopenharmony_ci			 * single free-list buffer; cf. the BUG above.
15178c2ecf20Sopenharmony_ci			 */
15188c2ecf20Sopenharmony_ci			if (++fl->cidx == fl->size)
15198c2ecf20Sopenharmony_ci				fl->cidx = 0;
15208c2ecf20Sopenharmony_ci			prefetch(fl->centries[fl->cidx].skb);
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_ci			if (unlikely(--fl->credits <
15238c2ecf20Sopenharmony_ci				     fl->size - SGE_FREEL_REFILL_THRESH))
15248c2ecf20Sopenharmony_ci				refill_free_list(sge, fl);
15258c2ecf20Sopenharmony_ci		} else
15268c2ecf20Sopenharmony_ci			sge->stats.pure_rsps++;
15278c2ecf20Sopenharmony_ci
15288c2ecf20Sopenharmony_ci		e++;
15298c2ecf20Sopenharmony_ci		if (unlikely(++q->cidx == q->size)) {
15308c2ecf20Sopenharmony_ci			q->cidx = 0;
15318c2ecf20Sopenharmony_ci			q->genbit ^= 1;
15328c2ecf20Sopenharmony_ci			e = q->entries;
15338c2ecf20Sopenharmony_ci		}
15348c2ecf20Sopenharmony_ci		prefetch(e);
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci		if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
15378c2ecf20Sopenharmony_ci			writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
15388c2ecf20Sopenharmony_ci			q->credits = 0;
15398c2ecf20Sopenharmony_ci		}
15408c2ecf20Sopenharmony_ci	}
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	flags = update_tx_info(adapter, flags, cmdq_processed[0]);
15438c2ecf20Sopenharmony_ci	sge->cmdQ[1].processed += cmdq_processed[1];
15448c2ecf20Sopenharmony_ci
15458c2ecf20Sopenharmony_ci	return done;
15468c2ecf20Sopenharmony_ci}
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_cistatic inline int responses_pending(const struct adapter *adapter)
15498c2ecf20Sopenharmony_ci{
15508c2ecf20Sopenharmony_ci	const struct respQ *Q = &adapter->sge->respQ;
15518c2ecf20Sopenharmony_ci	const struct respQ_e *e = &Q->entries[Q->cidx];
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci	return e->GenerationBit == Q->genbit;
15548c2ecf20Sopenharmony_ci}
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci/*
15578c2ecf20Sopenharmony_ci * A simpler version of process_responses() that handles only pure (i.e.,
15588c2ecf20Sopenharmony_ci * non data-carrying) responses.  Such respones are too light-weight to justify
15598c2ecf20Sopenharmony_ci * calling a softirq when using NAPI, so we handle them specially in hard
15608c2ecf20Sopenharmony_ci * interrupt context.  The function is called with a pointer to a response,
15618c2ecf20Sopenharmony_ci * which the caller must ensure is a valid pure response.  Returns 1 if it
15628c2ecf20Sopenharmony_ci * encounters a valid data-carrying response, 0 otherwise.
15638c2ecf20Sopenharmony_ci */
15648c2ecf20Sopenharmony_cistatic int process_pure_responses(struct adapter *adapter)
15658c2ecf20Sopenharmony_ci{
15668c2ecf20Sopenharmony_ci	struct sge *sge = adapter->sge;
15678c2ecf20Sopenharmony_ci	struct respQ *q = &sge->respQ;
15688c2ecf20Sopenharmony_ci	struct respQ_e *e = &q->entries[q->cidx];
15698c2ecf20Sopenharmony_ci	const struct freelQ *fl = &sge->freelQ[e->FreelistQid];
15708c2ecf20Sopenharmony_ci	unsigned int flags = 0;
15718c2ecf20Sopenharmony_ci	unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_ci	prefetch(fl->centries[fl->cidx].skb);
15748c2ecf20Sopenharmony_ci	if (e->DataValid)
15758c2ecf20Sopenharmony_ci		return 1;
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci	do {
15788c2ecf20Sopenharmony_ci		flags |= e->Qsleeping;
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci		cmdq_processed[0] += e->Cmdq0CreditReturn;
15818c2ecf20Sopenharmony_ci		cmdq_processed[1] += e->Cmdq1CreditReturn;
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ci		e++;
15848c2ecf20Sopenharmony_ci		if (unlikely(++q->cidx == q->size)) {
15858c2ecf20Sopenharmony_ci			q->cidx = 0;
15868c2ecf20Sopenharmony_ci			q->genbit ^= 1;
15878c2ecf20Sopenharmony_ci			e = q->entries;
15888c2ecf20Sopenharmony_ci		}
15898c2ecf20Sopenharmony_ci		prefetch(e);
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci		if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
15928c2ecf20Sopenharmony_ci			writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
15938c2ecf20Sopenharmony_ci			q->credits = 0;
15948c2ecf20Sopenharmony_ci		}
15958c2ecf20Sopenharmony_ci		sge->stats.pure_rsps++;
15968c2ecf20Sopenharmony_ci	} while (e->GenerationBit == q->genbit && !e->DataValid);
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_ci	flags = update_tx_info(adapter, flags, cmdq_processed[0]);
15998c2ecf20Sopenharmony_ci	sge->cmdQ[1].processed += cmdq_processed[1];
16008c2ecf20Sopenharmony_ci
16018c2ecf20Sopenharmony_ci	return e->GenerationBit == q->genbit;
16028c2ecf20Sopenharmony_ci}
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_ci/*
16058c2ecf20Sopenharmony_ci * Handler for new data events when using NAPI.  This does not need any locking
16068c2ecf20Sopenharmony_ci * or protection from interrupts as data interrupts are off at this point and
16078c2ecf20Sopenharmony_ci * other adapter interrupts do not interfere.
16088c2ecf20Sopenharmony_ci */
16098c2ecf20Sopenharmony_ciint t1_poll(struct napi_struct *napi, int budget)
16108c2ecf20Sopenharmony_ci{
16118c2ecf20Sopenharmony_ci	struct adapter *adapter = container_of(napi, struct adapter, napi);
16128c2ecf20Sopenharmony_ci	int work_done = process_responses(adapter, budget);
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_ci	if (likely(work_done < budget)) {
16158c2ecf20Sopenharmony_ci		napi_complete_done(napi, work_done);
16168c2ecf20Sopenharmony_ci		writel(adapter->sge->respQ.cidx,
16178c2ecf20Sopenharmony_ci		       adapter->regs + A_SG_SLEEPING);
16188c2ecf20Sopenharmony_ci	}
16198c2ecf20Sopenharmony_ci	return work_done;
16208c2ecf20Sopenharmony_ci}
16218c2ecf20Sopenharmony_ci
16228c2ecf20Sopenharmony_ciirqreturn_t t1_interrupt(int irq, void *data)
16238c2ecf20Sopenharmony_ci{
16248c2ecf20Sopenharmony_ci	struct adapter *adapter = data;
16258c2ecf20Sopenharmony_ci	struct sge *sge = adapter->sge;
16268c2ecf20Sopenharmony_ci	int handled;
16278c2ecf20Sopenharmony_ci
16288c2ecf20Sopenharmony_ci	if (likely(responses_pending(adapter))) {
16298c2ecf20Sopenharmony_ci		writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci		if (napi_schedule_prep(&adapter->napi)) {
16328c2ecf20Sopenharmony_ci			if (process_pure_responses(adapter))
16338c2ecf20Sopenharmony_ci				__napi_schedule(&adapter->napi);
16348c2ecf20Sopenharmony_ci			else {
16358c2ecf20Sopenharmony_ci				/* no data, no NAPI needed */
16368c2ecf20Sopenharmony_ci				writel(sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
16378c2ecf20Sopenharmony_ci				/* undo schedule_prep */
16388c2ecf20Sopenharmony_ci				napi_enable(&adapter->napi);
16398c2ecf20Sopenharmony_ci			}
16408c2ecf20Sopenharmony_ci		}
16418c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
16428c2ecf20Sopenharmony_ci	}
16438c2ecf20Sopenharmony_ci
16448c2ecf20Sopenharmony_ci	spin_lock(&adapter->async_lock);
16458c2ecf20Sopenharmony_ci	handled = t1_slow_intr_handler(adapter);
16468c2ecf20Sopenharmony_ci	spin_unlock(&adapter->async_lock);
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_ci	if (!handled)
16498c2ecf20Sopenharmony_ci		sge->stats.unhandled_irqs++;
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	return IRQ_RETVAL(handled != 0);
16528c2ecf20Sopenharmony_ci}
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ci/*
16558c2ecf20Sopenharmony_ci * Enqueues the sk_buff onto the cmdQ[qid] and has hardware fetch it.
16568c2ecf20Sopenharmony_ci *
16578c2ecf20Sopenharmony_ci * The code figures out how many entries the sk_buff will require in the
16588c2ecf20Sopenharmony_ci * cmdQ and updates the cmdQ data structure with the state once the enqueue
16598c2ecf20Sopenharmony_ci * has complete. Then, it doesn't access the global structure anymore, but
16608c2ecf20Sopenharmony_ci * uses the corresponding fields on the stack. In conjunction with a spinlock
16618c2ecf20Sopenharmony_ci * around that code, we can make the function reentrant without holding the
16628c2ecf20Sopenharmony_ci * lock when we actually enqueue (which might be expensive, especially on
16638c2ecf20Sopenharmony_ci * architectures with IO MMUs).
16648c2ecf20Sopenharmony_ci *
16658c2ecf20Sopenharmony_ci * This runs with softirqs disabled.
16668c2ecf20Sopenharmony_ci */
16678c2ecf20Sopenharmony_cistatic int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
16688c2ecf20Sopenharmony_ci		     unsigned int qid, struct net_device *dev)
16698c2ecf20Sopenharmony_ci{
16708c2ecf20Sopenharmony_ci	struct sge *sge = adapter->sge;
16718c2ecf20Sopenharmony_ci	struct cmdQ *q = &sge->cmdQ[qid];
16728c2ecf20Sopenharmony_ci	unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	spin_lock(&q->lock);
16758c2ecf20Sopenharmony_ci
16768c2ecf20Sopenharmony_ci	reclaim_completed_tx(sge, q);
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_ci	pidx = q->pidx;
16798c2ecf20Sopenharmony_ci	credits = q->size - q->in_use;
16808c2ecf20Sopenharmony_ci	count = 1 + skb_shinfo(skb)->nr_frags;
16818c2ecf20Sopenharmony_ci	count += compute_large_page_tx_descs(skb);
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_ci	/* Ethernet packet */
16848c2ecf20Sopenharmony_ci	if (unlikely(credits < count)) {
16858c2ecf20Sopenharmony_ci		if (!netif_queue_stopped(dev)) {
16868c2ecf20Sopenharmony_ci			netif_stop_queue(dev);
16878c2ecf20Sopenharmony_ci			set_bit(dev->if_port, &sge->stopped_tx_queues);
16888c2ecf20Sopenharmony_ci			sge->stats.cmdQ_full[2]++;
16898c2ecf20Sopenharmony_ci			pr_err("%s: Tx ring full while queue awake!\n",
16908c2ecf20Sopenharmony_ci			       adapter->name);
16918c2ecf20Sopenharmony_ci		}
16928c2ecf20Sopenharmony_ci		spin_unlock(&q->lock);
16938c2ecf20Sopenharmony_ci		return NETDEV_TX_BUSY;
16948c2ecf20Sopenharmony_ci	}
16958c2ecf20Sopenharmony_ci
16968c2ecf20Sopenharmony_ci	if (unlikely(credits - count < q->stop_thres)) {
16978c2ecf20Sopenharmony_ci		netif_stop_queue(dev);
16988c2ecf20Sopenharmony_ci		set_bit(dev->if_port, &sge->stopped_tx_queues);
16998c2ecf20Sopenharmony_ci		sge->stats.cmdQ_full[2]++;
17008c2ecf20Sopenharmony_ci	}
17018c2ecf20Sopenharmony_ci
17028c2ecf20Sopenharmony_ci	/* T204 cmdQ0 skbs that are destined for a certain port have to go
17038c2ecf20Sopenharmony_ci	 * through the scheduler.
17048c2ecf20Sopenharmony_ci	 */
17058c2ecf20Sopenharmony_ci	if (sge->tx_sched && !qid && skb->dev) {
17068c2ecf20Sopenharmony_ciuse_sched:
17078c2ecf20Sopenharmony_ci		use_sched_skb = 1;
17088c2ecf20Sopenharmony_ci		/* Note that the scheduler might return a different skb than
17098c2ecf20Sopenharmony_ci		 * the one passed in.
17108c2ecf20Sopenharmony_ci		 */
17118c2ecf20Sopenharmony_ci		skb = sched_skb(sge, skb, credits);
17128c2ecf20Sopenharmony_ci		if (!skb) {
17138c2ecf20Sopenharmony_ci			spin_unlock(&q->lock);
17148c2ecf20Sopenharmony_ci			return NETDEV_TX_OK;
17158c2ecf20Sopenharmony_ci		}
17168c2ecf20Sopenharmony_ci		pidx = q->pidx;
17178c2ecf20Sopenharmony_ci		count = 1 + skb_shinfo(skb)->nr_frags;
17188c2ecf20Sopenharmony_ci		count += compute_large_page_tx_descs(skb);
17198c2ecf20Sopenharmony_ci	}
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci	q->in_use += count;
17228c2ecf20Sopenharmony_ci	genbit = q->genbit;
17238c2ecf20Sopenharmony_ci	pidx = q->pidx;
17248c2ecf20Sopenharmony_ci	q->pidx += count;
17258c2ecf20Sopenharmony_ci	if (q->pidx >= q->size) {
17268c2ecf20Sopenharmony_ci		q->pidx -= q->size;
17278c2ecf20Sopenharmony_ci		q->genbit ^= 1;
17288c2ecf20Sopenharmony_ci	}
17298c2ecf20Sopenharmony_ci	spin_unlock(&q->lock);
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_ci	write_tx_descs(adapter, skb, pidx, genbit, q);
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci	/*
17348c2ecf20Sopenharmony_ci	 * We always ring the doorbell for cmdQ1.  For cmdQ0, we only ring
17358c2ecf20Sopenharmony_ci	 * the doorbell if the Q is asleep. There is a natural race, where
17368c2ecf20Sopenharmony_ci	 * the hardware is going to sleep just after we checked, however,
17378c2ecf20Sopenharmony_ci	 * then the interrupt handler will detect the outstanding TX packet
17388c2ecf20Sopenharmony_ci	 * and ring the doorbell for us.
17398c2ecf20Sopenharmony_ci	 */
17408c2ecf20Sopenharmony_ci	if (qid)
17418c2ecf20Sopenharmony_ci		doorbell_pio(adapter, F_CMDQ1_ENABLE);
17428c2ecf20Sopenharmony_ci	else {
17438c2ecf20Sopenharmony_ci		clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
17448c2ecf20Sopenharmony_ci		if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
17458c2ecf20Sopenharmony_ci			set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
17468c2ecf20Sopenharmony_ci			writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
17478c2ecf20Sopenharmony_ci		}
17488c2ecf20Sopenharmony_ci	}
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_ci	if (use_sched_skb) {
17518c2ecf20Sopenharmony_ci		if (spin_trylock(&q->lock)) {
17528c2ecf20Sopenharmony_ci			credits = q->size - q->in_use;
17538c2ecf20Sopenharmony_ci			skb = NULL;
17548c2ecf20Sopenharmony_ci			goto use_sched;
17558c2ecf20Sopenharmony_ci		}
17568c2ecf20Sopenharmony_ci	}
17578c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
17588c2ecf20Sopenharmony_ci}
17598c2ecf20Sopenharmony_ci
17608c2ecf20Sopenharmony_ci#define MK_ETH_TYPE_MSS(type, mss) (((mss) & 0x3FFF) | ((type) << 14))
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_ci/*
17638c2ecf20Sopenharmony_ci *	eth_hdr_len - return the length of an Ethernet header
17648c2ecf20Sopenharmony_ci *	@data: pointer to the start of the Ethernet header
17658c2ecf20Sopenharmony_ci *
17668c2ecf20Sopenharmony_ci *	Returns the length of an Ethernet header, including optional VLAN tag.
17678c2ecf20Sopenharmony_ci */
17688c2ecf20Sopenharmony_cistatic inline int eth_hdr_len(const void *data)
17698c2ecf20Sopenharmony_ci{
17708c2ecf20Sopenharmony_ci	const struct ethhdr *e = data;
17718c2ecf20Sopenharmony_ci
17728c2ecf20Sopenharmony_ci	return e->h_proto == htons(ETH_P_8021Q) ? VLAN_ETH_HLEN : ETH_HLEN;
17738c2ecf20Sopenharmony_ci}
17748c2ecf20Sopenharmony_ci
17758c2ecf20Sopenharmony_ci/*
17768c2ecf20Sopenharmony_ci * Adds the CPL header to the sk_buff and passes it to t1_sge_tx.
17778c2ecf20Sopenharmony_ci */
17788c2ecf20Sopenharmony_cinetdev_tx_t t1_start_xmit(struct sk_buff *skb, struct net_device *dev)
17798c2ecf20Sopenharmony_ci{
17808c2ecf20Sopenharmony_ci	struct adapter *adapter = dev->ml_priv;
17818c2ecf20Sopenharmony_ci	struct sge *sge = adapter->sge;
17828c2ecf20Sopenharmony_ci	struct sge_port_stats *st = this_cpu_ptr(sge->port_stats[dev->if_port]);
17838c2ecf20Sopenharmony_ci	struct cpl_tx_pkt *cpl;
17848c2ecf20Sopenharmony_ci	struct sk_buff *orig_skb = skb;
17858c2ecf20Sopenharmony_ci	int ret;
17868c2ecf20Sopenharmony_ci
17878c2ecf20Sopenharmony_ci	if (skb->protocol == htons(ETH_P_CPL5))
17888c2ecf20Sopenharmony_ci		goto send;
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci	/*
17918c2ecf20Sopenharmony_ci	 * We are using a non-standard hard_header_len.
17928c2ecf20Sopenharmony_ci	 * Allocate more header room in the rare cases it is not big enough.
17938c2ecf20Sopenharmony_ci	 */
17948c2ecf20Sopenharmony_ci	if (unlikely(skb_headroom(skb) < dev->hard_header_len - ETH_HLEN)) {
17958c2ecf20Sopenharmony_ci		skb = skb_realloc_headroom(skb, sizeof(struct cpl_tx_pkt_lso));
17968c2ecf20Sopenharmony_ci		++st->tx_need_hdrroom;
17978c2ecf20Sopenharmony_ci		dev_kfree_skb_any(orig_skb);
17988c2ecf20Sopenharmony_ci		if (!skb)
17998c2ecf20Sopenharmony_ci			return NETDEV_TX_OK;
18008c2ecf20Sopenharmony_ci	}
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci	if (skb_shinfo(skb)->gso_size) {
18038c2ecf20Sopenharmony_ci		int eth_type;
18048c2ecf20Sopenharmony_ci		struct cpl_tx_pkt_lso *hdr;
18058c2ecf20Sopenharmony_ci
18068c2ecf20Sopenharmony_ci		++st->tx_tso;
18078c2ecf20Sopenharmony_ci
18088c2ecf20Sopenharmony_ci		eth_type = skb_network_offset(skb) == ETH_HLEN ?
18098c2ecf20Sopenharmony_ci			CPL_ETH_II : CPL_ETH_II_VLAN;
18108c2ecf20Sopenharmony_ci
18118c2ecf20Sopenharmony_ci		hdr = skb_push(skb, sizeof(*hdr));
18128c2ecf20Sopenharmony_ci		hdr->opcode = CPL_TX_PKT_LSO;
18138c2ecf20Sopenharmony_ci		hdr->ip_csum_dis = hdr->l4_csum_dis = 0;
18148c2ecf20Sopenharmony_ci		hdr->ip_hdr_words = ip_hdr(skb)->ihl;
18158c2ecf20Sopenharmony_ci		hdr->tcp_hdr_words = tcp_hdr(skb)->doff;
18168c2ecf20Sopenharmony_ci		hdr->eth_type_mss = htons(MK_ETH_TYPE_MSS(eth_type,
18178c2ecf20Sopenharmony_ci							  skb_shinfo(skb)->gso_size));
18188c2ecf20Sopenharmony_ci		hdr->len = htonl(skb->len - sizeof(*hdr));
18198c2ecf20Sopenharmony_ci		cpl = (struct cpl_tx_pkt *)hdr;
18208c2ecf20Sopenharmony_ci	} else {
18218c2ecf20Sopenharmony_ci		/*
18228c2ecf20Sopenharmony_ci		 * Packets shorter than ETH_HLEN can break the MAC, drop them
18238c2ecf20Sopenharmony_ci		 * early.  Also, we may get oversized packets because some
18248c2ecf20Sopenharmony_ci		 * parts of the kernel don't handle our unusual hard_header_len
18258c2ecf20Sopenharmony_ci		 * right, drop those too.
18268c2ecf20Sopenharmony_ci		 */
18278c2ecf20Sopenharmony_ci		if (unlikely(skb->len < ETH_HLEN ||
18288c2ecf20Sopenharmony_ci			     skb->len > dev->mtu + eth_hdr_len(skb->data))) {
18298c2ecf20Sopenharmony_ci			netdev_dbg(dev, "packet size %d hdr %d mtu%d\n",
18308c2ecf20Sopenharmony_ci				   skb->len, eth_hdr_len(skb->data), dev->mtu);
18318c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
18328c2ecf20Sopenharmony_ci			return NETDEV_TX_OK;
18338c2ecf20Sopenharmony_ci		}
18348c2ecf20Sopenharmony_ci
18358c2ecf20Sopenharmony_ci		if (skb->ip_summed == CHECKSUM_PARTIAL &&
18368c2ecf20Sopenharmony_ci		    ip_hdr(skb)->protocol == IPPROTO_UDP) {
18378c2ecf20Sopenharmony_ci			if (unlikely(skb_checksum_help(skb))) {
18388c2ecf20Sopenharmony_ci				netdev_dbg(dev, "unable to do udp checksum\n");
18398c2ecf20Sopenharmony_ci				dev_kfree_skb_any(skb);
18408c2ecf20Sopenharmony_ci				return NETDEV_TX_OK;
18418c2ecf20Sopenharmony_ci			}
18428c2ecf20Sopenharmony_ci		}
18438c2ecf20Sopenharmony_ci
18448c2ecf20Sopenharmony_ci		/* Hmmm, assuming to catch the gratious arp... and we'll use
18458c2ecf20Sopenharmony_ci		 * it to flush out stuck espi packets...
18468c2ecf20Sopenharmony_ci		 */
18478c2ecf20Sopenharmony_ci		if ((unlikely(!adapter->sge->espibug_skb[dev->if_port]))) {
18488c2ecf20Sopenharmony_ci			if (skb->protocol == htons(ETH_P_ARP) &&
18498c2ecf20Sopenharmony_ci			    arp_hdr(skb)->ar_op == htons(ARPOP_REQUEST)) {
18508c2ecf20Sopenharmony_ci				adapter->sge->espibug_skb[dev->if_port] = skb;
18518c2ecf20Sopenharmony_ci				/* We want to re-use this skb later. We
18528c2ecf20Sopenharmony_ci				 * simply bump the reference count and it
18538c2ecf20Sopenharmony_ci				 * will not be freed...
18548c2ecf20Sopenharmony_ci				 */
18558c2ecf20Sopenharmony_ci				skb = skb_get(skb);
18568c2ecf20Sopenharmony_ci			}
18578c2ecf20Sopenharmony_ci		}
18588c2ecf20Sopenharmony_ci
18598c2ecf20Sopenharmony_ci		cpl = __skb_push(skb, sizeof(*cpl));
18608c2ecf20Sopenharmony_ci		cpl->opcode = CPL_TX_PKT;
18618c2ecf20Sopenharmony_ci		cpl->ip_csum_dis = 1;    /* SW calculates IP csum */
18628c2ecf20Sopenharmony_ci		cpl->l4_csum_dis = skb->ip_summed == CHECKSUM_PARTIAL ? 0 : 1;
18638c2ecf20Sopenharmony_ci		/* the length field isn't used so don't bother setting it */
18648c2ecf20Sopenharmony_ci
18658c2ecf20Sopenharmony_ci		st->tx_cso += (skb->ip_summed == CHECKSUM_PARTIAL);
18668c2ecf20Sopenharmony_ci	}
18678c2ecf20Sopenharmony_ci	cpl->iff = dev->if_port;
18688c2ecf20Sopenharmony_ci
18698c2ecf20Sopenharmony_ci	if (skb_vlan_tag_present(skb)) {
18708c2ecf20Sopenharmony_ci		cpl->vlan_valid = 1;
18718c2ecf20Sopenharmony_ci		cpl->vlan = htons(skb_vlan_tag_get(skb));
18728c2ecf20Sopenharmony_ci		st->vlan_insert++;
18738c2ecf20Sopenharmony_ci	} else
18748c2ecf20Sopenharmony_ci		cpl->vlan_valid = 0;
18758c2ecf20Sopenharmony_ci
18768c2ecf20Sopenharmony_cisend:
18778c2ecf20Sopenharmony_ci	ret = t1_sge_tx(skb, adapter, 0, dev);
18788c2ecf20Sopenharmony_ci
18798c2ecf20Sopenharmony_ci	/* If transmit busy, and we reallocated skb's due to headroom limit,
18808c2ecf20Sopenharmony_ci	 * then silently discard to avoid leak.
18818c2ecf20Sopenharmony_ci	 */
18828c2ecf20Sopenharmony_ci	if (unlikely(ret != NETDEV_TX_OK && skb != orig_skb)) {
18838c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
18848c2ecf20Sopenharmony_ci		ret = NETDEV_TX_OK;
18858c2ecf20Sopenharmony_ci	}
18868c2ecf20Sopenharmony_ci	return ret;
18878c2ecf20Sopenharmony_ci}
18888c2ecf20Sopenharmony_ci
18898c2ecf20Sopenharmony_ci/*
18908c2ecf20Sopenharmony_ci * Callback for the Tx buffer reclaim timer.  Runs with softirqs disabled.
18918c2ecf20Sopenharmony_ci */
18928c2ecf20Sopenharmony_cistatic void sge_tx_reclaim_cb(struct timer_list *t)
18938c2ecf20Sopenharmony_ci{
18948c2ecf20Sopenharmony_ci	int i;
18958c2ecf20Sopenharmony_ci	struct sge *sge = from_timer(sge, t, tx_reclaim_timer);
18968c2ecf20Sopenharmony_ci
18978c2ecf20Sopenharmony_ci	for (i = 0; i < SGE_CMDQ_N; ++i) {
18988c2ecf20Sopenharmony_ci		struct cmdQ *q = &sge->cmdQ[i];
18998c2ecf20Sopenharmony_ci
19008c2ecf20Sopenharmony_ci		if (!spin_trylock(&q->lock))
19018c2ecf20Sopenharmony_ci			continue;
19028c2ecf20Sopenharmony_ci
19038c2ecf20Sopenharmony_ci		reclaim_completed_tx(sge, q);
19048c2ecf20Sopenharmony_ci		if (i == 0 && q->in_use) {    /* flush pending credits */
19058c2ecf20Sopenharmony_ci			writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
19068c2ecf20Sopenharmony_ci		}
19078c2ecf20Sopenharmony_ci		spin_unlock(&q->lock);
19088c2ecf20Sopenharmony_ci	}
19098c2ecf20Sopenharmony_ci	mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
19108c2ecf20Sopenharmony_ci}
19118c2ecf20Sopenharmony_ci
19128c2ecf20Sopenharmony_ci/*
19138c2ecf20Sopenharmony_ci * Propagate changes of the SGE coalescing parameters to the HW.
19148c2ecf20Sopenharmony_ci */
19158c2ecf20Sopenharmony_ciint t1_sge_set_coalesce_params(struct sge *sge, struct sge_params *p)
19168c2ecf20Sopenharmony_ci{
19178c2ecf20Sopenharmony_ci	sge->fixed_intrtimer = p->rx_coalesce_usecs *
19188c2ecf20Sopenharmony_ci		core_ticks_per_usec(sge->adapter);
19198c2ecf20Sopenharmony_ci	writel(sge->fixed_intrtimer, sge->adapter->regs + A_SG_INTRTIMER);
19208c2ecf20Sopenharmony_ci	return 0;
19218c2ecf20Sopenharmony_ci}
19228c2ecf20Sopenharmony_ci
19238c2ecf20Sopenharmony_ci/*
19248c2ecf20Sopenharmony_ci * Allocates both RX and TX resources and configures the SGE. However,
19258c2ecf20Sopenharmony_ci * the hardware is not enabled yet.
19268c2ecf20Sopenharmony_ci */
19278c2ecf20Sopenharmony_ciint t1_sge_configure(struct sge *sge, struct sge_params *p)
19288c2ecf20Sopenharmony_ci{
19298c2ecf20Sopenharmony_ci	if (alloc_rx_resources(sge, p))
19308c2ecf20Sopenharmony_ci		return -ENOMEM;
19318c2ecf20Sopenharmony_ci	if (alloc_tx_resources(sge, p)) {
19328c2ecf20Sopenharmony_ci		free_rx_resources(sge);
19338c2ecf20Sopenharmony_ci		return -ENOMEM;
19348c2ecf20Sopenharmony_ci	}
19358c2ecf20Sopenharmony_ci	configure_sge(sge, p);
19368c2ecf20Sopenharmony_ci
19378c2ecf20Sopenharmony_ci	/*
19388c2ecf20Sopenharmony_ci	 * Now that we have sized the free lists calculate the payload
19398c2ecf20Sopenharmony_ci	 * capacity of the large buffers.  Other parts of the driver use
19408c2ecf20Sopenharmony_ci	 * this to set the max offload coalescing size so that RX packets
19418c2ecf20Sopenharmony_ci	 * do not overflow our large buffers.
19428c2ecf20Sopenharmony_ci	 */
19438c2ecf20Sopenharmony_ci	p->large_buf_capacity = jumbo_payload_capacity(sge);
19448c2ecf20Sopenharmony_ci	return 0;
19458c2ecf20Sopenharmony_ci}
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_ci/*
19488c2ecf20Sopenharmony_ci * Disables the DMA engine.
19498c2ecf20Sopenharmony_ci */
19508c2ecf20Sopenharmony_civoid t1_sge_stop(struct sge *sge)
19518c2ecf20Sopenharmony_ci{
19528c2ecf20Sopenharmony_ci	int i;
19538c2ecf20Sopenharmony_ci	writel(0, sge->adapter->regs + A_SG_CONTROL);
19548c2ecf20Sopenharmony_ci	readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
19558c2ecf20Sopenharmony_ci
19568c2ecf20Sopenharmony_ci	if (is_T2(sge->adapter))
19578c2ecf20Sopenharmony_ci		del_timer_sync(&sge->espibug_timer);
19588c2ecf20Sopenharmony_ci
19598c2ecf20Sopenharmony_ci	del_timer_sync(&sge->tx_reclaim_timer);
19608c2ecf20Sopenharmony_ci	if (sge->tx_sched)
19618c2ecf20Sopenharmony_ci		tx_sched_stop(sge);
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_NPORTS; i++)
19648c2ecf20Sopenharmony_ci		kfree_skb(sge->espibug_skb[i]);
19658c2ecf20Sopenharmony_ci}
19668c2ecf20Sopenharmony_ci
19678c2ecf20Sopenharmony_ci/*
19688c2ecf20Sopenharmony_ci * Enables the DMA engine.
19698c2ecf20Sopenharmony_ci */
19708c2ecf20Sopenharmony_civoid t1_sge_start(struct sge *sge)
19718c2ecf20Sopenharmony_ci{
19728c2ecf20Sopenharmony_ci	refill_free_list(sge, &sge->freelQ[0]);
19738c2ecf20Sopenharmony_ci	refill_free_list(sge, &sge->freelQ[1]);
19748c2ecf20Sopenharmony_ci
19758c2ecf20Sopenharmony_ci	writel(sge->sge_control, sge->adapter->regs + A_SG_CONTROL);
19768c2ecf20Sopenharmony_ci	doorbell_pio(sge->adapter, F_FL0_ENABLE | F_FL1_ENABLE);
19778c2ecf20Sopenharmony_ci	readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
19788c2ecf20Sopenharmony_ci
19798c2ecf20Sopenharmony_ci	mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
19808c2ecf20Sopenharmony_ci
19818c2ecf20Sopenharmony_ci	if (is_T2(sge->adapter))
19828c2ecf20Sopenharmony_ci		mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
19838c2ecf20Sopenharmony_ci}
19848c2ecf20Sopenharmony_ci
19858c2ecf20Sopenharmony_ci/*
19868c2ecf20Sopenharmony_ci * Callback for the T2 ESPI 'stuck packet feature' workaorund
19878c2ecf20Sopenharmony_ci */
19888c2ecf20Sopenharmony_cistatic void espibug_workaround_t204(struct timer_list *t)
19898c2ecf20Sopenharmony_ci{
19908c2ecf20Sopenharmony_ci	struct sge *sge = from_timer(sge, t, espibug_timer);
19918c2ecf20Sopenharmony_ci	struct adapter *adapter = sge->adapter;
19928c2ecf20Sopenharmony_ci	unsigned int nports = adapter->params.nports;
19938c2ecf20Sopenharmony_ci	u32 seop[MAX_NPORTS];
19948c2ecf20Sopenharmony_ci
19958c2ecf20Sopenharmony_ci	if (adapter->open_device_map & PORT_MASK) {
19968c2ecf20Sopenharmony_ci		int i;
19978c2ecf20Sopenharmony_ci
19988c2ecf20Sopenharmony_ci		if (t1_espi_get_mon_t204(adapter, &(seop[0]), 0) < 0)
19998c2ecf20Sopenharmony_ci			return;
20008c2ecf20Sopenharmony_ci
20018c2ecf20Sopenharmony_ci		for (i = 0; i < nports; i++) {
20028c2ecf20Sopenharmony_ci			struct sk_buff *skb = sge->espibug_skb[i];
20038c2ecf20Sopenharmony_ci
20048c2ecf20Sopenharmony_ci			if (!netif_running(adapter->port[i].dev) ||
20058c2ecf20Sopenharmony_ci			    netif_queue_stopped(adapter->port[i].dev) ||
20068c2ecf20Sopenharmony_ci			    !seop[i] || ((seop[i] & 0xfff) != 0) || !skb)
20078c2ecf20Sopenharmony_ci				continue;
20088c2ecf20Sopenharmony_ci
20098c2ecf20Sopenharmony_ci			if (!skb->cb[0]) {
20108c2ecf20Sopenharmony_ci				skb_copy_to_linear_data_offset(skb,
20118c2ecf20Sopenharmony_ci						    sizeof(struct cpl_tx_pkt),
20128c2ecf20Sopenharmony_ci							       ch_mac_addr,
20138c2ecf20Sopenharmony_ci							       ETH_ALEN);
20148c2ecf20Sopenharmony_ci				skb_copy_to_linear_data_offset(skb,
20158c2ecf20Sopenharmony_ci							       skb->len - 10,
20168c2ecf20Sopenharmony_ci							       ch_mac_addr,
20178c2ecf20Sopenharmony_ci							       ETH_ALEN);
20188c2ecf20Sopenharmony_ci				skb->cb[0] = 0xff;
20198c2ecf20Sopenharmony_ci			}
20208c2ecf20Sopenharmony_ci
20218c2ecf20Sopenharmony_ci			/* bump the reference count to avoid freeing of
20228c2ecf20Sopenharmony_ci			 * the skb once the DMA has completed.
20238c2ecf20Sopenharmony_ci			 */
20248c2ecf20Sopenharmony_ci			skb = skb_get(skb);
20258c2ecf20Sopenharmony_ci			t1_sge_tx(skb, adapter, 0, adapter->port[i].dev);
20268c2ecf20Sopenharmony_ci		}
20278c2ecf20Sopenharmony_ci	}
20288c2ecf20Sopenharmony_ci	mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
20298c2ecf20Sopenharmony_ci}
20308c2ecf20Sopenharmony_ci
20318c2ecf20Sopenharmony_cistatic void espibug_workaround(struct timer_list *t)
20328c2ecf20Sopenharmony_ci{
20338c2ecf20Sopenharmony_ci	struct sge *sge = from_timer(sge, t, espibug_timer);
20348c2ecf20Sopenharmony_ci	struct adapter *adapter = sge->adapter;
20358c2ecf20Sopenharmony_ci
20368c2ecf20Sopenharmony_ci	if (netif_running(adapter->port[0].dev)) {
20378c2ecf20Sopenharmony_ci	        struct sk_buff *skb = sge->espibug_skb[0];
20388c2ecf20Sopenharmony_ci	        u32 seop = t1_espi_get_mon(adapter, 0x930, 0);
20398c2ecf20Sopenharmony_ci
20408c2ecf20Sopenharmony_ci	        if ((seop & 0xfff0fff) == 0xfff && skb) {
20418c2ecf20Sopenharmony_ci	                if (!skb->cb[0]) {
20428c2ecf20Sopenharmony_ci	                        skb_copy_to_linear_data_offset(skb,
20438c2ecf20Sopenharmony_ci						     sizeof(struct cpl_tx_pkt),
20448c2ecf20Sopenharmony_ci							       ch_mac_addr,
20458c2ecf20Sopenharmony_ci							       ETH_ALEN);
20468c2ecf20Sopenharmony_ci	                        skb_copy_to_linear_data_offset(skb,
20478c2ecf20Sopenharmony_ci							       skb->len - 10,
20488c2ecf20Sopenharmony_ci							       ch_mac_addr,
20498c2ecf20Sopenharmony_ci							       ETH_ALEN);
20508c2ecf20Sopenharmony_ci	                        skb->cb[0] = 0xff;
20518c2ecf20Sopenharmony_ci	                }
20528c2ecf20Sopenharmony_ci
20538c2ecf20Sopenharmony_ci	                /* bump the reference count to avoid freeing of the
20548c2ecf20Sopenharmony_ci	                 * skb once the DMA has completed.
20558c2ecf20Sopenharmony_ci	                 */
20568c2ecf20Sopenharmony_ci	                skb = skb_get(skb);
20578c2ecf20Sopenharmony_ci	                t1_sge_tx(skb, adapter, 0, adapter->port[0].dev);
20588c2ecf20Sopenharmony_ci	        }
20598c2ecf20Sopenharmony_ci	}
20608c2ecf20Sopenharmony_ci	mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
20618c2ecf20Sopenharmony_ci}
20628c2ecf20Sopenharmony_ci
20638c2ecf20Sopenharmony_ci/*
20648c2ecf20Sopenharmony_ci * Creates a t1_sge structure and returns suggested resource parameters.
20658c2ecf20Sopenharmony_ci */
20668c2ecf20Sopenharmony_cistruct sge *t1_sge_create(struct adapter *adapter, struct sge_params *p)
20678c2ecf20Sopenharmony_ci{
20688c2ecf20Sopenharmony_ci	struct sge *sge = kzalloc(sizeof(*sge), GFP_KERNEL);
20698c2ecf20Sopenharmony_ci	int i;
20708c2ecf20Sopenharmony_ci
20718c2ecf20Sopenharmony_ci	if (!sge)
20728c2ecf20Sopenharmony_ci		return NULL;
20738c2ecf20Sopenharmony_ci
20748c2ecf20Sopenharmony_ci	sge->adapter = adapter;
20758c2ecf20Sopenharmony_ci	sge->netdev = adapter->port[0].dev;
20768c2ecf20Sopenharmony_ci	sge->rx_pkt_pad = t1_is_T1B(adapter) ? 0 : 2;
20778c2ecf20Sopenharmony_ci	sge->jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;
20788c2ecf20Sopenharmony_ci
20798c2ecf20Sopenharmony_ci	for_each_port(adapter, i) {
20808c2ecf20Sopenharmony_ci		sge->port_stats[i] = alloc_percpu(struct sge_port_stats);
20818c2ecf20Sopenharmony_ci		if (!sge->port_stats[i])
20828c2ecf20Sopenharmony_ci			goto nomem_port;
20838c2ecf20Sopenharmony_ci	}
20848c2ecf20Sopenharmony_ci
20858c2ecf20Sopenharmony_ci	timer_setup(&sge->tx_reclaim_timer, sge_tx_reclaim_cb, 0);
20868c2ecf20Sopenharmony_ci
20878c2ecf20Sopenharmony_ci	if (is_T2(sge->adapter)) {
20888c2ecf20Sopenharmony_ci		timer_setup(&sge->espibug_timer,
20898c2ecf20Sopenharmony_ci			    adapter->params.nports > 1 ? espibug_workaround_t204 : espibug_workaround,
20908c2ecf20Sopenharmony_ci			    0);
20918c2ecf20Sopenharmony_ci
20928c2ecf20Sopenharmony_ci		if (adapter->params.nports > 1)
20938c2ecf20Sopenharmony_ci			tx_sched_init(sge);
20948c2ecf20Sopenharmony_ci
20958c2ecf20Sopenharmony_ci		sge->espibug_timeout = 1;
20968c2ecf20Sopenharmony_ci		/* for T204, every 10ms */
20978c2ecf20Sopenharmony_ci		if (adapter->params.nports > 1)
20988c2ecf20Sopenharmony_ci			sge->espibug_timeout = HZ/100;
20998c2ecf20Sopenharmony_ci	}
21008c2ecf20Sopenharmony_ci
21018c2ecf20Sopenharmony_ci
21028c2ecf20Sopenharmony_ci	p->cmdQ_size[0] = SGE_CMDQ0_E_N;
21038c2ecf20Sopenharmony_ci	p->cmdQ_size[1] = SGE_CMDQ1_E_N;
21048c2ecf20Sopenharmony_ci	p->freelQ_size[!sge->jumbo_fl] = SGE_FREEL_SIZE;
21058c2ecf20Sopenharmony_ci	p->freelQ_size[sge->jumbo_fl] = SGE_JUMBO_FREEL_SIZE;
21068c2ecf20Sopenharmony_ci	if (sge->tx_sched) {
21078c2ecf20Sopenharmony_ci		if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204)
21088c2ecf20Sopenharmony_ci			p->rx_coalesce_usecs = 15;
21098c2ecf20Sopenharmony_ci		else
21108c2ecf20Sopenharmony_ci			p->rx_coalesce_usecs = 50;
21118c2ecf20Sopenharmony_ci	} else
21128c2ecf20Sopenharmony_ci		p->rx_coalesce_usecs = 50;
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ci	p->coalesce_enable = 0;
21158c2ecf20Sopenharmony_ci	p->sample_interval_usecs = 0;
21168c2ecf20Sopenharmony_ci
21178c2ecf20Sopenharmony_ci	return sge;
21188c2ecf20Sopenharmony_cinomem_port:
21198c2ecf20Sopenharmony_ci	while (i >= 0) {
21208c2ecf20Sopenharmony_ci		free_percpu(sge->port_stats[i]);
21218c2ecf20Sopenharmony_ci		--i;
21228c2ecf20Sopenharmony_ci	}
21238c2ecf20Sopenharmony_ci	kfree(sge);
21248c2ecf20Sopenharmony_ci	return NULL;
21258c2ecf20Sopenharmony_ci
21268c2ecf20Sopenharmony_ci}
2127